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);
133 if(outputBranch !=
nullptr) {
134 TBranch* inputBranch = inputTree->GetBranch(outputBranch->GetName());
136 if(inputBranch !=
nullptr) {
137 if(inputBranch->GetSplitLevel() != outputBranch->GetSplitLevel() ||
138 inputBranch->GetBasketSize() != outputBranch->GetBasketSize()) {
148 bool checkMatchingBranches(TBranchElement* inputBranch, TBranchElement* outputBranch) {
149 if(inputBranch->GetStreamerType() != outputBranch->GetStreamerType()) {
152 TObjArray* inputArray = inputBranch->GetListOfBranches();
153 TObjArray* outputArray = outputBranch->GetListOfBranches();
155 if(outputArray->GetSize() < inputArray->GetSize()) {
158 TIter iter(outputArray);
159 TObject*
obj =
nullptr;
160 while((obj = iter.Next()) !=
nullptr) {
161 TBranchElement* outBranch =
dynamic_cast<TBranchElement*
>(
obj);
163 TBranchElement* inBranch =
dynamic_cast<TBranchElement*
>(inputArray->FindObject(outBranch->GetName()));
167 if(!checkMatchingBranches(inBranch, outBranch)) {
178 if(inputTree ==
nullptr)
return false;
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) {
187 if(!checkMatchingBranches(inputBranch, outputBranch)) {
189 <<
"Fast Cloning disabled because a data member has been added to split branch: " << inputBranch->GetName() <<
"\n.";
200 if(readBranch->GetEntries() != expectedNumberOfEntries) {
209 if(in->GetEntries() != 0) {
210 TObjArray* branches =
tree_->GetListOfBranches();
213 std::map<Int_t, TBranch *> auxIndexes;
214 bool mustRemoveSomeAuxs =
false;
217 int auxIndex = branches->IndexOf(auxBranch);
218 assert (auxIndex >= 0);
219 auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
220 branches->RemoveAt(auxIndex);
222 mustRemoveSomeAuxs =
true;
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;
234 if(mustRemoveSomeAuxs) {
235 branches->Compress();
239 TTreeCloner cloner(dupTree.
tree(),
tree_, option.c_str(), TTreeCloner::kNoWarnings|TTreeCloner::kIgnoreMissingTopLevel);
241 if(!cloner.IsValid()) {
243 static const char* okerror =
"One of the export branch";
244 if(strncmp(cloner.GetWarning(), okerror, strlen(okerror)) == 0) {
248 <<
"invalid TTreeCloner (" << cloner.GetWarning() <<
")\n";
251 tree_->SetEntries(
tree_->GetEntries() + in->GetEntries());
253 rootHandler->ignoreWarningsWhileDoing([&cloner] { cloner.Exec(); });
255 if(mustRemoveSomeAuxs) {
256 for(
auto const& auxIndex : auxIndexes) {
258 Int_t
last = branches->GetLast();
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);
264 branches->AddAt(auxIndex.second, auxIndex.first);
266 branches->Add(auxIndex.second);
275 if(tree->GetNbranches() != 0) {
278 tree->SetEntries(-1);
281 tree->AutoSave(
"FlushBaskets");
335 TBranch*
branch =
tree_->Branch(branchName.c_str(),
340 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
T const & get(Event const &event, InputTag const &tag)
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)
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_
const eventsetup::produce::Produce produced
def branchType(schema, name)
std::string className(const T &t)
void fastCloneTTree(TTree *in, std::string const &option)
std::unique_ptr< TFile, CloseBeforeDelete > file_