Skip to content

Commit fda612a

Browse files
Allow branch status 0, reduce some repetition
1 parent e99219d commit fda612a

1 file changed

Lines changed: 67 additions & 151 deletions

File tree

rooutil/inc/Event.hh

Lines changed: 67 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define Event_hh_
33

44
#include <algorithm>
5+
#include <cxxabi.h> // For abi::__cxa_demangle
56

67
#include "EventNtuple/inc/EventInfo.hh"
78
#include "EventNtuple/inc/EventInfoMC.hh"
@@ -45,15 +46,9 @@
4546

4647
struct Event {
4748
Event(TChain* ntuple) {
48-
if (ntuple->GetBranch("evtinfo")) {
49-
ntuple->SetBranchAddress("evtinfo", &this->evtinfo);
50-
}
51-
if (ntuple->GetBranch("hitcount")) {
52-
ntuple->SetBranchAddress("hitcount", &this->hitcount);
53-
}
54-
if (ntuple->GetBranch("crvsummary")) {
55-
ntuple->SetBranchAddress("crvsummary", &this->crvsummary);
56-
}
49+
CheckForBranch(ntuple, "evtinfo", &this->evtinfo);
50+
CheckForBranch(ntuple, "hitcount", &this->hitcount);
51+
CheckForBranch(ntuple, "crvsummary", &this->crvsummary);
5752
const auto& branches = ntuple->GetListOfBranches();
5853
int i_trig_branch = 0;
5954
for (const auto& branch : *branches) {
@@ -65,159 +60,68 @@ struct Event {
6560
}
6661
}
6762

68-
if (ntuple->GetBranch("trk")) {
69-
ntuple->SetBranchAddress("trk", &this->trk);
70-
}
71-
if (ntuple->GetBranch("trksegs")) {
72-
ntuple->SetBranchAddress("trksegs", &this->trksegs);
73-
}
74-
if (ntuple->GetBranch("trkcalohit")) {
75-
ntuple->SetBranchAddress("trkcalohit", &this->trkcalohit);
76-
}
77-
if (ntuple->GetBranch("trkqual")) {
78-
ntuple->SetBranchAddress("trkqual", &this->trkqual);
79-
}
80-
if (ntuple->GetBranch("trkqual3")) { // TODO: un-hardcode those
81-
ntuple->SetBranchAddress("trkqual3", &this->trkqual_alt);
82-
}
83-
if (ntuple->GetBranch("crvcoincs")) {
84-
ntuple->SetBranchAddress("crvcoincs", &this->crvcoincs);
85-
}
86-
if (ntuple->GetBranch("trkpid")) {
87-
ntuple->SetBranchAddress("trkpid", &this->trkpid);
88-
}
63+
CheckForBranch(ntuple, "trk", &this->trk);
64+
CheckForBranch(ntuple, "trksegs", &this->trksegs);
65+
CheckForBranch(ntuple, "trkcalohit", &this->trkcalohit);
66+
CheckForBranch(ntuple, "trkqual", &this->trkqual);
67+
CheckForBranch(ntuple, "trkqual3", &this->trkqual_alt); // TODO: un-hardcode those
68+
CheckForBranch(ntuple, "crvcoincs", &this->crvcoincs);
69+
CheckForBranch(ntuple, "trkpid", &this->trkpid);
8970

9071
// Check if the MC branches exist
91-
if (ntuple->GetBranch("evtinfomc")) {
92-
ntuple->SetBranchAddress("evtinfomc", &this->evtinfomc);
93-
}
94-
if (ntuple->GetBranch("crvsummarymc")) {
95-
ntuple->SetBranchAddress("crvsummarymc", &this->crvsummarymc);
96-
}
97-
if (ntuple->GetBranch("trkmc")) {
98-
ntuple->SetBranchAddress("trkmc", &this->trkmc);
99-
}
100-
if (ntuple->GetBranch("trksegsmc")) {
101-
ntuple->SetBranchAddress("trksegsmc", &this->trksegsmc);
102-
}
103-
if (ntuple->GetBranch("trksegpars_lh")) {
104-
ntuple->SetBranchAddress("trksegpars_lh", &this->trksegpars_lh);
105-
}
106-
if (ntuple->GetBranch("trksegpars_ch")) {
107-
ntuple->SetBranchAddress("trksegpars_ch", &this->trksegpars_ch);
108-
}
109-
if (ntuple->GetBranch("trksegpars_kl")) {
110-
ntuple->SetBranchAddress("trksegpars_kl", &this->trksegpars_kl);
111-
}
112-
if (ntuple->GetBranch("trkcalohitmc")) {
113-
ntuple->SetBranchAddress("trkcalohitmc", &this->trkcalohitmc);
114-
}
115-
if (ntuple->GetBranch("crvcoincsmc")) {
116-
ntuple->SetBranchAddress("crvcoincsmc", &this->crvcoincsmc);
117-
}
118-
if (ntuple->GetBranch("crvdigis")) {
119-
ntuple->SetBranchAddress("crvdigis", &this->crvdigis);
120-
}
121-
if (ntuple->GetBranch("crvpulses")) {
122-
ntuple->SetBranchAddress("crvpulses", &this->crvpulses);
123-
}
124-
if (ntuple->GetBranch("crvpulsesmc")) {
125-
ntuple->SetBranchAddress("crvpulsesmc", &this->crvpulsesmc);
126-
}
127-
if (ntuple->GetBranch("crvcoincsmcplane")) {
128-
ntuple->SetBranchAddress("crvcoincsmcplane", &this->crvcoincsmcplane);
129-
}
130-
72+
CheckForBranch(ntuple, "evtinfomc", &this->evtinfomc);
73+
CheckForBranch(ntuple, "crvsummarymc", &this->crvsummarymc);
74+
CheckForBranch(ntuple, "trkmc", &this->trkmc);
75+
CheckForBranch(ntuple, "trksegsmc", &this->trksegsmc);
76+
CheckForBranch(ntuple, "trksegpars_lh", &this->trksegpars_lh);
77+
CheckForBranch(ntuple, "trksegpars_ch", &this->trksegpars_ch);
78+
CheckForBranch(ntuple, "trksegpars_kl", &this->trksegpars_kl);
79+
CheckForBranch(ntuple, "trkcalohitmc", &this->trkcalohitmc);
80+
CheckForBranch(ntuple, "crvcoincsmc", &this->crvcoincsmc);
81+
CheckForBranch(ntuple, "crvdigis", &this->crvdigis);
82+
CheckForBranch(ntuple, "crvpulses", &this->crvpulses);
83+
CheckForBranch(ntuple, "crvpulsesmc", &this->crvpulsesmc);
84+
CheckForBranch(ntuple, "crvcoincsmcplane", &this->crvcoincsmcplane);
85+
86+
CheckForBranch(ntuple, "trkmcsim", &this->trkmcsim);
87+
CheckForBranch(ntuple, "trkhits", &this->trkhits);
88+
CheckForBranch(ntuple, "trkhitsmc", &this->trkhitsmc);
89+
CheckForBranch(ntuple, "trkmats", &this->trkmats);
90+
CheckForBranch(ntuple, "trkhitcalibs", &this->trkhitcalibs);
91+
92+
CheckForBranch(ntuple, "caloclusters", &this->caloclusters);
93+
CheckForBranch(ntuple, "calohits", &this->calohits);
94+
CheckForBranch(ntuple, "calorecodigis", &this->calorecodigis);
95+
CheckForBranch(ntuple, "calodigis", &this->calodigis);
13196

132-
if (ntuple->GetBranch("trkmcsim")) {
133-
ntuple->SetBranchAddress("trkmcsim", &this->trkmcsim);
134-
}
135-
if (ntuple->GetBranch("trkhits")) {
136-
ntuple->SetBranchAddress("trkhits", &this->trkhits);
137-
}
138-
if (ntuple->GetBranch("trkhitsmc")) {
139-
ntuple->SetBranchAddress("trkhitsmc", &this->trkhitsmc);
140-
}
141-
if (ntuple->GetBranch("trkmats")) {
142-
ntuple->SetBranchAddress("trkmats", &this->trkmats);
143-
}
144-
if (ntuple->GetBranch("trkhitcalibs")) {
145-
ntuple->SetBranchAddress("trkhitcalibs", &this->trkhitcalibs);
146-
}
147-
148-
if (ntuple->GetBranch("caloclusters")) {
149-
ntuple->SetBranchAddress("caloclusters", &this->caloclusters);
150-
}
151-
if (ntuple->GetBranch("calohits")) {
152-
ntuple->SetBranchAddress("calohits", &this->calohits);
153-
}
154-
if (ntuple->GetBranch("calorecodigis")) {
155-
ntuple->SetBranchAddress("calorecodigis", &this->calorecodigis);
156-
}
157-
if (ntuple->GetBranch("calodigis")) {
158-
ntuple->SetBranchAddress("calodigis", &this->calodigis);
159-
}
97+
}
16098

161-
};
99+
// Check if a branch exists in the TChain, and optionally set its address
100+
bool CheckForBranch(TChain* ntuple, const char* branch_name, void* address = nullptr) {
101+
if(ntuple->GetBranch(branch_name) == nullptr || ntuple->GetBranchStatus(branch_name) == 0) return false;
102+
if(address != nullptr) ntuple->SetBranchAddress(branch_name, address);
103+
return true;
104+
}
162105

163106
void Update(bool debug = false) {
164107
if (debug) { std::cout << "Event::Update(): Clearing previous Tracks... " << std::endl; }
165108
tracks.clear();
166109
for (int i_track = 0; i_track < nTracks(); ++i_track) {
167110
if (debug) { std::cout << "Event::Update(): Creating Track " << i_track << "... " << std::endl; }
168111
Track track(&(trk->at(i_track)), &(trksegs->at(i_track)), &(trkcalohit->at(i_track))); // passing the addresses of the underlying structs
169-
if (trkmc != nullptr) {
170-
if (debug) { std::cout << "Event::Update(): Adding trkmc to Track " << i_track << "... " << std::endl; }
171-
track.trkmc = &(trkmc->at(i_track));
172-
}
173-
if (trksegsmc != nullptr) {
174-
if (debug) { std::cout << "Event::Update(): Adding trksegsmc to Track " << i_track << "... " << std::endl; }
175-
track.trksegsmc = &(trksegsmc->at(i_track));
176-
}
177-
if (trksegpars_lh != nullptr) {
178-
if (debug) { std::cout << "Event::Update(): Adding trksegpars_lh to Track " << i_track << "... " << std::endl; }
179-
track.trksegpars_lh = &(trksegpars_lh->at(i_track));
180-
}
181-
if (trksegpars_ch != nullptr) {
182-
if (debug) { std::cout << "Event::Update(): Adding trksegpars_ch to Track " << i_track << "... " << std::endl; }
183-
track.trksegpars_ch = &(trksegpars_ch->at(i_track));
184-
}
185-
if (trksegpars_kl != nullptr) {
186-
if (debug) { std::cout << "Event::Update(): Adding trksegpars_kl to Track " << i_track << "... " << std::endl; }
187-
track.trksegpars_kl = &(trksegpars_kl->at(i_track));
188-
}
189-
if (trkmcsim != nullptr) {
190-
if (debug) { std::cout << "Event::Update(): Adding trkmcsim to Track " << i_track << "... (size = " << trkmcsim->at(i_track).size() << ")" << std::endl; }
191-
track.trkmcsim = &(trkmcsim->at(i_track));
192-
}
193-
if (trkhits != nullptr) {
194-
if (debug) { std::cout << "Event::Update(): Adding trkhits to Track " << i_track << "... " << std::endl; }
195-
track.trkhits = &(trkhits->at(i_track));
196-
}
197-
if (trkhitsmc != nullptr) {
198-
if (debug) { std::cout << "Event::Update(): Adding trkhitsmc to Track " << i_track << "... " << std::endl; }
199-
track.trkhitsmc = &(trkhitsmc->at(i_track));
200-
}
201-
if (trkmats != nullptr) {
202-
if (debug) { std::cout << "Event::Update(): Adding trkmats to Track " << i_track << "... " << std::endl; }
203-
track.trkmats = &(trkmats->at(i_track));
204-
}
205-
if (trkhitcalibs != nullptr) {
206-
if (debug) { std::cout << "Event::Update(): Adding trkhitcalibs to Track " << i_track << "... " << std::endl; }
207-
track.trkhitcalibs = &(trkhitcalibs->at(i_track));
208-
}
209-
if (trkqual != nullptr) {
210-
if (debug) { std::cout << "Event::Update(): Adding trkqual to Track " << i_track << "... " << std::endl; }
211-
track.trkqual = &(trkqual->at(i_track));
212-
}
213-
if (trkqual_alt != nullptr) {
214-
if (debug) { std::cout << "Event::Update(): Adding trkqual_alt to Track " << i_track << "... " << std::endl; }
215-
track.trkqual_alt = &(trkqual_alt->at(i_track));
216-
}
217-
if (trkpid != nullptr) {
218-
if (debug) { std::cout << "Event::Update(): Adding trkpid to Track " << i_track << "... " << std::endl; }
219-
track.trkpid = &(trkpid->at(i_track));
220-
}
112+
UpdateObject(track.trkmc, trkmc, i_track, debug);
113+
UpdateObject(track.trksegsmc, trksegsmc, i_track, debug);
114+
UpdateObject(track.trksegpars_lh, trksegpars_lh, i_track, debug);
115+
UpdateObject(track.trksegpars_ch, trksegpars_ch, i_track, debug);
116+
UpdateObject(track.trksegpars_kl, trksegpars_kl, i_track, debug);
117+
UpdateObject(track.trkmcsim, trkmcsim, i_track, debug);
118+
UpdateObject(track.trkhits, trkhits, i_track, debug);
119+
UpdateObject(track.trkhitsmc, trkhitsmc, i_track, debug);
120+
UpdateObject(track.trkmats, trkmats, i_track, debug);
121+
UpdateObject(track.trkhitcalibs, trkhitcalibs, i_track, debug);
122+
UpdateObject(track.trkqual, trkqual, i_track, debug);
123+
UpdateObject(track.trkqual_alt, trkqual_alt, i_track, debug);
124+
UpdateObject(track.trkpid, trkpid, i_track, debug);
221125

222126
if (debug) { std::cout << "Event::Update(): Updating Track " << i_track << "... " << std::endl; }
223127
track.Update(debug);
@@ -246,6 +150,18 @@ struct Event {
246150
}
247151
}
248152

153+
template <typename T> void UpdateObject(T*& object, std::vector<T>* object_ptr, int index, bool debug = false) {
154+
if (object_ptr != nullptr) {
155+
if (debug) {
156+
std::cout << "Event::Update(): Adding "
157+
<< abi::__cxa_demangle(typeid(*object).name(), nullptr, nullptr, nullptr) << " to Track " << index << "... " << std::endl;
158+
}
159+
object = &(object_ptr->at(index));
160+
} else if(debug) {
161+
std::cout << "Event::Update(): No " << abi::__cxa_demangle(typeid(*object).name(), nullptr, nullptr, nullptr) << " to add to Track " << index << std::endl;
162+
}
163+
}
164+
249165
int nTracks() const {
250166
if (trk == nullptr) { return 0; }
251167
else { return trk->size(); }

0 commit comments

Comments
 (0)