Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
InputDataInfo.cxx
1/*
2 * ChainBuilder.cxx
3 *
4 * Created on: 19 cze 2024
5 * Author: daniel
6 */
7
8#include "InputDataInfo.h"
9
10#include <TChain.h>
11#include <TCollection.h>
12#include <TDirectoryFile.h>
13#include <TFile.h>
14#include <TKey.h>
15#include <TList.h>
16#include <TNamed.h>
17#include <TTree.h>
18#include <initializer_list>
19#include <iostream>
20
21
22#include "Cout.h"
23#include "Std.h"
24#include "StdString.h"
25#include "XMLNode.h"
26
27namespace Hal {
28
29
30 InputDataInfo::InputDataInfo(TString file) : fListName(file) {
31 if (file.EndsWith(".list")) {
32 auto vec = Hal::Std::GetLinesFromFile(file, kTRUE);
33 fFileNames.push_back(vec);
34 } else if (file.EndsWith(".xml")) {
35 XMLFile xmlFile(file);
36 auto root = xmlFile.GetRootNode();
37 auto files = root->GetChild("files");
38 for (int i = 0; i < files->GetNChildren(); i++) {
39 auto list = files->GetChild(i);
40 fFileNames.push_back(std::vector<TString>());
41 for (int j = 0; j < list->GetNChildren(); i++) {
42 fFileNames[i].push_back(list->GetChild(j)->GetValue());
43 }
44 }
45 } else {
46 fFileNames.resize(1);
47 fFileNames[0].push_back(file);
48 }
49 }
50
51 InputDataInfo::InputDataInfo(std::initializer_list<TString> fileLists) {
52 fListName = Form("initializer_list");
53 for (auto file : fileLists) {
54 fFileNames.push_back(Hal::Std::GetLinesFromFile(file, kTRUE));
55 }
56 }
57
58 InputDataInfo::InputDataInfo(std::vector<std::vector<TString>> files) {
59 fListName = Form("2d vector");
60 fFileNames = files;
61 }
62
63 TString InputDataInfo::GetSafeFile(Int_t level, Int_t entry) const {
64 if (level + 1 < fFileNames.size()) {
65 if (fFileNames[level + 1].size() > entry) return fFileNames[level + 1][entry];
66 }
67 return "";
68 }
69
70 std::vector<TString> InputDataInfo::GetSafeFiles(Int_t level) const {
71 if (level + 1 < fFileNames.size()) { return fFileNames[level + 1]; }
72 return std::vector<TString>();
73 }
74
75 void InputDataInfo::AddFile(TString file) { AddFriend(file, -1); }
76
77 void InputDataInfo::AddFriend(TString file, Int_t level) {
78 if (fFileNames.size() < level + 2) { fFileNames.resize(level + 2); }
79 fFileNames[level + 1].push_back(file);
80 }
81
82 Int_t InputDataInfo::GetNFiles() const {
83 if (fFileNames.size() == 0) return -1;
84 return fFileNames[0].size();
85 }
86
87 Int_t InputDataInfo::GetFriendsLevel() const { return fFileNames.size() - 1; }
88
89 TString InputRootDataInfo::GetChainName(TString file) const {
90 TFile* ftemp = new TFile(file);
91 auto list = (TList*) ftemp->GetListOfKeys();
92 for (int i = 0; i < list->GetEntries(); i++) {
93 TString keyName = ((TKey*) list->At(i))->GetName();
94 auto tree = dynamic_cast<TTree*>(ftemp->Get(keyName));
95 if (tree) {
96 ftemp->Close();
97 delete ftemp;
98 return keyName;
99 }
100 }
101 ftemp->Close();
102 delete ftemp;
103 return "";
104 }
105
106 void InputRootDataInfo::GuessTrees() {
107 for (auto& list : fFileNames) {
108 if (Hal::Std::FileExists(list[0]))
109 fTreeNames.push_back(GetChainName(list[0]));
110 else {
111 Hal::Cout::PrintInfo(Form("Can't find tree in %s", list[0].Data()), EInfo::kDebugInfo);
112 fTreeNames.push_back(" ");
113 }
114 }
115 }
116
117 InputRootDataInfo::InputRootDataInfo(TString file, TString treename) : InputDataInfo(file) {
118 if (treename.Length() != 0) {
119 fTreeNames.push_back(treename);
120 return;
121 }
122 if (file.EndsWith(".xml")) {
123 XMLFile xmlFile(file);
124 auto root = xmlFile.GetRootNode();
125 auto files = root->GetChild("files");
126 auto treenames = root->GetChild("treenames");
127 if (treenames) {
128 for (int i = 0; i < treenames->GetNChildren(); i++) {
129 fTreeNames.push_back(treenames->GetChild(i)->GetValue());
130 }
131 }
132 }
133 }
134
135 InputRootDataInfo::InputRootDataInfo(std::initializer_list<TString> fileLists, std::initializer_list<TString> treeNames) :
136 InputDataInfo(fileLists) {
137 if (treeNames.size() != 0) { fTreeNames = Hal::Std::GetVector(treeNames); }
138 }
139
140 InputRootDataInfo::InputRootDataInfo(std::vector<std::vector<TString>> files) : InputDataInfo(files) {}
141
143 if (fChain) return fChain;
144 if (fTreeNames.size() == 0) { GuessTrees(); }
145 fChain = new TChain(fTreeNames[0]);
146 for (auto name : fFileNames[0])
147 fChain->AddFile(name);
148 int entries = fChain->GetEntries();
149 for (unsigned int i = 1; i < fFileNames.size(); i++) {
150 TChain* minichain = new TChain(fTreeNames[i]);
151 for (auto friendName : fFileNames[i])
152 minichain->AddFile(friendName);
153 fChain->AddFriend(minichain);
154 if (minichain->GetEntries() != entries) {
155 Hal::Cout::PrintInfo("Warning different number of entries detected in ChainBuilder", Hal::EInfo::kError);
156 }
157 }
158 return fChain;
159 }
160
161 void InputDataInfo::Print(Option_t* option) const {
162 std::vector<TString> header;
163 header.push_back("No");
164 header.push_back("Main file");
165 for (int i = 1; i < fFileNames.size(); i++) {
166 header.push_back(Form("Friend_%i", i - 1));
167 }
168 Hal::Cout::SetLineLenght(26 * header.size());
169 Hal::Cout::Database(header);
170 for (unsigned int pos = 0; pos < fFileNames[0].size(); pos++) {
171 std::vector<TString> str;
172 str.push_back(Form("%i", pos));
173 for (unsigned int level = 0; level < fFileNames.size(); level++) {
174 if (fFileNames[level].size() > pos) {
175 TString path = fFileNames[level][pos];
176 if (path.Length() > 25) {
177 int crop = path.Length() - 25;
178 path = TString(path(crop, path.Length()));
179 path = Form("..%s", path.Data());
180 }
181 str.push_back(path);
182 } else {
183 str.push_back(" ");
184 }
185 }
187 }
189 }
190
191 void InputRootDataInfo::Print(Option_t* option) const {
192 std::vector<TString> header;
193 header.push_back("No");
194 header.push_back("Main file");
195 for (unsigned int i = 1; i < fFileNames.size(); i++) {
196 header.push_back(Form("Friend_%i", i - 1));
197 }
198 Hal::Cout::SetLineLenght(26 * header.size());
199 Hal::Cout::Database(header);
200 std::vector<TString> line;
201 line.push_back("Tree names:");
202 for (auto tree : fTreeNames) {
203 line.push_back(tree);
204 }
206 for (unsigned int pos = 0; pos < fFileNames[0].size(); pos++) {
207 std::vector<TString> str;
208 str.push_back(Form("%i", pos));
209 for (unsigned int level = 0; level < fFileNames.size(); level++) {
210 if (fFileNames[level].size() > pos) {
211 TString path = fFileNames[level][pos];
212 if (path.Length() > 25) {
213 int crop = path.Length() - 25;
214 path = TString(path(crop, path.Length()));
215 path = Form("..%s", path.Data());
216 }
217 str.push_back(path);
218 } else {
219 str.push_back(" ");
220 }
221 }
223 }
225 }
226
227} /* namespace Hal */
static void SetLineLenght(Int_t lenght)
Definition Cout.h:54
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void Database(Int_t no...)
InputDataInfo(TString file="data.root")
InputRootDataInfo(TString file="data.root", TString treename="")