16 #include "TBranchElement.h" 17 #include "TCollection.h" 19 #include "TTreeCloner.h" 25 #include "tbb/task_arena.h" 70 edm::LogWarning(
"DuplicateTreeSentry") <<
"Re-opening file for fast-cloning";
73 const TUrl*
url = file->GetEndpointUrl();
77 file_.reset(TFile::Open(url->GetUrl(),
"READWRAP"));
91 std::unique_ptr<TFile, CloseBeforeDelete>
file_;
99 int treeMaxVirtualSize)
105 unclonedReadBranches_(),
106 clonedReadBranchNames_(),
107 currentlyFastCloning_(),
108 fastCloneAuxBranches_(
false) {
109 if (treeMaxVirtualSize >= 0)
110 tree_->SetMaxVirtualSize(treeMaxVirtualSize);
114 tree->SetDirectory(filePtr);
125 if (tree->IsZombie())
133 assert(inputTree !=
nullptr);
137 if (outputBranch !=
nullptr) {
138 TBranch* inputBranch = inputTree->GetBranch(outputBranch->GetName());
140 if (inputBranch !=
nullptr) {
141 if (inputBranch->GetSplitLevel() != outputBranch->GetSplitLevel() ||
142 inputBranch->GetBasketSize() != outputBranch->GetBasketSize()) {
152 bool checkMatchingBranches(TBranchElement* inputBranch, TBranchElement* outputBranch) {
153 if (inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
156 TObjArray* inputArray = inputBranch->GetListOfBranches();
157 TObjArray* outputArray = outputBranch->GetListOfBranches();
159 if (outputArray->GetSize() < inputArray->GetSize()) {
162 TIter iter(outputArray);
163 TObject*
obj =
nullptr;
164 while ((obj = iter.Next()) !=
nullptr) {
165 TBranchElement* outBranch =
dynamic_cast<TBranchElement*
>(
obj);
167 TBranchElement* inBranch =
dynamic_cast<TBranchElement*
>(inputArray->FindObject(outBranch->GetName()));
171 if (!checkMatchingBranches(inBranch, outBranch)) {
181 if (inputTree ==
nullptr)
186 TBranchElement* outputBranch =
dynamic_cast<TBranchElement*
>(outputBr);
187 if (outputBranch !=
nullptr) {
188 TBranchElement* inputBranch =
dynamic_cast<TBranchElement*
>(inputTree->GetBranch(outputBranch->GetName()));
189 if (inputBranch !=
nullptr) {
191 if (!checkMatchingBranches(inputBranch, outputBranch)) {
192 LogInfo(
"FastCloning") <<
"Fast Cloning disabled because a data member has been added to split branch: " 193 << inputBranch->GetName() <<
"\n.";
204 if (readBranch->GetEntries() != expectedNumberOfEntries) {
212 if (in->GetEntries() != 0) {
213 TObjArray* branches =
tree_->GetListOfBranches();
216 std::map<Int_t, TBranch*> auxIndexes;
217 bool mustRemoveSomeAuxs =
false;
220 int auxIndex = branches->IndexOf(auxBranch);
221 assert(auxIndex >= 0);
222 auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
223 branches->RemoveAt(auxIndex);
225 mustRemoveSomeAuxs =
true;
230 int auxIndex = branches->IndexOf(auxBranch);
231 assert(auxIndex >= 0);
232 auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
233 branches->RemoveAt(auxIndex);
234 mustRemoveSomeAuxs =
true;
237 if (mustRemoveSomeAuxs) {
238 branches->Compress();
243 dupTree.
tree(),
tree_, option.c_str(), TTreeCloner::kNoWarnings | TTreeCloner::kIgnoreMissingTopLevel);
245 if (!cloner.IsValid()) {
247 static const char* okerror =
"One of the export branch";
248 if (strncmp(cloner.GetWarning(), okerror, strlen(okerror)) == 0) {
254 tree_->SetEntries(
tree_->GetEntries() + in->GetEntries());
256 rootHandler->ignoreWarningsWhileDoing([&cloner] { cloner.Exec(); });
258 if (mustRemoveSomeAuxs) {
259 for (
auto const& auxIndex : auxIndexes) {
261 Int_t
last = branches->GetLast();
263 branches->AddAtAndExpand(branches->At(last), last + 1);
264 for (Int_t ind = last - 1; ind >= auxIndex.first; --ind) {
265 branches->AddAt(branches->At(ind), ind + 1);
267 branches->AddAt(auxIndex.second, auxIndex.first);
269 branches->Add(auxIndex.second);
277 if (tree->GetNbranches() != 0) {
280 tree->SetEntries(-1);
283 tree->AutoSave(
"FlushBaskets");
293 bool canFastCloneAux,
324 tbb::this_task_arena::isolate([&] {
tree_->Fill(); });
336 TBranch*
branch =
tree_->Branch(branchName.c_str(), className.c_str(), &pProd, basketSize,
splitLevel);
337 assert(branch !=
nullptr);
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
std::vector< TBranch * > producedBranches_
std::vector< TBranch * > auxBranches_
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
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)
void operator()(TFile *iFile) const
bool currentlyFastCloning_
virtual std::string const * sourceCloneCacheHint() const =0
std::vector< TBranch * > unclonedReadBranches_
bool checkEntriesInReadBranches(Long64_t expectedNumberOfEntries) const
std::string const & BranchTypeToProductTreeName(BranchType const &branchType)
static TTree * makeTTree(TFile *filePtr, std::string const &name, int splitLevel)
T const & get(Event const &event, InputTag const &tag)(false)
DuplicateTreeSentry & operator=(DuplicateTreeSentry const &)=delete
static void writeTTree(TTree *tree)
std::vector< TBranch * > readBranches_
void maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree *tree, std::string const &option)
bool fastCloneAuxBranches_
def branchType(schema, name)
std::string className(const T &t)
void fastCloneTTree(TTree *in, std::string const &option)
std::unique_ptr< TFile, CloseBeforeDelete > file_