CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes
edm::RootOutputTree Class Reference

#include <RootOutputTree.h>

Public Member Functions

template<typename T >
void addAuxiliary (std::string const &branchName, T const *&pAux, int bufSize, bool allowCloning=true)
 
template<typename T >
void addAuxiliary (std::string const &branchName, T *&pAux, int bufSize, bool allowCloning=true)
 
void addBranch (std::string const &branchName, std::string const &className, void const *&pProd, int splitLevel, int basketSize, bool produced)
 
bool checkEntriesInReadBranches (Long64_t expectedNumberOfEntries) const
 
bool checkIfFastClonable (TTree *inputTree) const
 
bool checkSplitLevelsAndBasketSizes (TTree *inputTree) const
 
void close ()
 
void fastCloneTTree (TTree *in, std::string const &option)
 
void fillTree ()
 
bool isValid () const
 
void maybeFastCloneTree (bool canFastClone, bool canFastCloneAux, TTree *tree, std::string const &option)
 
RootOutputTreeoperator= (RootOutputTree const &)=delete
 
void optimizeBaskets (ULong64_t size)
 
 RootOutputTree (std::shared_ptr< TFile > filePtr, BranchType const &branchType, int splitLevel, int treeMaxVirtualSize, std::string const &processName=std::string())
 
 RootOutputTree (RootOutputTree const &)=delete
 
void setAutoFlush (Long64_t size)
 
void setEntries ()
 
void setSubBranchBasketSizes (TTree *inputTree) const
 
TTree const * tree () const
 
TTree * tree ()
 
bool uncloned (std::string const &branchName) const
 
void writeTree ()
 
 ~RootOutputTree ()
 

Static Public Member Functions

static TTree * assignTTree (TFile *file, TTree *tree)
 
static TTree * makeTTree (TFile *filePtr, std::string const &name, int splitLevel)
 
static void writeTTree (TTree *tree)
 

Static Private Member Functions

static void fillTTree (std::vector< TBranch * > const &branches)
 

Private Attributes

std::vector< TBranch * > auxBranches_
 
std::set< std::string > clonedReadBranchNames_
 
bool currentlyFastCloning_
 
bool fastCloneAuxBranches_
 
edm::propagate_const
< std::shared_ptr< TFile > > 
filePtr_
 
std::vector< TBranch * > producedBranches_
 
std::vector< TBranch * > readBranches_
 
edm::propagate_const< TTree * > tree_
 
std::vector< TBranch * > unclonedAuxBranches_
 
std::vector< TBranch * > unclonedReadBranches_
 

Detailed Description

Definition at line 24 of file RootOutputTree.h.

Constructor & Destructor Documentation

edm::RootOutputTree::RootOutputTree ( std::shared_ptr< TFile >  filePtr,
BranchType const &  branchType,
int  splitLevel,
int  treeMaxVirtualSize,
std::string const &  processName = std::string() 
)

Definition at line 96 of file RootOutputTree.cc.

References tree_.

101  : filePtr_(filePtr),
102  tree_(processName.empty()
103  ? makeTTree(filePtr.get(), BranchTypeToProductTreeName(branchType), splitLevel)
104  : makeTTree(filePtr.get(), BranchTypeToProductTreeName(branchType, processName), splitLevel)),
106  readBranches_(),
107  auxBranches_(),
111  fastCloneAuxBranches_(false) {
112  if (treeMaxVirtualSize >= 0)
113  tree_->SetMaxVirtualSize(treeMaxVirtualSize);
114  }
std::set< std::string > clonedReadBranchNames_
edm::propagate_const< TTree * > tree_
edm::propagate_const< std::shared_ptr< TFile > > filePtr_
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > auxBranches_
std::vector< TBranch * > unclonedReadBranches_
std::string const & BranchTypeToProductTreeName(BranchType const &branchType)
Definition: BranchType.cc:95
static TTree * makeTTree(TFile *filePtr, std::string const &name, int splitLevel)
std::vector< TBranch * > readBranches_
edm::RootOutputTree::~RootOutputTree ( )
inline

Definition at line 32 of file RootOutputTree.h.

32 {}
edm::RootOutputTree::RootOutputTree ( RootOutputTree const &  )
delete

Member Function Documentation

template<typename T >
void edm::RootOutputTree::addAuxiliary ( std::string const &  branchName,
T const *&  pAux,
int  bufSize,
bool  allowCloning = true 
)
inline

Definition at line 38 of file RootOutputTree.h.

References auxBranches_, tree_, and unclonedAuxBranches_.

Referenced by edm::RootOutputFile::RootOutputFile().

38  {
39  if (allowCloning) {
40  auxBranches_.push_back(tree_->Branch(branchName.c_str(), &pAux, bufSize, 0));
41  } else {
42  unclonedAuxBranches_.push_back(tree_->Branch(branchName.c_str(), &pAux, bufSize, 0));
43  }
44  }
edm::propagate_const< TTree * > tree_
std::vector< TBranch * > unclonedAuxBranches_
std::vector< TBranch * > auxBranches_
template<typename T >
void edm::RootOutputTree::addAuxiliary ( std::string const &  branchName,
T *&  pAux,
int  bufSize,
bool  allowCloning = true 
)
inline

Definition at line 47 of file RootOutputTree.h.

References auxBranches_, tree_, and unclonedAuxBranches_.

47  {
48  if (allowCloning) {
49  auxBranches_.push_back(tree_->Branch(branchName.c_str(), &pAux, bufSize, 0));
50  } else {
51  unclonedAuxBranches_.push_back(tree_->Branch(branchName.c_str(), &pAux, bufSize, 0));
52  }
53  }
edm::propagate_const< TTree * > tree_
std::vector< TBranch * > unclonedAuxBranches_
std::vector< TBranch * > auxBranches_
void edm::RootOutputTree::addBranch ( std::string const &  branchName,
std::string const &  className,
void const *&  pProd,
int  splitLevel,
int  basketSize,
bool  produced 
)

Definition at line 373 of file RootOutputTree.cc.

References cms::cuda::assert(), edm::BranchDescription::invalidBasketSize, edm::BranchDescription::invalidSplitLevel, producedBranches_, readBranches_, dtDQMMerge_cfg::splitLevel, and tree_.

Referenced by edm::RootOutputFile::RootOutputFile().

378  {
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  }
edm::propagate_const< TTree * > tree_
static int const invalidSplitLevel
static int const invalidBasketSize
assert(be >=bs)
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > readBranches_
std::string className(const T &t)
Definition: ClassName.h:31
TTree * edm::RootOutputTree::assignTTree ( TFile *  file,
TTree *  tree 
)
static

Definition at line 116 of file RootOutputTree.cc.

References SiStripPI::max, and tree().

Referenced by makeTTree().

116  {
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  }
TTree const * tree() const
bool edm::RootOutputTree::checkEntriesInReadBranches ( Long64_t  expectedNumberOfEntries) const

Definition at line 247 of file RootOutputTree.cc.

References readBranches_.

Referenced by edm::RootOutputFile::writeIndexIntoFile().

247  {
248  for (auto const& readBranch : readBranches_) {
249  if (readBranch->GetEntries() != expectedNumberOfEntries) {
250  return false;
251  }
252  }
253  return true;
254  }
std::vector< TBranch * > readBranches_
bool edm::RootOutputTree::checkIfFastClonable ( TTree *  inputTree) const

Definition at line 183 of file RootOutputTree.cc.

References readBranches_.

Referenced by edm::RootOutputFile::beginInputFile().

183  {
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  }
Log< level::Info, false > LogInfo
std::vector< TBranch * > readBranches_
bool edm::RootOutputTree::checkSplitLevelsAndBasketSizes ( TTree *  inputTree) const

Definition at line 135 of file RootOutputTree.cc.

References cms::cuda::assert(), and readBranches_.

Referenced by edm::RootOutputFile::beginInputFile().

135  {
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  }
assert(be >=bs)
std::vector< TBranch * > readBranches_
void edm::RootOutputTree::close ( void  )

Definition at line 398 of file RootOutputTree.cc.

References auxBranches_, filePtr_, producedBranches_, readBranches_, tree_, unclonedAuxBranches_, and unclonedReadBranches_.

Referenced by esMonitoring.AsyncLineReaderMixin::handle_close(), and esMonitoring.FDJsonServer::handle_close().

398  {
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  }
edm::propagate_const< TTree * > tree_
std::vector< TBranch * > unclonedAuxBranches_
edm::propagate_const< std::shared_ptr< TFile > > filePtr_
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > auxBranches_
std::vector< TBranch * > unclonedReadBranches_
std::vector< TBranch * > readBranches_
void edm::RootOutputTree::fastCloneTTree ( TTree *  in,
std::string const &  option 
)

Definition at line 256 of file RootOutputTree.cc.

References cms::cuda::assert(), auxBranches_, Exception, fastCloneAuxBranches_, edm::errors::FatalRootError, dqmdumpme::last, edm::DuplicateTreeSentry::tree(), tree_, and unclonedAuxBranches_.

Referenced by maybeFastCloneTree().

256  {
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  }
edm::propagate_const< TTree * > tree_
std::vector< TBranch * > unclonedAuxBranches_
assert(be >=bs)
std::vector< TBranch * > auxBranches_
tuple last
Definition: dqmdumpme.py:56
void edm::RootOutputTree::fillTree ( )

Definition at line 359 of file RootOutputTree.cc.

References auxBranches_, currentlyFastCloning_, fastCloneAuxBranches_, fillTTree(), producedBranches_, tree_, unclonedAuxBranches_, and unclonedReadBranches_.

Referenced by core.AutoFillTreeProducer.AutoFillTreeProducer::process().

359  {
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  }
edm::propagate_const< TTree * > tree_
static void fillTTree(std::vector< TBranch * > const &branches)
std::vector< TBranch * > unclonedAuxBranches_
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > auxBranches_
std::vector< TBranch * > unclonedReadBranches_
void edm::RootOutputTree::fillTTree ( std::vector< TBranch * > const &  branches)
staticprivate

Definition at line 331 of file RootOutputTree.cc.

References HcalObjRepresent::Fill(), and edm::for_all().

Referenced by fillTree().

331  {
332  for_all(branches, std::bind(&TBranch::Fill, std::placeholders::_1));
333  }
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:14
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
bool edm::RootOutputTree::isValid ( ) const
TTree * edm::RootOutputTree::makeTTree ( TFile *  filePtr,
std::string const &  name,
int  splitLevel 
)
static

Definition at line 124 of file RootOutputTree.cc.

References assignTTree(), Exception, edm::errors::FatalRootError, dtDQMMerge_cfg::splitLevel, and tree().

Referenced by edm::RootOutputFile::RootOutputFile().

124  {
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  }
TTree const * tree() const
static TTree * assignTTree(TFile *file, TTree *tree)
void edm::RootOutputTree::maybeFastCloneTree ( bool  canFastClone,
bool  canFastCloneAux,
TTree *  tree,
std::string const &  option 
)

Definition at line 337 of file RootOutputTree.cc.

References clonedReadBranchNames_, currentlyFastCloning_, fastCloneAuxBranches_, fastCloneTTree(), readBranches_, AlCaHLTBitMon_QueryRunRegistry::string, tree_, and unclonedReadBranches_.

Referenced by edm::RootOutputFile::beginInputFile().

340  {
341  unclonedReadBranches_.clear();
342  clonedReadBranchNames_.clear();
343  currentlyFastCloning_ = canFastClone && !readBranches_.empty();
344  if (currentlyFastCloning_) {
345  fastCloneAuxBranches_ = canFastCloneAux;
346  fastCloneTTree(tree, option);
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  }
std::set< std::string > clonedReadBranchNames_
edm::propagate_const< TTree * > tree_
TTree const * tree() const
std::vector< TBranch * > unclonedReadBranches_
std::vector< TBranch * > readBranches_
void fastCloneTTree(TTree *in, std::string const &option)
RootOutputTree& edm::RootOutputTree::operator= ( RootOutputTree const &  )
delete
void edm::RootOutputTree::optimizeBaskets ( ULong64_t  size)
inline

Definition at line 101 of file RootOutputTree.h.

References tree_.

Referenced by edm::RootOutputFile::writeLuminosityBlock(), and edm::RootOutputFile::writeRun().

101 { tree_->OptimizeBaskets(size); }
edm::propagate_const< TTree * > tree_
tuple size
Write out results.
void edm::RootOutputTree::setAutoFlush ( Long64_t  size)
inline

Definition at line 103 of file RootOutputTree.h.

References tree_.

Referenced by edm::RootOutputFile::RootOutputFile().

103 { tree_->SetAutoFlush(size); }
edm::propagate_const< TTree * > tree_
tuple size
Write out results.
void edm::RootOutputTree::setEntries ( )
inline

Definition at line 90 of file RootOutputTree.h.

References tree_.

Referenced by edm::RootOutputFile::respondToCloseInputFile(), and edm::RootOutputFile::writeEventAuxiliary().

90  {
91  if (tree_->GetNbranches() != 0)
92  tree_->SetEntries(-1);
93  }
edm::propagate_const< TTree * > tree_
void edm::RootOutputTree::setSubBranchBasketSizes ( TTree *  inputTree) const

Definition at line 231 of file RootOutputTree.cc.

References readBranches_.

Referenced by edm::RootOutputFile::beginInputFile().

231  {
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  }
std::vector< TBranch * > readBranches_
TTree const* edm::RootOutputTree::tree ( ) const
inline
TTree* edm::RootOutputTree::tree ( )
inline
bool edm::RootOutputTree::uncloned ( std::string const &  branchName) const
inline

Definition at line 95 of file RootOutputTree.h.

References clonedReadBranchNames_.

95  {
96  return clonedReadBranchNames_.find(branchName) == clonedReadBranchNames_.end();
97  }
std::set< std::string > clonedReadBranchNames_
void edm::RootOutputTree::writeTree ( )

Definition at line 335 of file RootOutputTree.cc.

References tree(), and writeTTree().

335 { writeTTree(tree()); }
TTree const * tree() const
static void writeTTree(TTree *tree)
void edm::RootOutputTree::writeTTree ( TTree *  tree)
static

Definition at line 321 of file RootOutputTree.cc.

References edm::setRefCoreStreamer().

Referenced by edm::RootOutputFile::finishEndFile(), and writeTree().

321  {
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  }
TTree const * tree() const
void setRefCoreStreamer(bool resetAll=false)

Member Data Documentation

std::vector<TBranch*> edm::RootOutputTree::auxBranches_
private

Definition at line 115 of file RootOutputTree.h.

Referenced by addAuxiliary(), close(), fastCloneTTree(), and fillTree().

std::set<std::string> edm::RootOutputTree::clonedReadBranchNames_
private

Definition at line 119 of file RootOutputTree.h.

Referenced by maybeFastCloneTree(), and uncloned().

bool edm::RootOutputTree::currentlyFastCloning_
private

Definition at line 120 of file RootOutputTree.h.

Referenced by fillTree(), and maybeFastCloneTree().

bool edm::RootOutputTree::fastCloneAuxBranches_
private

Definition at line 121 of file RootOutputTree.h.

Referenced by fastCloneTTree(), fillTree(), and maybeFastCloneTree().

edm::propagate_const<std::shared_ptr<TFile> > edm::RootOutputTree::filePtr_
private

Definition at line 110 of file RootOutputTree.h.

Referenced by close().

std::vector<TBranch*> edm::RootOutputTree::producedBranches_
private

Definition at line 113 of file RootOutputTree.h.

Referenced by addBranch(), close(), and fillTree().

std::vector<TBranch*> edm::RootOutputTree::readBranches_
private
edm::propagate_const<TTree*> edm::RootOutputTree::tree_
private
std::vector<TBranch*> edm::RootOutputTree::unclonedAuxBranches_
private

Definition at line 116 of file RootOutputTree.h.

Referenced by addAuxiliary(), close(), fastCloneTTree(), and fillTree().

std::vector<TBranch*> edm::RootOutputTree::unclonedReadBranches_
private

Definition at line 117 of file RootOutputTree.h.

Referenced by close(), fillTree(), and maybeFastCloneTree().