Skip to content

Commit

Permalink
add catch2 unit tests for Alignment/HIPAlignmentAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
mmusich committed Jun 19, 2024
1 parent 3d2772a commit 4957105
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Alignment/HIPAlignmentAlgorithm/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<environment>
<bin file="testHIPAnalyzers.cc" name="testHIPAnalyzers">
<use name="FWCore/TestProcessor"/>
<use name="catch2"/>
</bin>
</environment>
78 changes: 78 additions & 0 deletions Alignment/HIPAlignmentAlgorithm/test/testHIPAnalyzers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "FWCore/TestProcessor/interface/TestProcessor.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

// Function to run the catch2 tests
//___________________________________________________________________________________________
void runTestForAnalyzer(const std::string& baseConfig, const std::string& analyzerName) {
edm::test::TestProcessor::Config config{baseConfig};

SECTION(analyzerName + " base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }

SECTION(analyzerName + " No Runs data") {
edm::test::TestProcessor tester(config);
REQUIRE_NOTHROW(tester.testWithNoRuns());
}

SECTION(analyzerName + " beginJob and endJob only") {
edm::test::TestProcessor tester(config);
REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
}

// the following part is commented because the
// HIPTwoBodyDecayAnalyzer crashes on
// No "TransientTrackRecord" record found in the EventSetup.

/*
SECTION("No event data") {
edm::test::TestProcessor tester(config);
REQUIRE_NOTHROW(tester.test());
}
*/

SECTION("Run with no LuminosityBlocks") {
edm::test::TestProcessor tester(config);
REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
}

SECTION("LuminosityBlock with no Events") {
edm::test::TestProcessor tester(config);
REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
}
}

// Function to generate base configuration string
//___________________________________________________________________________________________
std::string generateBaseConfig(const std::string& analyzerName, const std::string& rootFileName) {
// Define a raw string literal
constexpr const char* rawString = R"_(from FWCore.TestProcessor.TestProcess import *
from Alignment.HIPAlignmentAlgorithm.{}_cfi import {}
process = TestProcess()
process.trackAnalyzer = {}
process.moduleToTest(process.trackAnalyzer)
process.load("TrackingTools.TransientTrack.TransientTrackBuilder_cfi")
process.add_(cms.ESProducer("TransientTrackBuilderESProducer"))
process.add_(cms.Service('MessageLogger'))
process.add_(cms.Service('JobReportService'))
process.add_(cms.Service('TFileService',fileName=cms.string('{}')))
)_";

// Format the raw string literal using fmt::format
return fmt::format(rawString, analyzerName, analyzerName, analyzerName, rootFileName);
}

//___________________________________________________________________________________________
TEST_CASE("LhcTrackAnalyzer tests", "[LhcTrackAnalyzer]") {
const std::string baseConfig = generateBaseConfig("lhcTrackAnalyzer", "testHIPAnalyzers1.root");
runTestForAnalyzer(baseConfig, "LhcTrackAnalyzer");
}

//___________________________________________________________________________________________
TEST_CASE("HIPTwoBodyDecayAnalyzer tests", "[HIPTwoBodyDecayAnalyzer]") {
const std::string baseConfig = generateBaseConfig("hipTwoBodyDecayAnalyzer", "testHIPAnalyzers2.root");
runTestForAnalyzer(baseConfig, "HIPTwoBodyDecayAnalyzer");
}

0 comments on commit 4957105

Please sign in to comment.