UManager.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <iostream>
  2. using namespace std;
  3. #include "TFile.h"
  4. #include "TTree.h"
  5. #include "TList.h"
  6. #include "UTask.h"
  7. #include "URun.h"
  8. #include "UEvent.h"
  9. #include "UManager.h"
  10. ClassImp(UManager)
  11. //____________________________________________________________________
  12. //
  13. // UManager
  14. //
  15. // This class is the singleton manager for organising I/O and task
  16. // execution.
  17. // SetInputFile and SetOutputFile are used to set the input and
  18. // output files.
  19. // User tasks, which are derived from the UTask class, with
  20. // implemented Init and Exec functions, are to be added using
  21. // AddTask function.
  22. // Init and Run functions make initialisation and execution
  23. //
  24. UManager *UManager::fgInstance = NULL;
  25. //--------------------------------------------------------------------
  26. UManager::UManager()
  27. {
  28. // Default constructor
  29. if(NULL != fgInstance) {
  30. Fatal("UManager", "Singleton class instantiated twice!");
  31. }
  32. fgInstance = this;
  33. fInFile = nullptr;
  34. fOutFile = nullptr;
  35. fInTree = nullptr;
  36. fOutTree = nullptr;
  37. fEvent = nullptr;
  38. fRun = nullptr;
  39. fTaskList = new TList();
  40. fWrite = kTRUE;
  41. }
  42. //--------------------------------------------------------------------
  43. //--------------------------------------------------------------------
  44. UManager::~UManager()
  45. {
  46. // Destructor
  47. fgInstance = nullptr;
  48. CloseInputFile();
  49. CloseOutputFile();
  50. fInTree = nullptr;
  51. fOutTree = nullptr;
  52. fEvent = nullptr;
  53. fRun = nullptr;
  54. delete fTaskList;
  55. }
  56. //--------------------------------------------------------------------
  57. //--------------------------------------------------------------------
  58. UManager* UManager::Instance()
  59. {
  60. // Get instance of singleton manager
  61. return fgInstance;
  62. }
  63. //--------------------------------------------------------------------
  64. //--------------------------------------------------------------------
  65. void UManager::SetInputFile(const char *name)
  66. {
  67. // Open the input file and get the run description and tree of events,
  68. // which later can be accessed by GetRun and GetEvent respectively.
  69. // name - name of the input file
  70. fInFile = TFile::Open(name);
  71. if(nullptr == fInFile) {
  72. Fatal("SetInputFile", "Input file does not exist!");
  73. }
  74. fRun = (URun*) fInFile->Get("run");
  75. fInTree = (TTree*) fInFile->Get("events");
  76. if(nullptr == fInTree) {
  77. Fatal("SetInputFile", "Input file has no events tree!");
  78. }
  79. cout << "-I- UManager : Input file has " << fInTree->GetEntries() << " events"
  80. << endl;
  81. if(nullptr == fEvent) {
  82. fEvent = new UEvent();
  83. }
  84. fInTree->SetBranchAddress("event", &fEvent);
  85. }
  86. //--------------------------------------------------------------------
  87. //--------------------------------------------------------------------
  88. void UManager::CloseInputFile()
  89. {
  90. // Close the input file
  91. if(nullptr != fInFile) {
  92. fInFile->Close();
  93. fInFile = nullptr;
  94. }
  95. }
  96. //--------------------------------------------------------------------
  97. //--------------------------------------------------------------------
  98. void UManager::SetOutputFile(const char *name, Bool_t writeTree)
  99. {
  100. // Open the output file. Create the output tree with event branch.
  101. // name - name of the output file
  102. fOutFile = TFile::Open(name, "RECREATE");
  103. if(fOutFile == nullptr) {
  104. Fatal("SetOutputFile", "Output file can not be created!");
  105. }
  106. if(writeTree) {
  107. // Added by Grigory Nigmatkulov
  108. fOutFile->SetCompressionLevel(9);
  109. int bufsize = 65536;
  110. int split = 99;
  111. //fOutTree = new TTree("events", "Event data");
  112. fOutTree = new TTree("events", "Event data", split);
  113. fOutTree->SetAutoSave(1000000);
  114. if(fEvent == nullptr) {
  115. fEvent = new UEvent();
  116. }
  117. //fOutTree->Branch("event", &fEvent);
  118. fOutTree->Branch("event", &fEvent, bufsize, split);
  119. }
  120. }
  121. //--------------------------------------------------------------------
  122. void UManager::CloseOutputFile()
  123. {
  124. // Close the output file
  125. if(nullptr != fOutFile) {
  126. fOutFile->Close();
  127. fOutFile = nullptr;
  128. }
  129. }
  130. //--------------------------------------------------------------------
  131. Int_t UManager::GetEntries()
  132. {
  133. // Get the number of entries in input tree
  134. if(nullptr == fInTree) return 0;
  135. return fInTree->GetEntries();
  136. }
  137. //--------------------------------------------------------------------
  138. void UManager::GetEntry(Int_t i)
  139. {
  140. // Get entry from the input tree.
  141. // i - index of entry
  142. if(nullptr == fInTree) return;
  143. fInTree->GetEntry(i);
  144. }
  145. //--------------------------------------------------------------------
  146. void UManager::Fill()
  147. {
  148. // Fill the output tree
  149. if(nullptr != fOutTree) {
  150. if(fWrite) {
  151. fOutTree->Fill();
  152. } else {
  153. fWrite = kTRUE;
  154. }
  155. }
  156. }
  157. //--------------------------------------------------------------------
  158. void UManager::AddTask(UTask *task) {
  159. // Add task to the task list.
  160. // task - pointer to the task
  161. fTaskList->Add(task);
  162. }
  163. //--------------------------------------------------------------------
  164. void UManager::createRun(const char* generator, const char* comment,
  165. const Int_t& aProj, const Int_t& zProj, const Double_t& pProj,
  166. const Int_t& aTarg, const Int_t& zTarg, const Double_t& pTarg,
  167. const Double_t& bMin, const Double_t& bMax, const Int_t& bWeight,
  168. const Double_t& phiMin, const Double_t& phiMax,
  169. const Double_t& sigma, const Int_t& nEvents) {
  170. // Creates the run description, which later can be accessed by GetRun method
  171. fRun = new URun(generator, comment,
  172. aProj, zProj, pProj, aTarg, zTarg, pTarg,
  173. bMin, bMax, bWeight, phiMin, phiMax, sigma, nEvents);
  174. }
  175. //--------------------------------------------------------------------
  176. void UManager::Init()
  177. {
  178. // Run initialisation of the tasks
  179. UTask *task;
  180. for(Int_t i = 0; i < fTaskList->GetSize(); i++) {
  181. task = (UTask*) fTaskList->At(i);
  182. task->Init();
  183. }
  184. }
  185. //--------------------------------------------------------------------
  186. void UManager::Run(Int_t a, Int_t b)
  187. {
  188. // Execute the tasks.
  189. // Run() - process all events from input tree.
  190. // Run(i) - process only i-th event
  191. // Run(i,j) - process events from i-th to j-th
  192. if(nullptr==fInTree && nullptr==fOutTree) return;
  193. UTask *task;
  194. if(nullptr==fInTree) {
  195. for(Int_t i = 0; i < a; i++) {
  196. for(Int_t i = 0; i < fTaskList->GetSize(); i++) {
  197. task = (UTask*) fTaskList->At(i);
  198. task->Exec();
  199. }
  200. Fill();
  201. }
  202. } else {
  203. if(-1==a && -1==b) {
  204. for(Int_t iEv = 0; iEv < GetEntries(); iEv++) {
  205. GetEntry(iEv);
  206. for(Int_t i = 0; i < fTaskList->GetSize(); i++) {
  207. task = (UTask*) fTaskList->At(i);
  208. task->Exec();
  209. }
  210. Fill();
  211. }
  212. } else if(a>-1 && -1==b) {
  213. GetEntry(a);
  214. for(Int_t i = 0; i < fTaskList->GetSize(); i++) {
  215. task = (UTask*) fTaskList->At(i);
  216. task->Exec();
  217. }
  218. Fill();
  219. } else {
  220. for(Int_t iEv = a; iEv < b; iEv++) {
  221. GetEntry(iEv);
  222. for(Int_t i = 0; i < fTaskList->GetSize(); i++) {
  223. task = (UTask*) fTaskList->At(i);
  224. task->Exec();
  225. }
  226. Fill();
  227. }
  228. }
  229. }
  230. for(Int_t i = 0; i < fTaskList->GetSize(); i++) {
  231. task = (UTask*) fTaskList->At(i);
  232. task->Finish();
  233. }
  234. if(nullptr != fOutFile) {
  235. if(nullptr != fOutTree) {
  236. fOutFile->cd();
  237. fOutTree->Write();
  238. }
  239. if(nullptr != fRun) {
  240. fOutFile->cd();
  241. fRun->Write();
  242. }
  243. }
  244. }