urqmd2mcpico.C 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <map>
  6. #include <Rtypes.h>
  7. #include <TFile.h>
  8. #include <TTree.h>
  9. #include <TMath.h>
  10. #include <TStopwatch.h>
  11. void urqmd2mcpico(TString inFileName = "", TString outFileName = "")
  12. {
  13. // Create a timer object to benchmark this loop
  14. TStopwatch timer;
  15. timer.Start();
  16. // Init event/particle parameters
  17. // Fields below marked with "*" in their comments are of importance as they will be used later in FlowANA for the flow measurements
  18. float d_bimp; // impact parameter *
  19. int d_npart; // number of participants
  20. float d_phi2; // EP/RP plane angle for v2
  21. float d_phi3; // EP/RP plane angle for v3
  22. float d_ecc2; // eccentricity for v2
  23. float d_ecc3; // eccentricity for v3
  24. static const int max_nh = 25000;
  25. int d_nh; // number of all particles in the event *
  26. float d_momx[max_nh]; //[momentum x] *
  27. float d_momy[max_nh]; //[momentum y] *
  28. float d_momz[max_nh]; //[momentum z] *
  29. float d_ene[max_nh]; //[energy] *
  30. int d_pdg[max_nh]; //[particle data group code] *
  31. int d_hid[max_nh]; //[history id]
  32. short d_charge[max_nh]; //[electric charge]
  33. // Init output file
  34. TFile *hfile;
  35. hfile = new TFile(outFileName.Data(), "RECREATE");
  36. // Init output file
  37. TTree *tree;
  38. tree = new TTree("mctree", "An example of a ROOT tree for mcpico format");
  39. tree->Branch("bimp", &d_bimp, "bimp/F"); // impact parametr
  40. tree->Branch("phi2", &d_phi2, "phi2/F"); // phiRP2
  41. tree->Branch("phi3", &d_phi3, "phi3/F"); // phiRP3
  42. tree->Branch("ecc2", &d_ecc2, "ecc2/F"); // eccentricity2
  43. tree->Branch("ecc3", &d_ecc3, "ecc3/F"); // eccentricity3
  44. tree->Branch("npart", &d_npart, "npart/I"); // number of participants
  45. tree->Branch("nh", &d_nh, "nh/I"); // number of particles
  46. tree->Branch("momx", &d_momx, "momx[nh]/F"); //[x projection of the particle's momentum]
  47. tree->Branch("momy", &d_momy, "momy[nh]/F"); //[y projection of the particle's momentum]
  48. tree->Branch("momz", &d_momz, "momz[nh]/F"); //[z projection of the particle's momentum]
  49. tree->Branch("ene", &d_ene, "ene[nh]/F"); //[energy]
  50. tree->Branch("hid", &d_hid, "hid[nh]/I"); //[histrory id]
  51. tree->Branch("pdg", &d_pdg, "pdg[nh]/I"); //[particle data group code]
  52. tree->Branch("charge", &d_charge, "charge[nh]/S"); //[electric charge]
  53. // Init objects for input file reading
  54. std::ifstream is;
  55. std::stringstream ss;
  56. std::string str;
  57. // Init temporary parameters during file reading
  58. Double_t B, PsiRP, Time,
  59. r0, rx, ry, rz, p0, px, py, pz,
  60. m, ityp, i3, ichg, ncl, lcl, orr, dump;
  61. Int_t Nev, Npart, count,
  62. pid;
  63. Int_t ev;
  64. Int_t printev;
  65. Double_t told = 0;
  66. Double_t tnew = 0;
  67. const Int_t count1 = 3, count2 = 12;
  68. // Table of the urqmd particle types and corresponding pdg codes
  69. // Dictionary: {itype, pdg}
  70. const std::map<Int_t, Int_t> particleURQMD = {{100, 22},
  71. {101, 211},
  72. {101, 111},
  73. {102, 221},
  74. {103, 223},
  75. {104, 213},
  76. {104, 113},
  77. {105, 9000221},
  78. {106, 321},
  79. {106, 311},
  80. {107, 331},
  81. {108, 323},
  82. {108, 313},
  83. {109, 333},
  84. {110, 9000311},
  85. {110, 9000321},
  86. {111, 9000321},
  87. {1, 2212},
  88. {1, 2112},
  89. {27, 3122}};
  90. // Open input output.f14 file and exit if we fail to do so
  91. is.open(inFileName.Data());
  92. if (is.fail())
  93. {
  94. std::cerr << "urqmd2mcpico: Open input file: " << inFileName.Data() << std::endl;
  95. return;
  96. }
  97. // Main loop where we read the file. We print out the event count every 100 events.
  98. ev = 0;
  99. printev = 100;
  100. while (!is.eof())
  101. {
  102. // Print out information
  103. if (ev % printev == 0)
  104. {
  105. tnew = timer.RealTime();
  106. printf("\tevent:%d,\trtime=%f s\n", ev, tnew - told);
  107. told = tnew;
  108. timer.Continue();
  109. }
  110. ss.str("");
  111. ss.clear();
  112. getline(is, str);
  113. if (str[0] == ' ')
  114. continue;
  115. for (Int_t i = 0; i < count1 - 1; i++)
  116. {
  117. getline(is, str);
  118. }
  119. // Read impact parameter
  120. ss.str("");
  121. ss.clear();
  122. getline(is, str);
  123. ss << str;
  124. ss >> str >> B;
  125. getline(is, str);
  126. // Read number of event
  127. ss.str("");
  128. ss.clear();
  129. getline(is, str);
  130. ss << str;
  131. ss >> str >> Nev;
  132. for (int j = 0; j < count2; j++)
  133. {
  134. getline(is, str);
  135. }
  136. PsiRP = 0.;
  137. if (is.eof())
  138. {
  139. break;
  140. }
  141. // Read number of particles and time
  142. ss.str("");
  143. ss.clear();
  144. ss << str;
  145. ss >> Npart >> Time;
  146. getline(is, str);
  147. // Skip the event if it has no produced particles
  148. if (Npart <= 0)
  149. continue;
  150. // Fill tree info
  151. d_bimp = B;
  152. d_nh = Npart;
  153. d_npart = 0;
  154. d_phi2 = PsiRP;
  155. d_phi3 = PsiRP;
  156. d_ecc2 = 0;
  157. d_ecc3 = 0;
  158. // Loop over particles in the event
  159. for (int j = 0; j < Npart; j++)
  160. {
  161. ss.str("");
  162. ss.clear();
  163. getline(is, str);
  164. ss << str;
  165. ss >> r0 >> rx >> ry >> rz >> p0 >> px >> py >> pz >> m >> ityp >> i3 >> ichg >> dump >> dump >> dump >> dump >> dump >> dump;
  166. if (particleURQMD.find(TMath::Abs(ityp)) != particleURQMD.end())
  167. {
  168. pid = TMath::Sign(particleURQMD.at(TMath::Abs(ityp)), ichg);
  169. // UrQMD has the same itype for differently charged particles - here's a quick fix for some particle species
  170. if (ichg == 0 && ityp == 1) pid = 2112;
  171. if (ichg != 0 && ityp == 1) pid = 2212;
  172. if (ichg == 0 && ityp == 101) pid = 111;
  173. if (ichg != 0 && ityp == 101) pid = 211;
  174. if (ichg == 0 && ityp == 104) pid = 113;
  175. if (ichg != 0 && ityp == 104) pid = 213;
  176. if (ichg == 0 && ityp == 106) pid = 311;
  177. if (ichg != 0 && ityp == 106) pid = 321;
  178. if (ichg == 0 && ityp == 108) pid = 323;
  179. if (ichg != 0 && ityp == 108) pid = 313;
  180. if (ichg == 0 && ityp == 110) pid = 9000311;
  181. if (ichg != 0 && ityp == 110) pid = 9000321;
  182. }
  183. else
  184. {
  185. pid = -999.;
  186. }
  187. // Fill tree info
  188. d_momx[j] = px;
  189. d_momy[j] = py;
  190. d_momz[j] = pz;
  191. d_ene[j] = p0;
  192. d_pdg[j] = pid;
  193. d_hid[j] = -1;
  194. d_charge[j] = ichg;
  195. }
  196. // Fill the event information in the TTree
  197. tree->Fill();
  198. ev++;
  199. } // end of the while loop
  200. // Write TTree into ROOT file
  201. hfile->cd();
  202. tree->Write();
  203. // Stop timer and print results
  204. timer.Stop();
  205. Double_t rtime = timer.RealTime();
  206. Double_t ctime = timer.CpuTime();
  207. printf("\n%d events has been processed.\n", ev);
  208. printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
  209. hfile->Close();
  210. }