HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoReaderRoot.cxx
Go to the documentation of this file.
1 //
2 // File HarpoReaderRoot.cxx
3 //
10 #include "HarpoReaderRoot.h"
11 #include "HarpoDetReader.h"
12 #include "HarpoConfig.h"
13 #include "HarpoRunHeader.h"
14 #include "HarpoDccHeader.h"
15 #include "HarpoFeminosHeader.h"
16 #include "HarpoEvent.h"
17 #include "HarpoRecoEvent.h"
18 #include "HarpoDccEvent.h"
19 #include "HarpoFeminosEvent.h"
20 #include "Pmm2Event.h"
21 #include "HarpoSimEvent.h"
22 #include "HarpoTimeStamp.h"
23 
24 #include "HarpoDccReader.h"
25 #include "HarpoSimReader.h"
26 
27 #include "TClonesArray.h"
28 #include "TBranch.h"
29 #include "TSystemDirectory.h"
30 
31 #include <climits>
32 #include <cstdio>
33 #include <arpa/inet.h>
34 #include <cstdlib>
35 //#include <cstring>
36 #include <iostream>
37 
38 
39 ClassImp(HarpoReaderRoot)
40 
41 
47 {
48  if(gHarpoDebug>0) Info("HarpoReaderRoot()"," ");
49 
50  // fNewFile = NULL;
51  // fNewFileName = (TString *) NULL;
52  // fSave = kFALSE;
53 
54  fSync = false;
55  fMode = hSyncTime;
56 }
57 
59 
63 HarpoReaderRoot::HarpoReaderRoot(HarpoConfig *cfg) : HarpoReaderBase(cfg)
64  {
65  if(gHarpoDebug>0) Info("HarpoReaderRoot(cfg)"," ");
66 
67  // fInputFileName = cfg->GetFile();
68  // fNewFile = NULL;
69  // fNewFileName = cfg->GetOutFile();
70  // fSave = kFALSE;
71 
72  fSync = false;
73  fMode = hSyncTime;
74 }
75 
77 {
78  if(gHarpoDebug>0) Info("~HarpoReaderRoot()"," ");
79  // ??? Delete Det Constructors
80  // if ( fNewFile != NULL) {
81  // fNewFile->Close();
82  // }
83 }
84 
85 void HarpoReaderRoot::SetDetFile(TString * /* name */ , Int_t /* det */)
86 {
87  // if(det==0)
88  // fInputFileName = name; //???
89 }
90 
91  void HarpoReaderRoot::SetRootFile(TString *name)
92  {
93  //if(gHarpoDebug>0)
94  Info("Dummy SetRootFile","%s",name->Data());
95  }
96 
98 {
99  if(gHarpoDebug>1) Info("NextEvent","%ld",fNevts);
100  Bool_t res = true;
101 
102  if(gHarpoDebug>1){
103  for (UInt_t ndet = 0; ndet < gkNDetectors; ndet++)
104  Info("NextEvent","*Det %d => %p",ndet,fEvents[ndet]);
105  Info("NextEvent","*Reco => %p",fRecoEvent);
106  }
107 
108  if(gHarpoDebug>1)
109  Info("NextEvent","GetEvent %ld",fNevts);
110  if(!fInputTree->GetEvent(fNevts))
111  return false;
112 
113  if(gHarpoDebug>1){
114  for (UInt_t ndet = 0; ndet < gkNDetectors; ndet++)
115  Info("NextEvent","Det %d => %p",ndet,fEvents[ndet]);
116  Info("NextEvent","Reco => %p",fRecoEvent);
117  }
118 
119  fNevts++;
120 
121  return res;
122 }
123 
125 {
126  if(gHarpoDebug>0) Info("GetEvent","event %ld",fNevts);
127  HarpoDetEvent *revt;
128  ULong_t ndet;
129 
130  fEvent = new HarpoEvent();
131  // /* if(fEventHeader==NULL) */
132  if(!fEventHeader) {
133  Warning("GetEvent","Event Header not available");
135  }//fEventHeader->SetEvtNo(fNevts);
136 
137  for(ndet=0;ndet<gkNDetectors;ndet++) {
138  if (fDetSet->isExist(ndet)) {
139  revt = fEvents[ndet];
140  if(gHarpoDebug>1)
141  Info("GetEvent","Det %ld exists, %p",ndet,revt);
142  // std::cout << ndet << ": evt " << revt << std::endl;
143  fEvent->SetDetEvent(revt,ndet);
144  }
145  }
146 
149 
150  for( UInt_t ndet = 0; ndet < 2; ndet++ ) {
151  HarpoFeminosHeader * fh = dynamic_cast<HarpoFeminosHeader *>(fRunHeader->GetHeader(ndet));
152  if (fh != NULL) {
153  // std::cout << "Fem Peds " << ndet << std::endl;
154  HarpoDccMap *dm = (fEvents[ndet])->GetMap();
155  dm->SetPedestals(fh->GetPedestals());
156  //fh->GetPedestals()->Print();
157  continue;
158  }
159  HarpoDccHeader * dh = dynamic_cast<HarpoDccHeader *>(fRunHeader->GetHeader(ndet));
160  if (dh != NULL) {
161  // std::cout << "Fem Peds " << ndet << std::endl;
162  HarpoDccMap *dm = (fEvents[ndet])->GetMap();
163  dm->SetPedestals(dh->GetPedestals());
164  //fh->GetPedestals()->Print();
165  continue;
166  }
167  Warning("GetEvent","Unknown Header %d",ndet);
168  }
169 
170 
171 
172  return fEvent;
173 }
174 
177 {
178  // fRunHeader = fInRootFile->Get("fRunHeader");
179  // return fRunHeader;
180  if(gHarpoDebug>0) Info("GetRunHeader"," ");
181  return fRunHeader;
182 }
183 
185 
189 {
190  if(gHarpoDebug>0) Info("Init","Readout mode %d",mode);
191  Bool_t res = true;
192  fMode = mode;
193  fDetSet->print();
194 
195  if(gHarpoDebug>0) Info("Init","Open ROOT file %s",gHConfig->GetInFile(0)->Data());
196  //fInRootFile = TFile::Open(gHConfig->GetInFile(0)->Data());
197  if(gHarpoDebug>0) Info("Init","File open");
199  if(gHarpoDebug>0) Info("Init","Get TTree HarpoTree");
200  fInputTree = new TChain("HarpoTree");
201  if(gHConfig->GetInFile(0)->EndsWith(".root")){
202  fInRootFile = TFile::Open(gHConfig->GetInFile(0)->Data());
203  fInputTree->Add(gHConfig->GetInFile(0)->Data());
204  Info("Init","Adding %s",gHConfig->GetInFile(0)->Data());
205  }else{
206 
207  TString strtmp = *gHConfig->GetInFile(0);
208  Ssiz_t pos = strtmp.Last('/');
209  TString dirname = strtmp(0,pos);
210  TString fileprefix = strtmp(pos+1,strtmp.Length());
211 
212  Info("Init","%s / %s",dirname.Data(), fileprefix.Data());
213 
214  Int_t nfiles = 0;
215  TSystemDirectory dir("datadir", dirname.Data());
216  TList *files = dir.GetListOfFiles();
217  if (files) {
218  TSystemFile *file;
219  TString fname;
220  TIter next(files);
221  while ((file=(TSystemFile*)next())) {
222  fname = file->GetName();
223  if(!fname.BeginsWith(fileprefix.Data()))
224  continue;
225  if(!fname.EndsWith(".root"))
226  continue;
227  Info("Init","Include file %s",fname.Data());
228 
229  if(nfiles==0)
230  fInRootFile = TFile::Open(Form("%s/%s",dirname.Data(),fname.Data()));
231  nfiles++;
232  }
233  }
234  if(nfiles==0){
235  Warning("Init","No file matching the pattern %s",gHConfig->GetInFile(0)->Data());
236  return false;
237  }
238 
239  Int_t nFiles = fInputTree->Add(Form("%s*.root",gHConfig->GetInFile(0)->Data()));
240  Info("Init","Adding %s (%d files)",Form("%s*.root",gHConfig->GetInFile(0)->Data()),nFiles);
241  }
242  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
243  Long64_t trdr[gkNDetectors];
244 
245  fRunHeader = NULL;
246  fRunHeader = //new HarpoRunHeader();
247  (HarpoRunHeader*)fInRootFile->Get("fRunHeader");
248 
249  if (fRunHeader == NULL )
250  Warning(__FUNCTION__,"HarpoRunHeader not found\n");
251 
252 
253  // Check mask
254  ULong_t gmask = fDetSet->GetMask(); // global processing mask
255  ULong_t hmask = fRunHeader->GetDetMask(); // maks for data in root file
256  if ( gmask != hmask ) {
257  Info(__FUNCTION__,"Processing mask 0x%lx <> Header mask 0x%lx.\n",gmask,hmask);
258 
259  ULong_t diff_mask = ( gmask ^ hmask );
260  ULong_t skip_mask = ( hmask & diff_mask ) ; // det to skip
261  ULong_t nodet_mask = ( gmask & diff_mask ) ; // no det in root files
262 
263  if (skip_mask)
264  Info(__FUNCTION__,"Processing skiping det 0x%lx.\n",skip_mask);
265  if (nodet_mask)
266  Warning(__FUNCTION__,"Defectors 0x%lx not in ROOT file!\n",nodet_mask);
267 
268  fDetSet->SetMask(gmask&hmask);
269  fRunHeader->SetDetMask(gmask&hmask);
270 
271  }
272  HarpoDetHeader *rawhdr;
273  // HarpoDccHeader *drawhdr;
274  // HarpoDeminosHeader *frawhdr;
275  ULong_t ndet;
276  for(ndet=0;ndet<gkNDetectors;ndet++) {
277  if (fDetSet->isExist(ndet)) {
278  std::cout << "Get Det Header " << ndet << std::endl;
279  rawhdr = (HarpoDetHeader*)fInRootFile->Get(Form("fDetHeader%ld",ndet));
280  if(rawhdr !=0 ) {
281  if ( ndet < 2 ) {
282  if (dynamic_cast<HarpoDccHeader *>(rawhdr) != NULL ) {
283  trdr[ndet] = 0;
284  gHConfig->SetDetRdrType(static_cast<HarpoDetType>(ndet),0);
285  std::cout << "Det " << ndet << " is T2K DCC" << std::endl;
286  } else if (dynamic_cast<HarpoFeminosHeader *>(rawhdr) != NULL ) {
287  trdr[ndet] = 1;
288  gHConfig->SetDetRdrType(static_cast<HarpoDetType>(ndet),1);
289  std::cout << "Det " << ndet << " is MINOS DCC" << std::endl;
290  } else {
291  trdr[ndet] = -1; // ??Reasonable value??
292  gHConfig->SetDetRdrType(static_cast<HarpoDetType>(ndet),-1);
293  std::cout << "Det " << ndet << " not a DCC" << std::endl;
294  }
295  }
296  fRunHeader->SetDetHeader(rawhdr,ndet);
297  std::cout << "Header Class Name " << rawhdr->ClassName() << std::endl;
298  }
299  }
300  }
301  //------------------------------------------------------------------------
302 
303  // fInputTree = (TTree*)fInRootFile->Get("HarpoTree");
304  if(gHarpoDebug>0) Info("Init","Initialise event objects");
305  // Long64_t trdr;
306  for (UInt_t ndet = 0; ndet < gkNDetectors; ndet++){
307  if (gHDetSet->isExist(ndet)){
308  // trdr = gHConfig->GetDetRdrType(static_cast<HarpoDetType>(ndet)); // Det Reader instance type
309  HarpoDet dt(ndet);
310  // TBranch * b = fInputTree->GetBranch(dt.GetName());
311  // const char * className = b->GetClassName();
312  // printf("Branch %s Class %s\n",dt.GetName(),className);
313  switch(ndet){
314  case XDCC:
315  if (trdr[ndet] == 0 ) {
316  //std::cout << "Det is T2K XDCC" << std::endl;
317  fEvents[ndet] = new HarpoDccEvent();
318  }
319  else if (trdr[ndet] == 1) {
320  //std::cout << "Det is MINOS XDCC" << std::endl;
321  fEvents[ndet] = new HarpoFeminosEvent();
322  }
323  else {
324  std::cout << "UNKNOWN XDCC" << std::endl;
325  fDetReader[ndet] = NULL;
326  }
327  break;
328  case YDCC:
329  if (trdr[ndet] == 0 ) {
330  //std::cout << "Det is T2K YDCC" << std::endl;
331  fEvents[ndet] = new HarpoDccEvent();
332  }
333  else if (trdr[ndet] == 1) {
334  //std::cout << "Det is MINOS YDCC" << std::endl;
335  fEvents[ndet] = new HarpoFeminosEvent();
336  }
337  else {
338  std::cout << "UNKNOWN YDCC" << std::endl;
339  fDetReader[ndet] = NULL;
340  }
341  break;
342  case PMM2:
343  fEvents[ndet] = new Pmm2Event();
344  break;
345  // case TEMP_PRESSION,:
346  // break;
347  case SIMDET:
348  fEvents[ndet] = new HarpoSimEvent();
349  break;
350  }
351  if(gHarpoDebug>0) Info("Init","Set Branch %d (%s)",ndet,dt.GetName());
352  fInputTree->SetBranchAddress(dt.GetName(),&fEvents[ndet]);
353  }
354  }
355  if(gHarpoDebug>0) Info("Init","Event objects initialised");
356  fInputTree->SetBranchAddress("BRawEventHeader",&fEventHeader);
357 
358  fRecoEvent = new HarpoRecoEvent();
359  if(gHarpoDebug>0) Info("Init","Set Branch Reco");
360  fInputTree->SetBranchAddress("RecoEvent",&fRecoEvent);
361  if(gHarpoDebug>0) Info("Init","Reco objects initialised");
362 
363  return res;
364 }
365 
366 Long_t HarpoReaderRoot::Loop(Long_t maxevents)
367 {
369 
370  Long_t cnt=0;
371  if(gHarpoDebug>0) Info("Loop", "maxevents = %ld",maxevents);
372 
373  Int_t nEntries = fInputTree->GetEntries();
374  if(nEntries<maxevents) maxevents=nEntries;
375 
376  while (NextEvent()) {
377  //for(Int_t ev = 0; ev<maxevents; ev++){
378  if(gHarpoDebug>1) Info("Loop", "########### event = %ld ############",fNevts);
379  GetEvent();
380 
381  // Pre
382  Int_t st = hAnaOK;
384 
385  for(Int_t i = 0; i<fNanalyses; i++){
386  if(gHarpoDebug>1) Info("Loop", "Start Analysis %d",i);
388  st = fEvent->GetHeader()->GetEvAnaStatus();
389  if ( st < hAnaNextEvent ) break;
390  }
391 
392  if (st == hAnaNextFile )
393  {
394  //how to go to next file ?
395  break;
396  }
397  else if (st == hAnaStop)
398  {
399  break;
400  }
401  else if (st <= hAnaAbort)
402  {
403  Error(__FUNCTION__,
404  "Fatal Analysis Error %d aborting prgram.\n",st);
405  std::abort();
406  }
407 
408  if (++cnt >= maxevents) break;
409  }
410  return cnt;
411 }
412 
413 Long_t HarpoReaderRoot::Find(Long_t eventNo)
414 {
415  if(gHarpoDebug>0) Info("Find","Event %ld",eventNo);
416  //Long_t cnt=0;
417  //Long_t maxevents = fMaxEvents;
418  //Bool_t found = false;
419  //HarpoEvent *evt;
420 
421  fNevts = eventNo+1;
422 
423  return fInputTree->GetEvent(eventNo);
424 }
425 
426 // void HarpoReaderRoot::SkipEvent()
427 // {
428 // if(gHarpoDebug>0) Info("SkipEvent","event %ld",fNevts);
429 // ULong_t ndet;
430 // for(ndet=0;ndet<gkNDetectors;ndet++) {
431 // if (fDetSet->isExist(ndet)) {
432 // fDetReader[ndet]->SkipEvent();
433 // }
434 // }
435 // }
A class hold HARPO run iformation.
HarpoRunHeader * fRunHeader
virtual void print()
Definition: HarpoDetSet.cxx:43
HarpoRecoEvent * SetRecoEvent(HarpoRecoEvent *evt)
Definition: HarpoEvent.h:45
Dcc Plane Y.
Definition: HarpoDet.h:20
A class hold HARPO run iformation.
HarpoDetHeader * GetHeader(UInt_t ndet=XDCC)
A virtual class store event data for un detector.
Definition: HarpoDetEvent.h:32
void SetRootFile(TString *name)
ULong_t GetDetMask()
Int_t GetEvAnaStatus()
Get Event Analise status.
const char * GetName() const
Definition: HarpoDet.h:33
HarpoRunHeader * GetRunHeader()
Create RunHeader with links to all raw headers.
Bool_t isExist(ULong_t det)
Detecror date exist //! Number of Real Detectors.
Definition: HarpoDetSet.h:33
Dcc Plane X.
Definition: HarpoDet.h:19
A class hold HARPO run iformation.
Object containing the reconstruction information for one event (with array of HarpoRecoClusters Harpo...
void SetEvAnaStatus(Int_t status)
Set Event Analise status.
TString * GetInFile(Int_t idet)
Get Input file name for detector.
Definition: HarpoConfig.h:96
Stop Event Loop.
HarpoAnalyse * fAnalyse[30]
void SetDetRdrType(HarpoDetType nDet, Long64_t rtype)
Set type of reader impementation for given Harpo Detector.
FullEvent Header not scecific to the detectors The class is ....
Base class for all Harpo Detectors.
Definition: HarpoDet.h:27
A class store HARPO row DCC event data and header. End provide access metods to the row data...
Definition: HarpoSimEvent.h:24
A class store HARPO row FEMINOS event data and header. End provide access metods to the row data...
unpacked dcc data The class contains the data map for DCC or Feminos The data is stored as a 2D TMatr...
Definition: HarpoDccMap.h:29
A base class for all top level HARPO readers.
HarpoPedestal * GetPedestals()
A class which read HARPO events from root tree.
void SetDetFile(TString *name, Int_t plane)
Data from Keller temperuture and pressure sensors.
Definition: HarpoDet.h:22
void SetHeader(HarpoEventHeader *data)
Definition: HarpoEvent.cxx:75
A class hold HARPO run iformation.
fname
Definition: HarpoDB.cxx:663
void SetMask(ULong_t detmask)
Redefine active detector set.
Definition: HarpoDetSet.cxx:29
Close current input file continue with next file.
ULong_t GetMask()
Definition: HarpoDetSet.h:55
Long64_t gHarpoDebug
Definition: HarpoDebug.cxx:9
Unknown Detector.
Definition: HarpoDet.h:18
void SetPedestals(HarpoPedestal *obj)
Ste/Get Pedstal object.
Definition: HarpoDccMap.h:67
Long_t Find(Long_t eventNo)
virtual void ProcessEvent(HarpoEvent *event)
Set pointer to new event for analyse, anlyse event , and delete.
stop event analysis, but write event
A class store HARPO raw PMM2 event buffer and header. End provide access metods to the row data...
Definition: Pmm2Event.h:19
HarpoRecoEvent * fRecoEvent
HarpoEventHeader * GetHeader()
Definition: HarpoEvent.cxx:80
Long64_t GetFromEvent()
Get First Event to process.
Definition: HarpoConfig.h:120
A class store HARPO row event data and header. Provide access metods to the row event data...
Definition: HarpoEvent.h:29
HarpoEventHeader * fEventHeader
unsigned int res
Definition: Pmm2Status.h:428
Bool_t fSync
Find raw event by event number.
HarpoPedestal * GetPedestals()
Abort program without saving the output or < -5.
HarpoEvent * fEvent
virtual ~HarpoReaderRoot()
const ULong_t gkNDetectors
Definition: HarpoDet.h:14
void SetDetEvent(HarpoDetEvent *event, ULong_t plane=XDCC)
Definition: HarpoEvent.cxx:85
HarpoConfig * gHConfig
void SetDetHeader(HarpoDetHeader *hdr, UInt_t ndet=XDCC)
HarpoDetEvent * fEvents[gkNDetectors]
Bool_t Init(hReadMode mode=hSyncTime)
Init method : Open in/out files and check run header signature.
void SetDetMask(ULong_t mask)
HarpoEvent * GetEvent()
hReadMode
A class which keeps track of the entire configuration of the analysis.
Definition: HarpoConfig.h:50
HarpoDetReader ** fDetReader
R__EXTERN HarpoDetSet * gHDetSet
Definition: HarpoDetSet.h:71
HarpoDetSet * fDetSet
A class store HARPO EVENT : header and row data. Its provide also the methods for access to this data...
Definition: HarpoDccEvent.h:22