MpdDstAnalysisTree.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // Standard c++ headers
  2. #include <iostream>
  3. #include <string>
  4. #include <utility>
  5. #include <set>
  6. #include <map>
  7. #include <functional>
  8. // Standard ROOT headers
  9. #include <Rtypes.h>
  10. #include <TFile.h>
  11. #include <TTree.h>
  12. #include <TString.h>
  13. #include <TStopwatch.h>
  14. #include <TClonesArray.h>
  15. #include <TObject.h>
  16. #include <TMath.h>
  17. #include <TDatabasePDG.h>
  18. #include <TParticlePDG.h>
  19. // FairRoot/MpdRoot headers
  20. #include "FairMCEventHeader.h"
  21. #ifdef _MCSTACK_
  22. #include "FairMCTrack.h"
  23. #endif
  24. #ifdef _MPDMCSTACK_
  25. #include "MpdMCTrack.h"
  26. #endif
  27. #include "MpdEvent.h"
  28. #include "MpdZdcDigi.h"
  29. #include "MpdPid.h"
  30. #include "MpdTrack.h"
  31. #include "MpdKalmanTrack.h"
  32. #include "MpdVertex.h"
  33. // AnalysisTree headers
  34. #include "AnalysisTree/Configuration.hpp"
  35. #include "AnalysisTree/DataHeader.hpp"
  36. #include "AnalysisTree/EventHeader.hpp"
  37. #include "AnalysisTree/Detector.hpp"
  38. #include "AnalysisTree/Matching.hpp"
  39. float get_beamP(float sqrtSnn, float m_target = 0.938, float m_beam = 0.938);
  40. float GetFHCalPhi(int iModule);
  41. TVector3 GetFHCalPos(int iModule);
  42. Bool_t IsCharged(Int_t pdg);
  43. Float_t GetRapidity(Float_t p, Float_t pz, Int_t pid_type);
  44. Float_t GetRapidityPDG(Float_t p, Float_t pz, Int_t pdg);
  45. int main(int argc, char **argv)
  46. {
  47. TString iFileName, oFileName;
  48. std::string system="";
  49. float sqrtSnn = -1.;
  50. bool isDataHeaderDefined = false;
  51. if (argc < 5)
  52. {
  53. std::cerr << "./MpdDst2AnalysisTree -i inputfile -o outputfile [OPTIONAL: --sqrtSnn sqrtSnn --system colliding_system_name]" << std::endl;
  54. return 1;
  55. }
  56. for (int i = 1; i < argc; i++)
  57. {
  58. if (std::string(argv[i]) != "-i" &&
  59. std::string(argv[i]) != "-o" &&
  60. std::string(argv[i]) != "--sqrtSnn" &&
  61. std::string(argv[i]) != "--system")
  62. {
  63. std::cerr << "\n[ERROR]: Unknown parameter " << i << ": " << argv[i] << std::endl;
  64. return 1;
  65. }
  66. else
  67. {
  68. if (std::string(argv[i]) == "-i" && i != argc - 1)
  69. {
  70. iFileName = argv[++i];
  71. continue;
  72. }
  73. if (std::string(argv[i]) == "-i" && i == argc - 1)
  74. {
  75. std::cerr << "\n[ERROR]: Input file name was not specified " << std::endl;
  76. return 1;
  77. }
  78. if (std::string(argv[i]) == "-o" && i != argc - 1)
  79. {
  80. oFileName = argv[++i];
  81. continue;
  82. }
  83. if (std::string(argv[i]) == "-o" && i == argc - 1)
  84. {
  85. std::cerr << "\n[ERROR]: Output file name was not specified " << std::endl;
  86. return 1;
  87. }
  88. if (std::string(argv[i]) == "--system" && i != argc - 1)
  89. {
  90. system = std::string(argv[++i]);
  91. isDataHeaderDefined = true;
  92. continue;
  93. }
  94. if (std::string(argv[i]) == "--system" && i == argc - 1)
  95. {
  96. std::cerr << "\n[ERROR]: Output file name was not specified " << std::endl;
  97. return 1;
  98. }
  99. if (std::string(argv[i]) == "--sqrtSnn" && i != argc - 1)
  100. {
  101. sqrtSnn = std::stof(std::string(argv[++i]));
  102. isDataHeaderDefined = true;
  103. continue;
  104. }
  105. if (std::string(argv[i]) == "--sqrtSnn" && i == argc - 1)
  106. {
  107. std::cerr << "\n[ERROR]: Output file name was not specified " << std::endl;
  108. return 1;
  109. }
  110. }
  111. }
  112. TStopwatch timer;
  113. timer.Start();
  114. // Open input dst tree
  115. TFile *fi = new TFile(iFileName.Data(),"read");
  116. if (!fi || fi->IsZombie())
  117. {
  118. std::cerr << "ERROR: Input file probably is empty. Exit the program." << std::endl;
  119. return 100;
  120. }
  121. TTree *dstTree = (TTree*) fi->Get("mpdsim");
  122. // PID related parameters
  123. const Double_t PIDsigM = 4.0;
  124. const Double_t PIDsigE = 4.0;
  125. const Double_t PIDenergy = 11.;
  126. const Double_t PIDkoeff = 1.;
  127. const TString PIDgenerator = "URQMD";
  128. const TString PIDtracking = "CF";
  129. const TString PIDparticles = "pikapr";
  130. const Int_t Num_Of_Modules = 90;
  131. MpdPid *pid = new MpdPid(PIDsigM, PIDsigE, PIDenergy, PIDkoeff, PIDgenerator, PIDtracking, PIDparticles);
  132. // Prepare to read input dst
  133. FairMCEventHeader *MCHeader;
  134. TClonesArray *MCTracks;
  135. MpdEvent *MPDEvent;
  136. TClonesArray *FHCalHits;
  137. TClonesArray *MpdGlobalTracks;
  138. MpdZdcDigi *FHCalHit;
  139. //TClonesArray *mpdKalmanTracks;
  140. TClonesArray *vertexes;
  141. MCHeader = nullptr;
  142. MCTracks = nullptr;
  143. MPDEvent = nullptr;
  144. FHCalHits = nullptr;
  145. //mpdKalmanTracks = nullptr;
  146. vertexes = nullptr;
  147. dstTree->SetBranchAddress("MCEventHeader.", &MCHeader);
  148. dstTree->SetBranchAddress("MCTrack", &MCTracks);
  149. dstTree->SetBranchAddress("MPDEvent.", &MPDEvent);
  150. dstTree->SetBranchAddress("ZdcDigi", &FHCalHits);
  151. //dstTree->SetBranchAddress("TpcKalmanTrack", &mpdKalmanTracks);
  152. dstTree->SetBranchAddress("Vertex", &vertexes);
  153. // Set up output dst
  154. TFile *outFile = new TFile(oFileName.Data(), "RECREATE");
  155. TTree *outTree = new TTree("aTree","AnalysisTree Dst at MPD");
  156. // Set up AnalysisTree data header
  157. AnalysisTree::DataHeader *dataHeader = new AnalysisTree::DataHeader;
  158. if (system != "") dataHeader->SetSystem(system);
  159. if (sqrtSnn > 0) dataHeader->SetBeamMomentum(get_beamP(sqrtSnn));
  160. auto &fhcal_mod_pos = dataHeader->AddDetector();
  161. TVector3 modulePos;
  162. for (int imodule=0; imodule<Num_Of_Modules; imodule++)
  163. {
  164. auto *module = fhcal_mod_pos.AddChannel();
  165. modulePos = GetFHCalPos(imodule);
  166. module->SetPosition(modulePos);
  167. }
  168. // Set up AnalysisTree configureation
  169. AnalysisTree::Configuration *out_config = new AnalysisTree::Configuration;
  170. // Set up AnalysisTree configuration
  171. std::string str_reco_event_branch = "RecoEvent";
  172. std::string str_mc_event_branch = "McEvent";
  173. std::string str_tpc_tracks_branch = "TpcTracks";
  174. std::string str_fhcal_branch = "FHCalModules";
  175. std::string str_mc_tracks_branch = "McTracks";
  176. std::string str_tpc2mc_tracks_branch = "TpcTracks2McTracks";
  177. AnalysisTree::BranchConfig reco_event_branch(str_reco_event_branch.c_str(), AnalysisTree::DetType::kEventHeader);
  178. AnalysisTree::BranchConfig mc_event_branch(str_mc_event_branch.c_str(), AnalysisTree::DetType::kEventHeader);
  179. AnalysisTree::BranchConfig fhcal_branch(str_fhcal_branch.c_str(), AnalysisTree::DetType::kModule);
  180. AnalysisTree::BranchConfig tpc_tracks_branch(str_tpc_tracks_branch.c_str(), AnalysisTree::DetType::kTrack);
  181. AnalysisTree::BranchConfig mc_tracks_branch(str_mc_tracks_branch.c_str(), AnalysisTree::DetType::kParticle);
  182. mc_event_branch.AddField<float>("B");
  183. mc_event_branch.AddField<float>("PhiRp");
  184. // mc_event_branch.AddField<float>("Centrality_B");
  185. // reco_event_branch.AddField<float>("refMult");
  186. // reco_event_branch.AddField<float>("Centrality_refMult");
  187. tpc_tracks_branch.AddField<int>("nhits");
  188. tpc_tracks_branch.AddField<int>("nhits_poss");
  189. tpc_tracks_branch.AddField<int>("charge");
  190. tpc_tracks_branch.AddFields<float>({"dca_x","dca_y","dca_z"});
  191. tpc_tracks_branch.AddField<float>("chi2");
  192. tpc_tracks_branch.AddField<float>("tof_mass2");
  193. tpc_tracks_branch.AddField<float>("tof_flag");
  194. tpc_tracks_branch.AddField<float>("dedx");
  195. tpc_tracks_branch.AddField<float>("pid_prob_pion");
  196. tpc_tracks_branch.AddField<float>("pid_prob_kaon");
  197. tpc_tracks_branch.AddField<float>("pid_prob_proton");
  198. tpc_tracks_branch.AddField<float>("mc_E");
  199. tpc_tracks_branch.AddField<float>("mc_pT");
  200. tpc_tracks_branch.AddField<float>("mc_eta");
  201. tpc_tracks_branch.AddField<float>("mc_phi");
  202. tpc_tracks_branch.AddField<float>("mc_rapidity");
  203. tpc_tracks_branch.AddField<int>("mc_mother_id");
  204. tpc_tracks_branch.AddField<int>("mc_pdg");
  205. tpc_tracks_branch.AddField<int>("eta_sign");
  206. tpc_tracks_branch.AddField<float>("rapidity_pdg");
  207. tpc_tracks_branch.AddField<float>("rapidity_pion");
  208. tpc_tracks_branch.AddField<float>("rapidity_kaon");
  209. tpc_tracks_branch.AddField<float>("rapidity_proton");
  210. fhcal_branch.AddField<float>("phi");
  211. fhcal_branch.AddField<float>("signal_eta_signed");
  212. mc_tracks_branch.AddField<int>("mother_id");
  213. mc_tracks_branch.AddField<bool>("is_charged");
  214. mc_tracks_branch.AddField<int>("eta_sign");
  215. auto hasher = std::hash<std::string>();
  216. // Initialize AnalysisTree Dst components
  217. out_config->AddBranchConfig(reco_event_branch);
  218. AnalysisTree::EventHeader *reco_event = new AnalysisTree::EventHeader( Short_t(hasher(reco_event_branch.GetName())) );
  219. out_config->AddBranchConfig(mc_event_branch);
  220. AnalysisTree::EventHeader *mc_event = new AnalysisTree::EventHeader( Short_t(hasher(mc_event_branch.GetName())) );
  221. out_config->AddBranchConfig(tpc_tracks_branch);
  222. AnalysisTree::TrackDetector *tpc_tracks = new AnalysisTree::TrackDetector( Short_t(hasher(tpc_tracks_branch.GetName())) );
  223. out_config->AddBranchConfig(fhcal_branch);
  224. AnalysisTree::ModuleDetector *fhcal_modules = new AnalysisTree::ModuleDetector( Short_t(hasher(fhcal_branch.GetName())) );
  225. out_config->AddBranchConfig(mc_tracks_branch);
  226. AnalysisTree::Particles *mc_tracks = new AnalysisTree::Particles( Short_t(hasher(mc_tracks_branch.GetName())) );
  227. AnalysisTree::Matching *tpc2mc_tracks = new AnalysisTree::Matching(out_config->GetBranchConfig(str_tpc_tracks_branch).GetId(), out_config->GetBranchConfig(str_mc_tracks_branch).GetId());
  228. out_config->AddMatch(tpc2mc_tracks);
  229. reco_event->Init(reco_event_branch);
  230. mc_event->Init(mc_event_branch);
  231. /* Correct branch id-s inside Configuration */
  232. for (const auto& branch_config : out_config->GetBranchConfigs()) {
  233. out_config->GetBranchConfig(branch_config.GetName()).SetId(Short_t(hasher(branch_config.GetName())));
  234. }
  235. // mc_event's additional field ids
  236. const int mceventid = mc_event->GetId();
  237. const int iB = out_config->GetBranchConfig(mceventid).GetFieldId("B");
  238. const int iPhiRp = out_config->GetBranchConfig(mceventid).GetFieldId("PhiRp");
  239. // fhcal_modules' additional field ids
  240. const int fhcalid = fhcal_modules->GetId();
  241. const int ifhcalphi = out_config->GetBranchConfig(fhcalid).GetFieldId("phi");
  242. const int ifhcalsign = out_config->GetBranchConfig(fhcalid).GetFieldId("signal_eta_signed");
  243. // tpc_tracks' additional field ids
  244. const int tpctracksid = tpc_tracks->GetId();
  245. const int inhits = out_config->GetBranchConfig(tpctracksid).GetFieldId("nhits");
  246. const int inhits_poss = out_config->GetBranchConfig(tpctracksid).GetFieldId("nhits_poss");
  247. const int icharge = out_config->GetBranchConfig(tpctracksid).GetFieldId("charge");
  248. const int idcax = out_config->GetBranchConfig(tpctracksid).GetFieldId("dca_x");
  249. const int idcay = out_config->GetBranchConfig(tpctracksid).GetFieldId("dca_y");
  250. const int idcaz = out_config->GetBranchConfig(tpctracksid).GetFieldId("dca_z");
  251. const int ichi2 = out_config->GetBranchConfig(tpctracksid).GetFieldId("chi2");
  252. const int itof_mass2 = out_config->GetBranchConfig(tpctracksid).GetFieldId("tof_mass2");
  253. const int itof_flag = out_config->GetBranchConfig(tpctracksid).GetFieldId("tof_flag");
  254. const int idedx = out_config->GetBranchConfig(tpctracksid).GetFieldId("dedx");
  255. const int ipid_prob_pion = out_config->GetBranchConfig(tpctracksid).GetFieldId("pid_prob_pion");
  256. const int ipid_prob_kaon = out_config->GetBranchConfig(tpctracksid).GetFieldId("pid_prob_kaon");
  257. const int ipid_prob_proton = out_config->GetBranchConfig(tpctracksid).GetFieldId("pid_prob_proton");
  258. const int imc_E = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_E");
  259. const int imc_pT = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_pT");
  260. const int imc_eta = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_eta");
  261. const int imc_phi = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_phi");
  262. const int imc_rapidity = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_rapidity");
  263. const int imc_mother_id = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_mother_id");
  264. const int imc_pdg = out_config->GetBranchConfig(tpctracksid).GetFieldId("mc_pdg");
  265. const int ietasign = out_config->GetBranchConfig(tpctracksid).GetFieldId("eta_sign");
  266. const int irapidity_pion = out_config->GetBranchConfig(tpctracksid).GetFieldId("rapidity_pion");
  267. const int irapidity_pdg = out_config->GetBranchConfig(tpctracksid).GetFieldId("rapidity_pdg");
  268. const int irapidity_kaon = out_config->GetBranchConfig(tpctracksid).GetFieldId("rapidity_kaon");
  269. const int irapidity_proton = out_config->GetBranchConfig(tpctracksid).GetFieldId("rapidity_proton");
  270. // mc_tracks' additional field ids
  271. const int mctracksid = mc_tracks->GetId();
  272. const int imother_id = out_config->GetBranchConfig(mctracksid).GetFieldId("mother_id");
  273. const int icharge_mc = out_config->GetBranchConfig(mctracksid).GetFieldId("is_charged");
  274. const int ietasign_mc = out_config->GetBranchConfig(mctracksid).GetFieldId("eta_sign");
  275. // Create branches in the output tree
  276. outTree->Branch(str_reco_event_branch.c_str(), "AnalysisTree::EventHeader", &reco_event, 32000, 99);
  277. outTree->Branch(str_mc_event_branch.c_str(), "AnalysisTree::EventHeader", &mc_event, 32000, 99);
  278. outTree->Branch(str_tpc_tracks_branch.c_str(), "AnalysisTree::TrackDetector", &tpc_tracks, 256000, 99);
  279. outTree->Branch(str_fhcal_branch.c_str(), "AnalysisTree::ModuleDetector", &fhcal_modules, 128000, 99);
  280. outTree->Branch(str_mc_tracks_branch.c_str(), "AnalysisTree::Particles", &mc_tracks, 256000, 99);
  281. outTree->Branch(str_tpc2mc_tracks_branch.c_str(), "AnalysisTree::Matching", &tpc2mc_tracks, 32000, 99);
  282. // Printout basic configuration info
  283. out_config->Print();
  284. // Starting event loop
  285. TVector3 primaryVertex, mc_assoc_mom;
  286. std::set <Int_t> UsedMCTracks; // using to remap mc-reco track matching
  287. std::map <Int_t,Int_t> InitMcNewMcId; // map[old-mc-id] = new-mc-id
  288. Float_t FHCalSumEnergy[Num_Of_Modules];
  289. Int_t FHCalNumOfHits[Num_Of_Modules];
  290. Int_t n_entries = dstTree->GetEntriesFast();
  291. bool isGoodPID;
  292. int charge;
  293. for (int iEv = 0; iEv < n_entries; iEv++)
  294. {
  295. std::cout << "Event [" << iEv << "/" << n_entries << "]" << std::endl;
  296. dstTree->GetEntry(iEv);
  297. UsedMCTracks.clear();
  298. InitMcNewMcId.clear();
  299. tpc2mc_tracks->Clear();
  300. for (int i=0; i<Num_Of_Modules; i++)
  301. {
  302. FHCalSumEnergy[i] = 0.;
  303. FHCalNumOfHits[i] = 0;
  304. }
  305. tpc_tracks->ClearChannels();
  306. fhcal_modules->ClearChannels();
  307. mc_tracks->ClearChannels();
  308. // Reading Reco Event
  309. MpdVertex *vertex = (MpdVertex *)vertexes->First();
  310. vertex->Position(primaryVertex);
  311. reco_event->SetVertexPosition3(primaryVertex);
  312. // Read MC Event
  313. TVector3 vtx(MCHeader->GetX(),MCHeader->GetY(),MCHeader->GetZ());
  314. mc_event->SetVertexPosition3(vtx);
  315. mc_event->SetField(float(MCHeader->GetB()), iB);
  316. mc_event->SetField(float(MCHeader->GetRotZ()), iPhiRp);
  317. // Read energy in FHCal modules
  318. fhcal_modules->Reserve(Num_Of_Modules);
  319. for (int imodule=0; imodule<Num_Of_Modules; imodule++)
  320. {
  321. auto *module = fhcal_modules->AddChannel();
  322. module->Init(out_config->GetBranchConfig(fhcalid));
  323. module->SetSignal(0.f);
  324. }
  325. Int_t number_of_FHCal_hits = FHCalHits->GetEntriesFast();
  326. for(int ihit=0; ihit<number_of_FHCal_hits; ihit++)
  327. {
  328. FHCalHit = (MpdZdcDigi*) FHCalHits->UncheckedAt(ihit);
  329. Int_t DetId = FHCalHit->GetDetectorID();
  330. Int_t ModId = FHCalHit->GetModuleID()-1;
  331. Int_t ModNumber = ModId + (Num_Of_Modules/2) * (DetId-1);
  332. FHCalSumEnergy[ModNumber] += FHCalHit->GetELoss();
  333. FHCalNumOfHits[ModNumber]++;
  334. }
  335. for (int imodule=0; imodule<Num_Of_Modules; imodule++)
  336. {
  337. auto& module = fhcal_modules->GetChannel(imodule);
  338. int fhcal_sign = (imodule < 45) ? -1 : 1;
  339. module.SetNumber(FHCalNumOfHits[imodule]); // Number of hits that got in the module
  340. module.SetSignal(FHCalSumEnergy[imodule]); // Total energy from hits in the module
  341. module.SetField(float(GetFHCalPhi(imodule)), ifhcalphi);
  342. module.SetField(float(FHCalSumEnergy[imodule]*(float)fhcal_sign), ifhcalsign);
  343. }
  344. // Reading Reco Tracks
  345. MpdGlobalTracks = (TClonesArray*)MPDEvent->GetGlobalTracks();
  346. Int_t Num_of_tpc_tracks = MpdGlobalTracks->GetEntriesFast();
  347. tpc_tracks->Reserve(Num_of_tpc_tracks);
  348. for (int itrack=0; itrack<Num_of_tpc_tracks; itrack++)
  349. {
  350. MpdTrack* mpdtrack = (MpdTrack*) MpdGlobalTracks->UncheckedAt(itrack);
  351. UsedMCTracks.insert(mpdtrack->GetID());
  352. auto *track = tpc_tracks->AddChannel();
  353. track->Init(out_config->GetBranchConfig(tpc_tracks->GetId()));
  354. int tpc_eta_sign = (mpdtrack->GetEta() < 0) ? -1 : 1;
  355. track->SetMomentum(mpdtrack->GetPx(),mpdtrack->GetPy(),mpdtrack->GetPz());
  356. track->SetField(int(mpdtrack->GetNofHits()), inhits);
  357. track->SetField(int(mpdtrack->GetNofHitsPossTpc()), inhits_poss);
  358. track->SetField(float(mpdtrack->GetTofMass2()), itof_mass2);
  359. track->SetField(float(mpdtrack->GetTofFlag()), itof_flag);
  360. track->SetField(float(mpdtrack->GetdEdXTPC()), idedx);
  361. track->SetField(float(mpdtrack->GetChi2()), ichi2);
  362. track->SetField(float(mpdtrack->GetDCAX()), idcax);
  363. track->SetField(float(mpdtrack->GetDCAY()), idcay);
  364. track->SetField(float(mpdtrack->GetDCAZ()), idcaz);
  365. charge = (mpdtrack->GetPt() < 0) ? 1 : -1;
  366. track->SetField(int(charge), icharge);
  367. track->SetField(int(tpc_eta_sign), ietasign);
  368. Float_t mpdtrack_p = TMath::Sqrt(TMath::Power(mpdtrack->GetPx(),2) + TMath::Power(mpdtrack->GetPy(),2) + TMath::Power(mpdtrack->GetPz(),2));
  369. track->SetField(float(GetRapidity(mpdtrack_p, mpdtrack->GetPz(), 0)), irapidity_pion);
  370. track->SetField(float(GetRapidity(mpdtrack_p, mpdtrack->GetPz(), 1)), irapidity_kaon);
  371. track->SetField(float(GetRapidity(mpdtrack_p, mpdtrack->GetPz(), 2)), irapidity_proton);
  372. // PID
  373. if (mpdtrack->GetTofFlag() == 2 || mpdtrack->GetTofFlag() == 6)
  374. {
  375. // TOF + TPC
  376. isGoodPID = pid->FillProbs(TMath::Abs(mpdtrack->GetPt()) * TMath::CosH(mpdtrack->GetEta()),
  377. mpdtrack->GetdEdXTPC(), mpdtrack->GetTofMass2(), charge);
  378. }
  379. else
  380. {
  381. // TPC only
  382. isGoodPID = pid->FillProbs(TMath::Abs(mpdtrack->GetPt()) * TMath::CosH(mpdtrack->GetEta()),
  383. mpdtrack->GetdEdXTPC(), charge);
  384. }
  385. if (isGoodPID)
  386. {
  387. track->SetField(float(pid->GetProbPi()), ipid_prob_pion);
  388. track->SetField(float(pid->GetProbKa()), ipid_prob_kaon);
  389. track->SetField(float(pid->GetProbPr()), ipid_prob_proton);
  390. }
  391. else
  392. {
  393. track->SetField(float(-999.), ipid_prob_pion);
  394. track->SetField(float(-999.), ipid_prob_kaon);
  395. track->SetField(float(-999.), ipid_prob_proton);
  396. }
  397. #ifdef _MCSTACK_
  398. FairMCTrack *mctrack = (FairMCTrack*) MCTracks->UncheckedAt(mpdtrack->GetID());
  399. #endif
  400. #ifdef _MPDMCSTACK_
  401. MpdMCTrack *mctrack = (MpdMCTrack*) MCTracks->UncheckedAt(mpdtrack->GetID());
  402. #endif
  403. mc_assoc_mom.SetXYZ(mctrack->GetPx(), mctrack->GetPy(), mctrack->GetPz());
  404. track->SetField(float(mctrack->GetEnergy()), imc_E);
  405. track->SetField(float(mc_assoc_mom.Pt()), imc_pT);
  406. track->SetField(float(mc_assoc_mom.Eta()), imc_eta);
  407. track->SetField(float(mc_assoc_mom.Phi()), imc_phi);
  408. track->SetField(float(mctrack->GetRapidity()), imc_rapidity);
  409. track->SetField(int(mctrack->GetMotherId()), imc_mother_id);
  410. track->SetField(int(mctrack->GetPdgCode()), imc_pdg);
  411. track->SetField(float(GetRapidityPDG(mpdtrack_p, mpdtrack->GetPz(), mctrack->GetPdgCode())), irapidity_pdg);
  412. } // End of the tpc track loop
  413. // Read Mc tracks
  414. Int_t Num_of_mc_tracks = MCTracks->GetEntriesFast();
  415. mc_tracks->Reserve(Num_of_mc_tracks);
  416. for (int imctrack=0; imctrack<Num_of_mc_tracks; imctrack++)
  417. {
  418. #ifdef _MCSTACK_
  419. FairMCTrack *mctrack = (FairMCTrack*) MCTracks->UncheckedAt(imctrack);
  420. #endif
  421. #ifdef _MPDMCSTACK_
  422. MpdMCTrack *mctrack = (MpdMCTrack*) MCTracks->UncheckedAt(imctrack);
  423. #endif
  424. bool isUsed = (UsedMCTracks.count(imctrack));
  425. // If motherId != 1 and mc track doesn't relate to any reco track - skip
  426. if (mctrack->GetMotherId() != -1 && !isUsed) continue;
  427. auto *track = mc_tracks->AddChannel();
  428. track->Init(out_config->GetBranchConfig(mc_tracks->GetId()));
  429. // Collect new Mc Ids
  430. InitMcNewMcId[imctrack] = track->GetId();
  431. int mc_eta_sign = (mctrack->GetRapidity() < 0) ? -1 : 1;
  432. track->SetMomentum(mctrack->GetPx(), mctrack->GetPy(), mctrack->GetPz());
  433. track->SetPid(int(mctrack->GetPdgCode()));
  434. track->SetMass(float(mctrack->GetMass()));
  435. track->SetField(int(mctrack->GetMotherId()), imother_id);
  436. track->SetField(bool(IsCharged(mctrack->GetPdgCode())), icharge_mc);
  437. track->SetField(int(mc_eta_sign), ietasign_mc);
  438. } // End of the mc track loop
  439. // reco-mc tracks matching
  440. for (int itrack=0; itrack<Num_of_tpc_tracks; itrack++)
  441. {
  442. MpdTrack* mpdtrack = (MpdTrack*) MpdGlobalTracks->UncheckedAt(itrack);
  443. const int tpc_track_id = itrack;
  444. const int mc_track_id = InitMcNewMcId[mpdtrack->GetID()];
  445. tpc2mc_tracks->AddMatch(tpc_track_id, mc_track_id);
  446. }
  447. outTree->Fill();
  448. } // End of the event loop
  449. outFile->cd();
  450. outTree->Print();
  451. outTree->Write();
  452. if (isDataHeaderDefined) dataHeader->Write("DataHeader");
  453. out_config->Write("Configuration");
  454. outFile->Close();
  455. timer.Stop();
  456. timer.Print();
  457. return 0;
  458. }
  459. float get_beamP(float sqrtSnn, float m_target, float m_beam)
  460. {
  461. return sqrt( pow((pow(sqrtSnn,2) - pow(m_target,2) - pow(m_beam,2))/(2*m_target), 2) - pow(m_beam,2) );
  462. }
  463. float GetFHCalPhi(int iModule)
  464. {
  465. const int Nmodules = 45;
  466. int xAxisSwitch = (iModule < Nmodules) ? 1 : -1;
  467. int module = (iModule < Nmodules) ? iModule : iModule - Nmodules;
  468. float x, y, phi = -999.;
  469. if (module >= 0 && module <= 4)
  470. {
  471. y = 45.;
  472. x = (module - 2) * 15.;
  473. phi = TMath::ATan2(y, x * xAxisSwitch);
  474. }
  475. else if ((module >= 5) && (module <= 39))
  476. {
  477. y = (3 - (module + 2) / 7) * 15.;
  478. x = (3 - (module + 2) % 7) * 15.;
  479. phi = TMath::ATan2(y, x * xAxisSwitch);
  480. }
  481. else if ((module >= 40) && (module <= 44))
  482. {
  483. y = -45.;
  484. x = (module - 42) * 15.;
  485. phi = TMath::ATan2(y, x * xAxisSwitch);
  486. }
  487. return phi;
  488. }
  489. TVector3 GetFHCalPos(int iModule)
  490. {
  491. const int Nmodules = 45;
  492. int xAxisSwitch = (iModule < Nmodules) ? 1 : -1;
  493. int module = (iModule < Nmodules) ? iModule : iModule - Nmodules;
  494. float x, y, z;
  495. z = (iModule < Nmodules) ? -320. : 320.;
  496. if (module >= 0 && module <= 4)
  497. {
  498. y = 45.;
  499. x = (module - 2) * 15.;
  500. }
  501. else if ((module >= 5) && (module <= 39))
  502. {
  503. y = (3 - (module + 2) / 7) * 15.;
  504. x = (3 - (module + 2) % 7) * 15.;
  505. }
  506. else if ((module >= 40) && (module <= 44))
  507. {
  508. y = -45.;
  509. x = (module - 42) * 15.;
  510. }
  511. TVector3 vec(x * xAxisSwitch, y, z);
  512. return vec;
  513. }
  514. Bool_t IsCharged(Int_t pdg)
  515. {
  516. auto particle = (TParticlePDG *)TDatabasePDG::Instance()->GetParticle(pdg);
  517. if (!particle)
  518. return false;
  519. return ( particle->Charge() != 0 );
  520. }
  521. Float_t GetRapidity(Float_t p, Float_t pz, Int_t pid_type)
  522. {
  523. Float_t mass; // in GeV/c^2
  524. if (pid_type == 0) mass = 0.13957; // pions
  525. if (pid_type == 1) mass = 0.493677; // kaons
  526. if (pid_type == 2) mass = 0.938272; // protons
  527. if (pid_type != 0 && pid_type != 1 && pid_type != 2) return -999.;
  528. Float_t E = TMath::Sqrt(p*p + mass*mass);
  529. return 0.5 * TMath::Log((E + pz)/(E-pz));
  530. }
  531. Float_t GetRapidityPDG(Float_t p, Float_t pz, Int_t pdg)
  532. {
  533. Float_t mass;
  534. auto pdgParticle = (TParticlePDG*)TDatabasePDG::Instance()->GetParticle(pdg);
  535. if (!pdgParticle) return -999.;
  536. mass = pdgParticle->Mass();
  537. Float_t E = TMath::Sqrt(p*p + mass*mass);
  538. return 0.5 * TMath::Log((E + pz)/(E - pz));
  539. }