CMS 3D CMS Logo

RootOutputTree.cc
Go to the documentation of this file.
1 
2 #include "RootOutputTree.h"
3 
14 
15 #include "TBranch.h"
16 #include "TBranchElement.h"
17 #include "TCollection.h"
18 #include "TFile.h"
19 #include "TTreeCloner.h"
20 #include "Rtypes.h"
21 #include "RVersion.h"
22 
23 #include <limits>
24 
25 namespace edm {
26 
40  {
41  public:
43  : tree_(tree)
44  {
45  dup();
46  }
47 
48  TTree *tree() const {return mytree_ ? mytree_.get() : tree_;}
49 
50  private:
51  DuplicateTreeSentry(DuplicateTreeSentry const&) = delete; // Disallow copying and moving
53  struct CloseBeforeDelete { void operator()(TFile* iFile) const { if( iFile) { iFile->Close(); } delete iFile; } };
54 
55  void dup()
56  {
58  if (!pSLC.isAvailable()) {return;}
59  if (pSLC->sourceCacheHint() && *(pSLC->sourceCacheHint()) == "lazy-download") {return;}
60  if (!pSLC->sourceCloneCacheHint() || *(pSLC->sourceCloneCacheHint()) != "lazy-download") {return;}
61  edm::LogWarning("DuplicateTreeSentry") << "Re-opening file for fast-cloning";
62 
63  TFile *file = tree_->GetCurrentFile();
64  const TUrl *url = file->GetEndpointUrl();
65  if (!url)
66  {
67  return;
68  }
69  file_.reset(TFile::Open(url->GetUrl(), "READWRAP")); // May throw an exception.
70  if (!file_)
71  {
72  return;
73  }
74  mytree_.reset(dynamic_cast<TTree*>(file_->Get(tree_->GetName())));
75  if (!mytree_) {return;}
76  }
77 
82  std::unique_ptr<TFile, CloseBeforeDelete> file_;
83  TTree *tree_ = nullptr;
84  std::unique_ptr<TTree> mytree_ = nullptr;
85  };
86 
88  std::shared_ptr<TFile> filePtr,
89  BranchType const& branchType,
90  int splitLevel,
91  int treeMaxVirtualSize) :
92  filePtr_(filePtr),
93  tree_(makeTTree(filePtr.get(), BranchTypeToProductTreeName(branchType), splitLevel)),
94  producedBranches_(),
95  readBranches_(),
96  auxBranches_(),
97  unclonedReadBranches_(),
98  clonedReadBranchNames_(),
99  currentlyFastCloning_(),
100  fastCloneAuxBranches_(false) {
101 
102  if(treeMaxVirtualSize >= 0) tree_->SetMaxVirtualSize(treeMaxVirtualSize);
103  }
104 
105  TTree*
106  RootOutputTree::assignTTree(TFile* filePtr, TTree* tree) {
107  tree->SetDirectory(filePtr);
108  // Turn off autosaving because it is such a memory hog and we are not using
109  // this check-pointing feature anyway.
110  tree->SetAutoSave(std::numeric_limits<Long64_t>::max());
111  return tree;
112  }
113 
114  TTree*
115  RootOutputTree::makeTTree(TFile* filePtr, std::string const& name, int splitLevel) {
116  TTree* tree = new TTree(name.c_str(), "", splitLevel);
117  if(!tree) throw edm::Exception(errors::FatalRootError)
118  << "Failed to create the tree: " << name << "\n";
119  if(tree->IsZombie())
121  << "Tree: " << name << " is a zombie." << "\n";
122 
123  return assignTTree(filePtr, tree);
124  }
125 
126  bool
128 
129  assert(inputTree != nullptr);
130 
131  // Do the split level and basket size match in the input and output?
132  for(auto const& outputBranch : readBranches_) {
133  if(outputBranch != nullptr) {
134  TBranch* inputBranch = inputTree->GetBranch(outputBranch->GetName());
135 
136  if(inputBranch != nullptr) {
137  if(inputBranch->GetSplitLevel() != outputBranch->GetSplitLevel() ||
138  inputBranch->GetBasketSize() != outputBranch->GetBasketSize()) {
139  return false;
140  }
141  }
142  }
143  }
144  return true;
145  }
146 
147  namespace {
148  bool checkMatchingBranches(TBranchElement* inputBranch, TBranchElement* outputBranch) {
149  if(inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
150  return false;
151  }
152  TObjArray* inputArray = inputBranch->GetListOfBranches();
153  TObjArray* outputArray = outputBranch->GetListOfBranches();
154 
155  if(outputArray->GetSize() < inputArray->GetSize()) {
156  return false;
157  }
158  TIter iter(outputArray);
159  TObject* obj = nullptr;
160  while((obj = iter.Next()) != nullptr) {
161  TBranchElement* outBranch = dynamic_cast<TBranchElement*>(obj);
162  if(outBranch) {
163  TBranchElement* inBranch = dynamic_cast<TBranchElement*>(inputArray->FindObject(outBranch->GetName()));
164  if(!inBranch) {
165  return false;
166  }
167  if(!checkMatchingBranches(inBranch, outBranch)) {
168  return false;
169  }
170  }
171  }
172  return true;
173  }
174  }
175 
176  bool RootOutputTree::checkIfFastClonable(TTree* inputTree) const {
177 
178  if(inputTree == nullptr) return false;
179 
180  // 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.
181  for(auto const& outputBr : readBranches_) {
182  TBranchElement* outputBranch = dynamic_cast<TBranchElement*>(outputBr);
183  if(outputBranch != nullptr) {
184  TBranchElement* inputBranch = dynamic_cast<TBranchElement*>(inputTree->GetBranch(outputBranch->GetName()));
185  if(inputBranch != nullptr) {
186  // We have a matching top level branch. Do the recursive check on subbranches.
187  if(!checkMatchingBranches(inputBranch, outputBranch)) {
188  LogInfo("FastCloning")
189  << "Fast Cloning disabled because a data member has been added to split branch: " << inputBranch->GetName() << "\n.";
190  return false;
191  }
192  }
193  }
194  }
195  return true;
196  }
197 
198  bool RootOutputTree::checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const {
199  for(auto const& readBranch : readBranches_) {
200  if(readBranch->GetEntries() != expectedNumberOfEntries) {
201  return false;
202  }
203  }
204  return true;
205  }
206 
207  void
209  if(in->GetEntries() != 0) {
210  TObjArray* branches = tree_->GetListOfBranches();
211  // If any products were produced (not just event products), the EventAuxiliary will be modified.
212  // In that case, don't fast copy auxiliary branches. Remove them, and add back after fast copying.
213  std::map<Int_t, TBranch *> auxIndexes;
214  bool mustRemoveSomeAuxs = false;
215  if(!fastCloneAuxBranches_) {
216  for(auto const& auxBranch : auxBranches_) {
217  int auxIndex = branches->IndexOf(auxBranch);
218  assert (auxIndex >= 0);
219  auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
220  branches->RemoveAt(auxIndex);
221  }
222  mustRemoveSomeAuxs = true;
223  }
224 
225  //Deal with any aux branches which can never be cloned
226  for(auto const& auxBranch : unclonedAuxBranches_) {
227  int auxIndex = branches->IndexOf(auxBranch);
228  assert (auxIndex >= 0);
229  auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
230  branches->RemoveAt(auxIndex);
231  mustRemoveSomeAuxs = true;
232  }
233 
234  if(mustRemoveSomeAuxs) {
235  branches->Compress();
236  }
237 
238  DuplicateTreeSentry dupTree(in);
239  TTreeCloner cloner(dupTree.tree(), tree_, option.c_str(), TTreeCloner::kNoWarnings|TTreeCloner::kIgnoreMissingTopLevel);
240 
241  if(!cloner.IsValid()) {
242  // Let's check why
243  static const char* okerror = "One of the export branch";
244  if(strncmp(cloner.GetWarning(), okerror, strlen(okerror)) == 0) {
245  // That's fine we will handle it;
246  } else {
248  << "invalid TTreeCloner (" << cloner.GetWarning() << ")\n";
249  }
250  }
251  tree_->SetEntries(tree_->GetEntries() + in->GetEntries());
252  Service<RootHandlers> rootHandler;
253  rootHandler->ignoreWarningsWhileDoing([&cloner] { cloner.Exec(); });
254 
255  if(mustRemoveSomeAuxs) {
256  for(auto const& auxIndex : auxIndexes) {
257  // Add the auxiliary branches back after fast copying the rest of the tree.
258  Int_t last = branches->GetLast();
259  if(last >= 0) {
260  branches->AddAtAndExpand(branches->At(last), last+1);
261  for(Int_t ind = last-1; ind >= auxIndex.first; --ind) {
262  branches->AddAt(branches->At(ind), ind+1);
263  };
264  branches->AddAt(auxIndex.second, auxIndex.first);
265  } else {
266  branches->Add(auxIndex.second);
267  }
268  }
269  }
270  }
271  }
272 
273  void
275  if(tree->GetNbranches() != 0) {
276  tree->SetEntries(-1);
277  }
278  setRefCoreStreamer(true);
279  tree->AutoSave("FlushBaskets");
280  }
281 
282  void
283  RootOutputTree::fillTTree(std::vector<TBranch*> const& branches) {
284  for_all(branches, std::bind(&TBranch::Fill, std::placeholders::_1));
285  }
286 
287  void
289  writeTTree(tree());
290  }
291 
292  void
293  RootOutputTree::maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree* tree, std::string const& option) {
294  unclonedReadBranches_.clear();
295  clonedReadBranchNames_.clear();
296  currentlyFastCloning_ = canFastClone && !readBranches_.empty();
298  fastCloneAuxBranches_ = canFastCloneAux;
299  fastCloneTTree(tree, option);
300  for(auto const& branch : readBranches_) {
301  if(branch->GetEntries() == tree_->GetEntries()) {
302  clonedReadBranchNames_.insert(std::string(branch->GetName()));
303  } else {
304  unclonedReadBranches_.push_back(branch);
305  }
306  }
307  Service<JobReport> reportSvc;
308  reportSvc->reportFastClonedBranches(clonedReadBranchNames_, tree_->GetEntries());
309  }
310  }
311 
312  void
319  } else {
320  tree_->Fill();
321  }
322  }
323 
324  void
326  std::string const& className,
327  void const*& pProd,
328  int splitLevel,
329  int basketSize,
330  bool produced) {
331  assert(splitLevel != BranchDescription::invalidSplitLevel);
332  assert(basketSize != BranchDescription::invalidBasketSize);
333  TBranch* branch = tree_->Branch(branchName.c_str(),
334  className.c_str(),
335  &pProd,
336  basketSize,
337  splitLevel);
338  assert(branch != nullptr);
339 /*
340  if(pProd != nullptr) {
341  // Delete the product that ROOT has allocated.
342  WrapperBase const* edp = static_cast<WrapperBase const *>(pProd);
343  delete edp;
344  pProd = nullptr;
345  }
346 */
347  if(produced) {
348  producedBranches_.push_back(branch);
349  } else {
350  readBranches_.push_back(branch);
351  }
352  }
353 
354  void
356  // The TFile was just closed.
357  // Just to play it safe, zero all pointers to quantities in the file.
358  auxBranches_.clear();
359  unclonedAuxBranches_.clear();
360  producedBranches_.clear();
361  readBranches_.clear();
362  unclonedReadBranches_.clear();
363  tree_ = nullptr; // propagate_const<T> has no reset() function
364  filePtr_ = nullptr; // propagate_const<T> has no reset() function
365  }
366 }
virtual std::string const * sourceCacheHint() const =0
std::set< std::string > clonedReadBranchNames_
edm::propagate_const< TTree * > tree_
static int const invalidSplitLevel
DuplicateTreeSentry(TTree *tree)
static int const invalidBasketSize
static void fillTTree(std::vector< TBranch * > const &branches)
bool checkSplitLevelsAndBasketSizes(TTree *inputTree) const
TTree const * tree() const
void setRefCoreStreamer(bool resetAll=false)
std::vector< TBranch * > unclonedAuxBranches_
edm::propagate_const< std::shared_ptr< TFile > > filePtr_
bool checkIfFastClonable(TTree *inputTree) const
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
std::unique_ptr< TTree > mytree_
RootOutputTree(std::shared_ptr< TFile > filePtr, BranchType const &branchType, int splitLevel, int treeMaxVirtualSize)
static TTree * assignTTree(TFile *file, TTree *tree)
void addBranch(std::string const &branchName, std::string const &className, void const *&pProd, int splitLevel, int basketSize, bool produced)
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
bool isAvailable() const
Definition: Service.h:46
virtual std::string const * sourceCloneCacheHint() const =0
std::vector< TBranch * > unclonedReadBranches_
bool checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const
std::string const & BranchTypeToProductTreeName(BranchType const &branchType)
Definition: BranchType.cc:103
static TTree * makeTTree(TFile *filePtr, std::string const &name, int splitLevel)
DuplicateTreeSentry & operator=(DuplicateTreeSentry const &)=delete
static void writeTTree(TTree *tree)
std::vector< TBranch * > readBranches_
HLT enums.
Definition: tree.py:1
void maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree *tree, std::string const &option)
const eventsetup::produce::Produce produced
Definition: ESProducts.cc:20
T get(const Candidate &c)
Definition: component.h:55
def branchType(schema, name)
Definition: revisionDML.py:112
std::string className(const T &t)
Definition: ClassName.h:30
void fastCloneTTree(TTree *in, std::string const &option)
std::unique_ptr< TFile, CloseBeforeDelete > file_