PicoDstConverter.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <iostream>
  2. #include <set>
  3. #include <TROOT.h>
  4. #include <TChain.h>
  5. #include <TH1.h>
  6. #include <TF1.h>
  7. #include <TFile.h>
  8. #include <TTree.h>
  9. #include <TRandom1.h>
  10. #include <TClonesArray.h>
  11. #include <TFile.h>
  12. #include <TTree.h>
  13. #include <TString.h>
  14. #include <TMath.h>
  15. #include <TSystem.h>
  16. #include <TVector3.h>
  17. #include <TStopwatch.h>
  18. #include "FairMCEventHeader.h"
  19. #include "FairMCTrack.h"
  20. #include "MpdEvent.h"
  21. #include "MpdZdcDigi.h"
  22. #include "MpdPid.h"
  23. #include "MpdTrack.h"
  24. #include "MpdKalmanTrack.h"
  25. #include "MpdVertex.h"
  26. #include "PicoDstMCEvent.h"
  27. #include "PicoDstRecoEvent.h"
  28. #include "PicoDstMCTrack.h"
  29. #include "PicoDstRecoTrack.h"
  30. #include "PicoDstFHCal.h"
  31. int main(int argc, char **argv)
  32. {
  33. TString iFileName, oFileName;
  34. if (argc < 5)
  35. {
  36. std::cerr << "./reducedTreeCreator -i inputfile -o outputfile" << std::endl;
  37. return 1;
  38. }
  39. for (int i = 1; i < argc; i++)
  40. {
  41. if (std::string(argv[i]) != "-i" &&
  42. std::string(argv[i]) != "-o")
  43. {
  44. std::cerr << "\n[ERROR]: Unknown parameter " << i << ": " << argv[i] << std::endl;
  45. return 1;
  46. }
  47. else
  48. {
  49. if (std::string(argv[i]) == "-i" && i != argc - 1)
  50. {
  51. iFileName = argv[++i];
  52. continue;
  53. }
  54. if (std::string(argv[i]) == "-i" && i == argc - 1)
  55. {
  56. std::cerr << "\n[ERROR]: Input file name was not specified " << std::endl;
  57. return 1;
  58. }
  59. if (std::string(argv[i]) == "-o" && i != argc - 1)
  60. {
  61. oFileName = argv[++i];
  62. continue;
  63. }
  64. if (std::string(argv[i]) == "-o" && i == argc - 1)
  65. {
  66. std::cerr << "\n[ERROR]: Output file name was not specified " << std::endl;
  67. return 1;
  68. }
  69. }
  70. }
  71. TStopwatch timer;
  72. timer.Start();
  73. ///////////////////////////////////////////
  74. //TChain *dstTree;
  75. TFile *fi = new TFile(iFileName.Data(),"read");
  76. TTree *dstTree = (TTree*) fi->Get("mpdsim");
  77. TFile *outFile = new TFile(oFileName.Data(), "RECREATE");
  78. // TTree connfiguration
  79. TTree *outTree = new TTree("picodst","Pico Dst for flow calculations at MPD");
  80. const Double_t PIDsigM = 4.0;
  81. const Double_t PIDsigE = 4.0;
  82. const Double_t PIDenergy = 11.;
  83. const Double_t PIDkoeff = 1.;
  84. const TString PIDgenerator = "URQMD";
  85. const TString PIDtracking = "CF";
  86. const TString PIDparticles = "pikapr";
  87. const Int_t Num_Of_Modules = 90;
  88. MpdPid *pid = new MpdPid(PIDsigM, PIDsigE, PIDenergy, PIDkoeff, PIDgenerator, PIDtracking, PIDparticles);
  89. // PicoDst-related variables
  90. PicoDstMCEvent *mcEvent = new PicoDstMCEvent();
  91. PicoDstRecoEvent *recoEvent = new PicoDstRecoEvent();
  92. TClonesArray *mcTracks = new TClonesArray("PicoDstMCTrack");
  93. TClonesArray *recoTracks = new TClonesArray("PicoDstRecoTrack");
  94. TClonesArray *fhcalModules = new TClonesArray("PicoDstFHCal");
  95. outTree->Branch("mcevent.", &mcEvent, 32000, 99);
  96. outTree->Branch("recoevent.", &recoEvent, 32000, 99);
  97. outTree->Branch("mctracks", &mcTracks, 256000, 99);
  98. outTree->Branch("recotracks", &recoTracks, 256000, 99);
  99. outTree->Branch("FHCalModules", &fhcalModules, 128000, 99);
  100. mcTracks->BypassStreamer();
  101. recoTracks->BypassStreamer();
  102. fhcalModules->BypassStreamer();
  103. FairMCEventHeader *MCHeader;
  104. TClonesArray *MCTracks;
  105. MpdEvent *MPDEvent;
  106. TClonesArray *FHCalHits;
  107. TClonesArray *MpdGlobalTracks;
  108. MpdZdcDigi *FHCalHit;
  109. //TClonesArray *mpdKalmanTracks;
  110. TClonesArray *vertexes;
  111. MCHeader = nullptr;
  112. MCTracks = nullptr;
  113. MPDEvent = nullptr;
  114. FHCalHits = nullptr;
  115. //mpdKalmanTracks = nullptr;
  116. vertexes = nullptr;
  117. dstTree->SetBranchAddress("MCEventHeader.", &MCHeader);
  118. dstTree->SetBranchAddress("MCTrack", &MCTracks);
  119. dstTree->SetBranchAddress("MPDEvent.", &MPDEvent);
  120. dstTree->SetBranchAddress("ZdcDigi", &FHCalHits);
  121. //dstTree->SetBranchAddress("TpcKalmanTrack", &mpdKalmanTracks);
  122. dstTree->SetBranchAddress("Vertex", &vertexes);
  123. // Starting event loop
  124. TVector3 primaryVertex;
  125. //std::set <Int_t> UsedMCTracks;
  126. std::map <Int_t,Int_t> InitMcRecoId; // map[init-mc-id] = reco-id
  127. std::map <Int_t,Int_t> InitMcNewMcId; // map[old-mc-id] = new-mc-id
  128. Float_t FHCalSumEnergy[Num_Of_Modules];
  129. Int_t n_entries = dstTree->GetEntriesFast();
  130. Bool_t isGoodPID;
  131. Short_t charge_mpd;
  132. for (int iEv = 0; iEv < n_entries; iEv++)
  133. {
  134. std::cout << "EVENT N " << iEv << std::endl;
  135. dstTree->GetEntry(iEv);
  136. mcTracks->Clear();
  137. recoTracks->Clear();
  138. InitMcRecoId.clear();
  139. InitMcNewMcId.clear();
  140. for (int i=0; i<Num_Of_Modules; i++)
  141. {
  142. FHCalSumEnergy[i] = 0.;
  143. }
  144. // Read MC Event
  145. mcEvent->SetB(MCHeader->GetB());
  146. mcEvent->SetPhiRP(MCHeader->GetRotZ());
  147. mcEvent->SetVertex(MCHeader->GetX(), MCHeader->GetY(), MCHeader->GetZ());
  148. // Reading Reco Event
  149. MpdVertex *vertex = (MpdVertex *)vertexes->First();
  150. vertex->Position(primaryVertex);
  151. recoEvent->SetVertex(primaryVertex.X(), primaryVertex.Y(), primaryVertex.Z());
  152. // Read energy reconstructed in the FHCal modules
  153. Int_t number_of_FHCal_hits = FHCalHits->GetEntriesFast();
  154. for(int ihit=0; ihit<number_of_FHCal_hits; ihit++)
  155. {
  156. FHCalHit = (MpdZdcDigi*) FHCalHits->UncheckedAt(ihit);
  157. Int_t DetId = FHCalHit->GetDetectorID();
  158. FHCalSumEnergy[(FHCalHit->GetModuleID()-1)+(Num_Of_Modules/2)*(DetId-1)] += FHCalHit->GetELoss();
  159. }
  160. // Fill fDetectorID = 1 part
  161. for (int imodule=0; imodule<Num_Of_Modules; imodule++)
  162. {
  163. PicoDstFHCal *fhcalModule = (PicoDstFHCal*)fhcalModules->ConstructedAt(imodule);
  164. fhcalModule->SetEnergy(FHCalSumEnergy[imodule]);
  165. }
  166. // Reading Reco Tracks
  167. MpdGlobalTracks = (TClonesArray*)MPDEvent->GetGlobalTracks();
  168. for (int itrack=0; itrack<MpdGlobalTracks->GetEntriesFast(); itrack++)
  169. {
  170. MpdTrack* mpdtrack = (MpdTrack*) MpdGlobalTracks->UncheckedAt(itrack);
  171. InitMcRecoId[mpdtrack->GetID()] = itrack;
  172. PicoDstRecoTrack *recoTrack = (PicoDstRecoTrack*)recoTracks->ConstructedAt(itrack);
  173. recoTrack->SetInitialMcId(mpdtrack->GetID());
  174. recoTrack->SetPx(mpdtrack->GetPx());
  175. recoTrack->SetPy(mpdtrack->GetPy());
  176. recoTrack->SetPz(mpdtrack->GetPz());
  177. recoTrack->SetNhits(mpdtrack->GetNofHits());
  178. recoTrack->SetNhitsPoss(mpdtrack->GetNofHitsPossTpc());
  179. recoTrack->SetTofMass2(mpdtrack->GetTofMass2());
  180. recoTrack->SetTofFlag(mpdtrack->GetTofFlag());
  181. recoTrack->SetTpcdEdx(mpdtrack->GetdEdXTPC());
  182. recoTrack->SetChi2(mpdtrack->GetChi2());
  183. recoTrack->SetDCAx(mpdtrack->GetDCAX());
  184. recoTrack->SetDCAy(mpdtrack->GetDCAY());
  185. recoTrack->SetDCAz(mpdtrack->GetDCAZ());
  186. charge_mpd = (mpdtrack->GetPt() < 0) ? 1 : -1;
  187. recoTrack->SetCharge(charge_mpd);
  188. // PID
  189. if (mpdtrack->GetTofFlag() == 2 || mpdtrack->GetTofFlag() == 6)
  190. {
  191. isGoodPID = pid->FillProbs(TMath::Abs(mpdtrack->GetPt()) * TMath::CosH(mpdtrack->GetEta()),
  192. mpdtrack->GetdEdXTPC(), mpdtrack->GetTofMass2(), charge_mpd);
  193. }
  194. else
  195. {
  196. isGoodPID = pid->FillProbs(TMath::Abs(mpdtrack->GetPt()) * TMath::CosH(mpdtrack->GetEta()),
  197. mpdtrack->GetdEdXTPC(), charge_mpd);
  198. }
  199. if (isGoodPID)
  200. {
  201. recoTrack->SetPidProbPion(pid->GetProbPi()); //mpdtrack->GetTPCPidProbPion();
  202. recoTrack->SetPidProbKaon(pid->GetProbKa()); //mpdtrack->GetTPCPidProbKaon();
  203. recoTrack->SetPidProbProton(pid->GetProbPr()); //mpdtrack->GetTPCPidProbProton();
  204. }
  205. else
  206. {
  207. recoTrack->SetPidProbPion(-999.);
  208. recoTrack->SetPidProbKaon(-999.);
  209. recoTrack->SetPidProbProton(-999.);
  210. }
  211. }
  212. // Reading MC Tracks
  213. int mcTrackCounter = 0;
  214. for (int imctrack=0; imctrack<MCTracks->GetEntriesFast(); imctrack++)
  215. {
  216. FairMCTrack *mctrack = (FairMCTrack*) MCTracks->UncheckedAt(imctrack);
  217. bool isUsed = (InitMcRecoId.count(imctrack));
  218. // If motherId != 1 and mc track doesn't relate to any reco track - skip
  219. //if (mctrack->GetMotherId() != -1) continue;
  220. if (mctrack->GetMotherId() != -1 && !isUsed) continue;
  221. if (isUsed) //std::cout << "VVVVVVV" << std::endl;
  222. {
  223. InitMcNewMcId[imctrack] = mcTrackCounter;
  224. }
  225. PicoDstMCTrack *mcPicoTrack = (PicoDstMCTrack*)mcTracks->ConstructedAt(mcTrackCounter);
  226. mcPicoTrack->SetInitialId(imctrack);
  227. mcPicoTrack->SetPx(mctrack->GetPx());
  228. mcPicoTrack->SetPy(mctrack->GetPy());
  229. mcPicoTrack->SetPz(mctrack->GetPz());
  230. mcPicoTrack->SetMotherId(mctrack->GetMotherId());
  231. mcPicoTrack->SetPdg(mctrack->GetPdgCode());
  232. mcPicoTrack->SetEnergy(mctrack->GetEnergy());
  233. mcTrackCounter++;
  234. }
  235. for (int itrack=0; itrack<recoTracks->GetEntriesFast(); itrack++)
  236. {
  237. PicoDstRecoTrack *recoTrack = (PicoDstRecoTrack*) recoTracks->UncheckedAt(itrack);
  238. MpdTrack* mpdtrack = (MpdTrack*) MpdGlobalTracks->UncheckedAt(itrack);
  239. recoTrack->SetMcId(InitMcNewMcId[mpdtrack->GetID()]);
  240. }
  241. outTree->Fill();
  242. } // End of the Event loop
  243. outFile->cd();
  244. outTree->Print();
  245. outTree->Write();
  246. outFile->Close();
  247. timer.Stop();
  248. timer.Print();
  249. return 0;
  250. }