test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RootOutputTree.cc
Go to the documentation of this file.
1 
2 #include "RootOutputTree.h"
3 
13 
14 #include "TBranch.h"
15 #include "TBranchElement.h"
16 #include "TCollection.h"
17 #include "TFile.h"
18 #include "TTreeCloner.h"
19 #include "Rtypes.h"
20 #include "RVersion.h"
21 
22 #include "boost/bind.hpp"
23 #include <limits>
24 
25 namespace edm {
26 
28  boost::shared_ptr<TFile> filePtr,
29  BranchType const& branchType,
30  int splitLevel,
31  int treeMaxVirtualSize) :
32  filePtr_(filePtr),
33  tree_(makeTTree(filePtr.get(), BranchTypeToProductTreeName(branchType), splitLevel)),
34  producedBranches_(),
35  readBranches_(),
36  auxBranches_(),
37  unclonedReadBranches_(),
38  clonedReadBranchNames_(),
39  currentlyFastCloning_(),
40  fastCloneAuxBranches_(false) {
41 
42  if(treeMaxVirtualSize >= 0) tree_->SetMaxVirtualSize(treeMaxVirtualSize);
43  }
44 
45  TTree*
46  RootOutputTree::assignTTree(TFile* filePtr, TTree* tree) {
47  tree->SetDirectory(filePtr);
48  // Turn off autosaving because it is such a memory hog and we are not using
49  // this check-pointing feature anyway.
50  tree->SetAutoSave(std::numeric_limits<Long64_t>::max());
51  return tree;
52  }
53 
54  TTree*
55  RootOutputTree::makeTTree(TFile* filePtr, std::string const& name, int splitLevel) {
56  TTree* tree = new TTree(name.c_str(), "", splitLevel);
57  if(!tree) throw edm::Exception(errors::FatalRootError)
58  << "Failed to create the tree: " << name << "\n";
59  if(tree->IsZombie())
61  << "Tree: " << name << " is a zombie." << "\n";
62 
63  return assignTTree(filePtr, tree);
64  }
65 
66  bool
68 
69  assert (inputTree != 0);
70 
71  // Do the split level and basket size match in the input and output?
72  for(std::vector<TBranch*>::const_iterator it = readBranches_.begin(), itEnd = readBranches_.end();
73  it != itEnd; ++it) {
74 
75  TBranch* outputBranch = *it;
76  if(outputBranch != 0) {
77  TBranch* inputBranch = inputTree->GetBranch(outputBranch->GetName());
78 
79  if(inputBranch != 0) {
80  if(inputBranch->GetSplitLevel() != outputBranch->GetSplitLevel() ||
81  inputBranch->GetBasketSize() != outputBranch->GetBasketSize()) {
82  return false;
83  }
84  }
85  }
86  }
87  return true;
88  }
89 
90  namespace {
91  bool checkMatchingBranches(TBranchElement* inputBranch, TBranchElement* outputBranch) {
92  if(inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
93  return false;
94  }
95  TObjArray* inputArray = inputBranch->GetListOfBranches();
96  TObjArray* outputArray = outputBranch->GetListOfBranches();
97 
98  if(outputArray->GetSize() < inputArray->GetSize()) {
99  return false;
100  }
101  TIter iter(outputArray);
102  TObject* obj = 0;
103  while((obj = iter.Next()) != 0) {
104  TBranchElement* outBranch = dynamic_cast<TBranchElement*>(obj);
105  if(outBranch) {
106  TBranchElement* inBranch = dynamic_cast<TBranchElement*>(inputArray->FindObject(outBranch->GetName()));
107  if(!inBranch) {
108  return false;
109  }
110  if(!checkMatchingBranches(inBranch, outBranch)) {
111  return false;
112  }
113  }
114  }
115  return true;
116  }
117  }
118 
119  bool RootOutputTree::checkIfFastClonable(TTree* inputTree) const {
120 
121  if(inputTree == 0) return false;
122 
123  // Do the sub-branches match in the input and output. Extra sub-branches in the input are OK for fast cloning, but not in the output.
124  for(std::vector<TBranch*>::const_iterator it = readBranches_.begin(), itEnd = readBranches_.end(); it != itEnd; ++it) {
125  TBranchElement* outputBranch = dynamic_cast<TBranchElement*>(*it);
126  if(outputBranch != 0) {
127  TBranchElement* inputBranch = dynamic_cast<TBranchElement*>(inputTree->GetBranch(outputBranch->GetName()));
128  if(inputBranch != 0) {
129  // We have a matching top level branch. Do the recursive check on subbranches.
130  if(!checkMatchingBranches(inputBranch, outputBranch)) {
131  LogInfo("FastCloning")
132  << "Fast Cloning disabled because a data member has been added to split branch: " << inputBranch->GetName() << "\n.";
133  return false;
134  }
135  }
136  }
137  }
138  return true;
139  }
140 
141  bool RootOutputTree::checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const {
142  for(std::vector<TBranch*>::const_iterator it = readBranches_.begin(), itEnd = readBranches_.end(); it != itEnd; ++it) {
143  if((*it)->GetEntries() != expectedNumberOfEntries) {
144  return false;
145  }
146  }
147  return true;
148  }
149 
150  void
152  if(in->GetEntries() != 0) {
153  TObjArray* branches = tree_->GetListOfBranches();
154  // If any products were produced (not just event products), the EventAuxiliary will be modified.
155  // In that case, don't fast copy auxiliary branches. Remove them, and add back after fast copying.
156  std::map<Int_t, TBranch *> auxIndexes;
157  bool mustRemoveSomeAuxs = false;
158  if(!fastCloneAuxBranches_) {
159  for(std::vector<TBranch *>::const_iterator it = auxBranches_.begin(), itEnd = auxBranches_.end();
160  it != itEnd; ++it) {
161  int auxIndex = branches->IndexOf(*it);
162  assert (auxIndex >= 0);
163  auxIndexes.insert(std::make_pair(auxIndex, *it));
164  branches->RemoveAt(auxIndex);
165  }
166  mustRemoveSomeAuxs = true;
167  }
168 
169  //Deal with any aux branches which can never be cloned
170  for(std::vector<TBranch *>::const_iterator it = unclonedAuxBranches_.begin(),
171  itEnd = unclonedAuxBranches_.end();
172  it != itEnd; ++it) {
173  int auxIndex = branches->IndexOf(*it);
174  assert (auxIndex >= 0);
175  auxIndexes.insert(std::make_pair(auxIndex, *it));
176  branches->RemoveAt(auxIndex);
177  mustRemoveSomeAuxs = true;
178  }
179 
180  if(mustRemoveSomeAuxs) {
181  branches->Compress();
182  }
183 
184  TTreeCloner cloner(in, tree_, option.c_str(), TTreeCloner::kNoWarnings|TTreeCloner::kIgnoreMissingTopLevel);
185 
186  if(!cloner.IsValid()) {
187  // Let's check why
188  static const char* okerror = "One of the export branch";
189  if(strncmp(cloner.GetWarning(), okerror, strlen(okerror)) == 0) {
190  // That's fine we will handle it;
191  } else {
193  << "invalid TTreeCloner (" << cloner.GetWarning() << ")\n";
194  }
195  }
196  tree_->SetEntries(tree_->GetEntries() + in->GetEntries());
197  Service<RootHandlers> rootHandler;
198  rootHandler->ignoreWarningsWhileDoing([&cloner] { cloner.Exec(); });
199  if(mustRemoveSomeAuxs) {
200  for(std::map<Int_t, TBranch *>::const_iterator it = auxIndexes.begin(), itEnd = auxIndexes.end();
201  it != itEnd; ++it) {
202  // Add the auxiliary branches back after fast copying the rest of the tree.
203  Int_t last = branches->GetLast();
204  if(last >= 0) {
205  branches->AddAtAndExpand(branches->At(last), last+1);
206  for(Int_t ind = last-1; ind >= it->first; --ind) {
207  branches->AddAt(branches->At(ind), ind+1);
208  };
209  branches->AddAt(it->second, it->first);
210  } else {
211  branches->Add(it->second);
212  }
213  }
214  }
215  }
216  }
217 
218  void
220  if(tree->GetNbranches() != 0) {
221  tree->SetEntries(-1);
222  }
223  setRefCoreStreamer(true);
224  tree->AutoSave("FlushBaskets");
225  }
226 
227  void
228  RootOutputTree::fillTTree(std::vector<TBranch*> const& branches) {
229  for_all(branches, boost::bind(&TBranch::Fill, _1));
230  }
231 
232  void
234  writeTTree(tree_);
235  }
236 
237  void
238  RootOutputTree::maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree* tree, std::string const& option) {
239  unclonedReadBranches_.clear();
240  clonedReadBranchNames_.clear();
241  currentlyFastCloning_ = canFastClone && !readBranches_.empty();
243  fastCloneAuxBranches_ = canFastCloneAux;
244  fastCloneTTree(tree, option);
245  for(std::vector<TBranch*>::const_iterator it = readBranches_.begin(), itEnd = readBranches_.end();
246  it != itEnd; ++it) {
247  if((*it)->GetEntries() == tree_->GetEntries()) {
248  clonedReadBranchNames_.insert(std::string((*it)->GetName()));
249  } else {
250  unclonedReadBranches_.push_back(*it);
251  }
252  }
253  Service<JobReport> reportSvc;
254  reportSvc->reportFastClonedBranches(clonedReadBranchNames_, tree_->GetEntries());
255  }
256  }
257 
258  void
265  } else {
266  tree_->Fill();
267  }
268  }
269 
270  void
272  std::string const& className,
273  WrapperInterfaceBase const* interface,
274  void const*& pProd,
275  int splitLevel,
276  int basketSize,
277  bool produced) {
278  assert(splitLevel != BranchDescription::invalidSplitLevel);
279  assert(basketSize != BranchDescription::invalidBasketSize);
280  TBranch* branch = tree_->Branch(branchName.c_str(),
281  className.c_str(),
282  &pProd,
283  basketSize,
284  splitLevel);
285  assert(branch != 0);
286  if(pProd != 0) {
287  // Delete the product that ROOT has allocated.
288  interface->deleteProduct(pProd);
289  pProd = 0;
290  }
291  if(produced) {
292  producedBranches_.push_back(branch);
293  } else {
294  readBranches_.push_back(branch);
295  }
296  }
297 
298  void
300  // The TFile was just closed.
301  // Just to play it safe, zero all pointers to quantities in the file.
302  auxBranches_.clear();
303  unclonedAuxBranches_.clear();
304  producedBranches_.clear();
305  readBranches_.clear();
306  unclonedReadBranches_.clear();
307  tree_ = 0;
308  filePtr_.reset();
309  }
310 }
std::set< std::string > clonedReadBranchNames_
void writeTree() const
static int const invalidSplitLevel
void fillTree() const
static int const invalidBasketSize
static void fillTTree(std::vector< TBranch * > const &branches)
bool checkSplitLevelsAndBasketSizes(TTree *inputTree) const
void setRefCoreStreamer(bool resetAll=false)
std::vector< TBranch * > unclonedAuxBranches_
bool checkIfFastClonable(TTree *inputTree) const
void addBranch(std::string const &branchName, std::string const &className, WrapperInterfaceBase const *interface, void const *&pProd, int splitLevel, int basketSize, bool produced)
BranchType
Definition: BranchType.h:11
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > auxBranches_
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:16
boost::shared_ptr< TFile > filePtr_
static TTree * assignTTree(TFile *file, TTree *tree)
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
std::vector< TBranch * > unclonedReadBranches_
bool checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const
std::string const & BranchTypeToProductTreeName(BranchType const &branchType)
Definition: BranchType.cc:102
static TTree * makeTTree(TFile *filePtr, std::string const &name, int splitLevel)
void deleteProduct(void const *me) const
eventsetup::produce::Produce produced
Definition: ESProducts.cc:20
RootOutputTree(boost::shared_ptr< TFile > filePtr, BranchType const &branchType, int splitLevel, int treeMaxVirtualSize)
static void writeTTree(TTree *tree)
std::vector< TBranch * > readBranches_
TTree * tree() const
volatile std::atomic< bool > shutdown_flag false
void maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree *tree, std::string const &option)
T get(const Candidate &c)
Definition: component.h:55
std::string className(const T &t)
Definition: ClassName.h:30
void fastCloneTTree(TTree *in, std::string const &option)