12 #include <analysis/variables/TriggerVariables.h>
13 #include <analysis/dataobjects/Particle.h>
16 #include <mdst/dataobjects/TRGSummary.h>
17 #include <mdst/dataobjects/SoftwareTriggerResult.h>
20 #include <mdst/dbobjects/TRGGDLDBFTDLBits.h>
21 #include <mdst/dbobjects/TRGGDLDBPrescales.h>
24 #include <mdst/dbobjects/DBRepresentationOfSoftwareTriggerCut.h>
27 #include <framework/logging/Logger.h>
28 #include <framework/datastore/StoreObjPtr.h>
29 #include <framework/database/DBObjPtr.h>
41 double extractSoftwareTriggerResultImplementation(
bool nonPrescaled,
const std::string& triggerIdentifier,
const Particle*)
44 StoreObjPtr<SoftwareTriggerResult> swtr;
45 if (!swtr)
return std::numeric_limits<float>::quiet_NaN();
51 swtcr = swtr->getNonPrescaledResult(triggerIdentifier);
53 swtcr = swtr->getResult(triggerIdentifier);
55 }
catch (
const std::out_of_range&) {
57 std::string err =
"The trigger identifier \"" + triggerIdentifier;
58 err +=
"\" was not found. Maybe you misspelled it?\n";
59 err +=
"Here are all possible trigger identifiers: \n";
60 auto res = swtr->getResults();
61 for (
auto& re : res) err += re.first +
"\n";
70 double L1Trigger(
const Particle*)
72 StoreObjPtr<TRGSummary> trg;
73 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
79 if (arguments.size() == 1) {
80 auto name = arguments[0];
81 auto func = [name](
const Particle*) ->
double {
82 StoreObjPtr<TRGSummary> trg;
83 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
84 return trg->testPsnm(name);
88 B2FATAL(
"Wrong number of arguments for L1PSNM function. The only argument must be the name of the PSNM trigger bit.");
94 if (arguments.size() == 1) {
95 auto name = arguments[0];
96 auto func = [name](
const Particle*) ->
double {
97 StoreObjPtr<TRGSummary> trg;
98 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
99 return trg->testFtdl(name);
103 B2FATAL(
"Wrong number of arguments for L1FTDL function. The only argument must be the name of the FTDL trigger bit.");
109 if (arguments.size() == 1) {
110 auto name = arguments[0];
111 auto func = [name](
const Particle*) ->
double {
112 StoreObjPtr<TRGSummary> trg;
113 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
114 return trg->testInput(name);
118 B2FATAL(
"Wrong number of arguments for L1Input function. The only argument must be the name of the input trigger bit.");
124 if (arguments.size() == 1) {
125 auto name = arguments[0];
126 auto func = [name](
const Particle*) ->
double {
127 static DBObjPtr<TRGGDLDBFTDLBits> ftdlBits;
128 static DBObjPtr<TRGGDLDBPrescales> prescales;
131 if (std::string(ftdlBits->getoutbitname((
int)bit)) == name) {
132 return prescales->getprescales(bit);
135 return std::numeric_limits<float>::quiet_NaN();
139 B2FATAL(
"Wrong number of arguments for L1Prescale function. The only argument must be the name of the PSNM trigger bit.");
143 double L1PSNMBit(
const Particle*,
const std::vector<double>& bit)
145 double isL1Trigger = 0.0;
147 if (bit.size() != 1)
return isL1Trigger;
150 const unsigned int trgWordSize = 32;
151 const unsigned int ntrgWords = 10;
152 if (bit[0] >= trgWordSize * ntrgWords or bit[0] < 0)
return isL1Trigger;
155 const unsigned int ntrgWord = (int) bit[0] / trgWordSize;
158 StoreObjPtr<TRGSummary> trg;
159 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
160 const unsigned int trgWord = trg->getPsnmBits(ntrgWord);
161 const unsigned int bitInWord = ((
unsigned int) bit[0] - ntrgWord * trgWordSize);
162 isL1Trigger = (trgWord >> bitInWord) & 1;
167 double L1FTDLBit(
const Particle*,
const std::vector<double>& bit)
169 double isL1Trigger = 0.0;
171 if (bit.size() != 1)
return isL1Trigger;
175 const unsigned int trgWordSize = 32;
176 const unsigned int ntrgWords = 10;
177 if (bit[0] >= trgWordSize * ntrgWords or bit[0] < 0)
return isL1Trigger;
181 const unsigned int ntrgWord = (int) bit[0] / trgWordSize;
185 StoreObjPtr<TRGSummary> trg;
186 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
187 const unsigned int trgWord = trg->getFtdlBits(ntrgWord);
188 const unsigned int bitInWord = ((
unsigned int) bit[0] - ntrgWord * trgWordSize);
189 isL1Trigger = (trgWord >> bitInWord) & 1;
195 double L1InputBit(
const Particle*,
const std::vector<double>& bit)
197 double isL1Trigger = 0.0;
199 if (bit.size() != 1)
return isL1Trigger;
202 const unsigned int trgWordSize = 32;
203 const unsigned int ntrgWords = 10;
204 if (bit[0] >= trgWordSize * ntrgWords or bit[0] < 0)
return isL1Trigger;
207 const unsigned int ntrgWord = (int) bit[0] / trgWordSize;
210 StoreObjPtr<TRGSummary> trg;
211 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
212 const unsigned int trgWord = trg->getInputBits(ntrgWord);
213 const unsigned int bitInWord = ((
unsigned int) bit[0] - ntrgWord * trgWordSize);
214 isL1Trigger = (trgWord >> bitInWord) & 1;
219 double L1PSNMBitPrescale(
const Particle*,
const std::vector<double>& bit)
221 double prescale = 0.0;
223 if (bit.size() != 1)
return prescale;
226 const unsigned int trgWordSize = 32;
227 const unsigned int ntrgWords = 10;
228 if (bit[0] >= trgWordSize * ntrgWords or bit[0] < 0)
return prescale;
232 const unsigned int ntrgWord = (int) bit[0] / trgWordSize;
233 const unsigned int bitInWord = ((
unsigned int) bit[0] - ntrgWord * trgWordSize);
235 StoreObjPtr<TRGSummary> trg;
236 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
237 prescale = trg->getPreScale(ntrgWord, bitInWord);
242 double getTimType(
const Particle*)
244 StoreObjPtr<TRGSummary> trg;
245 if (!trg)
return std::numeric_limits<float>::quiet_NaN();
246 return trg->getTimType();
258 if (args.size() != 1)
259 B2FATAL(
"Wrong number of arguments for the function softwareTriggerResult");
260 std::string triggerIdentifier = args[0];
262 using namespace std::placeholders;
263 return std::bind(extractSoftwareTriggerResultImplementation,
false, triggerIdentifier, _1);
275 if (args.size() != 1)
276 B2FATAL(
"Wrong number of arguments for the function softwareTriggerResultNonPrescaled");
277 std::string triggerIdentifier = args[0];
279 using namespace std::placeholders;
280 return std::bind(extractSoftwareTriggerResultImplementation,
true, triggerIdentifier, _1);
283 double passesAnyHighLevelTrigger(
const Particle* p)
287 std::vector<std::string> hardcodedname
288 = {
"software_trigger_cut&filter&total_result" };
289 double swtcr = softwareTriggerResult(hardcodedname)(p);
290 if (swtcr > 0.5)
return 1.0;
303 if (args.size() != 1)
304 B2FATAL(
"Wrong number of arguments for the function softwareTriggerPrescaling");
305 std::string triggerIdentifier = args[0];
307 auto outputFunction = [triggerIdentifier](
const Particle*) ->
double {
308 DBObjPtr<DBRepresentationOfSoftwareTriggerCut> downloadedCut(triggerIdentifier);
309 if (not downloadedCut)
311 B2FATAL(
"There is no trigger with the given name " << triggerIdentifier <<
"!");
314 return double(downloadedCut->getPreScaleFactor());
317 return outputFunction;
321 VARIABLE_GROUP(
"L1 Trigger");
322 REGISTER_VARIABLE(
"L1Trigger", L1Trigger ,
323 "[Eventbased] Returns 1 if at least one PSNM L1 trigger bit is true.");
324 REGISTER_VARIABLE(
"L1PSNM(name)", L1PSNM ,
325 "[Eventbased] Returns the PSNM (Prescale And Mask, after prescale) status of the trigger bit with the given name.");
326 REGISTER_VARIABLE(
"L1FTDL(name)", L1FTDL ,
327 "[Eventbased] Returns the FTDL (Final Trigger Decision Logic, before prescale) status of the trigger bit with the given name.");
328 REGISTER_VARIABLE(
"L1Input(name)", L1Input,
329 "[Eventbased] Returns the input bit status of the trigger bit with the given name.");
330 REGISTER_VARIABLE(
"L1Prescale(name)", L1Prescale,
331 "[Eventbased] Returns the PSNM (prescale and mask) prescale of the trigger bit with the given name.");
332 REGISTER_VARIABLE(
"L1PSNMBit(i)", L1PSNMBit ,
333 "[Eventbased] Returns the PSNM (Prescale And Mask, after prescale) status of i-th trigger bit.");
334 REGISTER_VARIABLE(
"L1FTDLBit(i)", L1FTDLBit ,
335 "[Eventbased] Returns the FTDL (Final Trigger Decision Logic, before prescale) status of i-th trigger bit.");
336 REGISTER_VARIABLE(
"L1InputBit(i)", L1InputBit,
337 "[Eventbased] Returns the input bit status of the i-th input trigger bit.");
338 REGISTER_VARIABLE(
"L1PSNMBitPrescale(i)", L1PSNMBitPrescale,
339 "[Eventbased] Returns the PSNM (prescale and mask) prescale of i-th trigger bit.");
340 REGISTER_VARIABLE(
"L1TimType", getTimType ,
341 "[Eventbased] Returns ETimingType time type.");
343 VARIABLE_GROUP(
"Software Trigger");
344 REGISTER_VARIABLE(
"SoftwareTriggerResult(triggerIdentifier)", softwareTriggerResult,
345 "[Eventbased] [Expert] returns the SoftwareTriggerCutResult, "
346 "defined as reject (-1), accept (1), or noResult (0). Note "
347 "that the meanings of these change depending if using trigger "
348 "or the skim stage, hence expert.");
349 REGISTER_VARIABLE(
"SoftwareTriggerResultNonPrescaled(triggerIdentifier)", softwareTriggerResultNonPrescaled,
350 "[Eventbased] [Expert] returns the SoftwareTriggerCutResult, "
351 "if this trigger would not be prescaled."
352 "Please note, this is not the final HLT decision! "
353 "It is defined as reject (-1), accept (1), or noResult (0). Note "
354 "that the meanings of these change depending if using trigger "
355 "or the skim stage, hence expert.");
356 REGISTER_VARIABLE(
"HighLevelTrigger", passesAnyHighLevelTrigger,
357 "[Eventbased] 1.0 if event passes the HLT trigger, 0.0 if not");
358 REGISTER_VARIABLE(
"SoftwareTriggerPrescaling(triggerIdentifier)", softwareTriggerPrescaling,
359 "[Eventbased] return the prescaling for the specific software trigger identifier. "
360 "Please note, this prescaling is taken from the currently setup database. It only corresponds "
361 "to the correct HLT prescale if you are using the online database!");