From d4b9c19b81df4b11d2acd5617cbb2b3fcc8f9402 Mon Sep 17 00:00:00 2001 From: Marvin Hemmer Date: Fri, 4 Dec 2026 11:25:59 +0100 Subject: [PATCH] [PWGJE] emcEventSelectionQA: compare EMCal trigger aliases with CTP classes and inputs Add an optional Run 3 QA (processCTPComparison, off by default) that checks the EMCal trigger alias bits from EventSelection/TriggerAliases against what the CTP recorded: - class: bc.triggerMask() against EMC-cluster, B-bunch classes taken from CTP/Config/Config by their input content - input: bc.inputMask() against the CTP inputs (0EMC, 0DMC, 0EG1/2, 0EJ1/2, 0DG1/2, 0DJ1/2, MTVX), with a fallback to the default input indices if an input is missing from the config Once per run, the CTP-derived class masks are compared with the uploaded TriggerAliases object, including the Next50 map, and the result goes to hAliasCCDBStatusPerRun plus one summary log line. Per-trigger details are logged at debug level. Per-BC agreement is stored in hTrgAliasVsClass, hTrgClassVsInput, hTrgAliasVsInput, and in bcID- and run-resolved mismatch histograms. The CCDB objects are loaded through a declarative CCDB table, so they are only fetched when the process switch is enabled. Link O2::DataFormatsCTP and O2Physics::AnalysisCCDB. Co-Authored-By: Claude Opus 5.5 --- PWGJE/Tasks/CMakeLists.txt | 2 +- PWGJE/Tasks/emcEventSelectionQA.cxx | 335 ++++++++++++++++++++++++++-- 2 files changed, 322 insertions(+), 15 deletions(-) diff --git a/PWGJE/Tasks/CMakeLists.txt b/PWGJE/Tasks/CMakeLists.txt index 3a558ae55df..9bc51ff4fae 100644 --- a/PWGJE/Tasks/CMakeLists.txt +++ b/PWGJE/Tasks/CMakeLists.txt @@ -70,7 +70,7 @@ o2physics_add_dpl_workflow(emc-clustermonitor COMPONENT_NAME Analysis) o2physics_add_dpl_workflow(emc-eventselection-qa SOURCES emcEventSelectionQA.cxx - PUBLIC_LINK_LIBRARIES O2::Framework O2::EMCALBase O2::EMCALCalib O2Physics::AnalysisCore + PUBLIC_LINK_LIBRARIES O2::Framework O2::EMCALBase O2::EMCALCalib O2::DataFormatsCTP O2Physics::AnalysisCore O2Physics::AnalysisCCDB COMPONENT_NAME Analysis) o2physics_add_dpl_workflow(emc-vertexselection-qa SOURCES emcVertexSelectionQA.cxx diff --git a/PWGJE/Tasks/emcEventSelectionQA.cxx b/PWGJE/Tasks/emcEventSelectionQA.cxx index ff39021dce6..47195b66bcf 100644 --- a/PWGJE/Tasks/emcEventSelectionQA.cxx +++ b/PWGJE/Tasks/emcEventSelectionQA.cxx @@ -11,7 +11,7 @@ /// \file emcEventSelectionQA.cxx /// \brief Monitoring task for EMCAL event selection -/// \author Markus Fasel , Oak Ridge National Laoratory +/// \author Markus Fasel , Oak Ridge National Laoratory; Marvin Hemmer , Goethe Univeristy Frankfurt #include "PWGJE/Core/utilsBcSelEMC.h" @@ -19,29 +19,96 @@ #include "Common/CCDB/TriggerAliases.h" #include "Common/DataModel/EventSelection.h" +#include +#include #include #include #include +#include #include #include #include +#include #include +#include +#include // IWYU pragma: keep (needed by DECLARE_SOA_CCDB_COLUMN...) +#include // IWYU pragma: keep (needed by DECLARE_SOA_CCDB_COLUMN...) #include +#include +#include +#include +#include +#include #include +#include +#include #include #include +#include using namespace o2; using namespace o2::framework; using namespace o2::framework::expressions; using namespace o2::emc_evsel; +namespace o2::aod +{ +namespace emceventselectionqa +{ +/// CTP configuration of the run, queried with the run number as metadata (run-dependence mode 1), +/// as CTP/Config/Config requires it to handle concurrent runs +DECLARE_SOA_CCDB_COLUMN_FULL(CTPConfiguration, "fCTPConfiguration", ctpConfiguration, o2::ctp::CTPConfiguration, "CTP/Config/Config", 1, //! + [](o2::ctp::CTPConfiguration* ctpConfig) { return ctpConfig; }); +/// Trigger aliases as uploaded by Common/CCDB/macros/upload_trigger_aliases_run3.C +DECLARE_SOA_CCDB_COLUMN(EvSelTriggerAliases, evSelTriggerAliases, ::TriggerAliases, "EventSelection/TriggerAliases"); //! +} // namespace emceventselectionqa + +/// Uniform in the run number: both objects are uploaded once per run, so the fetcher queries once per run. +DECLARE_SOA_UNIFORM_TABLE(EmcEventSelectionQACCDBObjects, aod::Timestamps, o2::aod::timestamp::Timestamp, + aod::BCs, o2::aod::bc::RunNumber, 1, "EMCEVSELQACCDB", //! + emceventselectionqa::CTPConfiguration, emceventselectionqa::EvSelTriggerAliases); +} // namespace o2::aod + using BCEvSels = o2::soa::Join; +using BCEvSelsCTP = o2::soa::Join; using CollEventSels = o2::soa::Join; using FilteredCells = o2::soa::Filtered; +namespace +{ +/// EMCal triggers compared between trigger alias, CTP trigger class and CTP input +struct EMCTriggerDefinition { + int alias; ///< trigger alias (Common/CCDB/TriggerAliases.h) + const char* label; ///< label used for histogram bins + const char* input; ///< CTP input name without level prefix + uint32_t defaultInputIndex; ///< CTP input index (1-48) from o2::ctp::CTPInputsConfiguration::CTPInputsDefault, used if the input is not in the run's CTP config + bool isL0; ///< L0 trigger: classes must not require any EMCal L1 input +}; +constexpr int NEMCTriggers = 11; +constexpr int TriggerMB = 0; +constexpr std::array EMCTriggers{{{kTVXinEMC, "TVXinEMC", "TVX", 3, true}, + {kEMC7, "EMC7", "EMC", 21, true}, + {kDMC7, "DMC7", "DMC", 14, true}, + {kEG1, "EG1", "EG1", 38, false}, + {kEG2, "EG2", "EG2", 40, false}, + {kEJ1, "EJ1", "EJ1", 37, false}, + {kEJ2, "EJ2", "EJ2", 39, false}, + {kDG1, "DG1", "DG1", 42, false}, + {kDG2, "DG2", "DG2", 44, false}, + {kDJ1, "DJ1", "DJ1", 41, false}, + {kDJ2, "DJ2", "DJ2", 43, false}}}; +/// Status of the trigger alias in the CCDB with respect to the classes found in the CTP configuration +enum AliasCCDBStatus { + kAliasNotInRun = 1, ///< no class in CTP config and no alias defined + kAliasNotDefined, ///< classes in CTP config, but no alias defined in CCDB + kAliasEqual, ///< alias class mask equal to CTP class mask + kAliasDifferent, ///< alias class mask different from CTP class mask + kAliasClassAbove50 ///< alias contains classes at position >= 50, not considered for Run 3 alias bits +}; +} // namespace + struct EmcEventSelectionQA { EMCEventSelection emcEvSel; // event selection and monitoring @@ -50,7 +117,13 @@ struct EmcEventSelectionQA { // Require EMCAL cells (CALO type 1) Filter emccellfilter = aod::calo::caloType == 1; - const int mRun3MinNumber = 300000; + static constexpr int mRun3MinNumber = 300000; + + // per-run cache for the comparison of trigger alias, CTP class and CTP input + int mLastRun = -1; + int mRunBin = -1; + std::array mClassMask{}; + std::array mInputMask{}; void init(o2::framework::InitContext const&) { @@ -113,6 +186,231 @@ struct EmcEventSelectionQA { initCollisionHistogram(mHistManager.get(HIST("hCollisionMatchingDJ2")).get()); emcEvSel.addHistograms(mHistManager); // collision monitoring + + if (doprocessCTPComparison) { + O2Axis triggerAxis{NEMCTriggers, -0.5, NEMCTriggers - 0.5, "trigger", "EMCal trigger"}, + comparisonAxis{4, -0.5, 3.5, "comparison", "Comparison"}, + runAxis{1, 0., 1., "run", "Run"}; // alphanumeric, extended with one bin per run + mHistManager.add("hTrgAliasVsClass", "Trigger alias vs. CTP trigger class", O2HistType::kTH2F, {triggerAxis, comparisonAxis}); + mHistManager.add("hTrgClassVsInput", "CTP trigger class vs. CTP input", O2HistType::kTH2F, {triggerAxis, comparisonAxis}); + mHistManager.add("hTrgAliasVsInput", "Trigger alias vs. CTP input", O2HistType::kTH2F, {triggerAxis, comparisonAxis}); + mHistManager.add("hBCTrgInput", "Bunch crossings with EMCal CTP input", O2HistType::kTH2F, {bcAxis, triggerAxis}); + mHistManager.add("hBCTrgClass", "Bunch crossings with EMCal CTP trigger class", O2HistType::kTH2F, {bcAxis, triggerAxis}); + mHistManager.add("hBCTrgAliasClassMismatch", "Bunch crossings with trigger alias != CTP trigger class", O2HistType::kTH2F, {bcAxis, triggerAxis}); + mHistManager.add("hTrgAliasClassMismatchPerRun", "Number of BCs with trigger alias != CTP trigger class", O2HistType::kTH2F, {runAxis, triggerAxis}); + mHistManager.add("hAliasCCDBStatusPerRun", "Trigger alias in CCDB vs. classes in CTP config (1: not in run, 2: alias not defined, 3: equal, 4: different, 5: class position >= 50)", O2HistType::kTH2F, {runAxis, triggerAxis}); + + initComparisonHistogram(mHistManager.get(HIST("hTrgAliasVsClass")).get(), "alias only", "class only"); + initComparisonHistogram(mHistManager.get(HIST("hTrgClassVsInput")).get(), "class only", "input only"); + initComparisonHistogram(mHistManager.get(HIST("hTrgAliasVsInput")).get(), "alias only", "input only"); + setTriggerLabels(mHistManager.get(HIST("hBCTrgInput")).get()->GetYaxis()); + setTriggerLabels(mHistManager.get(HIST("hBCTrgClass")).get()->GetYaxis()); + setTriggerLabels(mHistManager.get(HIST("hBCTrgAliasClassMismatch")).get()->GetYaxis()); + setTriggerLabels(mHistManager.get(HIST("hTrgAliasClassMismatchPerRun")).get()->GetYaxis()); + setTriggerLabels(mHistManager.get(HIST("hAliasCCDBStatusPerRun")).get()->GetYaxis()); + } + } + + void setTriggerLabels(TAxis* axis) + { + for (int itrg = 0; itrg < NEMCTriggers; itrg++) { + axis->SetBinLabel(itrg + 1, EMCTriggers[itrg].label); + } + } + + /// x: trigger, y: 0 = neither, 1 = only first, 2 = only second, 3 = both + void initComparisonHistogram(TH2* hist, const char* onlyFirst, const char* onlySecond) + { + setTriggerLabels(hist->GetXaxis()); + hist->GetYaxis()->SetBinLabel(1, "neither"); + hist->GetYaxis()->SetBinLabel(2, onlyFirst); + hist->GetYaxis()->SetBinLabel(3, onlySecond); + hist->GetYaxis()->SetBinLabel(4, "both"); + } + + static bool equalsIgnoreCase(std::string_view lhs, std::string_view rhs) + { + const auto toUpper = [](unsigned char c) { return std::toupper(c); }; + return std::ranges::equal(lhs, rhs, {}, toUpper, toUpper); + } + + /// Bunch type of a CTP class name ---, e.g. "B" for "CTVXEMC-B-NOPF-EMC". + /// Names without '-' (e.g. "minbias_TVX_L0" in early configurations) are treated as "B". + static std::string_view bunchType(std::string_view className) + { + const auto first = className.find('-'); + if (first == std::string_view::npos) { + return "B"; + } + const auto second = className.find('-', first + 1); + return className.substr(first + 1, second == std::string_view::npos ? std::string_view::npos : second - first - 1); + } + + /// Find the CTP input mask for an input name in the run's CTP config, trying the level prefixes M/0/1. + /// Fall back to the default input index if the input is not part of the config (returned as second). + static std::pair findInputMask(o2::ctp::CTPConfiguration const& cfg, const EMCTriggerDefinition& trg) + { + for (const char* level : {"M", "0", "1"}) { + const std::string name = std::string(level) + trg.input; + const auto mask = cfg.getInputMask(name); + if (mask != 0) { + LOGP(debug, " {}: CTP input {} found in CTP config, bit {}", trg.label, name, std::countr_zero(mask)); + return {mask, false}; + } + } + LOGP(debug, " {}: CTP input {} not in CTP config, using default index {} (bit {})", trg.label, trg.input, trg.defaultInputIndex, trg.defaultInputIndex - 1); + return {uint64_t{1} << (trg.defaultInputIndex - 1), true}; + } + + static const char* statusName(int status) + { + switch (status) { + case kAliasNotInRun: + return "not in run"; + case kAliasNotDefined: + return "alias not defined"; + case kAliasEqual: + return "equal"; + case kAliasDifferent: + return "different classes"; + case kAliasClassAbove50: + return "class position >= 50"; + default: + return "unknown"; + } + } + + /// Build per-run class and input masks from the CTP configuration and compare them with the uploaded trigger aliases + void updateCTPConfig(BCEvSelsCTP::iterator const& bc) + { + if (bc.runNumber() == mLastRun) { + return; + } + mLastRun = bc.runNumber(); + mClassMask.fill(0); + mInputMask.fill(0); + const std::string runLabel = std::to_string(mLastRun); + mRunBin = mHistManager.get(HIST("hTrgAliasClassMismatchPerRun")).get()->GetXaxis()->FindBin(runLabel.c_str()); + + // read the CCDB objects only here, on run change: the references are not valid across rows of a different run + auto const& cfg = bc.ctpConfiguration(); + + LOGP(debug, "Run {}: EMCal CTP inputs", mLastRun); + uint64_t emcL1InputMask = 0; + std::string defaultInputs; // triggers for which the input is not in the CTP config + for (int itrg = 0; itrg < NEMCTriggers; itrg++) { + const auto [inputMask, isDefault] = findInputMask(cfg, EMCTriggers[itrg]); + mInputMask[itrg] = inputMask; + if (isDefault) { + defaultInputs += std::string(defaultInputs.empty() ? "" : ", ") + EMCTriggers[itrg].label; + } + if (!EMCTriggers[itrg].isL0) { + emcL1InputMask |= mInputMask[itrg]; + } + } + + // assign EMCal classes (cluster EMC) to the triggers based on the inputs of their descriptor + std::array classNames; + for (const auto& cls : cfg.getCTPClasses()) { + if (!cls.cluster || !cls.descriptor || !equalsIgnoreCase(cls.cluster->name, "EMC")) { + continue; + } + if (!equalsIgnoreCase(bunchType(cls.name), "B")) { + continue; // only classes for colliding bunches + } + const uint64_t classInputs = cls.descriptor->getInputsMask(); + const bool hasEMCInput = std::ranges::any_of(cls.descriptor->inputs, [](const auto* inp) { return inp && inp->detID == o2::detectors::DetID::EMC; }); + for (int itrg = 0; itrg < NEMCTriggers; itrg++) { + bool matches = false; + if (itrg == TriggerMB) { + matches = (classInputs & mInputMask[itrg]) != 0 && !hasEMCInput; + } else { + matches = (classInputs & mInputMask[itrg]) != 0 && (!EMCTriggers[itrg].isL0 || (classInputs & emcL1InputMask) == 0); + } + if (matches) { + mClassMask[itrg] |= cls.classMask; + classNames[itrg] += (classNames[itrg].empty() ? "" : ", ") + cls.name + " (pos " + std::to_string(cls.getIndex()) + ")"; + } + } + } + + // compare with the uploaded trigger aliases + auto const& aliases = bc.evSelTriggerAliases(); + auto* hStatus = mHistManager.get(HIST("hAliasCCDBStatusPerRun")).get(); + const int statusRunBin = hStatus->GetXaxis()->FindBin(runLabel.c_str()); + LOGP(debug, "Run {}: EMCal trigger classes and trigger aliases", mLastRun); + std::string mismatches; // triggers whose alias does not match the CTP configuration + for (int itrg = 0; itrg < NEMCTriggers; itrg++) { + const auto& trg = EMCTriggers[itrg]; + uint64_t aliasMask = 0; + bool aliasDefined = false; + bool aliasAbove50 = false; + const auto& maskMap = aliases.GetAliasToTriggerMaskMap(); + if (auto it = maskMap.find(trg.alias); it != maskMap.end()) { + aliasMask = it->second; + aliasDefined = true; + } + const auto& maskMapNext50 = aliases.GetAliasToTriggerMaskNext50Map(); + if (auto it = maskMapNext50.find(trg.alias); it != maskMapNext50.end() && it->second != 0) { + aliasDefined = true; + aliasAbove50 = true; + } + int status = kAliasEqual; + if (aliasAbove50) { + status = kAliasClassAbove50; + } else if (!aliasDefined) { + status = mClassMask[itrg] == 0 ? kAliasNotInRun : kAliasNotDefined; + } else if (aliasMask != mClassMask[itrg]) { + status = kAliasDifferent; + } + hStatus->SetBinContent(statusRunBin, itrg + 1, status); + LOGP(debug, " {}: {}, CTP classes [{}], class mask CTP {:#018x}, alias mask CCDB {:#018x}, missing class bits {:#018x}, extra class bits {:#018x}", trg.label, statusName(status), classNames[itrg], + mClassMask[itrg], aliasMask, mClassMask[itrg] & ~aliasMask, aliasMask & ~mClassMask[itrg]); + if (status == kAliasNotDefined || status == kAliasDifferent || status == kAliasClassAbove50) { + mismatches += std::string(mismatches.empty() ? "" : ", ") + trg.label + " (" + statusName(status) + ")"; + } + } + + // one summary line per run, details with --severity debug + if (mismatches.empty()) { + LOGP(info, "Run {}: EMCal trigger aliases match the CTP configuration{}", mLastRun, defaultInputs.empty() ? "" : " (default CTP input index used for " + defaultInputs + ")"); + } else { + LOGP(warn, "Run {}: EMCal trigger aliases not matching the CTP configuration: {}{}. See hAliasCCDBStatusPerRun, details with --severity debug", mLastRun, mismatches, + defaultInputs.empty() ? "" : " (default CTP input index used for " + defaultInputs + ")"); + } + } + + /// Bin of the comparison histograms: 0 = neither, 1 = only first, 2 = only second, 3 = both + static int comparisonBin(bool first, bool second) + { + return (first ? 1 : 0) + (second ? 2 : 0); + } + + /// Compare trigger alias, CTP class and CTP input for all EMCal triggers in a BC + void compareTriggers(BCEvSelsCTP::iterator const& bc, uint64_t bcID) + { + updateCTPConfig(bc); + const uint64_t triggerMask = bc.triggerMask(); + const uint64_t inputMask = bc.inputMask(); + for (int itrg = 0; itrg < NEMCTriggers; itrg++) { + const bool isAlias = bc.alias_bit(EMCTriggers[itrg].alias); + const bool isClass = (triggerMask & mClassMask[itrg]) != 0; + const bool isInput = (inputMask & mInputMask[itrg]) != 0; + mHistManager.fill(HIST("hTrgAliasVsClass"), itrg, comparisonBin(isAlias, isClass)); + mHistManager.fill(HIST("hTrgClassVsInput"), itrg, comparisonBin(isClass, isInput)); + mHistManager.fill(HIST("hTrgAliasVsInput"), itrg, comparisonBin(isAlias, isInput)); + if (isInput) { + mHistManager.fill(HIST("hBCTrgInput"), bcID, itrg); + } + if (isClass) { + mHistManager.fill(HIST("hBCTrgClass"), bcID, itrg); + } + if (isAlias != isClass) { + mHistManager.fill(HIST("hBCTrgAliasClassMismatch"), bcID, itrg); + auto* hRun = mHistManager.get(HIST("hTrgAliasClassMismatchPerRun")).get(); + hRun->Fill(hRun->GetXaxis()->GetBinCenter(mRunBin), itrg); + } + } } PresliceUnsorted perFoundBC = aod::evsel::foundBCId; @@ -163,15 +461,14 @@ struct EmcEventSelectionQA { if (rejectionMask != 0) { // at least one event selection not satisfied --> reject the candidate continue; - } else { - if (isEMCALreadout) { - mHistManager.fill(HIST("hBCNotAcceptedButEMCalReadout"), bcID); - } - if (found != cellGlobalBCs.end()) { - // require at least 1 cell for global BC - if (found->second > 0) { - mHistManager.fill(HIST("hBCNotAcceptedButEmcalCellContent"), bcID); - } + } + if (isEMCALreadout) { + mHistManager.fill(HIST("hBCNotAcceptedButEMCalReadout"), bcID); + } + if (found != cellGlobalBCs.end()) { + // require at least 1 cell for global BC + if (found->second > 0) { + mHistManager.fill(HIST("hBCNotAcceptedButEmcalCellContent"), bcID); } } @@ -234,7 +531,7 @@ struct EmcEventSelectionQA { auto collisionsGrouped = collisions.sliceBy(perFoundBC, bc.globalIndex()); int collisionStatus = -1; - if (!collisionsGrouped.size()) { + if (collisionsGrouped.size() == 0) { collisionStatus = 0; } else if (collisionsGrouped.size() == 1) { collisionStatus = 1; @@ -296,6 +593,16 @@ struct EmcEventSelectionQA { } } + /// Run 3 only: compare EMCal trigger aliases with CTP trigger classes and CTP inputs for all BCs, independent of the BC selection. + /// Requires the CTP configuration and the trigger aliases in CCDB for every run, a missing object is fatal. + void processCTPComparison(BCEvSelsCTP const& bcs) + { + for (const auto& bc : bcs) { + compareTriggers(bc, bc.globalBC() % 3564); + } + } + PROCESS_SWITCH(EmcEventSelectionQA, processCTPComparison, "Run 3 only: compare EMCal trigger aliases with CTP trigger classes and CTP inputs", false); + void initCollisionHistogram(TH1* hist) { // Beautify collision type lables @@ -307,8 +614,8 @@ struct EmcEventSelectionQA { } }; -o2::framework::WorkflowSpec defineDataProcessing(o2::framework::ConfigContext const& cfgc) +o2::framework::WorkflowSpec defineDataProcessing(o2::framework::ConfigContext const& context) { return o2::framework::WorkflowSpec{ - o2::framework::adaptAnalysisTask(cfgc)}; + o2::framework::adaptAnalysisTask(context)}; }