16 #include "TBranchElement.h"
17 #include "TCollection.h"
19 #include "TTreeCloner.h"
61 edm::LogWarning(
"DuplicateTreeSentry") <<
"Re-opening file for fast-cloning";
64 const TUrl *
url = file->GetEndpointUrl();
69 file_.reset(TFile::Open(url->GetUrl(),
"READWRAP"));
82 std::unique_ptr<TFile, CloseBeforeDelete>
file_;
88 std::shared_ptr<TFile> filePtr,
91 int treeMaxVirtualSize) :
97 unclonedReadBranches_(),
98 clonedReadBranchNames_(),
99 currentlyFastCloning_(),
100 fastCloneAuxBranches_(
false) {
102 if(treeMaxVirtualSize >= 0)
tree_->SetMaxVirtualSize(treeMaxVirtualSize);
107 tree->SetDirectory(filePtr);
118 <<
"Failed to create the tree: " << name <<
"\n";
121 <<
"Tree: " << name <<
" is a zombie." <<
"\n";
129 assert(inputTree !=
nullptr);
135 TBranch* outputBranch = *it;
136 if(outputBranch !=
nullptr) {
137 TBranch* inputBranch = inputTree->GetBranch(outputBranch->GetName());
139 if(inputBranch !=
nullptr) {
140 if(inputBranch->GetSplitLevel() != outputBranch->GetSplitLevel() ||
141 inputBranch->GetBasketSize() != outputBranch->GetBasketSize()) {
151 bool checkMatchingBranches(TBranchElement* inputBranch, TBranchElement* outputBranch) {
152 if(inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
155 TObjArray* inputArray = inputBranch->GetListOfBranches();
156 TObjArray* outputArray = outputBranch->GetListOfBranches();
158 if(outputArray->GetSize() < inputArray->GetSize()) {
161 TIter
iter(outputArray);
162 TObject*
obj =
nullptr;
163 while((obj =
iter.Next()) !=
nullptr) {
164 TBranchElement* outBranch =
dynamic_cast<TBranchElement*
>(
obj);
166 TBranchElement* inBranch =
dynamic_cast<TBranchElement*
>(inputArray->FindObject(outBranch->GetName()));
170 if(!checkMatchingBranches(inBranch, outBranch)) {
181 if(inputTree ==
nullptr)
return false;
185 TBranchElement* outputBranch =
dynamic_cast<TBranchElement*
>(*it);
186 if(outputBranch !=
nullptr) {
187 TBranchElement* inputBranch =
dynamic_cast<TBranchElement*
>(inputTree->GetBranch(outputBranch->GetName()));
188 if(inputBranch !=
nullptr) {
190 if(!checkMatchingBranches(inputBranch, outputBranch)) {
192 <<
"Fast Cloning disabled because a data member has been added to split branch: " << inputBranch->GetName() <<
"\n.";
203 if((*it)->GetEntries() != expectedNumberOfEntries) {
212 if(in->GetEntries() != 0) {
213 TObjArray* branches =
tree_->GetListOfBranches();
216 std::map<Int_t, TBranch *> auxIndexes;
217 bool mustRemoveSomeAuxs =
false;
221 int auxIndex = branches->IndexOf(*it);
222 assert (auxIndex >= 0);
223 auxIndexes.insert(std::make_pair(auxIndex, *it));
224 branches->RemoveAt(auxIndex);
226 mustRemoveSomeAuxs =
true;
233 int auxIndex = branches->IndexOf(*it);
234 assert (auxIndex >= 0);
235 auxIndexes.insert(std::make_pair(auxIndex, *it));
236 branches->RemoveAt(auxIndex);
237 mustRemoveSomeAuxs =
true;
240 if(mustRemoveSomeAuxs) {
241 branches->Compress();
245 TTreeCloner cloner(dupTree.
tree(),
tree_, option.c_str(), TTreeCloner::kNoWarnings|TTreeCloner::kIgnoreMissingTopLevel);
247 if(!cloner.IsValid()) {
249 static const char* okerror =
"One of the export branch";
250 if(strncmp(cloner.GetWarning(), okerror, strlen(okerror)) == 0) {
254 <<
"invalid TTreeCloner (" << cloner.GetWarning() <<
")\n";
257 tree_->SetEntries(
tree_->GetEntries() + in->GetEntries());
259 rootHandler->ignoreWarningsWhileDoing([&cloner] { cloner.Exec(); });
261 if(mustRemoveSomeAuxs) {
262 for(std::map<Int_t, TBranch *>::const_iterator it = auxIndexes.begin(), itEnd = auxIndexes.end();
265 Int_t
last = branches->GetLast();
267 branches->AddAtAndExpand(branches->At(last), last+1);
268 for(Int_t ind = last-1; ind >= it->first; --ind) {
269 branches->AddAt(branches->At(ind), ind+1);
271 branches->AddAt(it->second, it->first);
273 branches->Add(it->second);
282 if(tree->GetNbranches() != 0) {
283 tree->SetEntries(-1);
286 tree->AutoSave(
"FlushBaskets");
309 if((*it)->GetEntries() ==
tree_->GetEntries()) {
341 TBranch* branch =
tree_->Branch(branchName.c_str(),
346 assert(branch !=
nullptr);
347 if(pProd !=
nullptr) {
std::set< std::string > clonedReadBranchNames_
static int const invalidSplitLevel
DuplicateTreeSentry(TTree *tree)
static int const invalidBasketSize
static void fillTTree(std::vector< TBranch * > const &branches)
bool checkSplitLevelsAndBasketSizes(TTree *inputTree) const
void setRefCoreStreamer(bool resetAll=false)
std::shared_ptr< TFile > filePtr_
std::vector< TBranch * > unclonedAuxBranches_
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)
eventsetup::produce::Produce produced
DuplicateTreeSentry & operator=(DuplicateTreeSentry const &)=delete
static void writeTTree(TTree *tree)
std::vector< TBranch * > readBranches_
virtual std::string const * sourceCacheHint() const =0
volatile std::atomic< bool > shutdown_flag false
void maybeFastCloneTree(bool canFastClone, bool canFastCloneAux, TTree *tree, std::string const &option)
bool fastCloneAuxBranches_
T get(const Candidate &c)
std::string className(const T &t)
void fastCloneTTree(TTree *in, std::string const &option)
std::unique_ptr< TFile, CloseBeforeDelete > file_