FairShip
Loading...
Searching...
No Matches
MuDISDefs.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5#ifndef NEWMUONDIS_MUDISDEFS_H_
6#define NEWMUONDIS_MUDISDEFS_H_
7
8#include <array>
9#include <iostream>
10#include <sstream>
11#include <string>
12#include <type_traits>
13#include <vector>
14
15#include "DISparticle.h"
16#include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
17#include "ShipMCTrack.h"
18#include "TChain.h" // for TTree
19#include "TROOT.h"
20#include "TTree.h" // for TTree
21#include "TVector3.h"
22#include "TimeDetPoint.h"
23#include "UpstreamTaggerPoint.h"
24#include "strawtubesPoint.h"
25#include "vetoPoint.h"
26
27namespace ShipMuDIS {
28static const unsigned nMats = 10;
29// Last shield material in accumulated z (cm), excluding gaps.
30inline constexpr double kMuonShieldTailZ = 20.;
31
32enum MatType {
33 MS = 0,
34 UBT = 1,
36 SBTfr = 3,
38 SSTfr = 5,
39 HE = 6,
40 AIR = 7,
42 REST = 9
43};
44
45inline const std::array<TString, nMats> MatTypeStr = {
46 "MS", "UBT", "SBTsens", "SBTfr", "SSTsens",
47 "SSTfr", "HE", "AIR", "HeBalloon", "REST"};
48
50 int nDISevts; // per input muon per volume
52 -1; // actual count before filtering; -1 if not crossed
53 double pPythia = -1.; // beam momentum passed to Pythia, in GeV/c
54 double wDIS; // per input muon per volume
55 std::vector<double> DISxsec; // per input muon per DIS
56 std::vector<bool> DIStarget; // per input muon per DIS, p=true, n=false
57 std::vector<double> DISvx;
58 std::vector<double> DISvy;
59 std::vector<double> DISvz;
60 std::vector<double> DISvt;
61 std::vector<int> nDISdau; // per DIS event muon
62 std::vector<DISparticle> DISparticles; // all DIS events together.
63
64 void InitTree(TTree*& aT, const TString& aLabel) {
65 aT->Branch("muon_nDISevt_" + aLabel, &nDISevts);
66 aT->Branch("muon_nDISGenerated_" + aLabel, &nDISevtsGenerated);
67 aT->Branch("muon_pPythia_" + aLabel, &pPythia);
68 aT->Branch("muon_wDIS_" + aLabel, &wDIS);
69 aT->Branch("mudis_DISxsec_" + aLabel, &DISxsec);
70 aT->Branch("mudis_DIStarget_" + aLabel, &DIStarget);
71 aT->Branch("mudis_DISvx_" + aLabel, &DISvx);
72 aT->Branch("mudis_DISvy_" + aLabel, &DISvy);
73 aT->Branch("mudis_DISvz_" + aLabel, &DISvz);
74 aT->Branch("mudis_DISvt_" + aLabel, &DISvt);
75 aT->Branch("mudis_nDISdaughters_" + aLabel, &nDISdau);
76 aT->Branch("mudis_DISproducts_" + aLabel, &DISparticles);
77 };
78
79 void initEvent(const int& nDIS) {
80 nDISevts = 0;
82 pPythia = -1.;
83 wDIS = 1;
84 DISxsec.clear();
85 DISxsec.reserve(nDIS);
86 DIStarget.clear();
87 DIStarget.reserve(nDIS);
88 DISvx.clear();
89 DISvx.reserve(nDIS);
90 DISvy.clear();
91 DISvy.reserve(nDIS);
92 DISvz.clear();
93 DISvz.reserve(nDIS);
94 DISvt.clear();
95 DISvt.reserve(nDIS);
96 nDISdau.clear();
97 nDISdau.reserve(nDIS);
98 DISparticles.clear();
99 DISparticles.reserve(10 * nDIS);
100 };
101};
102
104 std::vector<ShipMCTrack> mcTrks;
105 std::vector<vetoPoint> sbtPt;
106 std::vector<UpstreamTaggerPoint> ubtPt;
107 std::vector<strawtubesPoint> sstPt;
108 std::vector<TimeDetPoint> tdPt;
109 double pathLength = 0.;
110 std::array<double, nMats> pathLengthByMat = {};
112 void InitTree(TTree*& aT) {
113 aT->Branch("muon_MCTracks", &mcTrks);
114 aT->Branch("muon_SBTPoints", &sbtPt);
115 aT->Branch("muon_SSTPoints", &sstPt);
116 aT->Branch("muon_UBTPoints", &ubtPt);
117 aT->Branch("muon_TDPoints", &tdPt);
118 aT->Branch("muon_path_length", &pathLength);
119 for (unsigned i(0); i < nMats; ++i) {
120 aT->Branch("muon_path_length_" + MatTypeStr[i], &pathLengthByMat[i]);
121 br[i].InitTree(aT, MatTypeStr[i]);
122 }
123 };
124 void initEvent(const int& nMax = 100) {
125 mcTrks.clear();
126 mcTrks.reserve(nMax);
127 sbtPt.clear();
128 sbtPt.reserve(nMax);
129 ubtPt.clear();
130 ubtPt.reserve(nMax);
131 sstPt.clear();
132 sstPt.reserve(nMax);
133 tdPt.clear();
134 tdPt.reserve(nMax);
135 pathLength = 0.;
136 pathLengthByMat.fill(0.);
137 };
138};
139
140// Bind only on the current file's tree, never on TChain's persistent address
141// list. This checks both presence and type when a chain changes files.
142template <class T>
143bool BindInputBranch(TTree* tree, const TString& name, T* address,
144 bool required = true) {
145 auto* branch = tree->GetBranch(name);
146 if (!branch) {
147 if (required) LOG(error) << "Missing required input branch " << name;
148 return !required;
149 }
150 if constexpr (std::is_arithmetic_v<T>) {
151 // Reject object branches before ROOT attempts to bind a scalar address.
152 if (branch->GetClassName()[0] != '\0') {
153 LOG(error) << "Expected scalar input branch " << name;
154 return false;
155 }
156 }
157 branch->SetAutoDelete(false);
158 return tree->SetBranchAddress(name, address) >= 0;
159}
160
162 std::vector<ShipMCTrack>* MCTrack = nullptr;
163 std::vector<vetoPoint>* sbtPt = nullptr;
164 std::vector<UpstreamTaggerPoint>* ubtPt = nullptr;
165 std::vector<strawtubesPoint>* sstPt = nullptr;
166 std::vector<TimeDetPoint>* tdPt = nullptr;
167
168 bool Setup(TChain* tree) {
169 fTreeNumber = -1;
170 Clear();
171 return tree->GetEntries() == 0 || PrepareEntry(tree, 0);
172 }
173
174 bool PrepareEntry(TTree* tree, Long64_t entry) {
175 Clear();
176 if (tree->LoadTree(entry) < 0) return false;
177 if (tree->GetTreeNumber() != fTreeNumber) {
178 fTreeNumber = tree->GetTreeNumber();
179 auto* current = tree->GetTree();
180 fValid = BindInputBranch(current, "MCTrack", &MCTrack);
181 fValid &= BindInputBranch(current, "TimeDetPoint", &tdPt);
182 fValid &= BindInputBranch(current, "vetoPoint", &sbtPt);
183 fValid &= BindInputBranch(current, "UpstreamTaggerPoint", &ubtPt);
184 fValid &= BindInputBranch(current, "strawtubesPoint", &sstPt);
185 }
186 return fValid;
187 }
188
189 private:
190 void Clear() {
191 fTracks.clear();
192 fSBT.clear();
193 fUBT.clear();
194 fSST.clear();
195 fTD.clear();
196 MCTrack = &fTracks;
197 sbtPt = &fSBT;
198 ubtPt = &fUBT;
199 sstPt = &fSST;
200 tdPt = &fTD;
201 }
202 std::vector<ShipMCTrack> fTracks;
203 std::vector<vetoPoint> fSBT;
204 std::vector<UpstreamTaggerPoint> fUBT;
205 std::vector<strawtubesPoint> fSST;
206 std::vector<TimeDetPoint> fTD;
207 int fTreeNumber = -1;
208 bool fValid = false;
209};
210
211// For reading back the DIS tree
213 int nDISevts = 0; // per input muon per volume
215 double pPythia = -1.;
216 double wDIS = 1; // per input muon per volume
217 std::vector<double>* DISxsec = nullptr; // per input muon per DIS
218 std::vector<bool>* DIStarget =
219 nullptr; // per input muon per DIS, p=true, n=false
220 std::vector<double>* DISvx = nullptr;
221 std::vector<double>* DISvy = nullptr;
222 std::vector<double>* DISvz = nullptr;
223 std::vector<double>* DISvt = nullptr;
224 std::vector<int>* nDISdau = nullptr; // per DIS event muon
225 std::vector<DISparticle>* DISparticles = nullptr; // all DIS events together.
226
227 bool IsValid() const {
228 if (nDISevts < 0 || !DISxsec || !DIStarget || !DISvx || !DISvy || !DISvz ||
229 !DISvt || !nDISdau || !DISparticles)
230 return false;
231 const auto size = static_cast<std::size_t>(nDISevts);
232 if (DISxsec->size() != size || DIStarget->size() != size ||
233 DISvx->size() != size || DISvy->size() != size ||
234 DISvz->size() != size || DISvt->size() != size ||
235 nDISdau->size() != size)
236 return false;
237 std::size_t total = 0;
238 for (int count : *nDISdau) {
239 if (count < 0 ||
240 static_cast<std::size_t>(count) > DISparticles->size() - total)
241 return false;
242 total += count;
243 }
244 return total == DISparticles->size();
245 }
246
247 void Clear() {
248 nDISevts = 0;
250 pPythia = -1.;
251 wDIS = 1.;
252 fStorage.initEvent(0);
253 DISxsec = &fStorage.DISxsec;
254 DIStarget = &fStorage.DIStarget;
255 DISvx = &fStorage.DISvx;
256 DISvy = &fStorage.DISvy;
257 DISvz = &fStorage.DISvz;
258 DISvt = &fStorage.DISvt;
259 nDISdau = &fStorage.nDISdau;
260 DISparticles = &fStorage.DISparticles;
261 }
262
263 bool SetupTree(TTree* tree, const TString& label) {
264 Clear();
265 bool ok = BindInputBranch(tree, "muon_nDISevt_" + label, &nDISevts);
266 ok &= BindInputBranch(tree, "muon_nDISGenerated_" + label,
268 ok &= BindInputBranch(tree, "muon_pPythia_" + label, &pPythia);
269 ok &= BindInputBranch(tree, "muon_wDIS_" + label, &wDIS);
270 ok &= BindInputBranch(tree, "mudis_DISxsec_" + label, &DISxsec);
271 ok &= BindInputBranch(tree, "mudis_DIStarget_" + label, &DIStarget);
272 ok &= BindInputBranch(tree, "mudis_DISvx_" + label, &DISvx);
273 ok &= BindInputBranch(tree, "mudis_DISvy_" + label, &DISvy);
274 ok &= BindInputBranch(tree, "mudis_DISvz_" + label, &DISvz);
275 ok &= BindInputBranch(tree, "mudis_DISvt_" + label, &DISvt);
276 ok &= BindInputBranch(tree, "mudis_nDISdaughters_" + label, &nDISdau);
277 ok &= BindInputBranch(tree, "mudis_DISproducts_" + label, &DISparticles);
278 return ok;
279 }
280
281 template <class T>
282 std::string Print(const std::vector<T>& aVec, const TString& aName) {
283 std::ostringstream lOut;
284 lOut << " - " << aName << " size " << aVec.size() << " pointer " << &aVec
285 << ": ";
286 for (const auto& value : aVec) {
287 lOut << value << " ";
288 }
289 lOut << std::endl;
290 return lOut.str();
291 };
292
293 std::ostringstream Print(const unsigned& aEvt, const TString& aLabel) {
294 std::ostringstream lOut;
295 lOut << "------------ print evt " << aEvt << " branch " << aLabel
296 << " -------------" << std::endl
297 << " - nDISevts = " << nDISevts << std::endl
298 << " - wDIS = " << wDIS << std::endl;
299 if (DISxsec) lOut << Print(*DISxsec, "DISxsec");
300 if (DIStarget) lOut << Print(*DIStarget, "DIStarget");
301 if (DISvx) lOut << Print(*DISvx, "DISvx");
302 if (DISvy) lOut << Print(*DISvy, "DISvy");
303 if (DISvz) lOut << Print(*DISvz, "DISvz");
304 if (DISvt) lOut << Print(*DISvt, "DISvt");
305 if (nDISdau) lOut << Print(*nDISdau, "nDISdau");
306 if (DISparticles) lOut << Print(*DISparticles, "DISparticles");
307
308 return lOut;
309 };
310
311 private:
313};
314
316 std::vector<ShipMCTrack>* mcTrks = nullptr;
317 std::vector<vetoPoint>* sbtPt = nullptr;
318 std::vector<UpstreamTaggerPoint>* ubtPt = nullptr;
319 std::vector<strawtubesPoint>* sstPt = nullptr;
320 std::vector<TimeDetPoint>* tdPt = nullptr;
321 double pathLength = 0.;
322 std::array<double, nMats> pathLengthByMat = {};
324
325 bool Setup(TTree* tree) {
326 fTreeNumber = -1;
327 Clear();
328 return tree->GetEntries() == 0 || PrepareEntry(tree, 0);
329 }
330
331 bool PrepareEntry(TTree* tree, Long64_t entry) {
332 Clear();
333 if (tree->LoadTree(entry) < 0) return false;
334 if (tree->GetTreeNumber() != fTreeNumber) {
335 fTreeNumber = tree->GetTreeNumber();
336 auto* current = tree->GetTree();
337 fValid = BindInputBranch(current, "muon_MCTracks", &mcTrks);
338 fValid &= BindInputBranch(current, "muon_TDPoints", &tdPt);
339 fValid &= BindInputBranch(current, "muon_SBTPoints", &sbtPt);
340 fValid &= BindInputBranch(current, "muon_UBTPoints", &ubtPt);
341 fValid &= BindInputBranch(current, "muon_SSTPoints", &sstPt);
342 fValid &= BindInputBranch(current, "muon_path_length", &pathLength);
343 for (unsigned i = 0; i < nMats; ++i) {
344 const TString name = "muon_path_length_" + MatTypeStr[i];
345 fValid &= BindInputBranch(current, name, &pathLengthByMat[i]);
346 fValid &= br[i].SetupTree(current, MatTypeStr[i]);
347 }
348 }
349 return fValid;
350 }
351
352 private:
353 void Clear() {
354 fTracks.clear();
355 fSBT.clear();
356 fUBT.clear();
357 fSST.clear();
358 fTD.clear();
359 mcTrks = &fTracks;
360 sbtPt = &fSBT;
361 ubtPt = &fUBT;
362 sstPt = &fSST;
363 tdPt = &fTD;
364 pathLength = 0.;
365 pathLengthByMat.fill(0.);
366 for (auto& material : br) material.Clear();
367 }
368 std::vector<ShipMCTrack> fTracks;
369 std::vector<vetoPoint> fSBT;
370 std::vector<UpstreamTaggerPoint> fUBT;
371 std::vector<strawtubesPoint> fSST;
372 std::vector<TimeDetPoint> fTD;
373 int fTreeNumber = -1;
374 bool fValid = false;
375};
376
377} // namespace ShipMuDIS
378
379#endif // NEWMUONDIS_MUDISDEFS_H_
bool BindInputBranch(TTree *tree, const TString &name, T *address, bool required=true)
Definition MuDISDefs.h:143
constexpr double kMuonShieldTailZ
Definition MuDISDefs.h:30
const std::array< TString, nMats > MatTypeStr
Definition MuDISDefs.h:45
std::vector< ShipMCTrack > fTracks
Definition MuDISDefs.h:202
bool PrepareEntry(TTree *tree, Long64_t entry)
Definition MuDISDefs.h:174
std::vector< strawtubesPoint > fSST
Definition MuDISDefs.h:205
std::vector< TimeDetPoint > * tdPt
Definition MuDISDefs.h:166
std::vector< UpstreamTaggerPoint > * ubtPt
Definition MuDISDefs.h:164
std::vector< TimeDetPoint > fTD
Definition MuDISDefs.h:206
std::vector< vetoPoint > fSBT
Definition MuDISDefs.h:203
std::vector< strawtubesPoint > * sstPt
Definition MuDISDefs.h:165
std::vector< vetoPoint > * sbtPt
Definition MuDISDefs.h:163
bool Setup(TChain *tree)
Definition MuDISDefs.h:168
std::vector< UpstreamTaggerPoint > fUBT
Definition MuDISDefs.h:204
std::vector< ShipMCTrack > * MCTrack
Definition MuDISDefs.h:162
std::vector< strawtubesPoint > sstPt
Definition MuDISDefs.h:107
std::vector< TimeDetPoint > tdPt
Definition MuDISDefs.h:108
std::vector< vetoPoint > sbtPt
Definition MuDISDefs.h:105
MuonDISBranches br[nMats]
Definition MuDISDefs.h:111
void InitTree(TTree *&aT)
Definition MuDISDefs.h:112
std::array< double, nMats > pathLengthByMat
Definition MuDISDefs.h:110
std::vector< ShipMCTrack > mcTrks
Definition MuDISDefs.h:104
void initEvent(const int &nMax=100)
Definition MuDISDefs.h:124
std::vector< UpstreamTaggerPoint > ubtPt
Definition MuDISDefs.h:106
std::vector< DISparticle > DISparticles
Definition MuDISDefs.h:62
std::vector< double > DISvz
Definition MuDISDefs.h:59
void initEvent(const int &nDIS)
Definition MuDISDefs.h:79
std::vector< double > DISvx
Definition MuDISDefs.h:57
void InitTree(TTree *&aT, const TString &aLabel)
Definition MuDISDefs.h:64
std::vector< double > DISvy
Definition MuDISDefs.h:58
std::vector< double > DISxsec
Definition MuDISDefs.h:55
std::vector< double > DISvt
Definition MuDISDefs.h:60
std::vector< bool > DIStarget
Definition MuDISDefs.h:56
std::vector< int > nDISdau
Definition MuDISDefs.h:61
std::vector< double > * DISxsec
Definition MuDISDefs.h:217
std::ostringstream Print(const unsigned &aEvt, const TString &aLabel)
Definition MuDISDefs.h:293
std::vector< DISparticle > * DISparticles
Definition MuDISDefs.h:225
std::vector< double > * DISvt
Definition MuDISDefs.h:223
std::vector< bool > * DIStarget
Definition MuDISDefs.h:218
std::vector< double > * DISvx
Definition MuDISDefs.h:220
std::vector< double > * DISvy
Definition MuDISDefs.h:221
std::vector< double > * DISvz
Definition MuDISDefs.h:222
bool SetupTree(TTree *tree, const TString &label)
Definition MuDISDefs.h:263
std::string Print(const std::vector< T > &aVec, const TString &aName)
Definition MuDISDefs.h:282
std::vector< int > * nDISdau
Definition MuDISDefs.h:224
std::vector< TimeDetPoint > * tdPt
Definition MuDISDefs.h:320
std::vector< vetoPoint > fSBT
Definition MuDISDefs.h:369
std::vector< ShipMCTrack > fTracks
Definition MuDISDefs.h:368
bool PrepareEntry(TTree *tree, Long64_t entry)
Definition MuDISDefs.h:331
std::vector< UpstreamTaggerPoint > * ubtPt
Definition MuDISDefs.h:318
std::vector< TimeDetPoint > fTD
Definition MuDISDefs.h:372
std::vector< vetoPoint > * sbtPt
Definition MuDISDefs.h:317
std::vector< strawtubesPoint > fSST
Definition MuDISDefs.h:371
bool Setup(TTree *tree)
Definition MuDISDefs.h:325
std::vector< strawtubesPoint > * sstPt
Definition MuDISDefs.h:319
std::array< double, nMats > pathLengthByMat
Definition MuDISDefs.h:322
std::vector< ShipMCTrack > * mcTrks
Definition MuDISDefs.h:316
std::vector< UpstreamTaggerPoint > fUBT
Definition MuDISDefs.h:370
MuonDISInBranches br[nMats]
Definition MuDISDefs.h:323