MpdCpc.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //------------------------------------------------------------------------------------------------------------------------
  2. // -------------------------------------------------------------------------
  3. // ----- MpdCpc source file -----
  4. // -------------------------------------------------------------------------
  5. #include "iostream"
  6. #include "TClonesArray.h"
  7. #include "TLorentzVector.h"
  8. #include "TParticle.h"
  9. #include "TVirtualMC.h"
  10. #include "MpdMCTrack.h"
  11. #include "FairGeoInterface.h"
  12. #include "FairGeoLoader.h"
  13. #include "FairGeoNode.h"
  14. #include "FairGeoRootBuilder.h"
  15. #include "FairRootManager.h"
  16. #include "MpdStack.h"
  17. #include "FairRuntimeDb.h"
  18. #include "TObjArray.h"
  19. #include "FairRun.h"
  20. #include "FairVolume.h"
  21. #include "MpdCpc.h"
  22. #include "MpdCpcGeo.h"
  23. #include "MpdCpcPoint.h"
  24. #include "MpdCpcGeoPar.h"
  25. class FairVolume;
  26. //------------------------------------------------------------------------------------------------------------------------
  27. MpdCpc::MpdCpc()
  28. : FairDetector("CPC", kTRUE)
  29. {
  30. fCpcCollection = new TClonesArray("MpdCpcPoint");
  31. fPosIndex = 0;
  32. fVerboseLevel = 1;
  33. }
  34. //------------------------------------------------------------------------------------------------------------------------
  35. MpdCpc::MpdCpc(const char* name, Bool_t active)
  36. : FairDetector(name, active)
  37. {
  38. fCpcCollection = new TClonesArray("MpdCpcPoint");
  39. fPosIndex = 0;
  40. fVerboseLevel = 1;
  41. }
  42. //------------------------------------------------------------------------------------------------------------------------
  43. MpdCpc::~MpdCpc()
  44. {
  45. if(fCpcCollection){ fCpcCollection->Delete(); delete fCpcCollection; }
  46. }
  47. //------------------------------------------------------------------------------------------------------------------------
  48. Bool_t MpdCpc::ProcessHits(FairVolume* vol)
  49. {
  50. //Int_t gap, cell, module, region;
  51. TString Volname;
  52. // Set parameters at entrance of volume. Reset ELoss.
  53. if(gMC->IsTrackEntering())
  54. {
  55. fELoss = 0.;
  56. fTime = gMC->TrackTime() * 1.0e09;
  57. fLength = gMC->TrackLength();
  58. gMC->TrackPosition(fPos);
  59. gMC->TrackMomentum(fMom);
  60. }
  61. // Sum energy loss for all steps in the active volume
  62. fELoss += gMC->Edep();
  63. // Create MpdCpcPoint at ENTER of active volume; fELoss INVALID!!!
  64. //AZ if(gMC->IsTrackEntering())
  65. // Create MpdCpcPoint at exit of active volume
  66. if ((gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()) && fELoss > 0)
  67. {
  68. fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
  69. //Volname = vol->getRealName(); // EL
  70. //region = Volname[5] - '0'; //?????????????????????????
  71. //gMC->CurrentVolID(gap);
  72. //gMC->CurrentVolOffID(1, cell);
  73. //gMC->CurrentVolOffID(2, module);
  74. //fVolumeID = ((region-1)<<24);////////////// + ((module-1)<<14) + ((cell-1)<<4) + (gap-1);
  75. fVolumeID = vol->getMCid();
  76. AddHit(fTrackID, fVolumeID, TVector3(fPos.X(), fPos.Y(), fPos.Z()),
  77. TVector3(fMom.Px(), fMom.Py(), fMom.Pz()), fTime, fLength, fELoss);
  78. ((MpdStack*)gMC->GetStack())->AddPoint(kCPC);
  79. ResetParameters();
  80. }
  81. return kTRUE;
  82. }
  83. //------------------------------------------------------------------------------------------------------------------------
  84. void MpdCpc::EndOfEvent()
  85. {
  86. if(fVerboseLevel) Print();
  87. fCpcCollection->Delete();
  88. fPosIndex = 0;
  89. }
  90. //------------------------------------------------------------------------------------------------------------------------
  91. void MpdCpc::Register(){ FairRootManager::Instance()->Register("CPCPoint", "Cpc", fCpcCollection, kTRUE); }
  92. //------------------------------------------------------------------------------------------------------------------------
  93. TClonesArray* MpdCpc::GetCollection(Int_t iColl) const
  94. {
  95. if(iColl == 0) return fCpcCollection;
  96. return NULL;
  97. }
  98. //------------------------------------------------------------------------------------------------------------------------
  99. void MpdCpc::Print() const
  100. {
  101. Int_t nHits = fCpcCollection->GetEntriesFast();
  102. cout << "-I- MpdCpc: " << nHits << " points registered in this event." << endl;
  103. if(fVerboseLevel > 1)
  104. for(Int_t i=0; i<nHits; i++) (*fCpcCollection)[i]->Print();
  105. }
  106. //------------------------------------------------------------------------------------------------------------------------
  107. void MpdCpc::Reset(){ fCpcCollection->Delete(); ResetParameters(); }
  108. //------------------------------------------------------------------------------------------------------------------------
  109. void MpdCpc::CopyClones(TClonesArray* cl1, TClonesArray* cl2, Int_t offset)
  110. {
  111. Int_t nEntries = cl1->GetEntriesFast();
  112. cout << "-I- MpdCpc: " << nEntries << " entries to add." << endl;
  113. TClonesArray& clref = *cl2;
  114. MpdCpcPoint* oldpoint = NULL;
  115. for(Int_t i=0; i<nEntries; i++)
  116. {
  117. oldpoint = (MpdCpcPoint*) cl1->At(i);
  118. Int_t index = oldpoint->GetTrackID() + offset;
  119. oldpoint->SetTrackID(index);
  120. new (clref[fPosIndex]) MpdCpcPoint(*oldpoint);
  121. fPosIndex++;
  122. }
  123. cout << "-I- MpdCpc: " << cl2->GetEntriesFast() << " merged entries." << endl;
  124. }
  125. //------------------------------------------------------------------------------------------------------------------------
  126. void MpdCpc::ConstructGeometry()
  127. {
  128. TString fileName = GetGeometryFileName();
  129. if ( fileName.EndsWith(".root") ) {
  130. LOG(INFO) << "Constructing CPC geometry from ROOT file " << fileName.Data() << endl;
  131. ConstructRootGeometry();
  132. }
  133. else if ( fileName.EndsWith(".geo") ) {
  134. LOG(INFO) << "Constructing CPC geometry from ASCII file " << fileName.Data() << endl;
  135. ConstructAsciiGeometry();
  136. }
  137. /*else if ( fileName.EndsWith(".gdml") )
  138. {
  139. LOG(INFO) << "Constructing CPC geometry from GDML file " << fileName.Data() << endl;
  140. ConstructGDMLGeometry();
  141. }*/
  142. else
  143. {
  144. LOG(FATAL) << "Geometry format of CPC file " << fileName.Data() << " not supported." << endl;
  145. }
  146. }
  147. //------------------------------------------------------------------------------------------------------------------------
  148. void MpdCpc::ConstructAsciiGeometry()
  149. {
  150. int count=0;
  151. int count_tot=0;
  152. FairGeoLoader* geoLoad = FairGeoLoader::Instance();
  153. FairGeoInterface* geoFace = geoLoad->getGeoInterface();
  154. MpdCpcGeo* Geo = new MpdCpcGeo();
  155. Geo->setGeomFile(GetGeometryFileName());
  156. geoFace->addGeoModule(Geo);
  157. Bool_t rc = geoFace->readSet(Geo);
  158. if(rc) Geo->create(geoLoad->getGeoBuilder());
  159. else std::cerr<<"CpcDetector:: geometry could not be read!"<<std::endl;
  160. TList* volList = Geo->getListOfVolumes();
  161. // store geo parameter
  162. FairRun *fRun = FairRun::Instance();
  163. FairRuntimeDb *rtdb = FairRun::Instance()->GetRuntimeDb();
  164. MpdCpcGeoPar* par =(MpdCpcGeoPar*)(rtdb->getContainer("MpdCpcGeoPar"));
  165. TObjArray *fSensNodes = par->GetGeoSensitiveNodes();
  166. TObjArray *fPassNodes = par->GetGeoPassiveNodes();
  167. TListIter iter(volList);
  168. FairGeoNode *node = NULL;
  169. FairGeoVolume *aVol = NULL;
  170. while((node = (FairGeoNode*)iter.Next()))
  171. {
  172. aVol = dynamic_cast<FairGeoVolume*> (node);
  173. if(node->isSensitive()){ fSensNodes->AddLast(aVol); count++; }
  174. else fPassNodes->AddLast(aVol);
  175. count_tot++;
  176. }
  177. par->setChanged();
  178. par->setInputVersion(fRun->GetRunId(), 1);
  179. ProcessNodes(volList);
  180. }
  181. //------------------------------------------------------------------------------------------------------------------------
  182. MpdCpcPoint* MpdCpc::AddHit(Int_t trackID, Int_t detID, TVector3 pos,
  183. TVector3 mom, Double_t time, Double_t length,
  184. Double_t eLoss)
  185. {
  186. TClonesArray& clref = *fCpcCollection;
  187. Int_t size = clref.GetEntriesFast();
  188. return new(clref[size]) MpdCpcPoint(trackID, detID, pos, mom, time, length, eLoss);
  189. }
  190. //------------------------------------------------------------------------------------------------------------------------
  191. //Check if Sensitive-----------------------------------------------------------
  192. Bool_t MpdCpc::CheckIfSensitive(std::string name) {
  193. TString tsname = name;
  194. if (tsname.Contains("Active") || tsname.Contains("cpc01al")) {
  195. return kTRUE;
  196. }
  197. return kFALSE;
  198. }
  199. //------------------------------------------------------------------------------------------------------------------------
  200. ClassImp(MpdCpc)