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 #include "oneapi/tbb/task_arena.h"
26 
27 namespace edm {
28 
42  public:
44  DuplicateTreeSentry(DuplicateTreeSentry const&) = delete; // Disallow copying and moving
46 
47  TTree* tree() const { return mytree_ ? mytree_.get() : tree_; }
48 
49  private:
51  void operator()(TFile* iFile) const {
52  if (iFile) {
53  iFile->Close();
54  }
55  delete iFile;
56  }
57  };
58 
59  void dup() {
61  if (!pSLC.isAvailable()) {
62  return;
63  }
64  if (pSLC->sourceCacheHint() && *(pSLC->sourceCacheHint()) == "lazy-download") {
65  return;
66  }
67  if (!pSLC->sourceCloneCacheHint() || *(pSLC->sourceCloneCacheHint()) != "lazy-download") {
68  return;
69  }
70  edm::LogWarning("DuplicateTreeSentry") << "Re-opening file for fast-cloning";
71 
72  TFile* file = tree_->GetCurrentFile();
73  const TUrl* url = file->GetEndpointUrl();
74  if (!url) {
75  return;
76  }
77  file_.reset(TFile::Open(url->GetUrl(), "READWRAP")); // May throw an exception.
78  if (!file_) {
79  return;
80  }
81  mytree_.reset(dynamic_cast<TTree*>(file_->Get(tree_->GetName())));
82  if (!mytree_) {
83  return;
84  }
85  }
86 
91  std::unique_ptr<TFile, CloseBeforeDelete> file_;
92  TTree* tree_ = nullptr;
93  std::unique_ptr<TTree> mytree_ = nullptr;
94  };
95 
96  RootOutputTree::RootOutputTree(std::shared_ptr<TFile> filePtr,
97  BranchType const& branchType,
98  int splitLevel,
99  int treeMaxVirtualSize,
100  std::string const& processName)
101  : filePtr_(filePtr),
102  tree_(processName.empty()
103  ? makeTTree(filePtr.get(), BranchTypeToProductTreeName(branchType), splitLevel)
104  : makeTTree(filePtr.get(), BranchTypeToProductTreeName(branchType, processName), splitLevel)),
105  producedBranches_(),
106  readBranches_(),
107  auxBranches_(),
108  unclonedReadBranches_(),
109  clonedReadBranchNames_(),
110  currentlyFastCloning_(),
111  fastCloneAuxBranches_(false) {
112  if (treeMaxVirtualSize >= 0)
113  tree_->SetMaxVirtualSize(treeMaxVirtualSize);
114  }
115 
116  TTree* RootOutputTree::assignTTree(TFile* filePtr, TTree* tree) {
117  tree->SetDirectory(filePtr);
118  // Turn off autosaving because it is such a memory hog and we are not using
119  // this check-pointing feature anyway.
121  return tree;
122  }
123 
124  TTree* RootOutputTree::makeTTree(TFile* filePtr, std::string const& name, int splitLevel) {
125  TTree* tree = new TTree(name.c_str(), "", splitLevel);
126  if (!tree)
127  throw edm::Exception(errors::FatalRootError) << "Failed to create the tree: " << name << "\n";
128  if (tree->IsZombie())
129  throw edm::Exception(errors::FatalRootError) << "Tree: " << name << " is a zombie."
130  << "\n";
131 
132  return assignTTree(filePtr, tree);
133  }
134 
135  bool RootOutputTree::checkSplitLevelsAndBasketSizes(TTree* inputTree) const {
136  assert(inputTree != nullptr);
137 
138  // Do the split level and basket size match in the input and output?
139  for (auto const& outputBranch : readBranches_) {
140  if (outputBranch != nullptr) {
141  TBranch* inputBranch = inputTree->GetBranch(outputBranch->GetName());
142 
143  if (inputBranch != nullptr) {
144  if (inputBranch->GetSplitLevel() != outputBranch->GetSplitLevel() ||
145  inputBranch->GetBasketSize() != outputBranch->GetBasketSize()) {
146  return false;
147  }
148  }
149  }
150  }
151  return true;
152  }
153 
154  namespace {
155  bool checkMatchingBranches(TBranchElement* inputBranch, TBranchElement* outputBranch) {
156  if (inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
157  return false;
158  }
159  TObjArray* inputArray = inputBranch->GetListOfBranches();
160  TObjArray* outputArray = outputBranch->GetListOfBranches();
161 
162  if (outputArray->GetSize() < inputArray->GetSize()) {
163  return false;
164  }
165  TIter iter(outputArray);
166  TObject* obj = nullptr;
167  while ((obj = iter.Next()) != nullptr) {
168  TBranchElement* outBranch = dynamic_cast<TBranchElement*>(obj);
169  if (outBranch) {
170  TBranchElement* inBranch = dynamic_cast<TBranchElement*>(inputArray->FindObject(outBranch->GetName()));
171  if (!inBranch) {
172  return false;
173  }
174  if (!checkMatchingBranches(inBranch, outBranch)) {
175  return false;
176  }
177  }
178  }
179  return true;
180  }
181  } // namespace
182 
183  bool RootOutputTree::checkIfFastClonable(TTree* inputTree) const {
184  if (inputTree == nullptr)
185  return false;
186 
187  // 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.
188  for (auto const& outputBr : readBranches_) {
189  TBranchElement* outputBranch = dynamic_cast<TBranchElement*>(outputBr);
190  if (outputBranch != nullptr) {
191  TBranchElement* inputBranch = dynamic_cast<TBranchElement*>(inputTree->GetBranch(outputBranch->GetName()));
192  if (inputBranch != nullptr) {
193  // We have a matching top level branch. Do the recursive check on subbranches.
194  if (!checkMatchingBranches(inputBranch, outputBranch)) {
195  LogInfo("FastCloning") << "Fast Cloning disabled because a data member has been added to split branch: "
196  << inputBranch->GetName() << "\n.";
197  return false;
198  }
199  }
200  }
201  }
202  return true;
203  }
204 
205  namespace {
206  void setMatchingBranchSizes(TBranchElement* inputBranch, TBranchElement* outputBranch) {
207  if (inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
208  return;
209  }
210  TObjArray* inputArray = inputBranch->GetListOfBranches();
211  TObjArray* outputArray = outputBranch->GetListOfBranches();
212 
213  if (outputArray->GetSize() < inputArray->GetSize()) {
214  return;
215  }
216  TIter iter(outputArray);
217  TObject* obj = nullptr;
218  while ((obj = iter.Next()) != nullptr) {
219  TBranchElement* outBranch = dynamic_cast<TBranchElement*>(obj);
220  if (outBranch) {
221  TBranchElement* inBranch = dynamic_cast<TBranchElement*>(inputArray->FindObject(outBranch->GetName()));
222  if (inBranch) {
223  outBranch->SetBasketSize(inBranch->GetBasketSize());
224  setMatchingBranchSizes(inBranch, outBranch);
225  }
226  }
227  }
228  }
229  } // namespace
230 
231  void RootOutputTree::setSubBranchBasketSizes(TTree* inputTree) const {
232  if (inputTree == nullptr)
233  return;
234 
235  for (auto const& outputBr : readBranches_) {
236  TBranchElement* outputBranch = dynamic_cast<TBranchElement*>(outputBr);
237  if (outputBranch != nullptr) {
238  TBranchElement* inputBranch = dynamic_cast<TBranchElement*>(inputTree->GetBranch(outputBranch->GetName()));
239  if (inputBranch != nullptr) {
240  // We have a matching top level branch. Do the recursion on the subbranches.
241  setMatchingBranchSizes(inputBranch, outputBranch);
242  }
243  }
244  }
245  }
246 
247  bool RootOutputTree::checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const {
248  for (auto const& readBranch : readBranches_) {
249  if (readBranch->GetEntries() != expectedNumberOfEntries) {
250  return false;
251  }
252  }
253  return true;
254  }
255 
257  if (in->GetEntries() != 0) {
258  TObjArray* branches = tree_->GetListOfBranches();
259  // If any products were produced (not just event products), the EventAuxiliary will be modified.
260  // In that case, don't fast copy auxiliary branches. Remove them, and add back after fast copying.
261  std::map<Int_t, TBranch*> auxIndexes;
262  bool mustRemoveSomeAuxs = false;
263  if (!fastCloneAuxBranches_) {
264  for (auto const& auxBranch : auxBranches_) {
265  int auxIndex = branches->IndexOf(auxBranch);
266  assert(auxIndex >= 0);
267  auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
268  branches->RemoveAt(auxIndex);
269  }
270  mustRemoveSomeAuxs = true;
271  }
272 
273  //Deal with any aux branches which can never be cloned
274  for (auto const& auxBranch : unclonedAuxBranches_) {
275  int auxIndex = branches->IndexOf(auxBranch);
276  assert(auxIndex >= 0);
277  auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
278  branches->RemoveAt(auxIndex);
279  mustRemoveSomeAuxs = true;
280  }
281 
282  if (mustRemoveSomeAuxs) {
283  branches->Compress();
284  }
285 
286  DuplicateTreeSentry dupTree(in);
287  TTreeCloner cloner(
288  dupTree.tree(), tree_, option.c_str(), TTreeCloner::kNoWarnings | TTreeCloner::kIgnoreMissingTopLevel);
289 
290  if (!cloner.IsValid()) {
291  // Let's check why
292  static const char* okerror = "One of the export branch";
293  if (strncmp(cloner.GetWarning(), okerror, strlen(okerror)) == 0) {
294  // That's fine we will handle it;
295  } else {
296  throw edm::Exception(errors::FatalRootError) << "invalid TTreeCloner (" << cloner.GetWarning() << ")\n";
297  }
298  }
299  tree_->SetEntries(tree_->GetEntries() + in->GetEntries());
300  Service<RootHandlers> rootHandler;
301  rootHandler->ignoreWarningsWhileDoing([&cloner] { cloner.Exec(); });
302 
303  if (mustRemoveSomeAuxs) {
304  for (auto const& auxIndex : auxIndexes) {
305  // Add the auxiliary branches back after fast copying the rest of the tree.
306  Int_t last = branches->GetLast();
307  if (last >= 0) {
308  branches->AddAtAndExpand(branches->At(last), last + 1);
309  for (Int_t ind = last - 1; ind >= auxIndex.first; --ind) {
310  branches->AddAt(branches->At(ind), ind + 1);
311  };
312  branches->AddAt(auxIndex.second, auxIndex.first);
313  } else {
314  branches->Add(auxIndex.second);
315  }
316  }
317  }
318  }
319  }
320 
322  if (tree->GetNbranches() != 0) {
323  // This is required when Fill is called on individual branches
324  // in the TTree instead of calling Fill once for the entire TTree.
325  tree->SetEntries(-1);
326  }
327  setRefCoreStreamer(true);
328  tree->AutoSave("FlushBaskets");
329  }
330 
331  void RootOutputTree::fillTTree(std::vector<TBranch*> const& branches) {
332  for_all(branches, std::bind(&TBranch::Fill, std::placeholders::_1));
333  }
334 
336 
337  void RootOutputTree::maybeFastCloneTree(bool canFastClone,
338  bool canFastCloneAux,
339  TTree* tree,
340  std::string const& option) {
341  unclonedReadBranches_.clear();
342  clonedReadBranchNames_.clear();
343  currentlyFastCloning_ = canFastClone && !readBranches_.empty();
344  if (currentlyFastCloning_) {
345  fastCloneAuxBranches_ = canFastCloneAux;
347  for (auto const& branch : readBranches_) {
348  if (branch->GetEntries() == tree_->GetEntries()) {
349  clonedReadBranchNames_.insert(std::string(branch->GetName()));
350  } else {
351  unclonedReadBranches_.push_back(branch);
352  }
353  }
354  Service<JobReport> reportSvc;
355  reportSvc->reportFastClonedBranches(clonedReadBranchNames_, tree_->GetEntries());
356  }
357  }
358 
360  if (currentlyFastCloning_) {
366  } else {
367  // Isolate the fill operation so that IMT doesn't grab other large tasks
368  // that could lead to PoolOutputModule stalling
369  oneapi::tbb::this_task_arena::isolate([&] { tree_->Fill(); });
370  }
371  }
372 
374  std::string const& className,
375  void const*& pProd,
376  int splitLevel,
377  int basketSize,
378  bool produced) {
381  TBranch* branch = tree_->Branch(branchName.c_str(), className.c_str(), &pProd, basketSize, splitLevel);
382  assert(branch != nullptr);
383  /*
384  if(pProd != nullptr) {
385  // Delete the product that ROOT has allocated.
386  WrapperBase const* edp = static_cast<WrapperBase const *>(pProd);
387  delete edp;
388  pProd = nullptr;
389  }
390 */
391  if (produced) {
392  producedBranches_.push_back(branch);
393  } else {
394  readBranches_.push_back(branch);
395  }
396  }
397 
399  // The TFile was just closed.
400  // Just to play it safe, zero all pointers to quantities in the file.
401  auxBranches_.clear();
402  unclonedAuxBranches_.clear();
403  producedBranches_.clear();
404  readBranches_.clear();
405  unclonedReadBranches_.clear();
406  tree_ = nullptr; // propagate_const<T> has no reset() function
407  filePtr_ = nullptr; // propagate_const<T> has no reset() function
408  }
409 } // namespace edm
std::set< std::string > clonedReadBranchNames_
edm::propagate_const< TTree * > tree_
def readBranch(tree, branchName)
bool checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const
static int const invalidSplitLevel
void setSubBranchBasketSizes(TTree *inputTree) const
DuplicateTreeSentry(TTree *tree)
static int const invalidBasketSize
void setRefCoreStreamer(bool resetAll=false)
std::vector< TBranch * > unclonedAuxBranches_
edm::propagate_const< std::shared_ptr< TFile > > filePtr_
assert(be >=bs)
BranchType
Definition: BranchType.h:11
static void fillTTree(std::vector< TBranch *> const &branches)
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > auxBranches_
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:14
std::unique_ptr< TTree > mytree_
RootOutputTree(std::shared_ptr< TFile > filePtr, BranchType const &branchType, int splitLevel, int treeMaxVirtualSize, std::string const &processName=std::string())
bool checkSplitLevelsAndBasketSizes(TTree *inputTree) const
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)
std::string const & BranchTypeToProductTreeName(BranchType const &branchType)
Definition: BranchType.cc:95
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
virtual std::string const * sourceCloneCacheHint() const =0
std::vector< TBranch * > unclonedReadBranches_
static TTree * makeTTree(TFile *filePtr, std::string const &name, int splitLevel)
Log< level::Info, false > LogInfo
DuplicateTreeSentry & operator=(DuplicateTreeSentry const &)=delete
static void writeTTree(TTree *tree)
TTree const * tree() const
std::vector< TBranch * > readBranches_
HLT enums.
T const & get(Event const &event, InputTag const &tag) noexcept(false)
Definition: Event.h:680
virtual std::string const * sourceCacheHint() const =0
bool isAvailable() const
Definition: Service.h:40
Definition: tree.py:1
Log< level::Warning, false > LogWarning
void maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree *tree, std::string const &option)
bool checkIfFastClonable(TTree *inputTree) const
std::string className(const T &t)
Definition: ClassName.h:31
void fastCloneTTree(TTree *in, std::string const &option)
std::unique_ptr< TFile, CloseBeforeDelete > file_