Diana Software
QRootFileReaderBase.cc
Go to the documentation of this file.
1 #include "QRootFileReaderBase.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 
19 using std::string;
20 
21 //_____________________________________________________________________________
23  // std::map<std::string,QObject **>::iterator it;
24  // for(it=fObjectList.begin();it!=fObjectList.end();it++){
25  // if(*(it->second)) delete *(it->second);
26  // }
27  // fObjectList.clear();
28 }
29 //_____________________________________________________________________________
31  fObjectList.clear();
32 
33  // init chain
34  fChain = new QChain();
35  std::vector<std::string>::iterator iter;
36  int err;
37  //const char* file0 = "";
38  for(iter = fFileList.begin(); iter!= fFileList.end(); iter++) {
39  Debug("Adding to chain %s", iter->c_str());
40  err = fChain->Add(iter->c_str(),0);
41  //if(iter == fFileList.begin()) file0 = iter->c_str();
42  if(err == 0) Panic("Cannot open file %s", iter->c_str());
43  }
44 
45  fChain->SetCacheSize(100000000);
46  fChain->AddBranchToCache("*",kTRUE);
47  fEvents = fChain->GetEntries();
48  fChain->SetEstimate(fEvents);
49  Info("Number of events: %d", fEvents);
50 
51 }
52 //_____________________________________________________________________________
53 const QError& QRootFileReaderBase::JumpToEvent(Long64_t event) {
54  if(event > fEvents) {
55  fErr = QERR_OUT_OF_RANGE;
56  char buf[128];
57  snprintf(buf,128,"Total events: %llu; event requested: %llu", fEvents, event);
58  fErr.SetDescription(buf);
59  } else {
60  fErr = QERR_SUCCESS;
61  fEventNumber = event;
62  }
63  return fErr;
64 }
65 //_____________________________________________________________________________
66 void QRootFileReaderBase::AddToEvent(const char* brname, QObject** evData,
67  QEventAssembler& eva) {
68  QEventLabel label(brname);
69  std::string owner = label.GetOwner();
70  std::string name = label.GetName();
71 
72  Debug("Adding \"%s\" of owned by \"%s\"", name.c_str(), owner.c_str());
73  eva.Add(owner.c_str(),name.c_str(), evData);
74 }
75 //_____________________________________________________________________________
76 std::vector<TBranch*> QRootFileReaderBase::GetBranches(QChain* chain,
77  QEventAssembler& eva) {
78  chain->ResetLists();
79  TObjArray* branches = chain->GetListOfAllBranches();
80 
81  Int_t nbranches = branches->GetEntriesFast();
82  std::vector<TBranch*> myBranches;
83  std::stringstream msg;
84  bool firstCall = false;
85  if(eva.GetLabels().empty()) {
86  firstCall = true;
87  }
88  for (Int_t br = 0; br < nbranches; br++) {
89  TBranch* branch = (TBranch*) branches->UncheckedAt(br);
90  const char* brname = branch->GetName();
91  std::string brnamecut = brname;
92  brnamecut = brnamecut.substr(0,brnamecut.size()-1);
93 
94  QEventLabel label = brnamecut;
95  QObject** oldobj = eva.Get(label.owner.c_str(),label.name.c_str());
96  bool addBranch = firstCall || (oldobj && *oldobj &&
97  (*oldobj)->IsRequired());
98  // if required add anyway
99  if(addBranch) {
100  myBranches.push_back(branch);
101  msg<<" "<<brname;
102  if(!firstCall) (*oldobj)->SetIsRequired();
103  }
104 
105  }
106  Debug("Active branches: %s",msg.str().c_str());
107  return myBranches;
108 }
109 //_____________________________________________________________________________
111  std::vector<TBranch*> branches = GetBranches(fChain,eva);
112  Int_t nbranches = branches.size();
113  fChain->SetBranchStatus("*",0);
114 
115  for (Int_t br = 0; br < nbranches; br++) {
116  TBranch* branch = branches[br];
117  // assume one leaf per branch
118  const char* brname = branch->GetName();
119  TLeaf* leaf = branch->GetLeaf(brname);
120  const char* leafType = leaf->GetTypeName();
121  Debug("Found branch \"%s\" of type \"%s\"", brname, leafType);
122  TClass* thisClass = TClass::GetClass(leafType,false,false);
123  if(thisClass && thisClass->InheritsFrom("Diana::QObject")) {
124  std::string brnamecut = brname;
125  brnamecut = brnamecut.substr(0,brnamecut.size()-1);
126  std::string brnamed = brnamecut + "*";
127  fChain->SetBranchStatus(brnamed.c_str(),1);
128  if(fObjectList.find(brname) == fObjectList.end()) {
129  QObject** evData = new QObject*(0);
130  // QObject *evMem = (QObject *)thisClass.New();
131  // *evData=evMem;
132  branch->SetAddress(evData);
133  AddToEvent(brnamecut.c_str(),evData,eva);
134  fObjectList[brname] = evData;
135  (*evData)->SetIsRequired(GetBool(brnamecut,fInclusive, false ));
136  } else {
137  branch->SetAddress(fObjectList[brname]);
138  }
139  } else {
140  Error("Type \"%s\" (in branch \"%s\") does not inherit from "
141  "Diana::QObject: skipped",leafType,brname);
142  }
143  }
144 }
145 //_____________________________________________________________________________
err
Definition: CheckOF.C:114
@ QERR_OUT_OF_RANGE
Definition: QError.hh:28
@ QERR_SUCCESS
Definition: QError.hh:27
TChain used in diana.
Definition: QChain.hh:23
TObjArray * GetListOfAllBranches()
Definition: QChain.cc:178
void ResetLists()
Definition: QChain.cc:40
error class with error type and description
Definition: QError.hh:115
Visitor class of QEvent that provides full handling of QEvent.
std::vector< QEventLabel > GetLabels() const
Get the list of all QObject labels in the event.
void Get(const char *owner, WriteHandle< Q > &handle)
Get QObject from the event in write mode. This method has to be called in the event loop,...
void Add(const char *owner, WriteHandle< Q > &handle)
Add QObject to the event. This method has to be called before the event loop, e.g....
label for QObject in the QEvent
Definition: QEventLabel.hh:23
const std::string & GetOwner() const
get owner
Definition: QEventLabel.hh:55
const std::string & GetName() const
get name
Definition: QEventLabel.hh:57
std::string owner
owner of the QObject
Definition: QEventLabel.hh:60
std::string name
name of the QObject
Definition: QEventLabel.hh:62
virtual void SetBranches(Diana::QEventAssembler &eva)
Set the branch caches.
virtual std::vector< TBranch * > GetBranches(QChain *chain, Diana::QEventAssembler &eva)
Get a list of the branches needed by the event.
virtual void AddToEvent(const char *brname, Diana::QObject **evData, Diana::QEventAssembler &eva)
Add an object from a branch to the event.
virtual void LoadQChain()
Build a QChain.
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...