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);
222 auxIndexes.insert(std::make_pair(auxIndex, auxBranch));
223 branches->RemoveAt(auxIndex);
225 mustRemoveSomeAuxs =
true;
230 int auxIndex = branches->IndexOf(auxBranch);
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(); });