StHbtTTreeReader.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /***************************************************************************
  2. *
  3. * $Id: StHbtTTreeReader.cxx,v 1.8 2003/09/02 17:58:34 perev Exp $
  4. *
  5. * Author: Frank Laue, BNL, laue@bnl.gov
  6. ***************************************************************************/
  7. #include "StHbtMaker/Reader/StHbtTTreeReader.h"
  8. #include "StChain.h"
  9. #include "TChain.h"
  10. #include "TFile.h"
  11. #include "TTree.h"
  12. #include "StIOMaker/StIOMaker.h"
  13. #include "StEvent.h"
  14. #include "StEventTypes.h"
  15. #include <math.h>
  16. #include <string>
  17. #include <typeinfo>
  18. #include "SystemOfUnits.h" // has "tesla" in it
  19. #include "StHbtMaker/Infrastructure/StExceptions.hh"
  20. #include "StHbtMaker/Infrastructure/StHbtTrackCollection.hh"
  21. #include "StHbtMaker/Infrastructure/StHbtV0Collection.hh"
  22. #include "StHbtMaker/Infrastructure/StHbtXiCollection.hh"
  23. #include "StHbtMaker/Reader/StHbtGstarTxtReader.h"
  24. #include "StHbtMaker/Infrastructure/StHbtEvent.hh"
  25. #include "StHbtMaker/Base/StHbtEventCut.h"
  26. #include "StHbtMaker/Base/StHbtTrackCut.h"
  27. #include "StHbtMaker/Base/StHbtV0Cut.h"
  28. #include "StHbtMaker/Base/StHbtXiCut.h"
  29. #include "StHbtMaker/Base/StHbtKinkCut.h"
  30. #include "StStrangeMuDstMaker/StStrangeMuDstMaker.h"
  31. #include "StStrangeMuDstMaker/StV0MuDst.hh"
  32. #include "StStrangeMuDstMaker/StXiMuDst.hh"
  33. #include "StStrangeMuDstMaker/StKinkMuDst.hh"
  34. #include "StHbtMaker/Infrastructure/StHbtTTreeEvent.h"
  35. #include "StarClassLibrary/StMemoryInfo.hh"
  36. #ifdef __ROOT__
  37. ClassImp(StHbtTTreeReader)
  38. #endif
  39. #if !(ST_NO_NAMESPACES)
  40. using namespace units;
  41. #endif
  42. //__________________
  43. StHbtTTreeReader::StHbtTTreeReader(StHbtIOMode mode, StIOMaker* io,
  44. const char* dirName, const char* fileName, const char* extention,
  45. const char* filter, int maxFiles)
  46. : mIOMaker(io), mTrackType(primary), mIOMode(mode), mMaxFiles(maxFiles), mDebug(0), mCurrentFile(0),
  47. mTTree(0), split(99), comp(2), bufsize(256000/4) {
  48. if (mDebug) cout << "StHbtTTreeReader::StHbtTTreeReader(...)"<< endl;
  49. mDir = string(dirName);
  50. mFile = string(fileName);
  51. mExtention = string(extention);
  52. mFilter = string(filter);
  53. mReaderStatus = 0; // "good"
  54. mHbtTTreeEvent = new StHbtTTreeEvent();
  55. if (mDebug) cout << "StHbtTTreeReader::StHbtTTreeReader(...) - leaving"<< endl;
  56. }
  57. //__________________
  58. StHbtTTreeReader::~StHbtTTreeReader(){
  59. if (mIOMode==hbtWrite && mCurrentFile && mTTree) { mTTree->AutoSave(); mTTree=0;}
  60. if (mCurrentFile) { mCurrentFile->Close(); delete mCurrentFile; mCurrentFile = 0;}
  61. if (mEventCut) delete mEventCut;
  62. if (mTrackCut) delete mTrackCut;
  63. if (mV0Cut) delete mV0Cut;
  64. if (mXiCut) delete mXiCut;
  65. if (mKinkCut) delete mKinkCut;
  66. }
  67. //__________________
  68. StHbtString StHbtTTreeReader::Report(){
  69. StHbtString temp = "\n This is the StHbtTTreeReader\n";
  70. char ccc[100];
  71. sprintf(ccc," Track type is %d\n",mTrackType);
  72. temp += ccc;
  73. temp += "---> EventCuts in Reader: ";
  74. if (mEventCut) {
  75. temp += mEventCut->Report();
  76. }
  77. else {
  78. temp += "NONE";
  79. }
  80. temp += "\n---> TrackCuts in Reader: ";
  81. if (mTrackCut) {
  82. temp += mTrackCut->Report();
  83. }
  84. else {
  85. temp += "NONE";
  86. }
  87. temp += "\n---> V0Cuts in Reader: ";
  88. if (mV0Cut) {
  89. temp += mV0Cut->Report();
  90. }
  91. else {
  92. temp += "NONE";
  93. }
  94. temp += "\n---> XiCuts in Reader: ";
  95. if (mXiCut) {
  96. temp += mXiCut->Report();
  97. }
  98. else {
  99. temp += "NONE";
  100. }
  101. temp += "\n---> KinkCuts in Reader: ";
  102. if (mKinkCut) {
  103. temp += mKinkCut->Report();
  104. }
  105. else {
  106. temp += "NONE";
  107. }
  108. temp += "\n";
  109. return temp;
  110. }
  111. //__________________
  112. StHbtEvent* StHbtTTreeReader::ReturnHbtEvent(){
  113. if (mDebug) cout << "StHbtTTreeReader::ReturnHbtEvent()" << endl;
  114. StHbtEvent* hbtEvent = 0;
  115. try {
  116. if (mIOMode == hbtRead) hbtEvent = read();
  117. }
  118. catch(StExceptionEOF e) {
  119. e.print();
  120. mReaderStatus = 2;
  121. return 0;
  122. }
  123. catch(StException e) {
  124. e.print();
  125. mReaderStatus = 1;
  126. return 0;
  127. }
  128. if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
  129. if (!hbtEvent) cout << "StHbtTTreeReader::ReturnHbtEvent() - no hbtEvent" << endl;
  130. if (mEventCut && hbtEvent ) {
  131. if ( mEventCut->Pass(hbtEvent)==0 ) {
  132. delete hbtEvent;
  133. hbtEvent=0;
  134. }
  135. }
  136. return hbtEvent;
  137. }
  138. //__________________
  139. int StHbtTTreeReader::WriteHbtEvent(StHbtEvent* event){
  140. if (mDebug) cout << "StHbtTTreeReader::WriteHbtEvent()" << endl;
  141. StHbtEvent* hbtEvent = 0;
  142. try {
  143. if (mIOMode == hbtWrite) hbtEvent = write(event);
  144. }
  145. catch(StExceptionEOF e) {
  146. e.print();
  147. return 1;
  148. }
  149. catch(StException e) {
  150. e.print();
  151. return 0;
  152. }
  153. if (hbtEvent && mDebug>1) cout << *hbtEvent << endl;
  154. if (!hbtEvent) cout << "StHbtTTreeReader::WriteHbtEvent() - no hbtEvent" << endl;
  155. return 0;
  156. }
  157. //__________________
  158. StHbtEvent* StHbtTTreeReader::write(StHbtEvent* event){
  159. /////////////////
  160. // get StEvent //
  161. /////////////////
  162. if (!event){
  163. if (mDebug) cout << "StHbtTTreeReader::write() - No StHbtEvent!!! " << endl;
  164. return 0;
  165. }
  166. string ioMakerFileName;
  167. if (mIOMaker) { // if the ioMaker is specified, we take the output filename from the ioMaker
  168. ioMakerFileName = string(mIOMaker->GetFile());
  169. }
  170. else { // if not then construct the filename
  171. ioMakerFileName = mDir+mFile;
  172. }
  173. if (!mCurrentFile) initWrite(ioMakerFileName,mExtention); // if no file open, open the file
  174. if (mCurrentFileName != ioMakerFileName) { // if wrong file open, close file and open new file
  175. uninitWrite();
  176. initWrite(ioMakerFileName,mExtention);
  177. }
  178. if (!mCurrentFile) {
  179. if (mDebug) cout << "StHbtTTreeReader::write() - can not open file!!! " << endl;
  180. return 0;
  181. }
  182. if (mEventCut->Pass(event)) {
  183. try{
  184. mHbtTTreeEvent->clear();
  185. mHbtTTreeEvent->fill(event, mTrackCut, mV0Cut, mXiCut, mKinkCut);
  186. if (mDebug) cout << "StHbtTTreeReader::write() - now fill the tree " << endl;
  187. mTTree->Fill();
  188. }
  189. catch(StException e) {
  190. mHbtTTreeEvent =0;
  191. e.print();
  192. }
  193. }
  194. if (mDebug) cout << "StHbtTTreeReader::write() - leaving " << endl;
  195. return event;
  196. }
  197. int StHbtTTreeReader::initWrite(string ioMakerFileName, string extention){
  198. if (split) bufsize /= 4;
  199. string fileName = ioMakerFileName;
  200. while ( fileName.find("/") != string::npos ) {
  201. string::size_type pos = fileName.find("/");
  202. fileName.erase(0, pos+1 );
  203. }
  204. fileName = mDir +fileName + extention;
  205. if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - new fileName: " << fileName.c_str() << endl;
  206. // creat a Picoevent and and output file
  207. if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create file " << endl;
  208. mCurrentFile = new TFile(fileName.c_str(),"RECREATE","hbtTTreeMuDst");
  209. if (!mCurrentFile) {
  210. cout << "StHbtTTreeReader::initWrite(...) - Warning: no file opened = " << endl;
  211. assert(0);
  212. }
  213. //mCurrentFile->SetFormat(1);
  214. mCurrentFile->SetCompressionLevel(comp);
  215. // Create a ROOT Tree and one superbranch
  216. if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create tree " << endl;
  217. mTTree = new TTree("Tree", "hbtTTree",split);
  218. if (!mTTree) {
  219. cout << "StHbtTTreeReader::initWrite(...) - Warning: No Tree" << endl;
  220. assert(0);
  221. }
  222. mTTree->SetAutoSave(1000000); // autosave when 1 Mbyte written
  223. //mTTree->SetAutoSave(1000); // autosave when 1 Mbyte written
  224. if (mDebug) cout << "StHbtTTreeReader::initWrite(...) - now create branch " << endl;
  225. mTTree->Branch("mHbtTTreeEvent", "StHbtTTreeEvent", &mHbtTTreeEvent, bufsize, split);
  226. mCurrentFileName = ioMakerFileName;
  227. return 0;
  228. }
  229. int StHbtTTreeReader::uninitWrite(){
  230. mTTree->AutoSave();
  231. mCurrentFile->Close();
  232. if (mCurrentFile) delete mCurrentFile;
  233. if (mTTree) delete mTTree;
  234. mTTree = 0;
  235. mCurrentFile = 0;
  236. return 0;
  237. }
  238. StHbtEvent* StHbtTTreeReader::read(){
  239. if (!mTChain) {
  240. try {
  241. cout << initRead(mDir,mFile,mFilter,mExtention,mMaxFiles) << " files to analyse " << endl;
  242. }
  243. catch(StException e) {
  244. e.print();
  245. return 0;
  246. }
  247. }
  248. unsigned int nEvents = (unsigned int)mTChain->GetEntries();
  249. if (!nEvents) throw StException("StHbtTTreeReader::read() - no events to read ");
  250. mHbtTTreeEvent->clear();
  251. int iBytes= mTChain->GetEntry(mEventIndex++);
  252. if (mDebug) cout << "StHbtTTreeReader::read() - bytes read :" << iBytes << endl;
  253. if (nEvents<mEventIndex) throw StExceptionEOF("StHbtTTreeReader::read()");
  254. if (!iBytes) throw StException("StHbtTTreeReader::read() - no event ");
  255. StHbtEvent* hbtEvent = new StHbtEvent(mHbtTTreeEvent); // ok, here we got event read from the file
  256. // now we should use the copy constructor for StHbtEvent in order to appy the cuts
  257. return hbtEvent;
  258. }
  259. int StHbtTTreeReader::initRead(string dir, string file, string filter, string extention, int mMaxFiles){
  260. mEventIndex =0;
  261. mTChain = new TChain("Tree","hbtTree");
  262. int nFiles =0;
  263. if (file!="") { // if a filename was given
  264. if( strstr(file.c_str(),".lis") ) { // if a file list is specified
  265. try {
  266. nFiles = fillChain(mTChain, (dir+file).c_str(), mMaxFiles);
  267. }
  268. catch(StException e) {
  269. throw e;
  270. }
  271. }
  272. else { // a single file was specified
  273. mTChain->Add((dir+file).c_str());
  274. nFiles++;
  275. }
  276. }
  277. else {
  278. try {
  279. nFiles = fillChain(mTChain,dir.c_str(), filter.c_str(), extention.c_str(), mMaxFiles);
  280. }
  281. catch(StException e) {
  282. throw e;
  283. }
  284. } mTChain->SetBranchAddress("mHbtTTreeEvent",&mHbtTTreeEvent); // set the address where to read the event object
  285. return nFiles;
  286. }
  287. int StHbtTTreeReader::uninitRead(){
  288. if (mHbtTTreeEvent) delete mHbtTTreeEvent;
  289. if (mTChain) delete mTChain;
  290. mHbtTTreeEvent = 0;
  291. mTChain = 0;
  292. return 0;
  293. }
  294. int StHbtTTreeReader::fillChain(TChain* chain, const char* fileList, const int maxFiles) {
  295. ifstream* inputStream = new ifstream;
  296. inputStream->open(fileList);
  297. if (!(inputStream)) throw StException("StHbtTTreeReader::fillChain(string dir) - can not open directory");
  298. char* temp;
  299. int count=0;
  300. if (mDebug>1) cout << " StHbtTTreeReader::fillChain(...)- inputStream->good() : " << inputStream->good() << endl;
  301. for (;inputStream->good();) {
  302. temp = new char[200];
  303. inputStream->getline(temp,200);
  304. if (mDebug) cout << temp << endl;
  305. chain->Add(temp);
  306. delete temp;
  307. ++count;
  308. if (count>maxFiles) break;
  309. }
  310. delete inputStream;
  311. if (mDebug) cout << "StHbtTTreeReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
  312. return count;
  313. }
  314. int StHbtTTreeReader::fillChain(TChain* chain, const char* dir, const char* filter, const char* extention, const int maxFiles) {
  315. // read directory
  316. void *pDir = gSystem->OpenDirectory(dir);
  317. if(!pDir) throw StException("StHbtTTreeReader::fillChain(string dir) - can not open directory");
  318. // now find the files that end in the specified searchString
  319. const char* fileName(0);
  320. int count(0);
  321. while((fileName = gSystem->GetDirEntry(pDir))){
  322. if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
  323. if(strstr(fileName,filter) && strstr(fileName,extention)) { // found a match
  324. char* fullFile = gSystem->ConcatFileName(dir,fileName);
  325. // add it to the chain
  326. cout << "StHbtTTreeReader::fillChain(string dir) - Adding " << fullFile << " to the chain" << endl;
  327. chain->Add(fullFile);
  328. delete fullFile;
  329. ++count;
  330. if (count>maxFiles) break;
  331. }
  332. }
  333. cout << "StHbtTTreeReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
  334. return count;
  335. }
  336. /***************************************************************************
  337. *
  338. * $Log: StHbtTTreeReader.cxx,v $
  339. * Revision 1.8 2003/09/02 17:58:34 perev
  340. * gcc 3.2 updates + WarnOff
  341. *
  342. * Revision 1.7 2003/05/16 21:30:18 magestro
  343. * Removed obsolete include file
  344. *
  345. * Revision 1.6 2003/05/07 20:05:25 magestro
  346. * Removed StFlowTagMaker.h include
  347. *
  348. * Revision 1.5 2001/12/05 14:42:17 laue
  349. * updated for trigger(action)word and l3TriggerAlgorithm
  350. *
  351. * Revision 1.1 2001/06/21 19:18:42 laue
  352. * Modified Files: (to match the changed base classes)
  353. * StHbtAsciiReader.cxx StHbtAsciiReader.h
  354. * StHbtAssociationReader.cxx StHbtAssociationReader.h
  355. * StHbtBinaryReader.cxx StHbtBinaryReader.h
  356. * StHbtGstarTxtReader.cxx StHbtGstarTxtReader.h
  357. * StHbtStrangeMuDstEventReader.cxx
  358. * StHbtStrangeMuDstEventReader.h StStandardHbtEventReader.cxx
  359. * Added Files: new reader
  360. * StHbtTTreeReader.cxx StHbtTTreeReader.h
  361. *
  362. *
  363. **************************************************************************/