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