er  dev
ERRootSource.cxx
1 
2 #include "ERRootSource.h"
3 
4 #include "FairRootManager.h"
5 #include "FairRun.h"
6 
7 #include "ERHe8EventHeader.h"
8 
9 #include <iostream>
10 using namespace std;
11 
12 ERRootSource::ERRootSource():
13 fFile(NULL),
14 fTree(NULL),
15 fTreeName(""),
16 fBranchName(""),
17 fCurFile(0),
18 fOldEvents(0)
19 {
20 }
21 
22 ERRootSource::ERRootSource(const ERRootSource& source){
23 }
24 
25 ERRootSource::~ERRootSource(){
26 
27 }
28 
29 Bool_t ERRootSource::Init(){
30  //input files opening
31  if (fPath.size() == 0)
32  Fatal("ERRootSource", "No files for source ERRootSource");
33  if (fRawEvents.size() == 0)
34  Fatal("ERRootSource", "ERRootSource without regiistered events");
35 
36  OpenNextFile();
37 
38  FairRun* run = FairRun::Instance();
39  ERHe8EventHeader* header = (ERHe8EventHeader*)run->GetEventHeader();
40  header->Register(fTree, fBranchName);
41 
42  for (Int_t iREvent = 0; iREvent < fRawEvents.size(); iREvent++)
43  fRawEvents[iREvent]->Register(fTree, fBranchName);
44  return kTRUE;
45 }
46 
47 Int_t ERRootSource::ReadEvent(UInt_t id){
48  FairRootManager* ioman = FairRootManager::Instance();
49  if ( ! ioman ) Fatal("Init", "No FairRootManager");
50 
51  //Проверяем есть ли еще события для обработки
52  if (fTree->GetEntriesFast() == ioman->GetEntryNr()-fOldEvents){
53  fOldEvents += ioman->GetEntryNr();
54  if (OpenNextFile())
55  return 1;
56  }
57  //cout << "ev" << ioman->GetEntryNr() << endl;
58  fTree->GetEntry(ioman->GetEntryNr()-fOldEvents);
59 
60  for (Int_t iREvent = 0; iREvent < fRawEvents.size(); iREvent++)
61  fRawEvents[iREvent]->Process();
62  return 0;
63 }
64 
65 void ERRootSource::Close(){
66  if (fFile){
67  fFile->Close();
68  delete fFile;
69  }
70 }
71 
72 void ERRootSource::Reset(){
73 }
74 
75 void ERRootSource::SetFile(TString path, TString treeName, TString branchName){
76  fPath.push_back(path);
77  fTreeName = treeName;
78  fBranchName = branchName;
79  cout << "Input file " << path << " with tree name " << fTreeName <<" and branch name " <<
80  fBranchName << " added to source ERRootSource" << endl;
81 }
82 
83 
84 Int_t ERRootSource::OpenNextFile(){
85  if (fCurFile == fPath.size())
86  return 1;
87  fFile = new TFile(fPath[fCurFile++]);
88  if (!fFile->IsOpen())
89  Fatal("ERRootSource", "Can`t open file for source ERRootSource");
90  else
91  cout << fPath[fCurFile-1] << " opened for source ERRootSource" << endl;
92  fTree = (TTree*)fFile->Get(fTreeName);
93  if (!fTree)
94  Fatal("ERRootSource", "Can`t find tree in input file for source ERRootSource");
95  return 0;
96 }