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>
29#include <framework/utilities/Conversion.h>
32#include <boost/algorithm/string.hpp>
58 std::string fullFormatIdentifier(
const std::string& identifier)
60 std::string out = identifier;
61 boost::replace_all(out,
" ",
"&");
62 if (identifier.substr(0, 21) !=
"software_trigger_cut&")
63 out =
"software_trigger_cut&" + out;
71 double extractSoftwareTriggerResultImplementation(
bool nonPrescaled,
const std::string& triggerIdentifier,
const Particle*)
74 StoreObjPtr<SoftwareTriggerResult> swtr;
82 swtcr = swtr->getNonPrescaledResult(fullFormatIdentifier(triggerIdentifier));
84 swtcr = swtr->getResult(fullFormatIdentifier(triggerIdentifier));
86 }
catch (
const std::out_of_range&) {
96 double L1Trigger(
const Particle*)
98 StoreObjPtr<TRGSummary> trg;
106 if (arguments.size() == 1) {
107 auto name = arguments[0];
108 auto func = [name](
const Particle*) ->
double {
109 StoreObjPtr<TRGSummary> trg;
114 return trg->testPsnm(name);
115 }
catch (
const std::exception&)
123 B2FATAL(
"Wrong number of arguments for L1PSNM function. The only argument must be the name of the PSNM trigger bit.");
127 double L1PSNMBit(
const Particle*,
const std::vector<double>& arguments)
129 if (arguments.size() == 1) {
130 int testBit = std::lround(arguments[0]);
132 StoreObjPtr<TRGSummary> trg;
136 return trg->testPsnm(testBit);
137 }
catch (
const std::exception&) {
142 B2FATAL(
"Wrong number of arguments for L1PSNMBit function. The only argument must be the number of the PSNM trigger bit.");
148 if (arguments.size() == 1) {
149 auto name = arguments[0];
150 auto func = [name](
const Particle*) ->
double {
151 StoreObjPtr<TRGSummary> trg;
156 return trg->testFtdl(name);
157 }
catch (
const std::exception&)
165 B2FATAL(
"Wrong number of arguments for L1FTDL function. The only argument must be the name of the FTDL trigger bit.");
169 double L1FTDLBit(
const Particle*,
const std::vector<double>& arguments)
171 if (arguments.size() == 1) {
172 int testBit = std::lround(arguments[0]);
174 StoreObjPtr<TRGSummary> trg;
178 return trg->testFtdl(testBit);
179 }
catch (
const std::exception&) {
184 B2FATAL(
"Wrong number of arguments for L1FTDLBit function. The only argument must be the number of the FTDL trigger bit.");
190 if (arguments.size() == 1) {
191 auto name = arguments[0];
192 auto func = [name](
const Particle*) ->
double {
193 StoreObjPtr<TRGSummary> trg;
198 return trg->testInput(name);
199 }
catch (
const std::exception&)
207 B2FATAL(
"Wrong number of arguments for L1Input function. The only argument must be the name of the input trigger bit.");
211 double L1InputBit(
const Particle*,
const std::vector<double>& arguments)
213 if (arguments.size() == 1) {
214 int testBit = std::lround(arguments[0]);
216 StoreObjPtr<TRGSummary> trg;
220 return trg->testInput(testBit);
221 }
catch (
const std::exception&) {
226 B2FATAL(
"Wrong number of arguments for L1Input function. The only argument must be the number of the input trigger bit.");
232 if (arguments.size() == 1) {
233 auto name = arguments[0];
234 auto func = [name](
const Particle*) ->
double {
235 static DBObjPtr<TRGGDLDBFTDLBits> ftdlBits;
236 if (!ftdlBits.isValid())
238 static DBObjPtr<TRGGDLDBPrescales> prescales;
239 if (!prescales.isValid())
243 if (std::string(ftdlBits->getoutbitname((
int)bit)) == name)
244 return prescales->getprescales(bit);
250 B2FATAL(
"Wrong number of arguments for L1Prescale function. The only argument must be the name of the PSNM trigger bit.");
254 double L1PSNMBitPrescale(
const Particle*,
const std::vector<double>& arguments)
256 if (arguments.size() == 1) {
257 int testBit = std::lround(arguments[0]);
261 static DBObjPtr<TRGGDLDBPrescales> prescales;
262 if (!prescales.isValid())
264 return prescales->getprescales(testBit);
266 B2FATAL(
"Wrong number of arguments for L1BitPrescale function. The only argument must be the number of the PSNM trigger bit.");
270 double L1TimeType(
const Particle*)
272 StoreObjPtr<TRGSummary> trg;
275 return trg->getTimType();
278 double L1TimeQuality(
const Particle*)
280 StoreObjPtr<TRGSummary> trg;
283 return trg->getTimQuality();
286 double isPoissonInInjectionVeto(
const Particle*)
288 StoreObjPtr<TRGSummary> trg;
291 return trg->isPoissonInInjectionVeto();
303 if (args.size() != 1)
304 B2FATAL(
"Wrong number of arguments for the function softwareTriggerResult");
305 std::string triggerIdentifier = args[0];
307 using namespace std::placeholders;
308 return std::bind(extractSoftwareTriggerResultImplementation,
false, triggerIdentifier, _1);
320 if (args.size() != 1)
321 B2FATAL(
"Wrong number of arguments for the function softwareTriggerResultNonPrescaled");
322 std::string triggerIdentifier = args[0];
324 using namespace std::placeholders;
325 return std::bind(extractSoftwareTriggerResultImplementation,
true, triggerIdentifier, _1);
328 bool passesAnyHighLevelTrigger(
const Particle* p)
332 std::vector<std::string> hardcodedname
333 = {
"software_trigger_cut&filter&total_result" };
334 double swtcr = std::get<double>(softwareTriggerResult(hardcodedname)(p));
335 if (swtcr > 0.5)
return true;
348 if (args.size() != 1)
349 B2FATAL(
"Wrong number of arguments for the function softwareTriggerPrescaling");
350 std::string triggerIdentifier = args[0];
352 auto outputFunction = [triggerIdentifier](
const Particle*) ->
double {
354 DBObjPtr<DBRepresentationOfSoftwareTriggerCut> downloadedCut(fullFormatIdentifier(triggerIdentifier));
355 if (not downloadedCut)
357 return double(downloadedCut->getPreScaleFactor());
360 return outputFunction;
364 VARIABLE_GROUP(
"L1 Trigger");
365 REGISTER_VARIABLE(
"L1Trigger", L1Trigger,
366 "[Eventbased] Returns 1 if at least one PSNM L1 trigger bit is true.");
367 REGISTER_METAVARIABLE(
"L1PSNM(name)", L1PSNM,
369[Eventbased] Returns the PSNM (Prescale And Mask, after prescale) status (1 or 0) of the output trigger bit with the given name.
370For some output trigger bits, we assign a prescale factor to reduce the number of triggered events.
371For 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).
372Prescale factor of a given output trigger bit could be different in different datasets.
373It is recommended to use prescaled trigger bits (L1PSNM) or un-prescaled trigger bits (L1FTDL) for your analysis.
374In run-independent MC, configuration of the prescales in TSIM (trigger simulation) can be different from data, so L1 FTDL is recommended.
375In run-dependent MC, configuration of the prescales in TSIM is consistent with data, so L1PSNM is recommended.
376Please check on `the dedicated Confluence page <https://confluence.desy.de/display/BI/TriggerBitTable>`__ or 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.
378 Manager::VariableDataType::c_double);
379 REGISTER_METAVARIABLE("L1FTDL(name)", L1FTDL,
381[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 Confluence page <https://confluence.desy.de/display/BI/TriggerBitTable>`__ or 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.
383 Manager::VariableDataType::c_double);
384 REGISTER_METAVARIABLE("L1Input(name)", L1Input,
386[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 Confluence page <https://confluence.desy.de/display/BI/TriggerBitTable>`__ or 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.
388 Manager::VariableDataType::c_double);
389 REGISTER_METAVARIABLE("L1Prescale(name)", L1PSNMPrescale,
391[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.
393 Manager::VariableDataType::c_double);
394 REGISTER_VARIABLE("L1PSNMBit(i)", L1PSNMBit,
396[Eventbased] Returns the PSNM (Prescale And Mask, after prescale) status (1 or 0) of i-th trigger bit.
399 It is recommended to use this variable only for debugging and to use :b2:var:`L1PSNM`
400 with the explicit trigger bit name for physics analyses or performance studies.
402 REGISTER_VARIABLE("L1FTDLBit(i)", L1FTDLBit,
404[Eventbased] Returns the FTDL (Final Trigger Decision Logic, before prescale) status (1 or 0) of i-th trigger bit.
407 It is recommended to use this variable only for debugging and to use :b2:var:`L1FTDL`
408 with the explicit trigger bit name for physics analyses or performance studies.
410 REGISTER_VARIABLE("L1InputBit(i)", L1InputBit,
412[Eventbased] Returns the input bit status (1 or 0) of the i-th input trigger bit.
415 It is recommended to use this variable only for debugging and to use :b2:var:`L1Input`
416 with the explicit trigger bit name for physics analyses or performance studies.
418 REGISTER_VARIABLE("L1PSNMBitPrescale(i)", L1PSNMBitPrescale,
420[Eventbased] Returns the PSNM (prescale and mask) prescale of i-th trigger bit.
423 It is recommended to use this variable only for debugging and to use :b2:var:`L1Prescale`
424 with the explicit trigger bit name for physics analyses or performance studies.
426 REGISTER_VARIABLE("L1TimeType", L1TimeType,
427 "[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.");
428 REGISTER_VARIABLE(
"L1TimeQuality", L1TimeQuality,
429 "[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");
430 REGISTER_VARIABLE(
"isPoissonTriggerInInjectionVeto", isPoissonInInjectionVeto,
431 "[Eventbased] Returns 1 if the poisson random trigger is within the injection veto window.");
433 VARIABLE_GROUP(
"Software Trigger");
434 REGISTER_METAVARIABLE(
"SoftwareTriggerResult(triggerIdentifier)", softwareTriggerResult, R
"DOC(
435[Eventbased] [Expert] returns the SoftwareTriggerCutResult, defined as reject (-1), accept (1), or noResult (0).
436If the trigger identifier is not found, returns NaN.
442 SoftwareTriggerResult(filter 1_Estargt1_GeV_cluster_no_other_cluster_Estargt0.3_GeV)
444which is equivalent to
448 SoftwareTriggerResult(software_trigger_cut&filter&1_Estargt1_GeV_cluster_no_other_cluster_Estargt0.3_GeV)
451.. warning:: the meanings of these change depending if using trigger or the skim stage, hence expert.
453.. seealso:: ``b2hlt_triggers`` for possible triggerIdentifiers.
455 )DOC", Manager::VariableDataType::c_double);
457 REGISTER_METAVARIABLE("SoftwareTriggerResultNonPrescaled(triggerIdentifier)", softwareTriggerResultNonPrescaled,
458 "[Eventbased] [Expert] returns the SoftwareTriggerCutResult, "
459 "if this trigger would not be prescaled."
460 "Please note, this is not the final HLT decision! "
461 "It is defined as reject (-1), accept (1), or noResult (0). Note "
462 "that the meanings of these change depending if using trigger "
463 "or the skim stage, hence expert."
464 "If the trigger identifier is not found, returns NaN.", Manager::VariableDataType::c_double);
465 REGISTER_VARIABLE(
"HighLevelTrigger", passesAnyHighLevelTrigger,
466 "[Eventbased] True if event passes the HLT trigger, false if not");
467 REGISTER_METAVARIABLE(
"SoftwareTriggerPrescaling(triggerIdentifier)", softwareTriggerPrescaling,
468 "[Eventbased] return the prescaling for the specific software trigger identifier. "
469 "Please note, this prescaling is taken from the currently setup database. It only corresponds "
470 "to the correct HLT prescale if you are using the online database!"
471 "If the trigger identifier is not found, returns NaN.", Manager::VariableDataType::c_double);
static const double doubleNaN
quiet_NaN
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.