Oscar97Tree.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "Oscar97Tree.h"
  2. ClassImp(Oscar97Tree)
  3. //______________
  4. Oscar97Tree::Oscar97Tree(const char *f19List, const char *outRoot,
  5. int compression) {
  6. std::cout << "[Oscar97Tree] Creating an instance" << std::endl;
  7. mError = false;
  8. //
  9. // Creating output root file
  10. //
  11. std::cout << "[Oscar97Tree] Creating output root file: "
  12. << outRoot << "... ";
  13. mOutRoot = new TFile(outRoot, "RECREATE");
  14. if (!mOutRoot) {
  15. std::cout << "FAIL" << std::endl;
  16. mError = true;
  17. return;
  18. }
  19. else {
  20. mOutRoot->SetCompressionLevel(compression);
  21. std::cout << "OK" << std::endl;
  22. }
  23. //
  24. // Open list of f19 files
  25. //
  26. mF19List = fopen(f19List, "r");
  27. if (!mF19List) {
  28. std::cout << "[Oscar97Tree] Can't open f19 list file "
  29. << f19List << std::endl;
  30. mError = true;
  31. }
  32. //
  33. // Creating TTree
  34. //
  35. mEvent = new Oscar97Event();
  36. mTree = new TTree("Oscar97Dst", "OscarO97Dst");
  37. if (!mTree) {
  38. std::cout << "[Oscar97Tree] Can't create tree" << std::endl;
  39. mError = true;
  40. }
  41. else {
  42. // mTree->SetMaxTreeSize(MAXFILESIZE);
  43. mTree->Branch("Oscar97Event", "Oscar97Event", &mEvent);
  44. }
  45. mNBytes = 0;
  46. if (mError)
  47. std::cout << "[Oscar97Tree FAIL]" << std::endl;
  48. else
  49. std::cout << "[Oscar97Tree OK]" << std::endl;
  50. }
  51. void Oscar97Tree::MakeRoot() {
  52. #define BFSZ 0xFF
  53. char buf[BFSZ];
  54. //
  55. // Read data
  56. //
  57. int evNum, nTracks;
  58. float imp, planeRot;
  59. int trkRd, refMultA;
  60. bool readEvent = true; // if true - then keep reading events
  61. while (!feof(mF19List)) {
  62. char *retGetS = fgets(buf, BFSZ, mF19List);
  63. buf[strlen(buf) - 1] = '\0';
  64. FILE *f19 = fopen(buf, "r");
  65. if (!f19) break;
  66. std::cout << "[Oscar97Tree] File: " << buf << std::endl;
  67. //
  68. // Skip three useless lines
  69. //
  70. retGetS = fgets(buf, BFSZ, f19);
  71. retGetS = fgets(buf, BFSZ, f19);
  72. retGetS = fgets(buf, BFSZ, f19);
  73. //
  74. // Read events and tracks
  75. //
  76. while (!feof(f19)) {
  77. if (readEvent) {
  78. int ret = fscanf(f19, "%i %i %f %f",
  79. &evNum, &nTracks, &imp, &planeRot);
  80. if (ret == 4) {
  81. mEvent->Clear();
  82. mEvent->SetEventNumber(evNum);
  83. mEvent->SetNTracks(nTracks);
  84. mEvent->SetImpactPar(imp);
  85. mEvent->SetEventPlaneRot(planeRot);
  86. trkRd = 0;
  87. readEvent = false;
  88. }
  89. else // if can't read something like event, skip that line
  90. retGetS = fgets(buf, BFSZ, f19);
  91. }
  92. else {
  93. //
  94. // Read tracks from an event
  95. //
  96. int trkId, trkPdg;
  97. float trkPx, trkPy, trkPz, trkE, trkM,
  98. trkXfr, trkYfr, trkZfr, trkTfr;
  99. int ret1 = fscanf(f19,
  100. "%i %i %f %f %f %f",
  101. &trkId, &trkPdg, &trkPx, &trkPy, &trkPz, &trkE);
  102. int ret2 = fscanf(f19,
  103. "%f %f %f %f %f",
  104. &trkM, &trkXfr, &trkYfr, &trkZfr, &trkTfr);
  105. if (ret1 + ret2 == 11) {
  106. Oscar97Track *track = mEvent->AddTrack(trkRd++);
  107. track->SetId(trkId);
  108. track->SetPdgId(trkPdg);
  109. track->SetPx(trkPx);
  110. track->SetPy(trkPy);
  111. track->SetPz(trkPz);
  112. track->SetE(trkE);
  113. track->SetMass(trkM);
  114. track->SetXfr(trkXfr);
  115. track->SetYfr(trkYfr);
  116. track->SetZfr(trkZfr);
  117. track->SetTfr(trkTfr);
  118. track->SetIsSpec(trkXfr < 0.0001 &&
  119. trkYfr < 0.0001 &&
  120. trkZfr < 0.0001 &&
  121. trkTfr < 0.0001);
  122. if (trkRd == nTracks) {
  123. mNBytes += mTree->Fill();
  124. readEvent = true;
  125. }
  126. }
  127. else // if can't read something like track, skip that line
  128. retGetS = fgets(buf, BFSZ, f19);
  129. }
  130. }
  131. fclose(f19);
  132. }
  133. std::cout << "[Oscar97Tree] Done! Bytes = " << mNBytes << std::endl;
  134. fclose(mF19List);
  135. mOutRoot->Write();
  136. mOutRoot->Close();
  137. }
  138. //______________
  139. Oscar97Tree::~Oscar97Tree() {
  140. }
  141. //______________
  142. bool Oscar97Tree::GetError() {
  143. return mError;
  144. }