MpdDch.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //------------------------------------------------------------------------------------------------------------------------
  2. // -------------------------------------------------------------------------
  3. // ----- MpdDch source file -----
  4. // -------------------------------------------------------------------------
  5. #include <iostream>
  6. #include "TClonesArray.h"
  7. #include "TLorentzVector.h"
  8. #include "TParticle.h"
  9. #include "TVirtualMC.h"
  10. #include "TGeoManager.h"
  11. #include "TGeoMatrix.h"
  12. #include "FairGeoInterface.h"
  13. #include "FairGeoLoader.h"
  14. #include "FairGeoNode.h"
  15. #include "FairGeoRootBuilder.h"
  16. #include "MpdDchGeo.h"
  17. #include "FairRootManager.h"
  18. #include "MpdDch.h"
  19. #include "MpdDchPoint.h"
  20. #include "FairRuntimeDb.h"
  21. #include "MpdDchGeoPar.h"
  22. #include "TObjArray.h"
  23. #include "FairRun.h"
  24. #include "FairVolume.h"
  25. #include "TMath.h"
  26. #include "TParticlePDG.h"
  27. class FairVolume;
  28. //------------------------------------------------------------------------------------------------------------------------
  29. MpdDch::MpdDch()
  30. : FairDetector("Dch", kTRUE)
  31. {
  32. fPointCollection = new TClonesArray("MpdDchPoint");
  33. fPosIndex = 0;
  34. fVerboseLevel = 1;
  35. ResetParameters();
  36. }
  37. //------------------------------------------------------------------------------------------------------------------------
  38. MpdDch::MpdDch(const char* name, Bool_t active)
  39. : FairDetector(name, active)
  40. {
  41. fPointCollection = new TClonesArray("MpdDchPoint");
  42. fPosIndex = 0;
  43. fVerboseLevel = 1;
  44. ResetParameters();
  45. }
  46. //------------------------------------------------------------------------------------------------------------------------
  47. MpdDch::~MpdDch()
  48. {
  49. if(fPointCollection){fPointCollection->Delete(); delete fPointCollection; }
  50. }
  51. //------------------------------------------------------------------------------------------------------------------------
  52. int MpdDch::DistAndPoints(TVector3 p3, TVector3 p4, TVector3& pa, TVector3& pb) {
  53. pa=(p3+p4)*0.5;
  54. pb=pa;
  55. //pa=p3; //del
  56. //pb=pa; //del
  57. return 0;
  58. }
  59. //------------------------------------------------------------------------------------------------------------------------
  60. TVector3 MpdDch::GlobalToLocal(TVector3& global) {
  61. Double_t globPos[3];
  62. Double_t localPos[3];
  63. global.GetXYZ(globPos);
  64. gMC->Gmtod(globPos, localPos, 1);
  65. return TVector3(localPos);
  66. }
  67. //------------------------------------------------------------------------------------------------------------------------
  68. TVector3 MpdDch::LocalToGlobal(TVector3& local) {
  69. Double_t globPos[3];
  70. Double_t localPos[3];
  71. local.GetXYZ(localPos);
  72. gMC->Gdtom(localPos, globPos, 1);
  73. return TVector3(globPos);
  74. }
  75. //----------------------------------------------------------------------------------------------------------------------
  76. Bool_t MpdDch::ProcessHits(FairVolume* vol)
  77. {
  78. static TString proj = "xyuv";
  79. Int_t gap, cell, module, region, wheel, gasgap;
  80. // Set parameters at entrance of volume. Reset ELoss.
  81. if (gMC->IsTrackEntering()) {
  82. TString Volname = vol->getRealName(); // EL
  83. sscanf(&(Volname[4]),"%d",&wheel); // chamber
  84. //cout << "===============-----------------Volname="<< Volname << " " << wheel << endl;
  85. //cout<<"************-- "<<gMC->CurrentVolPath()<<endl;
  86. TString path = gMC->CurrentVolPath();
  87. Int_t pos = path.Last('_');
  88. Int_t iproj = proj.First(path[pos-1]); // projection
  89. gMC->CurrentVolOffID(0, gasgap); // plane 1 or 2
  90. //cout << iproj << " " << gasgap << endl;
  91. ResetParameters();
  92. fELoss = 0.;
  93. fTime = gMC->TrackTime() * 1.0e09;
  94. fLength = gMC->TrackLength();
  95. fIsPrimary = 0;
  96. fCharge = -1;
  97. fPdgId = 0;
  98. TLorentzVector PosIn;
  99. gMC->TrackPosition(PosIn);
  100. fPosIn.SetXYZ(PosIn.X(), PosIn.Y(), PosIn.Z());
  101. gMC->TrackMomentum(fMom);
  102. TParticle* part = 0;
  103. part = gMC->GetStack()->GetCurrentTrack();
  104. if (part) {
  105. fIsPrimary = (Int_t)part->IsPrimary();
  106. fCharge = (Int_t)part->GetPDG()->Charge();
  107. fPdgId = (Int_t)part->GetPdgCode();
  108. }
  109. fVolumeID = (wheel - 1) * 8 + iproj * 2 + gasgap;
  110. fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
  111. }
  112. // Sum energy loss for all steps in the active volume
  113. fELoss += gMC->Edep();
  114. // Create MpdDchPoint at EXIT of active volume;
  115. if ((gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()) && fELoss > 0) {
  116. TLorentzVector PosOut;
  117. gMC->TrackPosition(PosOut);
  118. fPosOut.SetXYZ(PosOut.X(), PosOut.Y(), PosOut.Z());
  119. // Line defined in local coordinates
  120. TVector3 p1(0, 0, 0); // 10 - arbitrary number...
  121. TVector3 p2(10,0, 0);
  122. // Conversion to global coordinates
  123. p1 = LocalToGlobal(p1);
  124. p2 = LocalToGlobal(p2);
  125. Double_t phi = TMath::ATan2 (p2.Y()-p1.Y(),p2.X()-p1.X());
  126. // "will-be-filled-out-soon" Points of closest approach
  127. TVector3 trackPosition(0,0,0); // trackPosition => point on track, fPos => point on straw
  128. // calculate points of closest approach between track and straw
  129. //int result =
  130. DistAndPoints(fPosIn, fPosOut, fPos, trackPosition);
  131. MpdDchPoint *p = AddHit(fTrackID, fVolumeID, fPos, fPos.Perp(),
  132. TVector3(fMom.Px(), fMom.Py(), fMom.Pz()),
  133. fTime, (fLength+gMC->TrackLength())/2, fELoss,
  134. fIsPrimary, fCharge, fPdgId, trackPosition);
  135. p->SetPhi(phi); //AZ
  136. //==((FairStack*)gMC->GetStack())->AddPoint(kECT); //==
  137. }
  138. return kTRUE;
  139. }
  140. //------------------------------------------------------------------------------------------------------------------------
  141. void MpdDch::EndOfEvent()
  142. {
  143. if(fVerboseLevel) Print();
  144. fPointCollection->Clear();
  145. fPosIndex = 0;
  146. }
  147. //------------------------------------------------------------------------------------------------------------------------
  148. void MpdDch::Register(){ FairRootManager::Instance()->Register("DchPoint", "Dch", fPointCollection, kTRUE); }
  149. //------------------------------------------------------------------------------------------------------------------------
  150. TClonesArray* MpdDch::GetCollection(Int_t iColl) const
  151. {
  152. if(iColl == 0) return fPointCollection;
  153. return NULL;
  154. }
  155. //------------------------------------------------------------------------------------------------------------------------
  156. void MpdDch::Print() const
  157. {
  158. Int_t nHits = fPointCollection->GetEntriesFast();
  159. cout << "-I- MpdDch: " << nHits << " points registered in this event." << endl;
  160. if(fVerboseLevel > 1)
  161. for(Int_t i=0; i<nHits; i++) (*fPointCollection)[i]->Print();
  162. }
  163. //------------------------------------------------------------------------------------------------------------------------
  164. void MpdDch::Reset(){ fPointCollection->Clear(); ResetParameters(); }
  165. //------------------------------------------------------------------------------------------------------------------------
  166. void MpdDch::CopyClones(TClonesArray* cl1, TClonesArray* cl2, Int_t offset)
  167. {
  168. Int_t nEntries = cl1->GetEntriesFast();
  169. cout << "-I- MpdDch: " << nEntries << " entries to add." << endl;
  170. TClonesArray& clref = *cl2;
  171. MpdDchPoint* oldpoint = NULL;
  172. for(Int_t i=0; i<nEntries; i++)
  173. {
  174. oldpoint = (MpdDchPoint*) cl1->At(i);
  175. Int_t index = oldpoint->GetTrackID() + offset;
  176. oldpoint->SetTrackID(index);
  177. new (clref[fPosIndex]) MpdDchPoint(*oldpoint);
  178. fPosIndex++;
  179. }
  180. cout << "-I- MpdDch: " << cl2->GetEntriesFast() << " merged entries." << endl;
  181. }
  182. //------------------------------------------------------------------------------------------------------------------------
  183. void MpdDch::ConstructGeometry()
  184. {
  185. Int_t count=0;
  186. Int_t count_tot=0;
  187. FairGeoLoader* geoLoad = FairGeoLoader::Instance();
  188. FairGeoInterface* geoFace = geoLoad->getGeoInterface();
  189. MpdDchGeo* geo = new MpdDchGeo();
  190. geo->setGeomFile(GetGeometryFileName());
  191. geoFace->addGeoModule(geo);
  192. Bool_t rc = geoFace->readSet(geo);
  193. if(rc) geo->create(geoLoad->getGeoBuilder());
  194. TList* volList = geo->getListOfVolumes();
  195. volList->Print();
  196. // store geo parameter
  197. FairRun *fRun = FairRun::Instance();
  198. FairRuntimeDb *rtdb = FairRun::Instance()->GetRuntimeDb();
  199. MpdDchGeoPar* par =(MpdDchGeoPar*)(rtdb->getContainer("MpdDchGeoPar"));
  200. TObjArray *fSensNodes = par->GetGeoSensitiveNodes();
  201. TObjArray *fPassNodes = par->GetGeoPassiveNodes();
  202. FairGeoNode *node = NULL;
  203. FairGeoVolume *aVol = NULL;
  204. TListIter iter(volList);
  205. while((node = (FairGeoNode*)iter.Next()))
  206. {
  207. aVol = dynamic_cast<FairGeoVolume*> (node);
  208. if(node->isSensitive()){ fSensNodes->AddLast(aVol); count++; }
  209. else fPassNodes->AddLast(aVol);
  210. count_tot++;
  211. }
  212. par->setChanged();
  213. par->setInputVersion(fRun->GetRunId(), 1);
  214. ProcessNodes(volList);
  215. }
  216. //------------------------------------------------------------------------------------------------------------------------
  217. MpdDchPoint* MpdDch::AddHit(Int_t trackID, Int_t detID, TVector3 pos, Double_t radius,
  218. TVector3 mom, Double_t time, Double_t length,
  219. Double_t eLoss, Int_t isPrimary, Double_t charge, Int_t pdgId, TVector3 trackPos)
  220. {
  221. TClonesArray& clref = *fPointCollection;
  222. Int_t size = clref.GetEntriesFast();
  223. //std::cout << "ELoss: " << eLoss << "\n";
  224. return new(clref[size]) MpdDchPoint(trackID, detID, pos, radius, mom, time, length, eLoss, isPrimary, charge, pdgId, trackPos);
  225. }
  226. //------------------------------------------------------------------------------------------------------------------------
  227. ClassImp(MpdDch)