StFemtoDstMaker_StarGen.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include "StFemtoDstMaker_StarGen.h"
  2. #include "StBTofHeader.h"
  3. #include "TBranch.h"
  4. #include "StPhysicalHelixD.hh"
  5. #include "StDcaGeometry.h"
  6. ClassImp(StFemtoDstMaker_StarGen)
  7. //
  8. // Set maximum file size to 1.9 GB (Root has a 2GB limit)
  9. //
  10. #define MAXFILESIZE 1900000000
  11. StFemtoDstMaker_StarGen::StFemtoDstMaker_StarGen(const char *dirName, const char *fileName,
  12. const char *oFileName,
  13. const char *filter, int maxFiles)
  14. {
  15. std::cout << "StFemtoDstMaker_StarGen::StFemtoDstMaker_StarGen - Creating an instance...";
  16. mOutFileName = oFileName;
  17. mCompression = 9;
  18. // initialize single-particle cut variables
  19. mTrackMomentum[0] = 0.1;
  20. mTrackMomentum[1] = 2.;
  21. mTrackEta[0] = -1.1;
  22. mTrackEta[1] = 1.1;
  23. mDir = string(dirName);
  24. mFileName = string(fileName);
  25. mFilter = filter;
  26. mMaxFiles = maxFiles;
  27. mDb = TDatabasePDG::Instance();
  28. mRandy = new TRandom3(0);
  29. mMatrix = new TMatrixTSym<double>(2);
  30. mTotalTracks = 0.;
  31. mTree = 0;
  32. mTChain = 0;
  33. mSGEvent = 0;
  34. mEventIndex = 0;
  35. InitRead(mDir, mFileName, mFilter, mMaxFiles);
  36. mNEvents = (unsigned int)mTChain->GetEntries();
  37. std::cout << "\t[DONE]\n";
  38. }
  39. StFemtoDstMaker_StarGen::~StFemtoDstMaker_StarGen()
  40. {
  41. delete mMatrix;
  42. }
  43. int StFemtoDstMaker_StarGen::FillChain(TChain *chain, char *dir,
  44. const char *filter, int maxFiles)
  45. {
  46. TSystem *gSystem = new TSystem();
  47. if (!gSystem)
  48. {
  49. cout << "StFemtoDstMaker_StarGen[ERROR]: cannot allocate memory for TSystem\n";
  50. return 0;
  51. }
  52. void *lDir = gSystem->OpenDirectory(dir);
  53. if (!lDir)
  54. {
  55. cout
  56. << "StFemtoDstMaker_StarGen[ERROR]: can't open directory "
  57. << dir << endl;
  58. delete gSystem;
  59. return 0;
  60. }
  61. int mCount = 0;
  62. char *lFileName;
  63. while ((lFileName = (char *)gSystem->GetDirEntry(dir)))
  64. {
  65. if (strcmp(lFileName, ".") == 0 || strcmp(lFileName, "..") == 0)
  66. continue;
  67. if (strstr(lFileName, filter))
  68. {
  69. char *lFullName = gSystem->ConcatFileName(dir, lFileName);
  70. cout
  71. << "StFemtoDstMaker_StarGen[INFO]: Adding "
  72. << lFullName << " to the chain\n";
  73. chain->Add(lFullName);
  74. delete lFullName;
  75. mCount++;
  76. if ((maxFiles > 0) && (mCount > maxFiles)) break;
  77. }
  78. }
  79. cout
  80. << "StFemtoDstMaker_StarGen[INFO]: Added " << mCount
  81. << " files to the chain\n";
  82. delete gSystem;
  83. return mCount;
  84. }
  85. int StFemtoDstMaker_StarGen::FillChain(TChain *chain, const char *fileName, int maxFiles)
  86. {
  87. ifstream lInStream(fileName);
  88. if (!lInStream.is_open())
  89. {
  90. cout
  91. << "StFemtoDstMaker_StarGen[ERROR]: can't open file list: "
  92. << fileName << endl;
  93. return 0;
  94. }
  95. cout
  96. << "StFemtoDstMaker_StarGen[INFO]: using file list: "
  97. << fileName << endl;
  98. int mCount = 0;
  99. char buf[0xFF];
  100. for ( ; lInStream.good(); )
  101. {
  102. lInStream.getline(buf, 0xFF);
  103. TString lFileName(buf);
  104. if (lFileName.Contains("root"))
  105. {
  106. chain->Add(buf);
  107. mCount++;
  108. if ((maxFiles > 0) && (mCount > maxFiles)) break;
  109. }
  110. }
  111. cout
  112. << "StFemtoDstMaker_StarGen[INFO]: Added " << mCount
  113. << " files to the chain\n";
  114. return mCount;
  115. }
  116. int StFemtoDstMaker_StarGen::InitRead(string aDir, string fileName,
  117. string aFilter, int maxFiles)
  118. {
  119. mEventIndex = 0;
  120. mTChain = new TChain("genevents", "genevents");
  121. cout << "StFemtoDstMaker_StarGen[INFO]: Call InitRead\n";
  122. int lNumFiles = 0;
  123. if (!fileName.empty())
  124. {
  125. if (strstr(fileName.c_str(), ".lis") ||
  126. strstr(fileName.c_str(), ".list"))
  127. {
  128. lNumFiles = FillChain( mTChain,
  129. (aDir + fileName).c_str(),
  130. maxFiles );
  131. }
  132. else
  133. {
  134. mTChain->Add((aDir + fileName).c_str());
  135. lNumFiles++;
  136. }
  137. } else
  138. {
  139. lNumFiles = FillChain(mTChain, (char *)aDir.c_str(), // not cool
  140. aFilter.c_str(), maxFiles);
  141. }
  142. mTChain->SetBranchAddress("primaryEvent", &mSGEvent);
  143. mNEvents = (unsigned int)mTChain->GetEntries();
  144. cout
  145. << "StFemtoDstMaker_StarGen::InitRead [INFO]"
  146. << " NEvents to read: " << mNEvents << endl;
  147. return lNumFiles;
  148. }
  149. void StFemtoDstMaker_StarGen::UninitRead()
  150. {
  151. if (mTChain) delete mTChain;
  152. mSGEvent = 0;
  153. mTChain = 0;
  154. }
  155. int StFemtoDstMaker_StarGen::GetNEvents()
  156. {
  157. if (mTChain) return mNEvents;
  158. else return -1;
  159. }
  160. Int_t StFemtoDstMaker_StarGen::Init()
  161. {
  162. std::cout << "StFemtoDstMaker_StarGen::Init - Initializing the maker\n"
  163. << "Creating output file: " << mOutFileName;
  164. mFemtoEvent = new StFemtoEvent();
  165. mOutFile = new TFile(mOutFileName, "RECREATE");
  166. mOutFile->cd();
  167. mOutFile->SetCompressionLevel(mCompression);
  168. std::cout << "\t[DONE]" << std::endl;
  169. mTree = new TTree("StFemtoDst","StFemtoDst");
  170. mTree->SetMaxTreeSize(MAXFILESIZE);
  171. mTree->Branch("StFemtoEvent","StFemtoEvent", &mFemtoEvent);
  172. mNBytes = 0;
  173. std::cout << "StFemtoDstMaker_StarGen::Init - Initialization has been finished\n";
  174. return StMaker::Init();
  175. }
  176. void StFemtoDstMaker_StarGen::Clear(Option_t *option)
  177. {
  178. StMaker::Clear();
  179. }
  180. Int_t StFemtoDstMaker_StarGen::Make()
  181. {
  182. if (!mNEvents)
  183. {
  184. cout << "StHbtStarGenReader[INFO]: no events to read\n";
  185. return 0;
  186. }
  187. int lBytes = mTChain->GetEntry(mEventIndex++);
  188. if (mNEvents < mEventIndex)
  189. {
  190. cout << "StHbtStarGenReader[INFO]: EOF\n";
  191. return 0;
  192. }
  193. if (!lBytes)
  194. {
  195. cout << "StHbtStarGenReader[INFO]: no event\n";
  196. return 0;
  197. }
  198. if (!mSGEvent) return 0;
  199. const float magRFF = -4.98; // [10^3 G], ref: p+Au 200 GeV y2015
  200. unsigned int nTracks = mSGEvent->GetNumberOfParticles(); // number of tracks in an event
  201. mFemtoEvent->SetEventId(mSGEvent->GetEventNumber());
  202. mFemtoEvent->SetRunId(mSGEvent->GetRunNumber());
  203. mFemtoEvent->SetCollisionType(false);
  204. mFemtoEvent->SetCent16(0);
  205. mFemtoEvent->SetMagneticField(magRFF);
  206. mFemtoEvent->SetPrimaryVertexPosition(0.0, 0.0, 0.0);
  207. mFemtoEvent->SetVpdVz(0.0);
  208. mFemtoEvent->SetTriggerId(0);
  209. mFemtoEvent->SetPrimaryVertexRanking(666);
  210. unsigned int refMult2 = 0;
  211. unsigned int refMult2Pos = 0;
  212. unsigned int refMult = 0;
  213. unsigned int refMultPos = 0;
  214. unsigned int nGoodTracks = 0;
  215. StFemtoTrack *mFTrack = NULL;
  216. //
  217. // For sphericity
  218. //
  219. float sph = -999.;
  220. float pt_full = 0.;
  221. mMatrix->Zero();
  222. //
  223. // Loop over primary tracks
  224. //
  225. for (unsigned int iTrk = 0; iTrk < nTracks; iTrk++)
  226. {
  227. StarGenParticle *sParticle = (*mSGEvent)[iTrk];
  228. //
  229. // Get charge
  230. //
  231. int pdg = sParticle->GetId();
  232. TParticlePDG *partPdg = mDb->GetParticle(pdg);
  233. if (partPdg == 0x0) continue;
  234. char charge = partPdg->Charge();
  235. if (charge == 0) continue;
  236. charge /= abs(charge);
  237. //
  238. // Get momentum, beta and DCA
  239. //
  240. const TLorentzVector &p = sParticle->momentum();
  241. float px = p.Px();
  242. float py = p.Py();
  243. float pz = p.Pz();
  244. float pt = p.Pt();
  245. if (pt < 1.0e-5) continue;
  246. float ptot = p.P();
  247. float eta = p.PseudoRapidity();
  248. float energy = p.Energy();
  249. float beta = ptot/energy;
  250. float xDCA = sParticle->GetVx();
  251. float yDCA = sParticle->GetVy();
  252. float zDCA = sParticle->GetVz();
  253. float dca = sqrt(xDCA*xDCA + yDCA*yDCA + zDCA*zDCA);
  254. //
  255. // Calculate sphericity
  256. //
  257. // find lead particle and calculate matrix
  258. if (pt > mPtCut)
  259. {
  260. if (fabs(eta) < 1.0)
  261. {
  262. (*mMatrix)(0, 0) += px*px/pt;
  263. (*mMatrix)(1, 1) += py*py/pt;
  264. (*mMatrix)(0, 1) += px*py/pt;
  265. (*mMatrix)(1, 0) += py*px/pt;
  266. pt_full += pt;
  267. }
  268. }
  269. if (fabs(eta) < 1.0 && dca < 3.0)
  270. {
  271. refMult2++;
  272. if (charge > 0) refMult2Pos++;
  273. if (fabs(eta) < 0.5)
  274. {
  275. refMult++;
  276. if (charge > 0) refMultPos++;
  277. }
  278. }
  279. float nSigmaElectron = -999.;
  280. float nSigmaPion = -999.;
  281. float nSigmaKaon = -999.;
  282. float nSigmaProton = -999.;
  283. switch (abs(pdg))
  284. {
  285. case 211:
  286. nSigmaPion = 0.0;
  287. break;
  288. case 321:
  289. nSigmaKaon = 0.0;
  290. break;
  291. case 2212:
  292. nSigmaProton = 0.0;
  293. break;
  294. default:
  295. continue;
  296. }
  297. if (!AcceptTrack(p)) continue;
  298. else nGoodTracks++;
  299. //
  300. // Calculate topology map
  301. //
  302. unsigned int tmap1 = 0;
  303. unsigned int tmap2 = 0;
  304. TopologyMap(&tmap1, &tmap2, ptot);
  305. // add femto track
  306. mFTrack = mFemtoEvent->AddFemtoTrack();
  307. mFTrack->SetId(iTrk);
  308. mFTrack->SetNHits(charge*45);
  309. mFTrack->SetNHitsPoss(45);
  310. mFTrack->SetNSigmaElectron(nSigmaElectron);
  311. mFTrack->SetNSigmaPion(nSigmaPion);
  312. mFTrack->SetNSigmaKaon(nSigmaKaon);
  313. mFTrack->SetNSigmaProton(nSigmaProton);
  314. mFTrack->SetDedx(dedxMean(sParticle->GetMass(), ptot));
  315. mFTrack->SetMap0(tmap1);
  316. mFTrack->SetMap1(tmap2);
  317. mFTrack->SetP(px, py, pz);
  318. mFTrack->SetDCAxGlobal(sParticle->GetVx());
  319. mFTrack->SetDCAyGlobal(sParticle->GetVy());
  320. mFTrack->SetDCAzGlobal(sParticle->GetVz());
  321. mFTrack->SetGlobalP(px, py, pz);
  322. mFTrack->SetBeta(beta);
  323. }
  324. //
  325. // Calculate eigenvalues
  326. //
  327. *mMatrix *= 1./pt_full;
  328. TMatrixDSymEigen death_machine(*mMatrix);
  329. TVectorD eigen = death_machine.GetEigenValues();
  330. sph = 2.*eigen.Min()/(eigen[0] + eigen[1]);
  331. //
  332. // Continue to fill femto event information
  333. //
  334. mFemtoEvent->SetSphericity(sph);
  335. mFemtoEvent->SetRefMult(refMult);
  336. mFemtoEvent->SetRefMultCorr(refMult);
  337. mFemtoEvent->SetRefMultCorrWeight(1.0);
  338. mFemtoEvent->SetRefMultPos(refMultPos);
  339. mFemtoEvent->SetNumberOfBTofHit(refMult);
  340. mFemtoEvent->SetNumberOfPrimaryTracks(nGoodTracks);
  341. mFemtoEvent->SetNumberOfGlobalTracks(nGoodTracks);
  342. mFemtoEvent->SetRefMult2(refMult2);
  343. mFemtoEvent->SetRefMult2Pos(refMult2Pos);
  344. mNBytes += mTree->Fill();
  345. mFemtoEvent->Clear();
  346. return kStOk;
  347. }
  348. bool StFemtoDstMaker_StarGen::AcceptTrack(const TLorentzVector &p)
  349. {
  350. return p.P() >= mTrackMomentum[0] && p.P() <= mTrackMomentum[1] &&
  351. p.PseudoRapidity() >= mTrackEta[0] && p.PseudoRapidity() <= mTrackEta[1];
  352. }
  353. Int_t StFemtoDstMaker_StarGen::Finish()
  354. {
  355. mOutFile->cd();
  356. mOutFile->Write();
  357. mOutFile->Close();
  358. std::cout
  359. << "***\n"
  360. << "*\n"
  361. << "* StFemtoDstMaker_StarGen has been finished\n"
  362. << "* bytes has been written: " << mNBytes << "\n"
  363. << "***\n";
  364. return kStOk;
  365. }
  366. void StFemtoDstMaker_StarGen::TopologyMap(unsigned int *tmap1, unsigned int *tmap2, float ptot)
  367. {
  368. if (ptot < 0.15) // tracks only in the inner sector. R=1m
  369. {
  370. *tmap1 = 0;
  371. *tmap2 = 0;
  372. for (int iBit = 0; iBit < 32; iBit++)
  373. {
  374. if (mRandy->Rndm() < 0.95)
  375. *tmap1 |= 1 << iBit;
  376. else
  377. *tmap1 |= 0 << iBit;
  378. }
  379. *tmap1 |= 1 << 0; // default set to the first bit: primary-vertex-used
  380. }
  381. else
  382. {
  383. if (ptot < 0.3) // tracks may come to the end of the outer sector: R = 2m
  384. {
  385. *tmap1 = 0;
  386. *tmap2 = 0;
  387. for (int iBit = 0; iBit < 32; iBit++)
  388. {
  389. if (mRandy->Rndm() < 0.95)
  390. *tmap1 |= 1 << iBit;
  391. else
  392. *tmap1 |= 0 << iBit;
  393. if (iBit < 27)
  394. {
  395. if (mRandy->Rndm() < 0.85)
  396. *tmap2 |= 1 << iBit;
  397. else
  398. *tmap2 |= 0 << iBit;
  399. }
  400. }
  401. *tmap1 |= 1 << 0; // default set to the first bit: primary-vertex-used
  402. }
  403. else
  404. {
  405. *tmap1 = 0;
  406. *tmap2 = 0;
  407. for (int iBit = 0; iBit < 32; iBit++)
  408. {
  409. if (mRandy->Rndm() < 0.95)
  410. *tmap1 |= 1 << iBit;
  411. else
  412. *tmap1 |= 0 << iBit;
  413. if(iBit < 27)
  414. {
  415. if(mRandy->Rndm() < 0.95)
  416. *tmap2 |= 1 << iBit;
  417. else
  418. *tmap2 |= 0 << iBit;
  419. }
  420. }
  421. *tmap1 |= 1 << 0; // default set to the first bit: primary-vertex-used
  422. }
  423. }
  424. }
  425. float StFemtoDstMaker_StarGen::dedxMean(float mass, float momentum)
  426. {
  427. double dedxMean;
  428. double tpcDedxGain = 0.174325e-06;
  429. double tpcDedxOffset = -2.71889;
  430. double tpcDedxRise = 776.626;
  431. double gamma = TMath::Sqrt(momentum*momentum/(mass*mass) + 1.0);
  432. double beta = TMath::Sqrt(1.0 - 1.0/(gamma*gamma));
  433. double rise = tpcDedxRise* beta*beta*gamma*gamma;
  434. if (beta > 0.0)
  435. dedxMean = tpcDedxGain/(beta*beta)*(0.5*TMath::Log(rise) - beta*beta
  436. - tpcDedxOffset);
  437. else
  438. dedxMean = 1000.0;
  439. return dedxMean;
  440. }