CbmRoot
Loading...
Searching...
No Matches
CbmTofMergeMcPoints.cxx
Go to the documentation of this file.
1/* Copyright (C) 2016-2020 Facility for Antiproton and Ion Research in Europe, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Pierre-Alain Loizeau [committer] */
4
5/*
6 * CbmTofMergeMcPoints.cxx
7 *
8 * Created on: Jan 08, 2016
9 * Author: P.-A. Loizeau
10 */
11
12#include "CbmTofMergeMcPoints.h"
13
14// TOF Classes and includes
15#include "CbmTofDetectorId_v12b.h" // in cbmdata/tof
16#include "CbmTofDetectorId_v14a.h" // in cbmdata/tof
17#include "CbmTofGeoHandler.h" // in tof/TofTools
18#include "CbmTofPoint.h" // in cbmdata/tof
19
20// CBMroot classes and includes
21#include "CbmMCTrack.h"
22#include "CbmMatch.h"
23
24// FAIR classes and includes
25#include "FairRootManager.h"
26#include <Logger.h>
27
28// ROOT Classes and includes
29#include "TClonesArray.h"
30
31#include "Riostream.h"
32
34 : FairTask()
35 , fGeoHandler(new CbmTofGeoHandler())
36 , fTofId(NULL)
37 , fMcTracksColl(NULL)
38 , fTofPointsColl(NULL)
39 , fTofPntTrkMap()
40 , fRealTofPoints(NULL)
41 , fTofRealPntMatches(NULL)
42{
43}
44
46{
47 if (fRealTofPoints != NULL) {
48 fRealTofPoints->Delete();
49 delete fRealTofPoints;
50 }
51
52 if (fTofRealPntMatches != NULL) {
53 fTofRealPntMatches->Delete();
54 delete fTofRealPntMatches;
55 }
56}
57
59{
61
62 // Initialize the TOF GeoHandler
63 Bool_t isSimulation = kFALSE;
64 Int_t iGeoVersion = fGeoHandler->Init(isSimulation);
65 LOG(info) << "CbmTofMergeMcPoints::Init with GeoVersion " << iGeoVersion;
66
67 if (k12b > iGeoVersion) {
68 LOG(error) << "CbmTofMergeMcPoints::Init => Only compatible with "
69 "geometries after v12b !!!";
70 return kFATAL;
71 } // if( k12b > iGeoVersion )
72
74
75 if (NULL != fTofId) LOG(info) << "CbmTofMergeMcPoints::Init with GeoVersion " << fGeoHandler->GetGeoVersion();
76 else {
77 switch (iGeoVersion) {
78 case k12b: fTofId = new CbmTofDetectorId_v12b(); break;
79 case k14a: fTofId = new CbmTofDetectorId_v14a(); break;
80 default: LOG(error) << "CbmTofMergeMcPoints::Init => Invalid geometry!!!" << iGeoVersion; return kFATAL;
81 } // switch(iGeoVersion)
82 } // else of if(NULL != fTofId)
83
84 return kSUCCESS;
85}
86
87void CbmTofMergeMcPoints::Exec(Option_t* /*opt*/)
88{
89 if (fRealTofPoints != NULL) fRealTofPoints->Delete();
90 if (fTofRealPntMatches != NULL) fTofRealPntMatches->Delete();
91 ;
92 // TOF: (MC)=>(Realistic MC) & (MC->RealisticMC)
94
95 static Int_t eventNo = 0;
96 LOG(info) << "CbmTofMergeMcPoints::Exec eventNo=" << eventNo++;
97}
98
100
102{
103 FairRootManager* ioman = FairRootManager::Instance();
104 if (NULL == ioman)
105 LOG(fatal) << "CbmTofMergeMcPoints::ReadAndCreateDataBranches() NULL "
106 "FairRootManager.";
107
108 // TOF
109 fMcTracksColl = (TClonesArray*) ioman->GetObject("MCTrack");
110 if (NULL == fMcTracksColl) {
111 LOG(fatal) << "CbmTofMergeMcPoints::ReadAndCreateDataBranches => Could not "
112 "get the MCTrack TClonesArray!!!";
113 } // if( NULL == fMcTracksColl)
114
115 fTofPointsColl = (TClonesArray*) ioman->GetObject("TofPoint");
116 if (NULL == fTofPointsColl) {
117 LOG(fatal) << "CbmTofMergeMcPoints::ReadAndCreateDataBranches => Could not "
118 "get the TofPoint TClonesArray!!!";
119 } // if( NULL == fTofPointsColl)
120
121 if (NULL != fTofPointsColl) {
122 fRealTofPoints = new TClonesArray("CbmTofPoint", 100);
123 ioman->Register("RealisticTofPoint", "TOF", fRealTofPoints, IsOutputBranchPersistent("RealisticTofPoint"));
124 fTofRealPntMatches = new TClonesArray("CbmMatch", 100);
125 ioman->Register("TofRealPntMatch", "TOF", fTofRealPntMatches, IsOutputBranchPersistent("TofRealPntMatch"));
126 }
127}
128
129void CbmTofMergeMcPoints::MergeRealisticTofPoints(const TClonesArray* tracks, const TClonesArray* points,
130 TClonesArray* realisticPoints, TClonesArray* pointsMatches)
131{
132 if (!(points && realisticPoints && pointsMatches)) return;
133
134 Int_t iNbTracks = tracks->GetEntriesFast();
135 Int_t iNbTofPoints = points->GetEntriesFast();
136
137 CbmMCTrack* pMcTrk = NULL;
138 CbmTofPoint* pPnt = NULL;
139 Int_t iTrackId = 0;
140 Int_t iDetId = 0;
141 Int_t iModType = 0;
142 Int_t iModule = 0;
143 Int_t iCounter = 0;
144
145 // Initial loop to fill one pair per track with TOF points in the map
146 for (Int_t iTrk = 0; iTrk < iNbTracks; iTrk++) {
147 pMcTrk = (CbmMCTrack*) tracks->At(iTrk);
148
149 if (0 < pMcTrk->GetNPoints(ECbmModuleId::kTof))
150 fTofPntTrkMap.insert(std::pair<Int_t, std::vector<Int_t>>(iTrk, std::vector<Int_t>()));
151 } // for (Int_t iTrk = 0; iTrk < iNbTofPoints; iTrk++)
152
153 // Prepare the vector to keep track of which mean TOF Point comes
154 // from each TofPoint
155 // Maybe more efficient to use std::list instead of std::vector as
156 // removinge elements inside the array later?
157 std::vector<Int_t> vMeanIdPnts(iNbTofPoints, -1);
158
159 // Initial loop to fill the vectors and order the points by tracks
160 for (Int_t iPnt = 0; iPnt < iNbTofPoints; iPnt++) {
161 pPnt = static_cast<CbmTofPoint*>(points->At(iPnt));
162
163 iTrackId = pPnt->GetTrackID();
164
165 fTofPntTrkMap[iTrackId].push_back(iPnt);
166 } // for (Int_t iPnt = 0; iPnt < iNbTofPoints; iPnt++)
167
168 // Second loop to generate the mean MC point per (track, det) pair
169 // and fill them in the output arrays
170 Int_t iMeanMcPointId = 0;
171 Int_t iMeanChannel = 0; // mean channel index
172 Double_t dMeanPosX = 0;
173 Double_t dMeanPosY = 0;
174 Double_t dMeanPosZ = 0;
175 Double_t dMeanTime = 0;
176 Double_t dMeanMomX = 0;
177 Double_t dMeanMomY = 0;
178 Double_t dMeanMomZ = 0;
179 Double_t dMeanLen = 0;
180 Double_t dTotELoss = 0;
181 Int_t iNbPntInMean = 0;
182 Int_t iMeanModType = -1;
183 Int_t iMeanModule = -1;
184 Int_t iMeanCounter = -1;
185 for (std::map<Int_t, std::vector<Int_t>>::iterator it = fTofPntTrkMap.begin(); it != fTofPntTrkMap.end(); ++it) {
186 // std::vector< Int_t > vTofPnt = it->second;
187
188 // Each pair associate a track ID with the list of id for its corresponding TofPoint
189 while (0 < (it->second).size()) {
190 // A - Start storing the info for the mean MC point with the first
191 // point
192 // Keep track of current mean MC Point index
193 Int_t iPntIdxList = (it->second).size() - 1;
194 pPnt = static_cast<CbmTofPoint*>(points->At((it->second)[iPntIdxList]));
195 iDetId = pPnt->GetDetectorID();
196
197 // First store the info identifying the counter
198 iMeanModType = fGeoHandler->GetSMType(iDetId);
199 iMeanModule = fGeoHandler->GetSModule(iDetId);
200 iMeanCounter = fGeoHandler->GetCounter(iDetId);
201 // Then store the MC Points information
202 iNbPntInMean = 1;
203 iMeanChannel = fGeoHandler->GetCell(iDetId); // mean channel index
204 dMeanPosX = pPnt->GetX();
205 dMeanPosY = pPnt->GetY();
206 dMeanPosZ = pPnt->GetZ();
207 dMeanTime = pPnt->GetTime();
208 dMeanMomX = pPnt->GetPx();
209 dMeanMomY = pPnt->GetPy();
210 dMeanMomZ = pPnt->GetPz();
211 dMeanLen = pPnt->GetLength();
212 dTotELoss = pPnt->GetEnergyLoss();
213 // Keep track of mean Point index match
214 vMeanIdPnts[(it->second)[iPntIdxList]] = iMeanMcPointId;
215 // Get rid of the point index in the list
216 (it->second).pop_back();
217
218 // B - Scan the remaining points to find the ones belonging to
219 // the same RPC counter
220 // Fill in parallel the vector keeping track of the match
221 iPntIdxList = (it->second).size() - 1;
222 while (0 <= iPntIdxList && 0 < (it->second).size()) {
223 pPnt = static_cast<CbmTofPoint*>(points->At((it->second)[iPntIdxList]));
224
225 // First access the info identifying the counter
226 iDetId = pPnt->GetDetectorID();
227 iModType = fGeoHandler->GetSMType(iDetId);
228 iModule = fGeoHandler->GetSModule(iDetId);
229 iCounter = fGeoHandler->GetCounter(iDetId);
230
231 if ((iMeanModType == iModType) && (iMeanModule == iModule) && (iMeanCounter == iCounter)) {
232 // Then store the MC Points information if counter match
233 iNbPntInMean++;
234 iMeanChannel += fGeoHandler->GetCell(iDetId); // mean channel index
235 dMeanPosX += pPnt->GetX();
236 dMeanPosY += pPnt->GetY();
237 dMeanPosZ += pPnt->GetZ();
238 dMeanTime += pPnt->GetTime();
239 dMeanMomX += pPnt->GetPx();
240 dMeanMomY += pPnt->GetPy();
241 dMeanMomZ += pPnt->GetPz();
242 dMeanLen += pPnt->GetLength();
243 dTotELoss += pPnt->GetEnergyLoss();
244
245 // Keep track of mean Point index match
246 vMeanIdPnts[(it->second)[iPntIdxList]] = iMeanMcPointId;
247
248 // Get rid of the point index in the list
249 (it->second).erase((it->second).begin() + iPntIdxList);
250 iPntIdxList = (it->second).size() - 1;
251 } // if same sounter as first point
252 else {
253 iPntIdxList--; // just got to the next point in the list
254 } // if different conter as first point
255 } // while( 0 <= iPntIdxList && 0 < (it->second).size() )
256
257 // C - Create new mean MC Point
258 // First do the mean
259 iMeanChannel /= iNbPntInMean; // mean channel index
260 dMeanPosX /= iNbPntInMean;
261 dMeanPosY /= iNbPntInMean;
262 dMeanPosZ /= iNbPntInMean;
263 dMeanTime /= iNbPntInMean;
264 dMeanMomX /= iNbPntInMean;
265 dMeanMomY /= iNbPntInMean;
266 dMeanMomZ /= iNbPntInMean;
267 dMeanLen /= iNbPntInMean;
268 // Store the new mean MC Point in the output TClonesArray
269 CbmTofDetectorInfo detInfo(ECbmModuleId::kTof, iMeanModType, iMeanModule, iMeanCounter, 0, iMeanChannel);
270 TVector3 meanPos(dMeanPosX, dMeanPosY, dMeanPosZ);
271 TVector3 meanMom(dMeanMomX, dMeanMomY, dMeanMomZ);
272 // CbmTofPoint* pMeanPoint =
273 new ((*realisticPoints)[iMeanMcPointId])
274 CbmTofPoint(it->first, fTofId->SetDetectorInfo(detInfo), meanPos, meanMom, dMeanTime, dMeanLen, dTotELoss);
275 // Update the index for the next mean Point
276 iMeanMcPointId++;
277
278 } // while( 0 < (it->second).size() )
279 } // for (Int_t iPnt = 0; iPnt < iNbTofPoints; iPnt++)
280
281 // For each input MC Point, create a CbmMatch object to keep track
282 // of the corresponding Mean MC TOF point and store it in the ouptut
283 // TClonesArray
284 for (Int_t iPnt = 0; iPnt < iNbTofPoints; iPnt++) {
285
286 CbmMatch* pntMatch = new ((*pointsMatches)[iPnt]) CbmMatch();
287 // Add link storing the ID of the corresponding mean MC point
288 pntMatch->AddLink(CbmLink(1.0, vMeanIdPnts[iPnt]));
289 } // for (Int_t iPnt = 0; iPnt < iNbTofPoints; iPnt++)
290}
TClonesArray * tracks
TClonesArray * points
@ kTof
Time-of-flight Detector.
@ k12b
@ k14a
FairTask for merging TOF MC Points into more realistic TOF MC points.
static constexpr size_t size()
Definition KfSimdPseudo.h:2
int32_t GetNPoints(ECbmModuleId detId) const
void AddLink(const CbmLink &newLink)
Definition CbmMatch.cxx:47
virtual int32_t SetDetectorInfo(const CbmTofDetectorInfo detectorInfo)=0
Int_t GetCell(Int_t uniqueId)
Int_t Init(Bool_t isSimulation=kFALSE)
Int_t GetSModule(Int_t uniqueId)
Int_t GetCounter(Int_t uniqueId)
Int_t GetSMType(Int_t uniqueId)
CbmTofDetectorId * GetDetIdPointer()
virtual void Finish()
Derived from FairTask.
CbmTofMergeMcPoints()
Constructor.
TClonesArray * fTofRealPntMatches
TClonesArray * fMcTracksColl
virtual ~CbmTofMergeMcPoints()
Destructor.
virtual void Exec(Option_t *opt)
Derived from FairTask.
virtual InitStatus Init()
Derived from FairTask.
void ReadAndCreateDataBranches()
Read and create data branches.
CbmTofGeoHandler * fGeoHandler
TClonesArray * fRealTofPoints
std::map< Int_t, std::vector< Int_t > > fTofPntTrkMap
TClonesArray * fTofPointsColl
void MergeRealisticTofPoints(const TClonesArray *tracks, const TClonesArray *points, TClonesArray *realisticPoints, TClonesArray *pointsMatches)
CbmTofDetectorId * fTofId
Geometric intersection of a MC track with a TOFb detector.
Definition CbmTofPoint.h:44