10#include <analysis/variables/TriggerVariables.h>
12#include <analysis/dataobjects/Particle.h>
15#include <mdst/dataobjects/TRGSummary.h>
16#include <mdst/dataobjects/SoftwareTriggerResult.h>
19#include <mdst/dbobjects/TRGGDLDBFTDLBits.h>
20#include <mdst/dbobjects/TRGGDLDBPrescales.h>
23#include <mdst/dbobjects/DBRepresentationOfSoftwareTriggerCut.h>
26#include <framework/logging/Logger.h>
27#include <framework/datastore/StoreObjPtr.h>
28#include <framework/database/DBObjPtr.h>
31#include <boost/algorithm/string.hpp>
57 std::string fullFormatIdentifier(
const std::string& identifier)
59 std::string out = identifier;
60 boost::replace_all(out,
" ",
"&");
61 if (identifier.substr(0, 21) !=
"software_trigger_cut&")
62 out =
"software_trigger_cut&" + out;
70 double extractSoftwareTriggerResultImplementation(
bool nonPrescaled,
const std::string& triggerIdentifier,
const Particle*)
81 swtcr = swtr->getNonPrescaledResult(fullFormatIdentifier(triggerIdentifier));
83 swtcr = swtr->getResult(fullFormatIdentifier(triggerIdentifier));
85 }
catch (
const std::out_of_range&) {
95 double L1Trigger(
const Particle*)
97 StoreObjPtr<TRGSummary> trg;
105 if (arguments.size() == 1) {
106 auto name = arguments[0];
107 auto func = [name](
const Particle*) ->
double {
108 StoreObjPtr<TRGSummary> trg;
113 return trg->testPsnm(name);
114 }
catch (
const std::exception&)
122 B2FATAL(
"Wrong number of arguments for L1PSNM function. The only argument must be the name of the PSNM trigger bit.");
126 double L1PSNMBit(
const Particle*,
const std::vector<double>& arguments)
128 if (arguments.size() == 1) {
129 StoreObjPtr<TRGSummary> trg;
133 int testBit = std::lround(arguments[0]);
134 return trg->testPsnm(testBit);
135 }
catch (
const std::exception&) {
140 B2FATAL(
"Wrong number of arguments for L1PSNMBit function. The only argument must be the number of the PSNM trigger bit.");
146 if (arguments.size() == 1) {
147 auto name = arguments[0];
148 auto func = [name](
const Particle*) ->
double {
149 StoreObjPtr<TRGSummary> trg;
154 return trg->testFtdl(name);
155 }
catch (
const std::exception&)
163 B2FATAL(
"Wrong number of arguments for L1FTDL function. The only argument must be the name of the FTDL trigger bit.");
167 double L1FTDLBit(
const Particle*,
const std::vector<double>& arguments)
169 if (arguments.size() == 1) {
170 StoreObjPtr<TRGSummary> trg;
174 int testBit = std::lround(arguments[0]);
175 return trg->testFtdl(testBit);
176 }
catch (
const std::exception&) {
181 B2FATAL(
"Wrong number of arguments for L1FTDLBit function. The only argument must be the number of the FTDL trigger bit.");
187 if (arguments.size() == 1) {
188 auto name = arguments[0];
189 auto func = [name](
const Particle*) ->
double {
190 StoreObjPtr<TRGSummary> trg;
195 return trg->testInput(name);
196 }
catch (
const std::exception&)
204 B2FATAL(
"Wrong number of arguments for L1Input function. The only argument must be the name of the input trigger bit.");
208 double L1InputBit(
const Particle*,
const std::vector<double>& arguments)
210 if (arguments.size() == 1) {
211 StoreObjPtr<TRGSummary> trg;
215 int testBit = std::lround(arguments[0]);
216 return trg->testInput(testBit);
217 }
catch (
const std::exception&) {
222 B2FATAL(
"Wrong number of arguments for L1Input function. The only argument must be the number of the input trigger bit.");
228 if (arguments.size() == 1) {
229 auto name = arguments[0];
230 auto func = [name](
const Particle*) ->
double {
231 static DBObjPtr<TRGGDLDBFTDLBits> ftdlBits;
232 if (!ftdlBits.isValid())
234 static DBObjPtr<TRGGDLDBPrescales> prescales;
235 if (!prescales.isValid())
239 if (std::string(ftdlBits->getoutbitname((
int)bit)) == name)
240 return prescales->getprescales(bit);
246 B2FATAL(
"Wrong number of arguments for L1Prescale function. The only argument must be the name of the PSNM trigger bit.");
250 double L1PSNMBitPrescale(
const Particle*,
const std::vector<double>& arguments)
252 if (arguments.size() == 1) {
253 int testBit = std::lround(arguments[0]);
257 static DBObjPtr<TRGGDLDBPrescales> prescales;
258 if (!prescales.isValid())
260 return prescales->getprescales(testBit);
262 B2FATAL(
"Wrong number of arguments for L1BitPrescale function. The only argument must be the number of the PSNM trigger bit.");
266 double L1TimeType(
const Particle*)
268 StoreObjPtr<TRGSummary> trg;
271 return trg->getTimType();
274 double L1TimeQuality(
const Particle*)
276 StoreObjPtr<TRGSummary> trg;
279 return trg->getTimQuality();
282 double isPoissonInInjectionVeto(
const Particle*)
284 StoreObjPtr<TRGSummary> trg;
287 return trg->isPoissonInInjectionVeto();
299 if (args.size() != 1)
300 B2FATAL(
"Wrong number of arguments for the function softwareTriggerResult");
301 std::string triggerIdentifier = args[0];
303 using namespace std::placeholders;
304 return std::bind(extractSoftwareTriggerResultImplementation,
false, triggerIdentifier, _1);
316 if (args.size() != 1)
317 B2FATAL(
"Wrong number of arguments for the function softwareTriggerResultNonPrescaled");
318 std::string triggerIdentifier = args[0];
320 using namespace std::placeholders;
321 return std::bind(extractSoftwareTriggerResultImplementation,
true, triggerIdentifier, _1);
324 bool passesAnyHighLevelTrigger(
const Particle* p)
328 std::vector<std::string> hardcodedname
329 = {
"software_trigger_cut&filter&total_result" };
330 double swtcr = std::get<double>(softwareTriggerResult(hardcodedname)(p));
331 if (swtcr > 0.5)
return true;
344 if (args.size() != 1)
345 B2FATAL(
"Wrong number of arguments for the function softwareTriggerPrescaling");
346 std::string triggerIdentifier = args[0];
348 auto outputFunction = [triggerIdentifier](
const Particle*) ->
double {
350 DBObjPtr<DBRepresentationOfSoftwareTriggerCut> downloadedCut(fullFormatIdentifier(triggerIdentifier));
351 if (not downloadedCut)
353 return double(downloadedCut->getPreScaleFactor());
356 return outputFunction;
360 VARIABLE_GROUP(
"L1 Trigger");
361 REGISTER_VARIABLE(
"L1Trigger", L1Trigger,
362 "[Eventbased] Returns 1 if at least one PSNM L1 trigger bit is true.");
363 REGISTER_METAVARIABLE(
"L1PSNM(name)", L1PSNM,
365[Eventbased] Returns the PSNM (Prescale And Mask, after prescale) status (1 or 0) of the output trigger bit with the given name.
366For some output trigger bits, we assign a prescale factor to reduce the number of triggered events.
367For example, we want to keep only 1% of Bhabha events. A prescale factor of 100 is then assigned to ``bha_3D`` (Bhabha selected in 3D criteria).
368Prescale factor of a given output trigger bit could be different in different datasets.
369It is recommended to use prescaled trigger bits (L1PSNM) or un-prescaled trigger bits (L1FTDL) for your analysis.
370In run-independent MC, configuration of the prescales in TSIM (trigger simulation) can be different from data, so L1 FTDL is recommended.
371In run-dependent MC, configuration of the prescales in TSIM is consistent with data, so L1PSNM is recommended.
372Please check on `the dedicated XWiki page <https://xwiki.desy.de/xwiki/rest/p/2471f>`__ or `the dedicated Belle II notes <https://docs.belle2.org/search?ln=en&p=%22Trigger+Summary%22&f=&action_search=Search&c=Belle+II+Notes>`__ to find out the definition of trigger bits.
374 Manager::VariableDataType::c_double);
375 REGISTER_METAVARIABLE("L1FTDL(name)", L1FTDL,
377[Eventbased] Returns the FTDL (Final Trigger Decision Logic, before prescale) status (1 or 0) of the output trigger bit with the given name. Output bits are the outputs of GDL, combining different input trigger bits for final decision. For example, ``ty_0/1/2/3`` is one of the input trigger bits meaning the number of neuro 3D tracks is one/two/three/more than three. While ``yyy`` is one of the output trigger bits meaning ``(ty_2 or ty_3) and !veto``. Please check on `the dedicated XWiki page <https://xwiki.desy.de/xwiki/rest/p/2471f>`__ or `the dedicated Belle II notes <https://docs.belle2.org/search?ln=en&p=%22Trigger+Summary%22&f=&action_search=Search&c=Belle+II+Notes>`__ to find out the definition of trigger bits.
379 Manager::VariableDataType::c_double);
380 REGISTER_METAVARIABLE("L1Input(name)", L1Input,
382[Eventbased] Returns the input bit status (1 or 0) of the trigger bit with the given name. Input trigger bits are predefined selections from each sub-detector, with adjustment of the delay and width, in order to fix latency on GDL. For example, ``ty_0/1/2/3`` is one of the input trigger bits meaning the number of neuro 3D tracks is one/two/three/more than three. Please check on `the dedicated XWiki page <https://xwiki.desy.de/xwiki/rest/p/2471f>`__ or `the dedicated Belle II notes <https://docs.belle2.org/search?ln=en&p=%22Trigger+Summary%22&f=&action_search=Search&c=Belle+II+Notes>`__ to find out the definition of trigger bits.
384 Manager::VariableDataType::c_double);
385 REGISTER_METAVARIABLE("L1Prescale(name)", L1PSNMPrescale,
387[Eventbased] Returns the PSNM (prescale and mask) prescale factor of the trigger bit with the given name. Definition of prescale factor is shown in a few lines before in `L1PSNM`. Prescale factors are usually dependent on different datasets.
389 Manager::VariableDataType::c_double);
390 REGISTER_VARIABLE("L1PSNMBit(i)", L1PSNMBit,
392[Eventbased] Returns the PSNM (Prescale And Mask, after prescale) status (1 or 0) of i-th trigger bit.
395 It is recommended to use this variable only for debugging and to use :b2:var:`L1PSNM`
396 with the explicit trigger bit name for physics analyses or performance studies.
398 REGISTER_VARIABLE("L1FTDLBit(i)", L1FTDLBit,
400[Eventbased] Returns the FTDL (Final Trigger Decision Logic, before prescale) status (1 or 0) of i-th trigger bit.
403 It is recommended to use this variable only for debugging and to use :b2:var:`L1FTDL`
404 with the explicit trigger bit name for physics analyses or performance studies.
406 REGISTER_VARIABLE("L1InputBit(i)", L1InputBit,
408[Eventbased] Returns the input bit status (1 or 0) of the i-th input trigger bit.
411 It is recommended to use this variable only for debugging and to use :b2:var:`L1Input`
412 with the explicit trigger bit name for physics analyses or performance studies.
414 REGISTER_VARIABLE("L1PSNMBitPrescale(i)", L1PSNMBitPrescale,
416[Eventbased] Returns the PSNM (prescale and mask) prescale of i-th trigger bit.
419 It is recommended to use this variable only for debugging and to use :b2:var:`L1Prescale`
420 with the explicit trigger bit name for physics analyses or performance studies.
422 REGISTER_VARIABLE("L1TimeType", L1TimeType,
423 "[Eventbased] Returns kind of detector which determines the Level1 trigger timing. 0:ECL, 1:TOP, 2:SELF(timing of PSNM bit), 3:CDC, 5:delayed bhabha, 7: random, 13:poisson.");
424 REGISTER_VARIABLE(
"L1TimeQuality", L1TimeQuality,
425 "[Eventbased] Returns expected Level1 timing resolution. This flag will be used for SVD 3-point sampling in future. 0:None; 1:Coarse (sigma > x ns); 2:FINE (sigma < x ns); x has been set to about 5ns before LS1 but can be changed in future");
426 REGISTER_VARIABLE(
"isPoissonTriggerInInjectionVeto", isPoissonInInjectionVeto,
427 "[Eventbased] Returns 1 if the poisson random trigger is within the injection veto window.");
429 VARIABLE_GROUP(
"Software Trigger");
430 REGISTER_METAVARIABLE(
"SoftwareTriggerResult(triggerIdentifier)", softwareTriggerResult, R
"DOC(
431[Eventbased] [Expert] returns the SoftwareTriggerCutResult, defined as reject (-1), accept (1), or noResult (0).
432If the trigger identifier is not found, returns NaN.
438 SoftwareTriggerResult(filter 1_Estargt1_GeV_cluster_no_other_cluster_Estargt0.3_GeV)
440which is equivalent to
444 SoftwareTriggerResult(software_trigger_cut&filter&1_Estargt1_GeV_cluster_no_other_cluster_Estargt0.3_GeV)
447.. warning:: the meanings of these change depending if using trigger or the skim stage, hence expert.
449.. seealso:: ``b2hlt_triggers`` for possible triggerIdentifiers.
451 )DOC", Manager::VariableDataType::c_double);
453 REGISTER_METAVARIABLE("SoftwareTriggerResultNonPrescaled(triggerIdentifier)", softwareTriggerResultNonPrescaled,
454 "[Eventbased] [Expert] returns the SoftwareTriggerCutResult, "
455 "if this trigger would not be prescaled."
456 "Please note, this is not the final HLT decision! "
457 "It is defined as reject (-1), accept (1), or noResult (0). Note "
458 "that the meanings of these change depending if using trigger "
459 "or the skim stage, hence expert."
460 "If the trigger identifier is not found, returns NaN.", Manager::VariableDataType::c_double);
461 REGISTER_VARIABLE(
"HighLevelTrigger", passesAnyHighLevelTrigger,
462 "[Eventbased] True if event passes the HLT trigger, false if not");
463 REGISTER_METAVARIABLE(
"SoftwareTriggerPrescaling(triggerIdentifier)", softwareTriggerPrescaling,
464 "[Eventbased] return the prescaling for the specific software trigger identifier. "
465 "Please note, this prescaling is taken from the currently setup database. It only corresponds "
466 "to the correct HLT prescale if you are using the online database!"
467 "If the trigger identifier is not found, returns NaN.", Manager::VariableDataType::c_double);
static const double doubleNaN
quiet_NaN
Class to store reconstructed particles.
Type-safe access to single objects in the data store.
static const unsigned int c_trgWordSize
size of a l1 trigger word
static const unsigned int c_ntrgWords
number of l1 trigger words
std::function< VarVariant(const Particle *)> FunctionPtr
functions stored take a const Particle* and return VarVariant.
SoftwareTriggerCutResult
Enumeration with all possible results of the SoftwareTriggerCut.
Abstract base class for different kinds of events.