Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
RootIOManager.cxx
1/*
2 * RootIoManager.cxx
3 *
4 * Created on: 6 maj 2022
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "RootIOManager.h"
11
12#include "Cout.h"
13#include "XMLNode.h"
14
15#include <fstream>
16#include <iostream>
17#include <utility>
18
19#include "InputDataInfo.h"
20#include <TBranch.h>
21#include <TBranchElement.h>
22#include <TChain.h>
23#include <TCollection.h>
24#include <TDirectoryFile.h>
25#include <TFile.h>
26#include <TFriendElement.h>
27#include <TKey.h>
28#include <TList.h>
29#include <TNamed.h>
30#include <TObjArray.h>
31#include <TObjString.h>
32#include <TTree.h>
33
34namespace Hal {
36
38
39 Bool_t RootIOManager::InitInternal() {
40 fInChain = ((Hal::InputRootDataInfo*) fDataInfo)->GetChain();
42 fEntries = fInChain->GetEntries();
43 fOutFile = new TFile(fOutFileName, "recreate");
44 fOutTree = new TTree(fOutTreeName, fOutTreeName);
45 return kTRUE;
46 }
47
48 Int_t RootIOManager::GetEntries() const { return fEntries; }
49
50 RootIOManager::~RootIOManager() {
51 if (fInChain) delete fInChain;
52 for (auto obj : fObjects) {
53 if (obj) delete obj;
54 }
55 if (fOutFile) delete fOutFile;
56 }
57
59 TObjArray* list_branch = fInChain->GetListOfBranches();
60 auto branches = GetListOfBranches(fInChain, kTRUE);
61 for (auto name : branches) {
62 TBranch* branch = fInChain->GetBranch(name);
63 if (FindBranch(name).GetFlag() != BranchInfo::EFlag::kNull) continue; // branch with given name already exist
64 if (!branch) { Hal::Cout::PrintInfo(Form("Branch %s not found!", name.Data()), EInfo::kError); }
65 TString className = branch->GetClassName();
66 auto classInfo = TClass::GetClass(className, 1);
67 bool object = false;
68 if (classInfo) {
69 if (classInfo->InheritsFrom("TObject")) { object = true; }
70 }
71 if (object) {
72 TObject** obj = new TObject*();
73 PushTObject(obj);
74 fInChain->SetBranchAddress(name, obj);
75 AddBranch(branch->GetName(), obj[0], BranchInfo::EFlag::kInPassive);
76 } else {
77 AddBranch(branch->GetName(), nullptr, BranchInfo::EFlag::kInPassive);
78 }
79 }
80 }
81
82 void RootIOManager::RegisterInternal(const char* name, const char* /*folderName*/, TNamed* obj, Bool_t toFile) {
83 if (toFile) { fOutTree->Branch(name, obj); }
84 }
85
86 void RootIOManager::RegisterInternal(const char* name, const char* /*Foldername*/, TCollection* obj, Bool_t toFile) {
87 if (toFile) { fOutTree->Branch(name, obj); }
88 }
89
90 void RootIOManager::SetInChain(TChain* /*tempChain*/, Int_t /*ident*/) {}
91
92 void RootIOManager::FillTree() { fOutTree->Fill(); }
93
95 fOutTree->Write();
96 fOutFile->Close();
97 }
98
99 Int_t Hal::RootIOManager::GetEntry(Int_t i, Int_t flag) {
100 fInChain->GetEntry(i, flag);
101 return 1;
102 }
103
104 void RootIOManager::PushTObject(TObject** obj) { fObjects.push_back(obj); }
105
107 for (auto branch : fBranches) {
108 if (branch.GetFlag() == BranchInfo::EFlag::kInPassive) {
109 fInChain->SetBranchStatus(branch.GetBranchName(), 0);
110 HalCoutDebug(Form("Locking branch %s", branch.GetBranchName().Data()));
111 }
112 }
113 }
114
115 std::vector<TString> RootIOManager::GetListOfBranches(TChain* chain, Bool_t friends) {
116
117 auto convert = [](std::vector<TString>& res, TObjArray* objar) {
118 for (int i = 0; i < objar->GetEntriesFast(); i++) {
119 TBranchElement* branch = (TBranchElement*) objar->At(i);
120 TString name = branch->GetName();
121 res.push_back(name);
122 }
123 };
124 std::vector<TString> list;
125 TObjArray* list_branch = chain->GetListOfBranches();
126 convert(list, list_branch);
127 if (friends) {
128 auto friends_trees = chain->GetListOfFriends();
129 if (friends_trees)
130 for (int i = 0; i < friends_trees->GetEntries(); i++) { // why ROOT doesnt do this?
131 auto tree = ((TFriendElement*) friends_trees->At(i))->GetTree();
132 auto lista = tree->GetListOfBranches();
133 convert(list, lista);
134 }
135 }
136 return list;
137 }
138
139} /* namespace Hal */
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
std::vector< Hal::BranchInfo > fBranches
Definition IOManager.h:63
BranchInfo FindBranch(TString name) const
Definition IOManager.cxx:41
void AddBranch(TString name, TObject *object, BranchInfo::EFlag flag)
Definition IOManager.cxx:33
virtual void UpdateBranches()
virtual void RegisterInternal(const char *name, const char *folderName, TNamed *obj, Bool_t toFile)
void PushTObject(TObject **obj)
static std::vector< TString > GetListOfBranches(TChain *chain, Bool_t friends)
virtual void CloseManager()
Int_t GetEntry(Int_t i, Int_t flag=1)
RootIOManager(TString name)
void SetInChain(TChain *tempChain, Int_t ident=-1)
Int_t GetEntries() const