NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
RTIInfoFill.cpp
Go to the documentation of this file.
1 #include "Containers/RTIInfo.h"
2 
3 #include "TMath.h"
4 
5 // gbatch headers
6 #include "GM_SubLibrary.h"
7 #include "root.h"
8 
9 #include <algorithm>
10 
11 namespace NAIA {
12 
13 static constexpr double Re = 6372.795477598e5; // cm
14 
15 template <typename ValueType, long unsigned int N>
16 constexpr int FindBin(ValueType value, const std::array<ValueType, N> &bins) {
17  auto binIt = std::lower_bound(begin(bins), end(bins), value, std::less_equal<ValueType>());
18  return binIt == end(bins) ? -1 : std::distance(begin(bins), binIt) - 1;
19 };
20 
21 template <typename ValueType>
22 int FindBin(ValueType value, const std::map<unsigned int, std::pair<ValueType, ValueType>> &bins) {
23  auto binIt = std::find_if(begin(bins), end(bins),
24  [&value](auto it) { return (value >= it.second.first && value < it.second.second); });
25  return binIt == end(bins) ? -1 : binIt->first;
26 };
27 
28 auto GoodParticle = [](ParticleR *part) {
29  BetaHR *beta = part->pBetaH();
30 
31  TrTrackR *trk = part->pTrTrack();
32  if (!trk)
33  return false;
34 
35  constexpr int refit = 31;
36  constexpr int fit_algo = 1;
37  int id_max = trk->iTrTrackPar(fit_algo, 0, refit);
38  if (id_max < 0)
39  return false;
40 
41  bool hasinnery = false;
42  bool L2y = false, L34y = false, L56y = false, L78y = false;
43  TrRecHitR *trkHit = nullptr;
44  trkHit = trk->GetHitLJ(2);
45  if (trkHit)
46  if (trkHit->pTrCluster('y')) {
47  L2y = true;
48  }
49  trkHit = trk->GetHitLJ(3);
50  if (trkHit)
51  if (trkHit->pTrCluster('y')) {
52  L34y = true;
53  }
54  trkHit = trk->GetHitLJ(4);
55  if (trkHit)
56  if (trkHit->pTrCluster('y')) {
57  L34y = true;
58  }
59  trkHit = trk->GetHitLJ(5);
60  if (trkHit)
61  if (trkHit->pTrCluster('y')) {
62  L56y = true;
63  }
64  trkHit = trk->GetHitLJ(6);
65  if (trkHit)
66  if (trkHit->pTrCluster('y')) {
67  L56y = true;
68  }
69  trkHit = trk->GetHitLJ(7);
70  if (trkHit)
71  if (trkHit->pTrCluster('y')) {
72  L78y = true;
73  }
74  trkHit = trk->GetHitLJ(8);
75  if (trkHit)
76  if (trkHit->pTrCluster('y')) {
77  L78y = true;
78  }
79  if (L2y && L34y && L56y && L78y)
80  hasinnery = true;
81 
82  float chi2 = trk->GetNormChisqY(id_max);
83  if (!beta || beta->GetSumHit() < 4 || !trk || id_max < 0 || !hasinnery || chi2 < 0 || chi2 > 10)
84  return false;
85 
86  return true;
87 };
88 
89 void RTIInfo::Fill(AMSSetupR::RTI *rtiPtr, unsigned int runTag) {
90  Run = rtiPtr->run;
91  RunTag = runTag;
92  FirstEvNo = rtiPtr->evno;
93  LastEvNo = rtiPtr->evnol;
94  LivetimeFraction = rtiPtr->lf;
95  MostProbableHeRig = rtiPtr->mphe;
96  Theta = rtiPtr->theta;
97  Phi = rtiPtr->phi;
98  Altitude = rtiPtr->r - Re;
99  Zenith = rtiPtr->zenith;
100  GalacticLat = rtiPtr->glat;
101  GalacticLong = rtiPtr->glong;
102  nEvent = rtiPtr->nev;
103  nError = rtiPtr->nerr;
104  nTrigger = rtiPtr->ntrig;
105  nHWError = rtiPtr->nhwerr;
106  nEventParticle = rtiPtr->npart;
107  nTRDHitAvg = rtiPtr->mtrdh;
108  nTrClusterAvg = rtiPtr->mtrcl;
109  good = rtiPtr->good;
110  UTCTime = rtiPtr->utime;
111 
112  for (unsigned int i = 0; i < 4; i++) {
113  for (unsigned int j = 0; j < 2; j++) {
114  MaxStoermerCutoff[i][j] = rtiPtr->cf[i][j];
115  MaxIGRFCutoff[i][j] = rtiPtr->cfi[i][j];
116  if (i < 2) {
117  MinIGRFCutoff[i][j] = rtiPtr->cfl[i][j];
118  }
119  }
120  }
121 
122  AMSPoint pn1, pn9, pd1, pd9;
123  AMSEventR::GetRTIdL1L9(0, pn1, pd1, UTCTime, 60);
124  AMSEventR::GetRTIdL1L9(1, pn9, pd9, UTCTime, 60);
125  MeanAlignDiffExtLayer[0] = {static_cast<float>(pd1.x()), static_cast<float>(pd1.y()), static_cast<float>(pd1.z())};
126  MeanAlignDiffExtLayer[1] = {static_cast<float>(pd9.x()), static_cast<float>(pd9.y()), static_cast<float>(pd9.z())};
127 
128  // again, this should be illegal
129  double GMC[3];
130  GM_GeoMagSphericalCoo(UTCTime, Altitude, Theta, Phi, GMC);
131 
132  MagThetaSphCoo = static_cast<float>(GMC[2]);
133  MagPhiSphCoo = static_cast<float>(GMC[1]);
134  MagAltSphCoo = static_cast<float>(GMC[0]);
135 
136  double lambda2 = cos(MagThetaSphCoo * TMath::DegToRad()) * cos(MagThetaSphCoo * TMath::DegToRad());
137  LShell = (MagAltSphCoo / Re) / lambda2;
138 
139  AMSSetupR amssetup;
140  AMSSetupR::DSPError dsperr;
141  DSPError = amssetup.getDSPError(dsperr, UTCTime);
142  DSPErrorJINJ = false;
143  for (int inode = 0x80; inode < 0x88; inode++)
144  if (dsperr.SearchNA(inode))
145  DSPErrorJINJ = true;
146  DSPErrorJLV1 = false;
147  for (int inode = 0x88; inode < 0x8C; inode++)
148  if (dsperr.SearchNA(inode))
149  DSPErrorJLV1 = true;
150  DSPErrorJINF = false;
151  for (int inode = 0x34; inode < 0x3E; inode++)
152  if (dsperr.SearchNA(inode))
153  DSPErrorJINF = true;
154  for (int inode = 0x96; inode < 0xCE; inode++)
155  if (dsperr.SearchNA(inode))
156  DSPErrorJINF = true;
157 }
158 
159 void RTIInfo::FillEventVariables() {
160  if (m_processedEvents == 0) {
161  return;
162  }
163 
164  auto normalize = [this](auto &value) { value /= m_processedEvents; };
165 
166  normalize(nTRDTrack);
167  normalize(nTRDHits);
168 
169  std::for_each(begin(nTRDLayerHits), begin(nTRDLayerHits), normalize);
170 
171  normalize(nEcalShower);
172  normalize(nEcalHits);
173 
174  normalize(nTrClusterX);
175  normalize(nTrClusterY);
176  normalize(nTrClusterL1X);
177  normalize(nTrClusterL1Y);
178 
179  normalize(nTofClusterH);
180 
181  normalize(nParticle);
182  normalize(nGoodParticle);
183  normalize(nPartTrkInn);
184  normalize(nPartTrk);
185 
186  for (auto &inner : nPartRate)
187  std::for_each(begin(inner), end(inner), normalize);
188  for (auto &inner : nPartBetaRate)
189  std::for_each(begin(inner), end(inner), normalize);
190  for (auto &inner : nPartInnRate)
191  std::for_each(begin(inner), end(inner), normalize);
192  std::for_each(begin(nPartAboveCutoff), end(nPartAboveCutoff), normalize);
193  std::for_each(begin(nPartBelowCutoff), end(nPartBelowCutoff), normalize);
194 
195  normalize(nTrk);
196  normalize(nTrkInn);
197 
198  for (auto &inner : nTrkRate)
199  std::for_each(begin(inner), end(inner), normalize);
200  for (auto &inner : nTrkInnRate)
201  std::for_each(begin(inner), end(inner), normalize);
202 
203  normalize(nACC);
204  std::for_each(begin(TrigPhysBPatt), end(TrigPhysBPatt), normalize);
205  std::for_each(begin(TrigJMembPatt), end(TrigJMembPatt), normalize);
206  std::for_each(begin(TrigRates), end(TrigRates), normalize);
207 
208  std::for_each(begin(TofFlags), end(TofFlags), normalize);
209 
210  normalize(TofRaw_nstdc);
211  normalize(TofRaw_nftdc);
212  normalize(TofRaw_nsumh);
213  normalize(TofRaw_nsumsh);
214 
215  normalize(AntiRaw_ntdct);
216  normalize(AntiRaw_nftdc);
217 
218  normalize(nRoomError);
219 
220  m_processedEvents = 0;
221 }
222 
223 void RTIInfo::AccumulateEventVariables(AMSEventR *evPtr) {
225 
226  // TRD
227  nTRDTrack += evPtr->NTrdTrack();
228  nTRDHits += evPtr->nTrdRawHit();
229 
230  std::for_each(begin(evPtr->TrdCluster()), end(evPtr->TrdCluster()),
231  [this](const TrdClusterR &cl) { nTRDLayerHits[cl.Layer]++; });
232 
233  // ECAL
234  nEcalShower += evPtr->NEcalShower();
235  nEcalHits += evPtr->nEcalHit();
236 
237  // TrCluster
238  std::for_each(begin(evPtr->TrRawCluster()), end(evPtr->TrRawCluster()), [this](TrRawClusterR &cl) {
239  if (cl.GetSide() == 0) {
240  nTrClusterX++;
241  if (cl.GetLayerJ() == 1)
242  nTrClusterL1X++;
243  } else {
244  nTrClusterY++;
245  if (cl.GetLayerJ() == 1)
246  nTrClusterL1Y++;
247  }
248  });
249 
250  // TOF
251  nTofClusterH += evPtr->nTofClusterH();
252 
253  // Particle rates
254  std::for_each(begin(evPtr->Particle()), end(evPtr->Particle()), [this](ParticleR &part) {
255  nParticle++;
256 
257  if (GoodParticle(&part)) {
258  nGoodParticle++;
259 
260  constexpr int refit = 31;
261  constexpr int fit_algo = 1;
262  int id_inner = part.pTrTrack()->iTrTrackPar(fit_algo, 3, refit);
263  if (id_inner >= 0) {
264  nPartTrkInn++;
265  float rig = part.pTrTrack()->GetRigidity(id_inner);
266  float q = part.pTrTrack()->GetQ(part.pBetaH()->GetBeta());
267  int rbin = FindBin(rig, rigbins);
268  int qbin = rig < 0 ? 0 : FindBin(q, qbins);
269  if (qbin >= 0 && rbin >= 0)
270  nPartInnRate[qbin][rbin]++;
271  }
272  int id_max = part.pTrTrack()->iTrTrackPar(fit_algo, 0, refit);
273  if (id_max >= 0) {
274  nPartTrk++;
275  float rig = part.pTrTrack()->GetRigidity(id_inner);
276  float q = part.pTrTrack()->GetQ(part.pBetaH()->GetBeta());
277  int rbin = FindBin(rig, rigbins);
278  int qbin = rig < 0 ? 0 : FindBin(q, qbins);
279  if (qbin >= 0 && rbin >= 0)
280  nPartRate[qbin][rbin]++;
281 
282  int bbin = FindBin(part.pBetaH()->GetBeta(), betabins);
283  nPartBetaRate[qbin][bbin]++;
284 
285  float maxrigcutoff = max(fabs(MaxIGRFCutoff[3][0]), fabs(MaxIGRFCutoff[3][1])); // 40 deg
286  float minrigcutoff = min(fabs(MinIGRFCutoff[1][0]), fabs(MinIGRFCutoff[1][1])); // 40 deg
287  if (qbin >= 0 && fabs(rig) < minrigcutoff)
288  nPartBelowCutoff[qbin]++;
289  if (qbin >= 0 && fabs(rig) > maxrigcutoff)
290  nPartAboveCutoff[qbin]++;
291  }
292  }
293  });
294 
295  std::for_each(begin(evPtr->TrTrack()), end(evPtr->TrTrack()), [this](TrTrackR &trtrack) {
296  constexpr int refit = 31;
297  constexpr int fit_algo = 1; // Kalman fitter
298 
299  int id_inner = trtrack.iTrTrackPar(fit_algo, 3, refit);
300  if (id_inner >= 0) {
301  nTrkInn++;
302  float rig = trtrack.GetRigidity(id_inner);
303  float q = trtrack.GetQ(1.0);
304  int rbin = FindBin(rig, rigbins);
305  int qbin = rig < 0 ? 0 : FindBin(q, qbins);
306  if (qbin >= 0 && rbin >= 0)
307  nTrkInnRate[qbin][rbin]++;
308  }
309  int id_max = trtrack.iTrTrackPar(fit_algo, 0, refit);
310  if (id_max >= 0) {
311  nTrk++;
312  float rig = trtrack.GetRigidity(id_max);
313  float q = trtrack.GetQ(1.0);
314  int rbin = FindBin(rig, rigbins);
315  int qbin = rig < 0 ? 0 : FindBin(q, qbins);
316  if (qbin >= 0 && rbin >= 0)
317  nTrkRate[qbin][rbin]++;
318  }
319  });
320 
321  auto *trig = evPtr->pLevel1(0);
322  if (trig) {
323  trig->RestorePhysBPat();
324  auto bpatt = std::bitset<8>(trig->PhysBPatt);
325  for (size_t ipatt = 0; ipatt < 8; ipatt++)
326  if (bpatt[ipatt])
327  TrigPhysBPatt[ipatt]++;
328 
329  auto jpatt = std::bitset<16>(trig->JMembPatt);
330  for (size_t ipatt = 0; ipatt < 16; ipatt++)
331  if (jpatt[ipatt])
332  TrigJMembPatt[ipatt]++;
333 
334  for (size_t i = 0; i < 19; i++)
335  TrigRates[i] += (float)trig->TrigRates[i];
336 
337  auto antipatt = std::bitset<8>(trig->AntiPatt);
338  for (size_t iacc = 0; iacc < 8; iacc++)
339  if (antipatt[iacc])
340  nACC++;
341 
342  if (trig->TofFlag1 < 0)
343  TofFlags[15]++; // No FTC
344  else if (trig->TofFlag1 < 15)
345  TofFlags[trig->TofFlag1]++;
346 
347  std::for_each(begin(evPtr->TofRawSide()), end(evPtr->TofRawSide()), [this](TofRawSideR &tofraw) {
348  TofRaw_nstdc += tofraw.nstdc; // Low Threshold History
349  TofRaw_nftdc += tofraw.nftdc; // Fast Trigger History
350  TofRaw_nsumh += tofraw.nsumh; // High Threshold History
351  TofRaw_nsumsh += tofraw.nsumsh; // Super-High Threshold History
352  });
353 
354  std::for_each(begin(evPtr->AntiRawSide()), end(evPtr->AntiRawSide()), [this](AntiRawSideR &antiraw) {
355  AntiRaw_ntdct += antiraw.ntdct; // Low Threshold History
356  AntiRaw_nftdc += antiraw.nftdc; // Fast Trigger History
357  });
358 
359  // Check for room errors
360  auto *daq = evPtr->pDaqEvent(0);
361  if (daq) {
362  bool jinj_room_error = false;
363  for (int ijinj = 0; ijinj < 4; ijinj++) {
364  if ((daq->JINJStatus[ijinj] >> 8) & 0x2) {
365  for (int ijinf = 0; ijinf < 24; ijinf++) {
366  if ((((daq->JError[ijinf]) >> 3) == 0x1A) || (((daq->JError[ijinf]) >> 3) == 0x12))
367  jinj_room_error = true;
368  }
369  }
370  }
371  if (jinj_room_error)
372  nRoomError++;
373  }
374  }
375 }
376 
377 } // namespace NAIA
float nTrigger
Number of events with trigger in this second.
Definition: RTIInfo.h:83
float nPartTrkInn
Average number of ParticleR with InnerTracker track.
Definition: RTIInfo.h:120
float nTRDHitAvg
Average number of TRD raw hits for one event.
Definition: RTIInfo.h:89
bool DSPErrorJLV1
Has a DSP error occurred in JLV1 in this second?
Definition: RTIInfo.h:97
float nEventParticle
Number of events with tof+trd+tracker+ecal data.
Definition: RTIInfo.h:85
float Altitude
Altitude (gtod coordinate system) (cm)
Definition: RTIInfo.h:77
std::array< std::array< float, 2 >, 4 > MaxStoermerCutoff
Max Stoermer cutoff within a 25,30,35,40 degrees field of view (for both negative and positive partic...
Definition: RTIInfo.h:71
unsigned int LastEvNo
Last event no in this second.
Definition: RTIInfo.h:67
unsigned int FirstEvNo
First event nnumber in this second.
Definition: RTIInfo.h:66
auto GoodParticle
Definition: RTIInfoFill.cpp:28
unsigned int RunTag
Run tag.
Definition: RTIInfo.h:65
std::array< std::array< float, 2 >, 4 > MaxIGRFCutoff
Max IGRF cutoff within a 25,30,35,40 degrees field of view (for both negative and positive particles)...
Definition: RTIInfo.h:72
float nACC
Average number of ACC sectors.
Definition: RTIInfo.h:140
std::array< std::array< float, nbetabins+1 >, nqbins+1 > nPartBetaRate
Average number of ParticleR object divided by charge and beta.
Definition: RTIInfo.h:124
static const std::array< float, nbetabins+1 > betabins
Bin edges for beta-binned variables.
Definition: RTIInfo.h:160
std::array< float, 16 > TofFlags
Average value of TofFlags.
Definition: RTIInfo.h:141
std::array< std::array< float, nrigbins+1 >, nqbins+1 > nTrkInnRate
Average number of TrTrackR object divided by charge and rigidity (inner tracker fit) ...
Definition: RTIInfo.h:134
std::array< float, nqbins+1 > nPartAboveCutoff
Average number of ParticleR objects above cutoff.
Definition: RTIInfo.h:127
float nTrClusterL1X
Average number of TrRawCluster on x side of L1.
Definition: RTIInfo.h:113
float nTrClusterY
Average number of TrRawCluster on y side.
Definition: RTIInfo.h:112
std::array< std::array< float, nrigbins+1 >, nqbins+1 > nTrkRate
Average number of TrTrackR object divided by charge and rigidity.
Definition: RTIInfo.h:133
std::array< float, 8 > TrigPhysBPatt
Average fraction of each trigger pattern.
Definition: RTIInfo.h:137
NAIAChain::EventItr begin(NAIAChain &chain)
Definition: NAIAChain.h:297
std::array< float, 16 > TrigJMembPatt
Average fraction of each trigger pattern.
Definition: RTIInfo.h:138
float nHWError
Number of events with HW error (from JINJStatus)
Definition: RTIInfo.h:84
float nParticle
Average number of ParticleR objects.
Definition: RTIInfo.h:118
float nTrkInn
Average number of TrTrackR objects with inner fit.
Definition: RTIInfo.h:131
std::array< float, nqbins+1 > nPartBelowCutoff
Average number of ParticleR objects below cutoff.
Definition: RTIInfo.h:128
float MagAltSphCoo
Magnetic coordinates altitude.
Definition: RTIInfo.h:101
float nTrClusterAvg
Average number of Tracker raw clusters for one event.
Definition: RTIInfo.h:90
float MostProbableHeRig
Most probable He rigidity for this second;.
Definition: RTIInfo.h:74
float LivetimeFraction
Livetime fraction during this second.
Definition: RTIInfo.h:68
float nTRDHits
Average number of total TrdRawHit.
Definition: RTIInfo.h:105
float nEcalShower
Average number of EcalShower objects.
Definition: RTIInfo.h:108
float Phi
Phi (gtod coordinate system) (rad)
Definition: RTIInfo.h:76
float nTrk
Average number of TrTrackR objects with default fit.
Definition: RTIInfo.h:130
float TofRaw_nsumh
High Threshold history.
Definition: RTIInfo.h:145
unsigned int UTCTime
JMDC unix time (seconds since 1 Jan 1970)
Definition: RTIInfo.h:92
float Theta
Theta (gtod coordinate system) (rad)
Definition: RTIInfo.h:75
bool DSPErrorJINF
Has a DSP error occurred in JINF in this second?
Definition: RTIInfo.h:96
float GalacticLong
Galactic longitude of AMS pointing direction (degrees)
Definition: RTIInfo.h:80
float Zenith
AMS zenith angle (degrees)
Definition: RTIInfo.h:78
static const std::map< unsigned int, std::pair< float, float > > qbins
Bin edges for charge-binned variables, depending on particle species.
Definition: RTIInfo.h:163
bool DSPError
Has a DSP error occurred in this second?
Definition: RTIInfo.h:94
float nEcalHits
Average number of EcalHit objects.
Definition: RTIInfo.h:109
float MagThetaSphCoo
Magnetic coordinates theta.
Definition: RTIInfo.h:99
float TofRaw_nftdc
Fast Trigger history.
Definition: RTIInfo.h:144
static constexpr double Re
Definition: RTIInfoFill.cpp:13
float nEvent
Total number of events in this second.
Definition: RTIInfo.h:81
float MagPhiSphCoo
Magnetic coordinates phi.
Definition: RTIInfo.h:100
std::array< float, 19 > TrigRates
Average value of trigger rates.
Definition: RTIInfo.h:139
float nError
Number of missing events due to errors.
Definition: RTIInfo.h:82
float nPartTrk
Average number of ParticleR with Tracker track.
Definition: RTIInfo.h:121
unsigned long long m_processedEvents
Definition: RTIInfo.h:170
std::array< float, 20 > nTRDLayerHits
Average number of TRD hits in each layer.
Definition: RTIInfo.h:106
int good
0 if good (Thanks, Qi. Very descriptive)
Definition: RTIInfo.h:91
float nTrClusterX
Average number of TrRawCluster on x side.
Definition: RTIInfo.h:111
std::array< std::array< float, 3 >, 2 > MeanAlignDiffExtLayer
mean difference(um) bewteen PG ad CIEMAT alignment of L1 and L9(XYZ)
Definition: RTIInfo.h:87
float LShell
L shell.
Definition: RTIInfo.h:102
constexpr int FindBin(ValueType value, const std::array< ValueType, N > &bins)
Definition: RTIInfoFill.cpp:16
float AntiRaw_nftdc
Fast Trigger history.
Definition: RTIInfo.h:149
std::array< std::array< float, 2 >, 2 > MinIGRFCutoff
Min IGRF cutoff within a 25,40 degrees field of view (for both negative and positive particles) ...
Definition: RTIInfo.h:73
float nTofClusterH
Average number of TofClusterH objects.
Definition: RTIInfo.h:116
float nGoodParticle
Average number of &quot;good&quot; ParticleR objects.
Definition: RTIInfo.h:119
float TofRaw_nstdc
Low Threshold history.
Definition: RTIInfo.h:143
std::array< std::array< float, nrigbins+1 >, nqbins+1 > nPartRate
Average number of ParticleR object divided by charge and rigidity.
Definition: RTIInfo.h:123
NAIAChain::EventItr end(NAIAChain &chain)
Definition: NAIAChain.h:298
float AntiRaw_ntdct
Low Threshold history.
Definition: RTIInfo.h:148
std::array< std::array< float, nrigbins+1 >, nqbins+1 > nPartInnRate
Average number of ParticleR object divided by charge and rigidity (inner tracker fit) ...
Definition: RTIInfo.h:125
bool DSPErrorJINJ
Has a DSP error occurred in JINJ in this second?
Definition: RTIInfo.h:95
float nTRDTrack
Average number of TrdTrack objects.
Definition: RTIInfo.h:104
float TofRaw_nsumsh
Super-High Threshold history.
Definition: RTIInfo.h:146
float nTrClusterL1Y
Average number of TrRawCluster on y side of L1.
Definition: RTIInfo.h:114
float GalacticLat
Galactic latitude of AMS pointing direction (degrees)
Definition: RTIInfo.h:79
static const std::array< float, nrigbins+1 > rigbins
Bin edges for rigidity-binned variables.
Definition: RTIInfo.h:159
RTIInfo container class description.
float nRoomError
Average number of room errors.
Definition: RTIInfo.h:151
unsigned int Run
Run number.
Definition: RTIInfo.h:64