HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoDccReader.cxx
Go to the documentation of this file.
1 //
2 // File HarpoDccReader.cxx
3 //
11 #include "HarpoDccReader.h"
12 #include "HarpoDet.h"
13 #include <TSystem.h>
14 
15 #include <climits>
16 #include <cstdio>
17 #include <arpa/inet.h>
18 
19 #include <iostream>
20 
21 ClassImp(HarpoDccReader)
22 
23 #define EXPECTED_NB_DCC_FRAGMENTS 18
24 #define NB_FEM_FRAGMENTS 72
25 #define MAX_FEM_DATA_BUFFER 2097152
26 
28 
33 {
34 
35  if(gHarpoDebug>0) Info("HarpoDccReader","Create DCC Reader");
36 
37  //fRunNo = 0;
38  fDone = false;
39  fError = false;
40  fPlane = -1;
41 
42  // fSaveTree = kFALSE;
43 
44  //fInFile = (TString *) NULL;
45  fIn = (FILE *) NULL;
46  //fNevts = 0;
47 
48  //fNewFile = (TFile *) NULL;
49  fNewNb = 0;
50 
51  //fEvent = (HarpoDccEvent *) NULL;
52  fEvent = new HarpoDccEvent();
53 
54  fDccAnalyse = (HarpoDccAnalyse *) NULL;
55  //fMaxEvents = LONG_MAX;
56  //fPrevTimeStamp = 0;
57  isEventSaved = false;
58 
59 }
60 
62 
67 {
68  // Config Function
69  fDone = false;
70  fError = false;
71 
72  //fSaveTree = kFALSE;
73 
74  //fRunNo = cfg->GetRunNo();
75  //fInFile = cfg->GetInFile();
76  fIn = (FILE *) NULL;
77  //fNevts = 0;
78  //fMaxEvents = cfg->GetMaxEvents();
79 
80  TString *FileName = cfg->GetOutFile();
81  fNewNb = 0;
82 
83  fEvent = new HarpoDccEvent();
84  //fEvent = (HarpoDccEvent *) NULL;
85 
86  fDccAnalyse = (HarpoDccAnalyse *) NULL;
87 
88  if ( FileName != (TString *) NULL )
89  SetRootFile(FileName);
90 
91  //fPrevTimeStamp = 0;
92  isEventSaved = false;
93 }
94 
96 {
97  std::fclose(fIn);
98 }
99 
100 void HarpoDccReader::SetDccFile(TString *name)
101 {
102  fInFile = name;
103 }
104 
105 void HarpoDccReader::SetRootFile(TString *name)
106 {
107  fNewFileName = name;
108  // fNewFile = new TFile(name->Data(),"RECREATE","HARPO DCC Events");
109  //fSaveTree = kTRUE;
110 }
111 
112 void HarpoDccReader::Save() // TODO
113 {
114  // if(!fSaveTree) return;
115  // fTree->Write();
116  // fNewFile->Close();
117 }
118 
120 {
121  fDccAnalyse = func;
122 }
123 
125 {
126 
127  if(gHarpoDebug>1) Info("NextEvent"," ");
128  // EventHeader_t lHeader; // local buffer
129  // UShort_t lData[EVENT_BUFFER_SIZE]; // local buffer
130  if ( isEventSaved ) {
131  isEventSaved = false;
132  fEvent = fSaveEvent;
133  return true;
134  }
135 
136  if (fDone) return false; // EOF after last Event Data read
137 
139 
140  if (std::fread(&lHeader, sizeof(EventHeader_t), 1, fIn) != 1)
141  {
142  if(std::feof(fIn))
143  {
144  fDone = true;
145  }
146  else
147  {
148  fError = true;
149  }
150  return false;
151  }
152  else
153  {
154  // if(fEvent==NULL)
155  fEvent = new HarpoDccEvent();
156  //else
157  //fEvent->ResetData();
158  //
159  fEvent->SetDet(fDet);
162  // Event Data
163  size_t dataLen = (fEvent->GetHeader())->eventSize - sizeof(EventHeader_t);
164  if (std::fread(&lData,dataLen, 1, fIn) != 1)
165  {
166  if(std::feof(fIn)) // Read after EOF
167  {
168  fDone = true;
169  }
170  else
171  {
172  fError = true;
173  }
174  return false;
175  }
176  else
177  {
178  // std::cout << "======= Before SetData " << dataLen << std::endl;
179  fEvent->SetData(lData,dataLen);
181  fNevts++;
182  }
183  }
184 
185  return true;
186 }
187 
189 {
190  HarpoDccHeader *dh = static_cast<HarpoDccHeader *>(fRunHeader);
192 }
193 
195 {
196  if(gHarpoDebug>1) Info("GetEvent "," ");
197  fEvent->UnPackData();
198  AddPedestals();
199  return fEvent;
200 }
201 
203 {
204  return static_cast<HarpoDccHeader *>(fRunHeader);
205 }
206 
208 
212 {
213 
214  // if(fSaveTree){
215  // fTree = new TTree("HarpoDccData","HARPO events");
216  // fTree->Bronch("Events","HarpoDccEvent",&fEvent);
217  // }
218 
219  if(fPlane==-1)
220  fPlane = fDet;
221 
222  const char * fn = fInFile->Data();
223  if(gHarpoDebug>0) Info("Init","%s",fn);
224  if ( fInFile->BeginsWith("/" )) { // full path
225  if ( gSystem->AccessPathName(fn,kReadPermission) ) {
226  perror("Dcc File access");
227  return false;
228  }
229  } else {
230  if ( gSystem->AccessPathName(fn,kReadPermission) ) // file not exist
231  {
232  // TODO
233  // TString *DataDir = gHDccCfg->GetDataDir();
234  // Not a best way but i can't use config hire
235  TString DataDir(gSystem->Getenv("HARPO_DATA_DIR"));
236 
237  if ( ! DataDir.EndsWith("/") ) DataDir.Append("/");
238  fInFile->Prepend(DataDir);
239  fn = fInFile->Data();
240  if ( gSystem->AccessPathName(fn,kReadPermission) ) {
241  perror("Dcc File access");
242  return false;
243  }
244  }
245  }
246 
247  if(gHarpoDebug>0) Info("Init","Guessed %s",fn);
248 
249  fIn = std::fopen(fn,"r");
250  if ( fIn != NULL )
251  {
254  return static_cast<HarpoDccHeader *>(fRunHeader)->check(this);
255  }
256  else
257  {
258  perror("Dcc File open");
259  return false;
260  }
261 }
262 
263 
264 Long_t HarpoDccReader::Loop(Long_t maxevents)
265 {
266  Long_t cnt=0;
267  HarpoDccEvent *evt;
268 
269  fDccAnalyse->Init();
270 
271  while (NextEvent()) {
272  evt = (HarpoDccEvent*)GetEvent();
274 
275  // if(fSaveTree) fTree->Fill();
276 
277  evt->Delete();
278  // delete evt;
279  if (++cnt >= maxevents) break;
280  }
281  return cnt;
282 
283  fDccAnalyse->Save();
284 
285 }
286 
288 {
289  Long_t maxevents = fMaxEvents;
290  return Loop(maxevents);
291 }
292 
293 // Bool_t HarpoDccReader::Find(Long_t eventNo)
294 // {
295 // Long_t cnt=0;
296 // Long_t maxevents = fMaxEvents;
297 // Bool_t found = false;
298 
299 // while (NextEvent()) {
300 
301 // if ((fEvent->GetHeader())->eventNumb == eventNo) {
302 // found = true;
303 // break;
304 // }
305 // SkipEvent();
306 // if (++cnt >= maxevents) break;
307 // }
308 // return found;
309 // }
311 {
312  //isEventSaved = false;
313  HarpoDccEvent *evt = GetEvent(); // We need read event data
314  if ( evt != NULL ) delete evt ; // We will not process this event,
315  // return NULL on read arror
316 }
317 
319 {
320  HarpoDet dt(fDet);
321  fSaveEvent = fEvent;
322  fEvent = NULL;
323  isEventSaved = true;
324  // std::cout << "PushBack " << dt.GetName() << " Evt " << fNevts << std::endl;
325  Info("PushBack","%s (%ld), evt %ld",dt.GetDescription(), fDet, fNevts);
326 
327 }
void SetRootFile(TString *name)
A class which read HARPO dcc file from one DCC and creant Dcc Events and Dcc Headers for DCC Events o...
UShort_t lData[EVENT_BUFFER_SIZE]
Long_t Loop()
Read errors.
A class hold HARPO run iformation.
void PushBack()
Skeeping not "intersting" event.
Bool_t UnPackData()
ULong_t fTimeStamp
TString * GetOutFile()
Get Output root file name.
Definition: HarpoConfig.h:98
Bool_t Init()
Init method : Open in/out files and check run header signature.
HarpoDccEvent * fSaveEvent
void SetRunHeader(HarpoDetHeader *rh)
Return pointer to decoded pmm2 data vector.
Definition: HarpoDetEvent.h:52
HarpoDccHeader * GetDetHeader()
virtual void Init()
virtual void Save(char *mode=NULL)
UInt_t eventSize
Raw Event size.
Definition: HarpoDetEvent.h:28
void SetDet(Int_t det)
Definition: HarpoDetEvent.h:43
EventHeader_t lHeader
void SetHeader(UShort_t *data)
Base class for all Harpo Detectors.
Definition: HarpoDet.h:27
ULong_t GetTimeStamp(Int_t after=0)
ULong_t fPrevTimeStamp
void SetReaderType(int Id)
Set HaroReaderType.
A virtual base class for all HARPO detectors readers.
void SetAnalyseFunction(HarpoDccAnalyse *func)
TString * fNewFileName
HarpoDetHeader * fRunHeader
virtual ~HarpoDccReader()
Long64_t gHarpoDebug
Definition: HarpoDebug.cxx:9
A virtual class which define intrafece between HARPO Reader and Event Analysis code.
Harpo Event Header.
Definition: HarpoDetEvent.h:26
TString * fInFile
HarpoDccEvent * GetEvent()
virtual void ProcessEvent(HarpoDccEvent *event)
virtual const EventHeader_t * GetHeader() const
Definition: HarpoDetEvent.h:38
HarpoDccEvent * fEvent
HarpoPedestal * GetPedestals()
const char * GetDescription() const
Definition: HarpoDet.h:34
void SetDccFile(TString *name)
HarpoDccAnalyse * fDccAnalyse
void SetPedestals(HarpoPedestal *p)
Definition: HarpoDccEvent.h:48
void SetData(UShort_t *data, size_t dlen=EVENT_BUFFER_SIZE)
A class which keeps track of the entire configuration of the analysis.
Definition: HarpoConfig.h:50
UInt_t eventNumb
Event number in run.
Definition: HarpoDetEvent.h:29
void SkipEvent()
Read next event from raw file.
A class store HARPO EVENT : header and row data. Its provide also the methods for access to this data...
Definition: HarpoDccEvent.h:22