GRSISort
Created by P.C. Bender
Developement Team: P.C. Bender, R. Dunlop, V. Bildstein
An extension of the ROOT analysis Framework
bufferclean.cxx
Go to the documentation of this file.
1 // Author: Ryan Dunlop
2 
3 // g++ offsetfind.cxx `root-config --cflags --libs` -I${GRSISYS}/include -L${GRSISYS}/libraries -lMidasFormat
4 // -lXMLParser -ooffsetfind
5 #include "TMidasFile.h"
6 #include "TMidasEvent.h"
7 #include "TStopwatch.h"
8 #include "TSystem.h"
9 #include <map>
10 #include <iostream>
11 #include <fstream>
12 #include <chrono>
13 #include <utility>
14 
15 UInt_t chanId_threshold = 100;
16 
17 Bool_t CheckEvent(const std::shared_ptr<TMidasEvent>& evt)
18 {
19  // This function does not work if a Midas event contains multiple fragments
20  static std::map<Int_t, Bool_t> triggermap; // Map of Digitizer vs have we had a triggerId < threshold yet?
21  // First parse the Midas Event.
22  evt->SetBankList();
23 
24  // Need to put something in that says "if not a Griffin fragment (ie epics) return true"
25  void* ptr;
26  int banksize = evt->LocateBank(nullptr, "GRF2", &ptr);
27  int bank = 2;
28 
29  if(banksize == 0) {
30  banksize = evt->LocateBank(nullptr, "GRF1", &ptr);
31  bank = 1;
32  }
33  uint32_t type = 0xffffffff;
34  uint32_t value = 0xffffffff;
35 
36  UInt_t chanadd = 0;
37  UInt_t trigId = 0;
38  // UInt_t dettype = 0;
39  // Int_t dignum = -1;
40 
41  for(int x = 0; x < banksize; x++) {
42  value = *(reinterpret_cast<int*>(ptr) + x);
43  type = value & 0xf0000000;
44 
45  switch(type) {
46  case 0x80000000:
47  switch(bank) {
48  case 1: // header format from before May 2015 experiments
49  // Sets:
50  // The number of filters
51  // The Data Type
52  // Number of Pileups
53  // Channel Address
54  // Detector Type
55  if((value & 0xf0000000) != 0x80000000) {
56  return false;
57  }
58  chanadd = (value & 0x0003fff0) >> 4;
59  // dettype = (value &0x0000000f);
60 
61  // if(frag-DetectorType==2)
62  // frag->ChannelAddress += 0x8000;
63  break;
64  case 2:
65  // Sets:
66  // The number of filters
67  // The Data Type
68  // Number of Pileups
69  // Channel Address
70  // Detector Type
71  if((value & 0xf0000000) != 0x80000000) {
72  return false;
73  }
74  chanadd = (value & 0x000ffff0) >> 4;
75  // dettype = (value &0x0000000f);
76 
77  // if(frag-DetectorType==2)
78  // frag->ChannelAddress += 0x8000;
79  break;
80  default: printf("This bank not yet defined.\n"); break;
81  };
82  // dignum = chanadd&0x0000ff00;
83  break;
84  case 0x90000000: trigId = value & 0x0fffffff;
85  };
86  }
87  if(triggermap.find(chanadd) == triggermap.end()) {
88  triggermap[chanadd] = false; // initialize the new digitizer number to false.
89  }
90  // Check to make sure we aren't getting any triggerId's = 0. I think these are corrupt events RD.
91  if(trigId == 0) {
92  return false;
93  }
94 
95  // Check against map of trigger Id's to see if we have hit the elusive < threshold mark yet.
96  if(triggermap.find(chanadd)->second) {
97  return true;
98  }
99  if(trigId < chanId_threshold) {
100  triggermap.find(chanadd)->second = true;
101  return true;
102  } else {
103  return false;
104  }
105 }
106 
107 /*void AddToQueue(std::shared_ptr<TMidasEvent> evt){
108  evtQ.push(evt);
109 }
110 */
111 
112 void Write(std::shared_ptr<TMidasEvent> evt, TMidasFile* outfile)
113 {
114  outfile->FillBuffer(std::move(evt));
115 
116  // if(outfile->GetBufferSize() > 100000){
117  // outfile->WriteBuffer();
118  // }
119 }
120 
121 int main(int argc, char** argv)
122 {
123  if(argc < 2) {
124  printf("Usage: ./bufferclear <input.mid> <output.mid>\n");
125  return 1;
126  }
127 
128  if(argv[1] == argv[2]) {
129  printf("input.mid and output.mid must have different names\n");
130  return 1;
131  }
132 
133  auto* file = new TMidasFile;
134  // TMidasFile *outfile = new TMidasFile;
135  file->Open(argv[1]);
136  int runnumber = file->GetRunNumber();
137  int subrunnumber = file->GetSubRunNumber();
138  if(argc < 3) {
139  file->OutOpen(Form("cleanrun%05d_%03d.mid", runnumber, subrunnumber));
140  } else {
141  file->OutOpen(argv[2]);
142  }
143 
144  std::ifstream in(file->GetFilename(), std::ifstream::in | std::ifstream::binary);
145  in.seekg(0, std::ifstream::end);
146  long long filesize = in.tellg();
147  in.close();
148 
149  int bytes = 0;
150  long long bytesread = 0;
151 
152  TStopwatch w;
153  w.Start();
154 
155  UInt_t num_bad_evt = 0;
156  UInt_t num_evt = 0;
157  std::shared_ptr<TMidasEvent> event = std::make_shared<TMidasEvent>(); // need to new for each event
158 
159  while(true) {
160  bytes = file->Read(event);
161  if(bytes == 0) {
162  printf(DMAGENTA "\tfile: %s ended on %s" RESET_COLOR "\n", file->GetFilename(), file->GetLastError());
163  if(file->GetLastErrno() == -1) { // try to read some more...
164  continue;
165  }
166  break;
167  }
168  bytesread += bytes;
169 
170  switch(event->GetEventId()) {
171  case 0x8000:
172  printf("start of run\n");
173  // file->Write(event,"q");
174  Write(event, file);
175  printf(DGREEN);
176  event->Print();
177  printf(RESET_COLOR);
178  break;
179  case 0x8001:
180  printf("end of run\n");
181  printf(DRED);
182  event->Print();
183  printf(RESET_COLOR);
184  // file->Write(event,"q");
185  Write(event, file);
186  break;
187  case 0x0001: // This is a GRIFFIN digitizer event
188  if(CheckEvent(event)) {
189  Write(event, file);
190  // file->Write(event,"q");
191  } else {
192  num_bad_evt++;
193  }
194  num_evt++;
195  break;
196  default: // Probably epics
197  Write(event, file);
198  // file->Write(event,"q");
199  break;
200  };
201 
202  if(num_evt % 5000 == 0) {
203  gSystem->ProcessEvents();
204  printf(HIDE_CURSOR " bad events %u/%u have processed %.2fMB/%.2f MB => %.1f MB/s " SHOW_CURSOR
205  "\r",
206  num_bad_evt, num_evt, (bytesread / 1000000.0), (filesize / 1000000.0),
207  (bytesread / 1000000.0) / w.RealTime());
208  w.Continue();
209  }
210  }
211  file->WriteBuffer();
212 
213  printf("\n");
214 
215  file->Close();
216  file->OutClose();
217  delete file;
218  // delete outfile;
219 }
220 // void WriteEventToFile(TMidasFile*,std::shared_ptr<TMidasEvent> Option_t);
int main(int argc, char **argv)
#define DRED
Definition: Globals.h:17
#define RESET_COLOR
Definition: Globals.h:4
#define DGREEN
Definition: Globals.h:16
void Write(std::shared_ptr< TMidasEvent > evt, TMidasFile *outfile)
#define HIDE_CURSOR
Definition: Globals.h:31
Reader for MIDAS .mid files.
Definition: TMidasFile.h:32
Bool_t CheckEvent(const std::shared_ptr< TMidasEvent > &evt)
Definition: bufferclean.cxx:17
UInt_t chanId_threshold
Definition: bufferclean.cxx:15
void FillBuffer(const std::shared_ptr< TMidasEvent > &midasEvent, Option_t *opt="")
Definition: TMidasFile.cxx:458
bool Open(const char *filename) override
Open input file.
Definition: TMidasFile.cxx:116
#define DMAGENTA
Definition: Globals.h:19
#define SHOW_CURSOR
Definition: Globals.h:32