StFemtoDstReader.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Allows to read femtoDst file or a list of femtoDst files
  3. //
  4. // C++ headers
  5. #include <string>
  6. #include <sstream>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <assert.h>
  10. // FemtoDst headers
  11. #include "StFemtoMessMgr.h"
  12. #include "StFemtoDstReader.h"
  13. #include "StFemtoEvent.h"
  14. #include "StFemtoTrack.h"
  15. #include "StFemtoV0.h"
  16. #include "StFemtoXi.h"
  17. #include "StFemtoArrays.h"
  18. #include "StFemtoDst.h"
  19. // ROOT headers
  20. #include "TRegexp.h"
  21. ClassImp(StFemtoDstReader)
  22. //_________________
  23. StFemtoDstReader::StFemtoDstReader(const Char_t* inFileName) :
  24. mFemtoDst(new StFemtoDst()), mChain(NULL), mTree(NULL),
  25. mEventCounter(0), mFemtoArrays{}, mStatusArrays{} {
  26. // Constructor
  27. streamerOff();
  28. createArrays();
  29. std::fill_n(mStatusArrays, sizeof(mStatusArrays) / sizeof(mStatusArrays[0]), 1);
  30. mInputFileName = inFileName;
  31. }
  32. //_________________
  33. StFemtoDstReader::~StFemtoDstReader() {
  34. // Destructor
  35. if(mChain) {
  36. delete mChain;
  37. }
  38. if(mFemtoDst) {
  39. delete mFemtoDst;
  40. }
  41. }
  42. //_________________
  43. void StFemtoDstReader::clearArrays() {
  44. // Clear arrays
  45. for(Int_t iArr=0; iArr<StFemtoArrays::NAllFemtoArrays; iArr++) {
  46. mFemtoArrays[iArr]->Clear();
  47. }
  48. }
  49. //_________________
  50. void StFemtoDstReader::SetStatus(const Char_t *branchNameRegex, Int_t enable) {
  51. // Set branch status
  52. if(strncmp(branchNameRegex, "St", 2) == 0) {
  53. // Ignore first "St"
  54. branchNameRegex += 2;
  55. }
  56. TRegexp re(branchNameRegex, 1);
  57. for(Int_t iArr=0; iArr<StFemtoArrays::NAllFemtoArrays; iArr++) {
  58. Ssiz_t len;
  59. if(re.Index(StFemtoArrays::femtoArrayNames[iArr], &len) < 0) continue;
  60. LOG_INFO << "StFemtoDstMaker::SetStatus " << enable
  61. << " to " << StFemtoArrays::femtoArrayNames[iArr] << endm;
  62. mStatusArrays[iArr] = enable;
  63. }
  64. setBranchAddresses(mChain);
  65. }
  66. //_________________
  67. void StFemtoDstReader::setBranchAddresses(TChain *chain) {
  68. // Set addresses of branches listed in femtoArrays
  69. if (!chain) return;
  70. chain->SetBranchStatus("*", 0);
  71. TString ts;
  72. for (Int_t i = 0; i < StFemtoArrays::NAllFemtoArrays; ++i) {
  73. if (mStatusArrays[i] == 0) continue;
  74. char const* bname = StFemtoArrays::femtoArrayNames[i];
  75. TBranch* tb = chain->GetBranch(bname);
  76. if (!tb) {
  77. LOG_WARN << "setBranchAddress: Branch name " << bname << " does not exist!" << endm;
  78. continue;
  79. }
  80. ts = bname;
  81. ts += "*";
  82. chain->SetBranchStatus(ts, 1);
  83. chain->SetBranchAddress(bname, mFemtoArrays + i);
  84. assert(tb->GetAddress() == (char*)(mFemtoArrays + i));
  85. }
  86. mTree = mChain->GetTree();
  87. }
  88. //_________________
  89. void StFemtoDstReader::streamerOff() {
  90. // This is to to save space on the file. No need for TObject bits for this structure.
  91. // see: https://root.cern.ch/doc/master/classTClass.html#a606b0442d6fec4b1cd52f43bca73aa51
  92. StFemtoEvent::Class()->IgnoreTObjectStreamer();
  93. StFemtoTrack::Class()->IgnoreTObjectStreamer();
  94. }
  95. //_________________
  96. void StFemtoDstReader::createArrays() {
  97. // Create arrays
  98. for(Int_t iArr=0; iArr<StFemtoArrays::NAllFemtoArrays; iArr++) {
  99. mFemtoArrays[iArr] = new TClonesArray(StFemtoArrays::femtoArrayTypes[iArr],
  100. StFemtoArrays::femtoArraySizes[iArr]);
  101. }
  102. mFemtoDst->set(mFemtoArrays);
  103. }
  104. //_________________
  105. void StFemtoDstReader::Finish() {
  106. // Finish and clean everything
  107. if(mChain) {
  108. delete mChain;
  109. }
  110. mChain = NULL;
  111. }
  112. //_________________
  113. void StFemtoDstReader::Init() {
  114. // FemtoDst initialization
  115. if(!mChain) {
  116. mChain = new TChain("FemtoDst");
  117. }
  118. std::string const dirFile = mInputFileName.Data();
  119. if( dirFile.find(".list") != std::string::npos ||
  120. dirFile.find(".lis") != std::string::npos ) {
  121. std::ifstream inputStream( dirFile.c_str() );
  122. if(!inputStream) {
  123. LOG_ERROR << "ERROR: Cannot open list file " << dirFile << endm;
  124. }
  125. Int_t nFile = 0;
  126. std::string file;
  127. while(getline(inputStream, file)) {
  128. if(file.find(".femtoDst.root") != std::string::npos) {
  129. TFile* ftmp = TFile::Open(file.c_str());
  130. if(ftmp && !ftmp->IsZombie() && ftmp->GetNkeys()) {
  131. LOG_INFO << " Read in femtoDst file " << file << endm;
  132. mChain->Add(file.c_str());
  133. ++nFile;
  134. } //if(ftmp && !ftmp->IsZombie() && ftmp->GetNkeys())
  135. if (ftmp) {
  136. ftmp->Close();
  137. } // if (ftmp)
  138. } //if(file.find(".femtoDst.root") != std::string::npos)
  139. } //while (getline(inputStream, file))
  140. LOG_INFO << " Total " << nFile << " files have been read in. " << endm;
  141. } //if(dirFile.find(".list") != std::string::npos || dirFile.find(".lis" != string::npos))
  142. else if(dirFile.find(".femtoDst.root") != std::string::npos) {
  143. mChain->Add( dirFile.c_str() );
  144. }
  145. else {
  146. LOG_WARN << " No good input file to read ... " << endm;
  147. }
  148. if(mChain) {
  149. setBranchAddresses(mChain);
  150. mChain->SetCacheSize(50e6);
  151. mChain->AddBranchToCache("*");
  152. mFemtoDst->set(mFemtoArrays);
  153. }
  154. }
  155. //_________________
  156. Bool_t StFemtoDstReader::readFemtoEvent(Long64_t iEvent) {
  157. // Read femtoEvent
  158. Int_t mStatusRead = true; // true - okay, false - nothing to read
  159. if (!mChain) {
  160. LOG_WARN << " No input files ... ! EXIT" << endm;
  161. mStatusRead = false;
  162. return mStatusRead;
  163. }
  164. Int_t bytes = mChain->GetEntry(mEventCounter++);
  165. Int_t nCycles = 0;
  166. while( bytes <= 0) {
  167. if( mEventCounter >= mChain->GetEntriesFast() ) {
  168. }
  169. LOG_WARN << "Encountered invalid entry or I/O error while reading event "
  170. << mEventCounter << " from \"" << mChain->GetName() << "\" input tree\n";
  171. bytes = mChain->GetEntry(mEventCounter++);
  172. nCycles++;
  173. LOG_WARN << "Not input has been found for: " << nCycles << " times" << endm;
  174. if(nCycles >= 10) {
  175. LOG_ERROR << "Terminating StFemtoDstReader::ReadProcess(Long64_t) after "
  176. << nCycles << " times!" << endm;
  177. mStatusRead = false;
  178. break;
  179. }
  180. }
  181. return mStatusRead;
  182. }