Skip to content

Commit 184b11f

Browse files
Merge pull request #323 from michaelmackenzie/TriggerInfo
Add Trigger rooutil object
2 parents 66ba976 + 50d1341 commit 184b11f

6 files changed

Lines changed: 95 additions & 22 deletions

File tree

rooutil/examples/PrintEvents.C

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ void PrintEvents(std::string filename) {
259259
}
260260

261261
// trigger branches
262-
for (const auto& pair : event.trigNameMap) {
263-
std::cout << pair.first << ": " << event.triginfo._triggerArray[pair.second] << std::endl;
262+
for (const auto& pair : event.trigger.NameToIndexMap()) {
263+
std::cout << pair.first << ": " << event.trigger.Fired(pair.second) << std::endl;
264264
}
265265

266266
}

rooutil/inc/Event.hh

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "EventNtuple/rooutil/inc/Track.hh"
4242
#include "EventNtuple/rooutil/inc/CrvCoinc.hh"
4343
#include "EventNtuple/rooutil/inc/CaloCluster.hh"
44+
#include "EventNtuple/rooutil/inc/Trigger.hh"
4445

4546
#include "TChain.h"
4647

@@ -50,16 +51,7 @@ namespace rooutil {
5051
CheckForBranch(ntuple, "evtinfo", &this->evtinfo);
5152
CheckForBranch(ntuple, "hitcount", &this->hitcount);
5253
CheckForBranch(ntuple, "crvsummary", &this->crvsummary);
53-
const auto& branches = ntuple->GetListOfBranches();
54-
int i_trig_branch = 0;
55-
for (const auto& branch : *branches) {
56-
std::string brname = branch->GetName();
57-
if (brname.substr(0, 5) == "trig_") {
58-
ntuple->SetBranchAddress(brname.c_str(), &this->triginfo._triggerArray[i_trig_branch]);
59-
trigNameMap.insert({brname, i_trig_branch});
60-
i_trig_branch++;
61-
}
62-
}
54+
AddTriggerInfo(ntuple);
6355

6456
CheckForBranch(ntuple, "trk", &this->trk);
6557
CheckForBranch(ntuple, "trksegs", &this->trksegs);
@@ -104,6 +96,24 @@ namespace rooutil {
10496
return true;
10597
}
10698

99+
// Add trigger branches and store the path name information
100+
void AddTriggerInfo(TChain* ntuple) {
101+
trigger.SetTrigInfo(&triginfo); // pointer to the underlying trigger data read in event-by-event
102+
103+
// trigger branches are named "trig_<trigger path name>" --> look for these
104+
const auto& branches = ntuple->GetListOfBranches();
105+
int i_trig_branch = 0;
106+
for (const auto& branch : *branches) {
107+
std::string brname = branch->GetName();
108+
if (brname.substr(0, 5) == "trig_") {
109+
ntuple->SetBranchAddress(brname.c_str(), &this->triginfo._triggerArray[i_trig_branch]);
110+
const std::string trigname = brname.substr(5); // name of the trigger path
111+
trigger.AssignIndex(i_trig_branch, trigname); // map the array index to the path name
112+
i_trig_branch++;
113+
}
114+
}
115+
}
116+
107117
void Update(bool debug = false) {
108118
if (debug) { std::cout << "Event::Update(): Clearing previous Tracks... " << std::endl; }
109119
tracks.clear();
@@ -300,6 +310,7 @@ namespace rooutil {
300310
mu2e::CrvSummaryReco* crvsummary = nullptr;
301311
mu2e::CrvSummaryMC* crvsummarymc = nullptr;
302312
mu2e::TrigInfo triginfo; // not a pointer because we give the address of array elements inside this
313+
Trigger trigger; // contains additional trigger information
303314

304315
std::vector<mu2e::TrkInfo>* trk = nullptr;
305316
std::vector<mu2e::TrkInfoMC>* trkmc = nullptr;
@@ -331,9 +342,6 @@ namespace rooutil {
331342
std::vector<mu2e::CrvPlaneInfoMC>* crvcoincsmcplane = nullptr;
332343

333344
std::vector<std::vector<mu2e::SimInfo>>* trkmcsim = nullptr;
334-
335-
// Need to keep track of trigger name to element in triginfo
336-
std::map<std::string, unsigned int> trigNameMap;
337345
};
338346
} // namespace rooutil
339347
#endif

rooutil/inc/RooUtil.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ namespace rooutil {
144144

145145
if(event->trkmcsim) { output_ntuple->Branch("trkmcsim", event->trkmcsim); }
146146

147-
for (const auto& pair : event->trigNameMap) {
148-
output_ntuple->Branch(pair.first.c_str(), &event->triginfo._triggerArray[pair.second]);
147+
for (const auto& pair : event->trigger.NameToIndexMap()) {
148+
output_ntuple->Branch(("trig_" + pair.first).c_str(), &event->triginfo._triggerArray[pair.second]);
149149
}
150150

151151
// Write out histograms from input to output

rooutil/inc/Trigger.hh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#ifndef rooutil_Trigger_hh_
2+
#define rooutil_Trigger_hh_
3+
4+
#include <functional>
5+
#include <map>
6+
#include "EventNtuple/inc/TrigInfo.hh"
7+
8+
namespace rooutil {
9+
struct Trigger {
10+
Trigger(mu2e::TrigInfo* trig = nullptr)
11+
: _trig(trig) {
12+
13+
}
14+
15+
void Update(bool debug = false) {
16+
}
17+
18+
void SetTrigInfo(const mu2e::TrigInfo* trig) { _trig = trig; }
19+
20+
void AssignIndex(const int index, const std::string& name) {
21+
_indexToName[index] = name;
22+
_nameToIndex[name] = index;
23+
}
24+
25+
int GetIndex(const std::string& name) const {
26+
if(_nameToIndex.count(name) == 0) return -1;
27+
return _nameToIndex.at(name);
28+
}
29+
30+
bool Fired(const int index) const {
31+
if(!_trig) return false;
32+
if(index >= int(mu2e::TrigInfo::ntrig_)) return false;
33+
if(_indexToName.count(index) == 0) return false; // not in the trigger menu
34+
return _trig->_triggerArray[index];
35+
}
36+
37+
bool Fired(const std::string& name) const {
38+
if(_nameToIndex.count(name) == 0) return false;
39+
return Fired(_nameToIndex.at(name));
40+
}
41+
42+
// Check if any trigger with this pattern string was fired
43+
bool FiredByTag(const std::string pattern) const {
44+
bool fired = false;
45+
for(const auto& itr : NameToIndexMap()) {
46+
const std::string name = itr.first;
47+
if(name.find(pattern) != std::string::npos) fired |= Fired(itr.second);
48+
if(fired) break;
49+
}
50+
return fired;
51+
}
52+
53+
// Data accessors
54+
const mu2e::TrigInfo* TrigInfo () const { return _trig; }
55+
const std::map<std::string, int>& NameToIndexMap() const { return _nameToIndex; }
56+
const std::map<int, std::string>& IndexToNameMap() const { return _indexToName; }
57+
58+
// Pointer to the data
59+
const mu2e::TrigInfo* _trig = nullptr;
60+
std::map<int, std::string> _indexToName;
61+
std::map<std::string, int> _nameToIndex;
62+
};
63+
64+
typedef std::function<bool(Trigger&)> TriggerCut;
65+
} // namespace rooutil
66+
#endif

rooutil/inc/common_cuts.hh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,9 @@ bool from_start_process(Track& track, mu2e::ProcessCode::enum_type proc) { // Tr
286286

287287
//+ Trigger Cut
288288
bool passes_trigger(Event& event, std::string trigname) { // true if the event passed the named trigger
289-
if (trigname.substr(0, 5) != "trig_") { // we need to add the prefix to get the correct element
290-
trigname = "trig_" + trigname;
289+
if (trigname.substr(0, 5) == "trig_") { // drop this prefix
290+
trigname = trigname.substr(5);
291291
}
292-
const auto i_trig_element = event.trigNameMap[trigname];
293-
return event.triginfo._triggerArray[i_trig_element];
292+
return event.trigger.Fired(trigname);
294293
}
295294
#endif

validation/create_val_file_rooutil.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ void create_val_file_rooutil(std::string filename, std::string outfilename) {
507507
TH1F* h_triginfo = new TH1F("h_triginfo", "", mu2e::TrigInfo::ntrig_,0,mu2e::TrigInfo::ntrig_);
508508
// Grab the first event to name the bins
509509
const auto& evt = util.GetEvent(0);
510-
for (const auto pair : evt.trigNameMap) {
510+
for (const auto pair : evt.trigger.NameToIndexMap()) {
511511
h_triginfo->GetXaxis()->SetBinLabel(pair.second+1, pair.first.c_str());
512512
}
513513

0 commit comments

Comments
 (0)