er  dev
ERN15B11Detector.cxx
1 #include "ERN15B11Detector.h"
2 
3 // ROOT
4 #include <TClonesArray.h>
5 #include <TGeoMatrix.h>
6 #include <TVirtualMC.h>
7 #include <TGeoManager.h>
8 #include <TRefArray.h>
9 
10 // FairRoot
11 //#include <FairRun.h>
12 //#include <FairRuntimeDb.h>
13 #include <FairRootManager.h>
14 //#include <FairLogger.h>
15 #include <FairVolume.h>
16 
17 // Project
18 #include "cppTokenizer.h"
19 #include "ERPoint.h"
20 
21 /*static*/
22 struct stcNodeSig ERN15B11Detector::mCurNodeSig;
23 
24 /*static*/
25 Int_t ERN15B11Detector::mCurNodeUID = -1;
26 
27 ERN15B11Detector::ERN15B11Detector() :
28  FairDetector()
29 {
30  LOG(DEBUG) << "ERN15B11Detector::ERN15B11Detector()" << FairLogger::endl;
31 }
32 
33 ERN15B11Detector::ERN15B11Detector(const char* Name, Bool_t Active, Int_t DetId) :
34  FairDetector(Name, Active, DetId)
35 {
36  LOG(DEBUG) << "ERN15B11Detector::ERN15B11Detector(" << Name << ")" << FairLogger::endl;
37 }
38 
39 ERN15B11Detector::~ERN15B11Detector()
40 {
41 }
42 
43 void ERN15B11Detector::ConstructGeometry()
44 {
45  TString fileName = GetGeometryFileName();
46  if(fileName.EndsWith(".root")) {
47  LOG(INFO) << "Constructing geometry from ROOT file '" << fileName.Data() << "'" << FairLogger::endl;
48  this->ConstructRootGeometry();
49  }
50  else if (fileName.EndsWith(".gdml")) {
51  LOG(INFO) << "Constructing geometry from GDML file '" << fileName.Data() << "'" << FairLogger::endl;
52  TGeoRotation* zeroRotation = new TGeoRotation("zeroRotation", 0., 0., 0.);
53  this->ConstructGDMLGeometry(new TGeoCombiTrans(0., 0., 0., zeroRotation));
54  }
55  else {
56  LOG(FATAL) << "Unknown geometry file format." << FairLogger::endl;
57  }
58 }
59 
60 Bool_t ERN15B11Detector::CheckIfSensitive(std::string name)
61 {
62  LOG(DEBUG) << "ERN15B11Detector::CheckIfSensitive(" << name << ")" << FairLogger::endl;
63 
64 /* FairVolume* v_vol;
65  std::cout << svList->GetEntries() << std::endl;
66 
67  for (Int_t i=0; i<svList->GetEntries(); i++) {
68  v_vol = (FairVolume*)svList->At(i);
69  std::cout << v_vol->GetName() << std::endl;
70  }
71 */
72 
73  if (name == "vDetGasPart") {
74  return kTRUE;
75  }
76  if (name == "vSemi") {
77  return kTRUE;
78  }
79 
80  return kFALSE;
81 }
82 
83 Bool_t ERN15B11Detector::ProcessHits(FairVolume* v)
84 {
85  const char* curNodePath = gMC->CurrentVolPath();
86  Int_t curNodeId = this->GetCurNodeSig(curNodePath);
87  // The mCurNodeSig and mCurNodeUID are filled by the previous GetCurNodeSig() call
88 
89  LOG(DEBUG2) << "ERN15B11Detector::ProcessHits(" << v->GetName() << ") "
90  << "particle charge: " << gMC->TrackCharge() << " "
91  << curNodePath << ": " << mCurNodeUID
92  << FairLogger::endl;
93 
94 
95  std::map<Int_t, TClonesArray*>::iterator iter = mOutputPoints.find(mCurNodeUID);
96 
97  TClonesArray* v_outputCollection = NULL;
98 
99  if (iter != mOutputPoints.end()) {
100  // Collection exists
101  v_outputCollection = iter->second;
102  LOG(DEBUG2) << "Collection found." << FairLogger::endl;
103 
104  } else {
105  LOG(FATAL) << "Collection not found." << FairLogger::endl;
106  return kFALSE;
107  }
108 
109  fELoss += gMC->Edep();
110 
111 
112  if (gMC->IsTrackEntering()) {
113  StartNewPoint();
114  }
115 
116  if (gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()) {
117  FinishNewPoint(v_outputCollection);
118  }
119 
120  return kTRUE;
121 }
122 
123 void ERN15B11Detector::EndOfEvent()
124 {
125  LOG(DEBUG) << "ERN15B11Detector::EndOfEvent" << FairLogger::endl;
126  this->Reset();
127 }
128 
129 void ERN15B11Detector::Register()
130 {
131  LOG(DEBUG) << "ERN15B11Detector::Register" << FairLogger::endl;
132 
133  FairRootManager* ioman = FairRootManager::Instance();
134  if (!ioman) {
135  LOG(FATAL) << "IO manager is not set" << FairLogger::endl;
136  }
137 
138  TGeoIterator next(gGeoManager->GetTopVolume());
139 
140  TGeoNode *node;
141  while ((node=next())) {
142  TString curPath;
143  next.GetPath(curPath);
144 
145  Bool_t curSig = this->GetCurNodeSig(curPath);
146  if (curSig) {
147  LOG(DEBUG) << curPath << ": " << mCurNodeUID << FairLogger::endl;
148 
149 
150  std::map<Int_t, TClonesArray*>::iterator iter = mOutputPoints.find(mCurNodeUID);
151 
152  if (iter != mOutputPoints.end()) {
153  // Collection exists
154  TClonesArray* v_collection = iter->second;
155  LOG(DEBUG) << "Collection found." << FairLogger::endl;
156 
157  } else {
159 
160  // Collection does not exist - create it
161  TClonesArray* v_newCollection = new TClonesArray("ERPoint");
162  mOutputPoints.insert(std::pair<Int_t, TClonesArray*>(mCurNodeUID, v_newCollection));
163 
164  TString v_name;
165 
166  if ((mCurNodeSig.iSemi == -1) && (mCurNodeSig.iDetGasPart != -1)) {
167  // DetGasPart
168  v_name.Form("Gas_Layer_%d_Det_%d", mCurNodeSig.iLayer, mCurNodeSig.iDetContainer);
169  } else if ((mCurNodeSig.iSemi != -1) && (mCurNodeSig.iDetGasPart == -1)) {
170  // Semi
171  v_name.Form("Semi_Layer_%d_Det_%d", mCurNodeSig.iLayer, mCurNodeSig.iDetContainer);
172  }
173  ioman->Register(v_name + TString("_Point"), v_name, v_newCollection, kTRUE);
174 
175  LOG(DEBUG) << "Collection not found - create it: " << v_name << FairLogger::endl;
176  }
177 
178  }
179  }
180 }
181 
182 TClonesArray* ERN15B11Detector::GetCollection(Int_t iColl) const
183 {
184  LOG(DEBUG) << "ERN15B11Detector::GetCollection" << FairLogger::endl;
185 
186  /*if (fSenVolumes.size() > iColl) {
187  auto it = fSenVolumes.begin();
188  for (Int_t i = 0; i<iColl; i++) {
189  it++;
190  }
191  return it->second;
192  } else {
193  return NULL;
194  }*/
195 
196  return NULL;
197 }
198 
199 void ERN15B11Detector::Reset()
200 {
201  LOG(DEBUG) << "ERN15B11Detector::Reset" << FairLogger::endl;
202 
203  std::map<Int_t, TClonesArray*>::iterator iter;
204 
205  for (iter = mOutputPoints.begin(); iter != mOutputPoints.end(); ++iter) {
206  TClonesArray* curCollection = iter->second;
207  curCollection->Clear();
208  }
209 }
210 
211 Bool_t ERN15B11Detector::GetCurNodeSig(const char* nodePath) const
212 {
214  mCurNodeSig.iLayer = -1;
215  mCurNodeSig.iDetContainer = -1;
216  mCurNodeSig.iDetGasPart = -1;
217  mCurNodeSig.iSemi = -1;
218 
219  std::vector<std::string> mVolumesList;
220 
222  mVolumesList.push_back(std::string("vOneLayer_")); // 0-th item
223  mVolumesList.push_back(std::string("vDetContainer_")); // 1-st item
224  mVolumesList.push_back(std::string("vDetGasPart_")); // 2-nd item
225  mVolumesList.push_back(std::string("vSemi_")); // 3-d item
226 
227  std::string fullPath(nodePath);
228  std::string delimiter("/");
229  std::vector<std::string> tokens = tokenize(fullPath, delimiter);
230 
231  std::vector<std::string>::const_iterator iter;
232  for (iter = tokens.begin(); iter != tokens.end(); ++iter) {
233 
235 
236  UChar_t iCounter = 0;
237  std::vector<std::string>::const_iterator iter2;
238  for (iter2 = mVolumesList.begin(); iter2 != mVolumesList.end(); ++iter2) {
239 
240  size_t curStrLen = (*iter).size();
241  size_t curSearchedStrLen = (*iter2).size();
242 
243  //std::cout << (*iter) << " (" << (*iter2) << "): len=" << curStrLen
244  // << " (len=" << curSearchedStrLen << ")";
245 
246  if ((*iter).compare(0, curSearchedStrLen, (*iter2)) == 0) {
247  Short_t res = std::stoi((*iter).substr(curSearchedStrLen));
248  //std::cout << " index=" << res << std::endl;
249 
250  switch (iCounter) {
251  case 0: mCurNodeSig.iLayer = res; break;
252  case 1: mCurNodeSig.iDetContainer = res; break;
253  case 2: mCurNodeSig.iDetGasPart = res; break;
254  case 3: mCurNodeSig.iSemi = res; break;
255  default: break;
256  }
257  } else {
258  //std::cout << std::endl;
259  }
260 
261  iCounter++;
262  }
263 
264  }
265 
266  LOG(DEBUG3) << nodePath
267  << " SIGNATURE:"
268  << " iLayer=" << mCurNodeSig.iLayer
269  << " iDetContainer=" << mCurNodeSig.iDetContainer
270  << " iDetGasPart=" << mCurNodeSig.iDetGasPart
271  << " iSemi=" << mCurNodeSig.iSemi
272  << FairLogger::endl;
273 
274  if ((mCurNodeSig.iDetContainer != -1) && (mCurNodeSig.iLayer != -1))
275  {
276  if ((mCurNodeSig.iSemi == -1) && (mCurNodeSig.iDetGasPart != -1)) {
277  // DetGasPart
278  mCurNodeUID = mCurNodeSig.iLayer * 100 + mCurNodeSig.iDetContainer;
280  return kTRUE;
281  } else if ((mCurNodeSig.iSemi != -1) && (mCurNodeSig.iDetGasPart == -1)) {
282  // Semi
283  mCurNodeUID = 10000 + mCurNodeSig.iLayer * 100 + mCurNodeSig.iDetContainer;
285  return kTRUE;
286  } else {
288  return kFALSE;
289  }
290  }
292  return kFALSE;
293 }
294 
295 void ERN15B11Detector::StartNewPoint(void)
296 {
297  fELoss = 0.;
298  fEventID = gMC->CurrentEvent();
299  gMC->TrackPosition(fPosIn);
300  gMC->TrackMomentum(fMomIn);
301  fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
302  fTimeIn = gMC->TrackTime() * 1.0e09; //current time of flight of the track being transported
303  fTrackLength = gMC->TrackLength(); //length of the current track from its origin (in cm)
304  fMot0TrackID = gMC->GetStack()->GetCurrentTrack()->GetMother(0);
305  fMass = gMC->ParticleMass(gMC->TrackPid()); // GeV/c2
306  Int_t curVolId, corOffVolId;
307  curVolId = gMC->CurrentVolID(fVolNb);
308  TGeoHMatrix matrix;
309  gMC->GetTransformation(gMC->CurrentVolPath(), matrix);
310  Double_t globalPos[3], localPos[3];
311  fPosIn.Vect().GetXYZ(globalPos);
312  matrix.MasterToLocal(globalPos, localPos);
313  fPosInLocal.SetXYZ(localPos[0], localPos[1], localPos[2]);
314 }
315 
316 void ERN15B11Detector::FinishNewPoint(TClonesArray* p_collection)
317 {
318  gMC->TrackPosition(fPosOut);
319  gMC->TrackMomentum(fMomOut);
320  fTimeOut = gMC->TrackTime() * 1.0e09;
321 
322  if (fELoss > 0.) {
323  AddPoint(p_collection);
324  }
325 }
326 
327 ERPoint* ERN15B11Detector::AddPoint(TClonesArray* p_collection)
328 {
329  if (p_collection == NULL) {
330  LOG(WARNING) << "Trying to add an output point into a NULL collection." << FairLogger::endl;
331  return NULL;
332  }
333 
334  TClonesArray& clref = *p_collection;
335  Int_t size = clref.GetEntriesFast();
336  return new(clref[size]) ERPoint(fEventID, fTrackID, fMot0TrackID, fVolNb,
337  fMass, fPosIn.Vect(),fPosInLocal,fPosOut.Vect(),fMomIn.Vect(),fMomOut.Vect(),fTimeIn,
338  fTimeOut, fTrackLength, fELoss, 0., gMC->TrackPid(), gMC->TrackCharge());
339 }
340 
341 ClassImp(ERN15B11Detector)
Bool_t GetCurNodeSig(const char *nodePath) const
The data class for storing pieces of charged tracks in sensitive volumes in NeuRad.
Definition: ERPoint.h:23