00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <sys/time.h>
00013 #include <assert.h>
00014 #include <signal.h>
00015 #include <errno.h>
00016
00017
00018 #include "TMidasEvent.h"
00019 #include "TMidasFile.h"
00020
00021 #include <vector>
00022
00023 int gEventCutoff = 0;
00024
00025
00026 void HandleMidasEvent(TMidasEvent& event)
00027 {
00028 int eventId = event.GetEventId();
00029 int eventSN = event.GetSerialNumber();
00030
00031
00032
00033
00034
00035 const char* fBank = "OMCM" ;
00036 int bankLength = 0;
00037 int bankType = 0;
00038 void *pdata = 0;
00039
00040 int found = event.FindBank(fBank, &bankLength, &bankType, &pdata);
00041 if(found)
00042 {
00043 if (0) {
00044 printf("Dumping bank %s in hex\n",fBank);
00045 for (int j = 0; j < bankLength; j++)
00046 printf("0x%08x%c", ((uint32_t*)pdata)[j], (j%10==9)?'\n':' ');
00047 printf("\n");
00048 printf("Dumping bank %s in dec\n",fBank);
00049 for (int j = 0; j < bankLength; j++)
00050 printf(" %8d%c", ((uint32_t*)pdata)[j], (j%10==9)?'\n':' ');
00051 printf("\n");
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 int eventNum = ((uint32_t*)pdata)[1];
00075 int spillNum = ((uint32_t*)pdata)[2] & 0xFFFF;
00076 int trigBits = (((uint32_t*)pdata)[2] >> 16) & 0xFFFF;
00077 const char* trigType = "";
00078 int trigBeam = 1;
00079 switch (trigBits)
00080 {
00081 case 1:
00082 trigType = "Beam spill";
00083 break;
00084 }
00085 if (trigBits == trigBeam) {
00086
00087 printf(" eventNum %d, spillNum %d, trigBits 0x%04x, %s\n",eventNum,spillNum,trigBits,trigType);
00088 }
00089 } else {
00090 printf("Can't find bank %s\n",fBank);
00091 }
00092
00093
00094 }
00095
00096 #if 0
00097 int SetOutputFile(const char*foutname)
00098 {
00099 TMidasFile f;
00100 bool tryOpen = f.OutOpen(foutname);
00101
00102 if (!tryOpen)
00103 {
00104 printf("Cannot open output file \"%s\"\n",foutname);
00105 printf("Last error is %d, %s\n",f.GetLastErrno(),f.GetLastError());
00106 printf("Exiting program\n");
00107 exit (1);
00108 }
00109 return 0;
00110 }
00111 #endif
00112
00113 int ProcessMidasFile(const char* fname, const char* foutname)
00114 {
00115 TMidasFile f;
00116 TMidasFile fout;
00117
00118 bool tryOpen = f.Open(fname);
00119
00120 if (!tryOpen)
00121 {
00122 printf("Cannot open input file \"%s\"\n",fname);
00123 return -1;
00124 }
00125
00126 bool tryOutOpen = fout.OutOpen(foutname);
00127
00128 if (!tryOutOpen)
00129 {
00130 printf("Cannot open output file \"%s\"\n",foutname);
00131 printf("Last error is %d, %s\n",f.GetLastErrno(),f.GetLastError());
00132 printf("Exiting program\n");
00133 exit (1);
00134 }
00135
00136 int i=0;
00137 while (1)
00138 {
00139 TMidasEvent event;
00140 if (!f.Read(&event))
00141 break;
00142
00143 int eventId = event.GetEventId();
00144 printf("Have an event of type %d\n",eventId);
00145
00146 if ((eventId & 0xFFFF) == 0x8000)
00147 {
00148
00149 event.Print();
00150 }
00151 else if ((eventId & 0xFFFF) == 0x8001)
00152 {
00153
00154 event.Print();
00155 }
00156 else
00157 {
00158 event.SetBankList();
00159
00160 HandleMidasEvent(event);
00161 }
00162 if((i%500)==0)
00163 {
00164 printf("Processing event %d\n",i);
00165 }
00166
00167 i++;
00168 if ((gEventCutoff!=0)&&(i>=gEventCutoff))
00169 {
00170 printf("Reached event %d, exiting loop.\n", i);
00171 break;
00172 }
00173
00174 if (!fout.Write(&event)) {
00175 printf("Error writing event to output\n");
00176 break;
00177 }
00178 printf("Event written to out\n");
00179
00180 }
00181
00182 f.Close();
00183 fout.OutClose();
00184
00185 return 0;
00186 }
00187
00188
00189 void help()
00190 {
00191 printf("\nUsage:\n");
00192 printf("\n./event_skim.exe [-h] [-eMaxEvents] [file1 file2 ...]\n");
00193 printf("\n");
00194 printf("\t-h: print this help message\n");
00195 printf("\t-e: Number of events to read from input data files\n");
00196 printf("\t-o: Generate output file \n");
00197 printf("\n");
00198 printf("Example1: print events from file: ./event_skim.exe /data/alpha/current/run00500.mid.gz\n");
00199 printf("Example2: output selected events: ./event_skim.exe -o skim_00500.mid.gz /data/alpha/current/run00500.mid.gz\n");
00200 exit(1);
00201 }
00202
00203
00204
00205 int main(int argc, char *argv[])
00206 {
00207 setbuf(stdout,NULL);
00208 setbuf(stderr,NULL);
00209
00210 signal(SIGILL, SIG_DFL);
00211 signal(SIGBUS, SIG_DFL);
00212 signal(SIGSEGV, SIG_DFL);
00213 signal(SIGPIPE, SIG_DFL);
00214
00215 std::vector<std::string> args;
00216 for (int i=0; i<argc; i++)
00217 {
00218 if (strcmp(argv[i],"-h")==0)
00219 help();
00220 args.push_back(argv[i]);
00221 }
00222
00223 const char* outname = NULL;
00224
00225 for (unsigned int i=1; i<args.size(); i++)
00226 {
00227 const char* arg = args[i].c_str();
00228
00229
00230 if (strncmp(arg,"-e",2)==0)
00231 gEventCutoff = atoi(arg+2);
00232 else if (strncmp(arg,"-o",2)==0) {
00233 outname = args[i+1].c_str();
00234 i++;
00235 }
00236 else if (strcmp(arg,"-h")==0)
00237 help();
00238 else if (arg[0] == '-')
00239 help();
00240 }
00241
00242 for (unsigned int i=1; i<args.size(); i++)
00243 {
00244 const char* arg = args[i].c_str();
00245
00246 if (arg[0] != '-')
00247 {
00248 ProcessMidasFile(arg, outname);
00249 }
00250 }
00251
00252 return 0;
00253 }
00254
00255