MpdGetNumEvents.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "MpdGetNumEvents.h"
  2. #include "FairRootManager.h"
  3. #include "TChain.h"
  4. #include "TString.h"
  5. #include <iostream>
  6. using namespace std;
  7. MpdLibZ::MpdLibZ(const char* filename)
  8. {
  9. fileName = filename;
  10. }
  11. MpdLibZ::~MpdLibZ()
  12. {
  13. if (file != NULL)
  14. {
  15. close();
  16. file = NULL;
  17. }
  18. }
  19. /* Opens a gzip (.gz) file for reading or writing. The mode parameter
  20. is as in fopen ("rb" or "wb") but can also include a compression level
  21. ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
  22. Huffman only compression as in "wb1h", or 'R' for run-length encoding
  23. as in "wb1R". (See the description of deflateInit2 for more information
  24. about the strategy parameter.)
  25. gzopen can be used to read a file which is not in gzip format; in this
  26. case gzread will directly read from the file without decompression.
  27. gzopen returns NULL if the file could not be opened or if there was
  28. insufficient memory to allocate the (de)compression state; errno
  29. can be checked to distinguish the two cases (if errno is zero, the
  30. zlib error is Z_MEM_ERROR). */
  31. int MpdLibZ::open(const char * mode)
  32. {
  33. file = gzopen(fileName, mode);
  34. if (file == NULL) return -1;
  35. else return 0;
  36. }
  37. /* Returns 1 when EOF has previously been detected reading the given
  38. input stream, otherwise zero. */
  39. int MpdLibZ::eof() {return gzeof(file);}
  40. /* Flushes all pending output if necessary, closes the compressed file
  41. and deallocates all the (de)compression state. The return value is the zlib
  42. error number (see function gzerror below). */
  43. int MpdLibZ::close()
  44. {
  45. int ret = gzclose(file);
  46. if (ret == 0) file = NULL;
  47. return ret;
  48. }
  49. /* Writes the given null-terminated string to the compressed file, excluding
  50. the terminating null character.
  51. gzputs returns the number of characters written, or -1 in case of error. */
  52. int MpdLibZ::puts(char *s) {return gzputs(file, s);}
  53. /* Reads bytes from the compressed file until len-1 characters are read, or
  54. a newline character is read and transferred to buf, or an end-of-file
  55. condition is encountered. The string is then terminated with a null
  56. character.
  57. gzgets returns buf, or Z_NULL in case of error. */
  58. char* MpdLibZ::gets(char* buf, int len){return gzgets(file, buf, len);}
  59. /* Reads the given number of uncompressed bytes from the compressed file.
  60. If the input file was not in gzip format, gzread copies the given number
  61. of bytes into the buffer.
  62. gzread returns the number of uncompressed bytes actually read (0 for
  63. end of file, -1 for error). */
  64. int MpdLibZ::read(voidp buf, unsigned len){return gzread(file, buf, len);}
  65. /* Writes the given number of uncompressed bytes into the compressed file.
  66. gzwrite returns the number of uncompressed bytes actually written
  67. (0 in case of error). */
  68. int MpdLibZ::write(voidpc buf, unsigned len) {return gzwrite(file, buf, len);}
  69. /* Sets the starting position for the next gzread or gzwrite on the given
  70. compressed file. The offset represents a number of bytes in the
  71. uncompressed data stream. The whence parameter is defined as in lseek(2);
  72. the value SEEK_END is not supported.
  73. If the file is opened for reading, this function is emulated but can be
  74. extremely slow. If the file is opened for writing, only forward seeks are
  75. supported; gzseek then compresses a sequence of zeroes up to the new
  76. starting position.
  77. gzseek returns the resulting offset location as measured in bytes from
  78. the beginning of the uncompressed stream, or -1 in case of error, in
  79. particular if the file is opened for writing and the new starting position
  80. would be before the current position. */
  81. off_t MpdLibZ::seek(off_t pos, int whence)
  82. {
  83. switch (whence)
  84. {
  85. case 0:
  86. return gzseek(file, pos, SEEK_CUR);
  87. case 1:
  88. return gzseek(file, pos, SEEK_SET);
  89. default:
  90. cerr<<__FILE__<<__func__<<" wrong gzseek.";
  91. return -1;
  92. }
  93. }
  94. /* Returns the starting position for the next gzread or gzwrite on the given
  95. compressed file. This position represents a number of bytes in the
  96. uncompressed data stream, and is zero when starting, even if appending or
  97. reading a gzip stream from the middle of a file using gzdopen().
  98. gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) */
  99. off_t MpdLibZ::tell(){return gztell(file);}
  100. ClassImp(MpdLibZ);
  101. bool MpdGetNumEvents::GetQGSMEventHeader(char* ss, MpdLibZ* libz, Int_t& fQGSM_format_ID)
  102. {
  103. libz->gets(ss, 250);
  104. // If end of input file is reached : close it and abort run
  105. if (libz->eof())
  106. {
  107. libz->close();
  108. return false;
  109. }
  110. bool wrong_file=false;
  111. TString tss(ss);
  112. if (tss.Contains("QGSM")) // 0
  113. {
  114. libz->gets(ss, 250);
  115. tss=ss;
  116. Int_t lines = 0;
  117. while (!(tss.Contains("particles, b,bx,by")))
  118. {
  119. if (tss.Contains("QGSM format ID"))
  120. {
  121. sscanf(ss,"QGSM format ID=%d", &fQGSM_format_ID);
  122. // correction of incorrect format_ID in some data files
  123. if (fQGSM_format_ID == 2)
  124. {
  125. off_t file_pos = libz->tell();
  126. for (int k = 0; k < 5; k++)
  127. libz->gets(ss,250);
  128. if (strlen(ss) > 90)
  129. fQGSM_format_ID = 3;
  130. libz->seek(file_pos, 0);
  131. }
  132. }
  133. libz->gets(ss, 250);
  134. tss = ss;
  135. lines++;
  136. if ((fQGSM_format_ID >= 2) && (lines >= 4)) return true;
  137. if (lines > 25)
  138. {
  139. wrong_file = true;
  140. break;
  141. }
  142. }// while (!(tss.Contains("particles, b,bx,by")))
  143. }
  144. else
  145. {
  146. if (fQGSM_format_ID >= 2) return true;
  147. if (!tss.Contains("particles, b,bx,by"))
  148. {
  149. libz->gets(ss, 250);
  150. tss=ss;
  151. }
  152. }//else
  153. if (wrong_file)
  154. {
  155. cout<<"Wrong file header! 1 "<<endl;
  156. libz->close();
  157. return false;
  158. }
  159. // 3
  160. if (!tss.Contains("event"))
  161. {
  162. if (fQGSM_format_ID >= 2) return true;
  163. else
  164. {
  165. cout<<"Wrong event header! 2 "<<endl;
  166. printf("Format id:%d \n %s \n", fQGSM_format_ID, ss);
  167. libz->close();
  168. return false;
  169. }//else
  170. }
  171. char tmp[250];
  172. switch (fQGSM_format_ID)
  173. {
  174. case 0:
  175. case 1:
  176. libz->gets(tmp, 250);
  177. break;
  178. case 2:
  179. case 3:{
  180. libz->gets(ss, 250);
  181. libz->gets(ss, 250);
  182. break;
  183. }
  184. default:
  185. break;
  186. }//switch
  187. return true;
  188. }
  189. Int_t MpdGetNumEvents::GetNumPHSDEvents(const char* filename)
  190. {
  191. MpdLibZ *libz = new MpdLibZ(filename);
  192. libz->open("rb");
  193. char fbuffer[256];
  194. Int_t fntr;
  195. Float_t fb;
  196. int num = 0;
  197. while (!libz->eof())
  198. {
  199. libz->gets(fbuffer, 256); // 1st line
  200. if (libz->eof()) return -1; // end of file reached
  201. int res=sscanf(fbuffer, "%d %*d %*d %e", &fntr, &fb);
  202. if (res != 2)
  203. {
  204. printf("selftest error in header, scan %d of 2\n", res);
  205. return -1;
  206. }
  207. libz->gets(fbuffer,256); // 2nd line
  208. if (libz->eof())
  209. {
  210. printf("unexpected end of file\n");
  211. return -1;
  212. }
  213. for(Int_t i = 1; i <= fntr; i++)
  214. {
  215. /* read track */
  216. libz->gets(fbuffer,256);
  217. if (libz->eof())
  218. {
  219. printf("unexpected end of file\n");
  220. return -1;
  221. }// if
  222. }// for
  223. num++;
  224. }// while (!libz->eof())
  225. libz->close();
  226. delete libz;
  227. cout<<"The total number of events in the PHSD file will be processed - "<<num<<endl;
  228. return num;
  229. }
  230. Int_t MpdGetNumEvents::GetNumQGSMEvents(const char* fileName)
  231. {
  232. Int_t fQGSM_format_ID = 0;
  233. MpdLibZ* libz = new MpdLibZ(fileName);
  234. libz->open("rb");
  235. char ss[252];
  236. TString sss = ss;
  237. Int_t eventId = 0, nTracks = 0, num = 0;
  238. while (GetQGSMEventHeader(ss, libz, fQGSM_format_ID))
  239. {
  240. num++;
  241. sss = ss;
  242. // read event
  243. sscanf(ss, " %d %d", &eventId, &nTracks);
  244. // Loop over tracks in the current event
  245. for (Int_t itrack=0; itrack < nTracks; itrack++)
  246. libz->gets(ss,250);
  247. }
  248. libz->close();
  249. delete libz;
  250. cout<<"The total number of events in the QGSM file will be processed - "<<num<<endl;
  251. return num;
  252. }
  253. Int_t MpdGetNumEvents::GetNumURQMDEvents(const char* fileName)
  254. {
  255. MpdLibZ *libz = new MpdLibZ(fileName);
  256. libz->open("rb");
  257. char read[200];
  258. int ntracks, num = 0;
  259. while (!libz->eof())
  260. {
  261. // ---> Read and check first event header line from input file
  262. libz->gets(read, 200);
  263. Int_t urqmdVersion = 0;
  264. sscanf(read, "UQMD version: %d 1000 %d output_file 14", &urqmdVersion, &urqmdVersion);
  265. if (read[0] != 'U')
  266. {
  267. cout<<"Wrong event header"<<endl;
  268. return -1;
  269. }
  270. // ---> Read rest of event header
  271. for (int iline = 0; iline < ((urqmdVersion == 30400) ? 16 : 13); iline++)
  272. libz->gets(read, 200);
  273. libz->gets(read, 200);
  274. sscanf(read, "%d", &ntracks);
  275. libz->gets(read, 200);
  276. for(int itrack=0; itrack < ntracks; itrack++)
  277. libz->gets(read, 200);
  278. num++;
  279. }
  280. libz->close();
  281. delete libz;
  282. cout<<"The total number of events in the UrQMD file will be processed - "<<num<<endl;
  283. return num;
  284. }
  285. Int_t MpdGetNumEvents::GetNumDCMSMMEvents(const char* fileName)
  286. {
  287. MpdLibZ *libz = new MpdLibZ(fileName);
  288. libz->open("rb");
  289. char r200[200];
  290. for( Int_t i=0; i<3; i++) libz->gets(r200, 200);
  291. char read[80];
  292. int ntracks, num = 0;
  293. Int_t evnr=0;
  294. while (!libz->eof())
  295. {
  296. libz->gets(read, 80);
  297. sscanf(read, "%d", &evnr);
  298. if( evnr-num != 1) break; // otherwise it gives 1 more event after eof
  299. for( Int_t ibeam=0; ibeam<3; ibeam++) {
  300. libz->gets(read, 80);
  301. sscanf(read, "%d", &ntracks);
  302. for( Int_t i=0; i<ntracks; i++) libz->gets(read, 80);
  303. }
  304. num++;
  305. }
  306. libz->close();
  307. delete libz;
  308. cout<<"The total number of events in the DCMSMM file will be processed - "<<num<<endl;
  309. return num;
  310. }
  311. Int_t MpdGetNumEvents::GetNumROOTEvents(const char* filename)
  312. {
  313. TChain* fileTree = new TChain(FairRootManager::GetTreeName());
  314. fileTree->Add(filename);
  315. Int_t num = fileTree->GetEntries();
  316. delete fileTree;
  317. cout<<"The total number of events in the ROOT file will be processed - "<<num<<endl;
  318. return num;
  319. }
  320. ClassImp(MpdGetNumEvents);