ROOTANA
event_skim.cxx
Go to the documentation of this file.
1 //
2 // Example event dump program
3 //
4 // K.Olchanski
5 //
6 // $Id: event_dump.cxx 70 2009-06-20 02:12:14Z olchansk $
7 //
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/time.h>
13 #include <assert.h>
14 #include <signal.h>
15 #include <errno.h>
16 
17 //#include "TMidasOnline.h"
18 #include "TMidasEvent.h"
19 #include "midasio.h"
20 
21 #include <vector>
22 
23 int gEventCutoff = 0;
24 //bool gSaveOdb = false;
25 
27 {
28  //int eventId = event.GetEventId();
29  //int eventSN = event.GetSerialNumber();
30  //event.Print("a");
31  //event.Print();
32  // printf("EventSerial#: %i, EventID: %i\n",eventSN,eventId);
33 
34  // Look for some banks:
35  const char* fBank = "OMCM" ;
36  int bankLength = 0;
37  int bankType = 0;
38  void *pdata = 0;
39 
40  int found = event.FindBank(fBank, &bankLength, &bankType, &pdata);
41  if(found)
42  {
43  if (0) {
44  printf("Dumping bank %s in hex\n",fBank);
45  for (int j = 0; j < bankLength; j++)
46  printf("0x%08x%c", ((uint32_t*)pdata)[j], (j%10==9)?'\n':' ');
47  printf("\n");
48  printf("Dumping bank %s in dec\n",fBank);
49  for (int j = 0; j < bankLength; j++)
50  printf(" %8d%c", ((uint32_t*)pdata)[j], (j%10==9)?'\n':' ');
51  printf("\n");
52  }
53  /// Trigger word definition (64 bit unsigned long):
54  /// - Bits 0-31: Event number
55  /// - Bits 32-47: Spill number
56  /// - Bits 48-57: Trigger bits
57  /// - Bit 48: Beam spill
58  /// - Bit 49: Pedestal/noise
59  /// - Bit 50: TPC laser
60  /// - Bit 51: POD LED
61  /// - Bit 52: ECAL LED
62  /// - Bit 53: FGD LED
63  /// - Bit 54: FEE calibration
64  /// - Bit 55: TRIP-t cosmic
65  /// - Bit 56: Reserved for FGD cosmic
66  /// - Bit 57: unused
67  /// - Bits 58-63: Instruction
68  /// - Bit 58: Reserved for start-of-run
69  /// - Bit 59: Reserved for end-of-run
70  /// - Bit 60: 1 PPS / counter reset
71  /// - Bit 61: Go to spill mode
72  /// - Bit 62: Go to cosmic mode
73  /// - Bit 63: Decline CTM request/abort
74  int eventNum = ((uint32_t*)pdata)[1];
75  int spillNum = ((uint32_t*)pdata)[2] & 0xFFFF;
76  int trigBits = (((uint32_t*)pdata)[2] >> 16) & 0xFFFF;
77  const char* trigType = "";
78  int trigBeam = 1;
79  switch (trigBits)
80  {
81  case 1:
82  trigType = "Beam spill";
83  break;
84  }
85  if (trigBits == trigBeam) {
86  // printf("EventSerial#: %i, EventID: %i\n",eventSN,eventId);
87  printf(" eventNum %d, spillNum %d, trigBits 0x%04x, %s\n",eventNum,spillNum,trigBits,trigType);
88  }
89  } else {
90  printf("Can't find bank %s\n",fBank);
91  }
92  //printf("\n");
93 
94 }
95 
96 int ProcessMidasFile(const char* fname, TMWriterInterface* writer)
97 {
98  TMReaderInterface* reader = TMNewReader(fname);
99 
100  if (reader->fError)
101  {
102  printf("Cannot open input file \"%s\"\n",fname);
103  delete reader;
104  return -1;
105  }
106 
107  int i=0;
108  while (1)
109  {
110  TMidasEvent event;
111  if (!TMReadEvent(reader, &event))
112  break;
113 
114  int eventId = event.GetEventId();
115  printf("Have an event of type %d\n",eventId);
116 
117  if ((eventId & 0xFFFF) == 0x8000)
118  {
119  // begin run
120  event.Print();
121  }
122  else if ((eventId & 0xFFFF) == 0x8001)
123  {
124  // end run
125  event.Print();
126  }
127  else
128  {
129  event.SetBankList();
130  //event.Print();
131  HandleMidasEvent(event);
132  }
133  if((i%500)==0)
134  {
135  printf("Processing event %d\n",i);
136  }
137 
138  i++;
139  if ((gEventCutoff!=0)&&(i>=gEventCutoff))
140  {
141  printf("Reached event %d, exiting loop.\n", i);
142  break;
143  }
144 
145  // Output all events
146  if (writer) {
147  bool ok = TMWriteEvent(writer, &event);
148  if (!ok) {
149  return -1;
150  }
151  }
152  }
153 
154  reader->Close();
155  delete reader;
156 
157  return 0;
158 }
159 
160 
161 void help()
162 {
163  printf("\nUsage:\n");
164  printf("\n./event_skim.exe [-h] [-eMaxEvents] [file1 file2 ...]\n");
165  printf("\n");
166  printf("\t-h: print this help message\n");
167  printf("\t-e: Number of events to read from input data files\n");
168  printf("\t-o: Generate output file \n");
169  printf("\n");
170  printf("Example1: print events from file: ./event_skim.exe /data/alpha/current/run00500.mid.gz\n");
171  printf("Example2: output selected events: ./event_skim.exe -o skim_00500.mid.gz /data/alpha/current/run00500.mid.gz\n");
172  exit(1);
173 }
174 
175 // Main function call
176 
177 int main(int argc, char *argv[])
178 {
179  setbuf(stdout,NULL);
180  setbuf(stderr,NULL);
181 
182  signal(SIGILL, SIG_DFL);
183  signal(SIGBUS, SIG_DFL);
184  signal(SIGSEGV, SIG_DFL);
185  signal(SIGPIPE, SIG_DFL);
186 
187  std::vector<std::string> args;
188  for (int i=0; i<argc; i++)
189  {
190  if (strcmp(argv[i],"-h")==0)
191  help(); // does not return
192  args.push_back(argv[i]);
193  }
194 
195  const char* outname = NULL;
196 
197  for (unsigned int i=1; i<args.size(); i++) // loop over the commandline options
198  {
199  const char* arg = args[i].c_str();
200  //printf("argv[%d] is %s\n",i,arg);
201 
202  if (strncmp(arg,"-e",2)==0) // Event cutoff flag (only applicable in offline mode)
203  gEventCutoff = atoi(arg+2);
204  else if (strncmp(arg,"-o",2)==0) { // Skim to output file
205  outname = args[i+1].c_str();
206  i++;
207  }
208  else if (strcmp(arg,"-h")==0)
209  help(); // does not return
210  else if (arg[0] == '-')
211  help(); // does not return
212  }
213 
214  TMWriterInterface* writer = NULL;
215 
216  if (outname) {
217  writer = TMNewWriter(outname);
218 
219  if (!writer) {
220  printf("Cannot open file \"%s\" for writing!\n", outname);
221  exit(1);
222  }
223  }
224 
225  for (unsigned int i=1; i<args.size(); i++)
226  {
227  const char* arg = args[i].c_str();
228 
229  if (arg[0] != '-')
230  {
231  ProcessMidasFile(arg, writer);
232  }
233  }
234 
235  writer->Close();
236  delete writer;
237 
238  return 0;
239 }
240 
241 //end
virtual int Close()=0
virtual int Close()=0
MIDAS event.
Definition: TMidasEvent.h:22
uint16_t GetEventId() const
return the event id
Definition: TMidasEvent.cxx:93
void HandleMidasEvent(TMidasEvent &event)
Definition: event_skim.cxx:26
int main(int argc, char *argv[])
Definition: event_skim.cxx:177
int gEventCutoff
Definition: event_skim.cxx:23
void help()
Definition: event_skim.cxx:161
int ProcessMidasFile(const char *fname, TMWriterInterface *writer)
Definition: event_skim.cxx:96
void TMWriteEvent(TMWriterInterface *writer, const TMEvent *event)
Definition: midasio.cxx:651
TMReaderInterface * TMNewReader(const char *source)
Definition: midasio.cxx:447
TMWriterInterface * TMNewWriter(const char *destination)
Definition: midasio.cxx:543
TMEvent * TMReadEvent(TMReaderInterface *reader)
Definition: midasio.cxx:585