oscar97merger.C 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <stdio.h>
  2. #include <TH1F.h>
  3. #include <math.h>
  4. #include <TFile.h>
  5. #define BFSZ 0xFF
  6. FILE *nextF19(FILE *fList);
  7. void oscar97merger(const char *inF19 = "f19.list",
  8. const char *outF19 = "OutSum.f19") {
  9. char buf[BFSZ] = {0};
  10. FILE *fList = fopen(inF19, "r");
  11. if (!fList) {
  12. printf("Can't open list file %s\nExiting...\n", inF19);
  13. return;
  14. }
  15. FILE *fOut = fopen(outF19, "w");
  16. if (!fOut) {
  17. printf("Can't create output file %s\nExiting...\n", outF19);
  18. return;
  19. }
  20. bool firstFile = true;
  21. int evNum, nTracks, realEvNum = 0, trRd = 0;
  22. float imp, planeRot;
  23. bool readEvent = true;
  24. while (!feof(fList)) {
  25. FILE *f19 = nextF19(fList);
  26. if (!f19)
  27. continue;
  28. //
  29. // Read header (first 3 lines) of the
  30. // first f19 file
  31. //
  32. if (firstFile) {
  33. fgets(buf, BFSZ, f19);
  34. fputs(buf, fOut);
  35. fgets(buf, BFSZ, f19);
  36. fputs(buf, fOut);
  37. fgets(buf, BFSZ, f19);
  38. fputs(buf, fOut);
  39. firstFile = false;
  40. }
  41. else {
  42. //
  43. // skip three useless lines
  44. //
  45. fgets(buf, BFSZ, f19);
  46. fgets(buf, BFSZ, f19);
  47. fgets(buf, BFSZ, f19);
  48. }
  49. //
  50. // Read events and tracks
  51. //
  52. while (!feof(f19)) {
  53. if (readEvent) {
  54. int ret = fscanf(f19, "%i %i %f %f", &evNum, &nTracks, &imp, &planeRot);
  55. if (ret == 4) {
  56. /*
  57. printf("EVENT INFO:\n\t ID = %i, NTRACKS = %i, IMP = %f, PLANEROT = %f, REAL_EVENT_NUM = %i\n",
  58. evNum, nTracks, imp, planeRot, realEvNum);
  59. */
  60. readEvent = false;
  61. realEvNum++;
  62. fprintf(fOut, "%10i %10i %8.3f %8.3f\n", realEvNum, nTracks, imp, planeRot);
  63. trRd = 0;
  64. fgets(buf, BFSZ, f19); // read \n
  65. }
  66. else // if can't read something like event, skip that line
  67. fgets(buf, BFSZ, f19);
  68. }
  69. else {
  70. //
  71. // Read tracks from an event
  72. //
  73. fgets(buf, BFSZ, f19);
  74. fputs(buf, fOut);
  75. trRd++;
  76. if (trRd == nTracks)
  77. readEvent = true;
  78. }
  79. }
  80. fclose(f19);
  81. }
  82. fclose(fOut);
  83. fclose(fList);
  84. }
  85. FILE *nextF19(FILE *fList) {
  86. char buf[BFSZ] = {0};
  87. if (fgets(buf, BFSZ, fList)) {
  88. buf[strlen(buf) - 1] = '\0';
  89. FILE *f19 = fopen(buf, "r");
  90. if (!f19)
  91. printf("Can't open %s\nSkipping...\n", buf);
  92. else
  93. printf("File: %s\n", buf);
  94. return f19;
  95. }
  96. else {
  97. return 0;
  98. }
  99. }