Diana Software
MRootFileReader.cc
Go to the documentation of this file.
1 #include "MRootFileReader.hh"
2 #include "QEvent.hh"
3 #include "QEventAssembler.hh"
4 #include "QBaseTree.hh"
5 #include "QObject.hh"
6 #include "QString.hh"
7 #include "QChain.hh"
8 #include "QTree.hh"
9 #include <TLeaf.h>
10 #include <TFile.h>
11 #include <TEntryList.h>
12 #include <TFriendElement.h>
13 #include "QVectorC.hh"
14 #include "QRawEvent.hh"
15 #include <sstream>
16 
17 using namespace Diana;
18 
20 
21 using std::string;
22 
23 //_____________________________________________________________________________
24 // Init method is called before event loop
26 
27  // Reads multiple or single files
28  InitFileManager();
29  fDeleteInput = GetBool("DeleteInputFiles",false,false);
30  fRWCommon->fDeleteInputFiles = fDeleteInput;
31  fInclusive = GetBool("InclusiveMode",true,false);
32  fRWCommon->fInclusiveMode = fInclusive;
33  fRootInput = NULL;
34 
35  // Load the chain
36  LoadQChain();
37 
38  // read in the cut string
39  fEntryList = 0;
40  fEntryListEntry = 0;
41  std::string cut = GetString("Cut","",false);
42  if (cut != "") {
43  Warn("The \"Cut\" option is not orthodox. Users must give to Marco Vignati"
44  " developer 0.89 Euro for each use");
45  fChain->Draw(">>anakaEventList", cut.c_str(), "entrylist");
46  fEntryList = (TEntryList*) gDirectory->Get("anakaEventList");
47  Long64_t nsel = fEntryList->GetN();
48  if(nsel == 0) {
49  Error("The Cut selected zero entries");
50  return;
51  }
52  fEvents = fEntryList->GetN();
53  fChain->SetEntryList(fEntryList);
54  Info("The Cut selected %lu entries",fEvents);
55 
56  }
57 
58  // Init branch
59  fEventNumber = 0;
60  fRootInput = 0;
61  fFileCounter = -1;//initialized to -1 because it should be always positive
62  if(fEvents > 0 ) {
63  SetBranches(eva);
64  //MV FIXME: Move to QReader (see FIXME in QReader.cc:47
65  //QGlobalReaderDispatcher::GetInstance().SetCurrentReaderFilename(file0);
66  } else {
67  Error("Zero entries!");
68  return;
69  }
70 
71  // Get Aliases
72  TList* falias = fChain->GetListOfAllAliases();
73  if(!falias) {
74  Debug("Found no alias in the first file");
75  } else {
76  TIter next(falias);
77  Debug("Found %d aliases in the first file",falias->GetSize());
78  while(TObject *obj = next()) {
79  const char* name = obj->GetName();
80  const char* path = obj->GetTitle();
81  Debug("Alias %s defined in file as %s",name,path);
82  eva.GetAliases().Add(path,name);
83  }
84  }
85 }
86 //_____________________________________________________________________________
87 // Doit method is called for each event, getting the event as argument
89 {
90  if(fEntryList) {
91  if(fEntryListEntry >= fEvents) return false;
92  Int_t treenum=0;
93  fEventNumber = fEntryList->GetEntryAndTree(fEntryListEntry,treenum);
94  fEventNumber += fChain->GetTreeOffset()[treenum];
95  fEntryListEntry++;
96  } else {
97  if(fEventNumber >= fEvents)
98  return false;
99  }
100  fChain->GetEntry(fEventNumber);
101 
102  if(fChain->GetFile()->GetFileCounter() != fFileCounter) {
103  fRootInput = fChain->GetFile();
104  fFileCounter = fChain->GetFile()->GetFileCounter();
105 
106  // here we can set the required only branches from the event
107  SetBranches(eva);
108  // fChain->SetCacheSize(50000000);
109  // fChain->AddBranchToCache("*",kTRUE);
110  // reget as we have reset branches
111  fChain->GetEntry(fEventNumber);
112  // notify open new file
113  std::string fileName = fChain->GetFile()->GetName();
114  fRWCommon->fReaderTreeEvents[fileName] = fChain->GetTree()->GetEntries();
115  OpenNewFile(fileName);
116  Info("Current file is %s", fileName.c_str());
117  }
118  fEventNumber++;
119  return true;
120 }
121 //_____________________________________________________________________________
122 // Done method is called after event loop
124  delete fChain;
125  fRootInput = NULL;
126  if(fDeleteInput) {
127  std::vector<std::string>::iterator iter;
128  for(iter = fFileList.begin(); iter!= fFileList.end(); iter++) {
129  Info("Removing %s",iter->c_str());
130  remove(iter->c_str());
131  }
132  if(fReadFileList) {
133  remove(fFileListPath.c_str());
134  Info("Removing %s", fFileListPath.c_str());
135  }
136  }
137 }
138 //_____________________________________________________________________________
139 const QError& MRootFileReader::JumpToEvent(Long64_t event) {
140  if(fEntryList) {
141  fErr = QERR_NOT_IMPLEMENTED;
142  char buf[128];
143  snprintf(buf,128,"Cut option is not compatible with JumpToEvent");
144  fErr.SetDescription(buf);
145  }
146  else
148  return fErr;
149 }
150 //_____________________________________________________________________________
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
@ QERR_NOT_IMPLEMENTED
Definition: QError.hh:109
Reader for diana and apollo root files.
virtual void Init(Diana::QEventAssembler &eva)
Init method is called before event loop.
virtual bool Do(Diana::QEventAssembler &eva)
Do method is called for each event.
const QError & JumpToEvent(Long64_t event)
Jump to particular event in the loop.
virtual void Done()
Done method is called after event loop.
void Add(const QEventLabel &label, const std::string &path, const std::string &alias)
Definition: QAliases.cc:50
error class with error type and description
Definition: QError.hh:115
Visitor class of QEvent that provides full handling of QEvent.
const QAliases & GetAliases() const
get aliases (const version)
virtual const QError & JumpToEvent(Long64_t event)
Jump to a particular event in the chain.
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...