Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
IOManager.cxx
1/*
2 * IOManager.cxx
3 *
4 * Created on: 10 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 "IOManager.h"
11
12#include "Cout.h"
13#include "InputDataInfo.h"
14#include "Package.h"
15#include "PackageTable.h"
16#include "Parameter.h"
17#include "Std.h"
18#include "XMLNode.h"
19
20#include <fstream>
21#include <utility>
22#include <vector>
23
24#include <RtypesCore.h>
25#include <TFile.h>
26#include <TList.h>
27#include <TNamed.h>
28#include <TObjString.h>
29
30
31namespace Hal {
32
33 void IOManager::AddBranch(TString name, TObject* object, BranchInfo::EFlag flag) {
34 for (auto str : fBranches) {
35 if (str.GetBranchName().EqualTo(name)) return;
36 }
37 fBranches.push_back(BranchInfo(name, object, flag));
38 fBranchNameList.push_back(name);
39 }
40
42 for (auto branch : fBranches) {
43 if (branch.GetBranchName().EqualTo(name)) return branch;
44 }
45 BranchInfo br(name, nullptr, BranchInfo::EFlag::kNull);
46 br.GetBranchName() = name;
47 return br;
48 }
49
50 void IOManager::Register(const char* name, const char* folderName, TNamed* obj, Bool_t toFile) {
51 if (toFile) {
52 AddBranch(name, obj, BranchInfo::EFlag::kOut);
53 } else {
54 AddBranch(name, obj, BranchInfo::EFlag::kVirtual);
55 }
56 RegisterInternal(name, folderName, obj, toFile);
57 }
58
59 void IOManager::Register(const char* name, const char* Foldername, TCollection* obj, Bool_t toFile) {
60 if (toFile) {
61 AddBranch(name, obj, BranchInfo::EFlag::kOut);
62 } else {
63 AddBranch(name, obj, BranchInfo::EFlag::kVirtual);
64 }
65 RegisterInternal(name, Foldername, obj, toFile);
66 }
67
68 BranchInfo::EFlag IOManager::GetBranchStatus(const char* BrName) {
70 TString name = BrName;
71 for (auto branch : fBranches) {
72 if (branch.GetBranchName().EqualTo(name)) return branch.GetFlag();
73 }
74 return BranchInfo::EFlag::kNull;
75 }
76
77 TObject* IOManager::GetObject(const char* BrName) {
78 TString name = BrName;
79 for (auto& branch : fBranches) {
80 if (branch.GetBranchName().EqualTo(name)) {
81 if (branch.GetFlag() == BranchInfo::EFlag::kInPassive) { branch.SetFlag(BranchInfo::EFlag::kInActive); }
82 auto pointer = branch.GetPointer();
83 if (!pointer) { Hal::Cout::PrintInfo(Form("Branch %s exists, but contains non-TObject", name.Data()), EInfo::kError); }
84 return pointer;
85 }
86 }
87 // refresh and try again
89 for (auto& branch : fBranches) {
90 if (branch.GetBranchName().EqualTo(name)) {
91 if (branch.GetFlag() == BranchInfo::EFlag::kInPassive) { branch.SetFlag(BranchInfo::EFlag::kInActive); }
92 auto pointer = branch.GetPointer();
93 if (!pointer) { Hal::Cout::PrintInfo(Form("Branch %s exists, but contains non-TObject", name.Data()), EInfo::kError); }
94 return pointer;
95 }
96 }
97 return nullptr;
98 }
99
100 void IOManager::ActivateBranch(TString brName) {
101 for (auto& branch : fBranches) {
102 if (branch.GetBranchName().EqualTo(brName)) {
103 if (branch.GetFlag() == BranchInfo::EFlag::kInPassive) {
104 branch.SetFlag(BranchInfo::EFlag::kInActive);
105 return;
106 }
107 }
108 }
109 // refresh and try again
111 for (auto& branch : fBranches) {
112 if (branch.GetBranchName().EqualTo(brName)) {
113 if (branch.GetFlag() == BranchInfo::EFlag::kInPassive) {
114 branch.SetFlag(BranchInfo::EFlag::kInActive);
115 return;
116 }
117 }
118 }
119 }
120
122 Cout::PrintInfo(Form("IO Manager %s", ClassName()), EInfo::kInfo);
123 TString inFile = "";
124 Cout::PrintInfo(Form("Input file %s", fDataInfo->GetSourceName().Data()), EInfo::kInfo);
126 Cout::Database({"Branch Name", "Type"});
127 TString types[] = {"Input (Active)", "Input (Passive)", "Output", "Virtual"};
128 BranchInfo::EFlag flags[] = {
129 BranchInfo::EFlag::kInActive, BranchInfo::EFlag::kInPassive, BranchInfo::EFlag::kOut, BranchInfo::EFlag::kVirtual};
130 for (int i = 0; i < 4; i++) {
131 for (auto branch : fBranches) {
132 if (branch.GetFlag() == flags[i]) Cout::Database({branch.GetBranchName(), types[i]});
133 }
134 }
135 }
136
137 IOManager::~IOManager() {
138 if (fDataInfo) delete fDataInfo;
139 }
140
142 TList* list = new TList();
143 list->SetName(Form("Branch List"));
144
145 auto getStr = [](BranchInfo::EFlag flag) {
146 TString flagStr = "unknown";
147 switch (flag) {
148 case BranchInfo::EFlag::kInActive: {
149 flagStr = "INPUT (ACTIVE)";
150 } break;
151 case BranchInfo::EFlag::kInPassive: {
152 flagStr = "INPUT (PASSIVE)";
153 } break;
154 case BranchInfo::EFlag::kOut: {
155 flagStr = "OUTPUT";
156 } break;
157 case BranchInfo::EFlag::kVirtual: {
158 flagStr = "VIRTUAL";
159 } break;
160 case BranchInfo::EFlag::kNull: {
161 flagStr = "NULL";
162 } break;
163 default: break;
164 };
165 return flagStr;
166 };
167 for (auto branch : fBranches) {
168 TString flagStr = getStr(branch.GetFlag());
169 TString name = branch.GetBranchName();
170 TObject* obj = branch.GetPointer();
171 TString className = "unknown";
172 if (obj) { className = obj->ClassName(); }
173 list->Add(new ParameterString(Form("Branch: %s", name.Data()), Form("%s [%s]", className.Data(), flagStr.Data())));
174 }
175 list->Add(new Hal::ParameterString("IOManager", this->ClassName()));
176 return list;
177 }
179 if (!fDataInfo) {
180 Hal::Cout::PrintInfo("IO manager cannot InputDataInfo !", EInfo::kCriticalError);
181 return kFALSE;
182 }
183 if (fDataInfo->GetSafeFiles(-1).size() == 0) {
184 Hal::Cout::PrintInfo("IO manager cannot find any files !", EInfo::kCriticalError);
185 return kFALSE;
186 }
187 return InitInternal();
188 }
189
190 IOManager::IOManager(InputDataInfo* info) { fDataInfo = info; }
191
192 TString IOManager::GetSourceName() const { return fDataInfo->GetSourceName(); };
193
194 Int_t IOManager::GetNFiles() const { return fDataInfo->GetNFiles(); }
195
196 std::vector<TString> IOManager::GetFilesNames(Int_t entry) const { return fDataInfo->GetSafeFiles(entry); }
197
198 Int_t IOManager::GetFriendsLevel() const { return fDataInfo->GetFriendsLevel(); }
199
200 void IOManager::AddFile(TString name) { fDataInfo->AddFile(name); }
201
202 void IOManager::AddFriend(TString friendName, Int_t level) { fDataInfo->AddFriend(friendName, level); }
203
204 TString IOManager::GetFirstDataFileName() const { return fDataInfo->GetSafeFile(-1, 0); }
205
206 TString IOManager::GetFirstFriendFileName(Int_t level) const { return fDataInfo->GetSafeFile(level, 0); }
207
208 std::vector<TString> IOManager::GetFileNameList(Int_t level) const { return fDataInfo->GetSafeFiles(level); }
209
210} // namespace Hal
211
212
213/* namespace Hal */
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void Database(Int_t no...)
Int_t GetFriendsLevel() const
virtual TList * GetBranchesList() const
virtual void UpdateBranches()
Definition IOManager.h:159
Int_t GetNFiles() const
BranchInfo::EFlag GetBranchStatus(const char *BrName)
Definition IOManager.cxx:68
void ActivateBranch(TString brName)
std::vector< TString > GetFileNameList(Int_t level) const
TString GetFirstDataFileName() const
std::vector< TString > GetFilesNames(Int_t entry=0) const
void AddFriend(TString friendName, Int_t level)
std::vector< Hal::BranchInfo > fBranches
Definition IOManager.h:63
BranchInfo FindBranch(TString name) const
Definition IOManager.cxx:41
void Register(const char *name, const char *folderName, TNamed *obj, Bool_t toFile)
Definition IOManager.cxx:50
void AddFile(TString name)
TString GetSourceName() const
void AddBranch(TString name, TObject *object, BranchInfo::EFlag flag)
Definition IOManager.cxx:33
TString GetFirstFriendFileName(Int_t level) const
virtual TObject * GetObject(const char *BrName)
Definition IOManager.cxx:77
virtual void RegisterInternal(const char *name, const char *folderName, TNamed *obj, Bool_t toFile)=0
std::vector< TString > GetSafeFiles(Int_t level) const
TString GetSafeFile(Int_t level, Int_t entry) const