Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
AnaFile.cxx
1/*
2 * HalPackageExtractor.cxx
3 *
4 * Created on: 23-08-2013
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "AnaFile.h"
11
12
13#include <TCollection.h>
14#include <TDirectory.h>
15#include <TFile.h>
16#include <TH1.h>
17#include <TKey.h>
18#include <TList.h>
19#include <TNamed.h>
20#include <TObjArray.h>
21#include <TSeqCollection.h>
22#include <cstdlib>
23#include <cstring>
24
25#include "Cout.h"
26#include "Package.h"
27#include "Package2HTML.h"
28#include "Parameter.h"
29#include "StdString.h"
30namespace Hal {
31 AnaFile::AnaFile(TString filename, TString packname) :
32 fHTMLCounter(0), fMainPackageArray(NULL), fCutContainerArray(NULL), fMetaDataPack(NULL), fFilename(filename) {
33 fFile = new TFile(filename);
34 if (fFile->IsZombie()) {
35 delete fFile;
36 exit(0);
37 }
38 fMetaDataPack = (Package*) fFile->Get("HalInfo/RunInfo");
39 ParameterString* str = (ParameterString*) fMetaDataPack->GetObjectByName("Software ver");
40 Int_t SoftVer = Hal::Std::VersionId(str->GetValue());
41 if (SoftVer <= 201612) {
42 Cout::PrintInfo("This file version might be not compatible wtih current version, use "
43 "macro/path/fix_files.C to fix it",
44 Hal::EInfo::kWarning);
45 }
46 TDirectory* PhysDir = (TDirectory*) fFile->Get("HalPhysics");
47 fPhysKeys = PhysDir->GetListOfKeys();
48 fCurrentPackID = 0;
49 fMainPackageArray = new TObjArray();
50 fCutContainerArray = new TObjArray();
51 fMainPackageArray->SetOwner(kTRUE);
52 fCutContainerArray->SetOwner(kFALSE);
53 if (packname != "") {
54 fMaxPackID = 1;
55 for (int i = 0; i < fPhysKeys->GetEntries(); i++) {
56 TString name = ((TKey*) (fPhysKeys->At(0)))->GetName();
57 if (name != packname) {
58 fPhysKeys->RemoveAt(i);
59 } else {
60 fMainPackageArray->AddLast(dynamic_cast<Package*>(PhysDir->Get(name)));
61 if (FindCutContainer(0) == kFALSE) { Cout::PrintInfo("Cut container not found !", Hal::EInfo::kWarning); }
62 }
63 }
64 } else {
65 for (int i = 0; i < fPhysKeys->GetEntries(); i++) {
66 TString name = ((TKey*) (fPhysKeys->At(i)))->GetName();
67 TObject* pack_candidate = PhysDir->Get(name);
68 if (!pack_candidate->InheritsFrom("Hal::Package")) {
69 fPhysKeys->RemoveAt(i);
70 delete pack_candidate;
71 } else {
72 fMainPackageArray->AddLast(pack_candidate);
73 }
74 }
75 fMaxPackID = fPhysKeys->GetEntries();
76 for (int i = 0; i < fMaxPackID; i++) {
77 if (FindCutContainer(i) == kFALSE) { Cout::PrintInfo("Cut container not found !", Hal::EInfo::kWarning); }
78 }
79 }
80 fMainPackageArray->Compress();
81 }
82
83 AnaFile::AnaFile(TString filename, Int_t id) :
84 fHTMLCounter(0), fMainPackageArray(NULL), fCutContainerArray(NULL), fMetaDataPack(NULL), fFilename(filename) {
85 fFile = new TFile(filename);
86 if (fFile->IsZombie()) { exit(0); }
87 fMetaDataPack = (Package*) fFile->Get("HalInfo/RunInfo");
88 ParameterString* str = (ParameterString*) fMetaDataPack->GetObjectByName("Software ver");
89 Int_t SoftVer = Hal::Std::VersionId(str->GetValue());
90 if (SoftVer <= 201612) {
91 Cout::PrintInfo("This file version might be not compatible wtih current version, use "
92 "macro/path/fix_files.C to fix it",
93 Hal::EInfo::kWarning);
94 }
95 TDirectory* PhysDir = (TDirectory*) fFile->Get("HalPhysics");
96 fPhysKeys = (TList*) PhysDir->GetListOfKeys()->Clone();
97 fCurrentPackID = 0;
98 fMainPackageArray = new TObjArray();
99 fCutContainerArray = new TObjArray();
100 fMainPackageArray->SetOwner(kTRUE);
101 fCutContainerArray->SetOwner(kFALSE);
102 if (id >= 0) {
103 fMaxPackID = 1;
104 if (id > fPhysKeys->GetEntries()) {
105 Cout::PrintInfo(Form("Package with id = %i not found !", id), Hal::EInfo::kWarning);
106 return;
107 }
108 TString name = ((TKey*) (fPhysKeys->At(id)))->GetName();
109 fMainPackageArray->AddLast(dynamic_cast<Package*>(PhysDir->Get(name)));
110 if (FindCutContainer(0) == kFALSE) { Cout::PrintInfo("Cut container not found !", Hal::EInfo::kWarning); }
111 } else {
112 for (int i = 0; i < fPhysKeys->GetEntries(); i++) {
113 TString name = ((TKey*) (fPhysKeys->At(i)))->GetName();
114 TObject* pack_candidate = PhysDir->Get(name);
115 if (!pack_candidate->InheritsFrom("Hal::Package")) {
116 fPhysKeys->RemoveAt(i--);
117 delete pack_candidate;
118 } else {
119 fMainPackageArray->AddLast(pack_candidate);
120 }
121 }
122 fMaxPackID = fPhysKeys->GetEntries();
123 for (int i = 0; i < fMaxPackID; i++) {
124 if (FindCutContainer(i) == kFALSE) { Cout::PrintInfo("Cut container not found !", Hal::EInfo::kWarning); }
125 }
126 }
127 fMainPackageArray->Compress();
128 }
129
130 TObject* AnaFile::GetMainObject(Int_t i) const { return GetMainPackage()->GetObject(i); }
131
132 TObject* AnaFile::GetMainObject(TString name, Int_t index) const { return GetMainPackage()->GetObjectByName(name, index); }
133
134 void AnaFile::PrintMonitorCutList(TList* list) const {
135 Cout::Database({"No", "Type", "UnitX", "UnitY", "UnitZ"});
136 TString no, type, unitx, unity, unitz;
137 for (int i = 0; i < list->GetEntries(); i++) {
138 Package* mon = (Package*) list->At(i);
139 TString size = mon->GetComment();
140 no = Hal::Std::RoundToString(i);
141 type = size;
142 unitx = unity = unitz = "-";
143 ParameterString* xN = ((ParameterString*) mon->GetObjectByName("AxisX", 0, kTRUE));
144 if (xN) unitx = xN->GetValue();
145 xN = ((ParameterString*) mon->GetObjectByName("AxisY", 0, kTRUE));
146 if (xN) unity = xN->GetValue();
147 xN = ((ParameterString*) mon->GetObjectByName("AxisZ", 0, kTRUE));
148 if (xN) unitz = xN->GetValue();
149 Cout::Database({no, type, unitx, unity, unitz});
150 }
151 }
152
153 Int_t AnaFile::GetMerged() const { return GetMainPackage()->GetMerged(); }
154
155 Package* AnaFile::GetCutCollection(Hal::ECutUpdate update, Int_t no) const {
156 TList* list = NULL;
157 TString listName = Form("%sCutCollectionList", Hal::Std::UpdateEnumToString(update).Data());
158 list = ((TList*) GetCutContainer()->GetObjectByName(listName));
159 return (Package*) list->At(no);
160 }
161
162 void AnaFile::ExportHTML(TString fdirname, Int_t task_id) const {
163 if (task_id < 0) {
164 Package2HTML* pack = new Package2HTML(fFilename, fdirname);
165 delete pack;
166 } else {
167 Package2HTML* pack = new Package2HTML(GetMainPackage(), fMetaDataPack, fdirname, task_id);
168 delete pack;
169 }
170 }
171
172 Int_t AnaFile::GetCollectionsNo(Hal::ECutUpdate update) const {
173 TString label = "";
174 switch (update) {
175 case Hal::ECutUpdate::kEvent: label = "Event_collections_No"; break;
176 case Hal::ECutUpdate::kTrack: label = "Track_collections_No"; break;
177 case Hal::ECutUpdate::kTwoTrack: label = "TwoTrack_collections_No"; break;
178 case Hal::ECutUpdate::kTwoTrackBackground: label = "TwoTrack_collections_background_No"; break;
179 default: return 0; break;
180 }
181 if (GetCutContainer()->Exist(label, 0)) { return ((ParameterInt*) GetCutContainer()->GetObjectByName(label))->GetValue(); }
182 return 0;
183 }
184
185 TH1* AnaFile::GetHistogramPassed(Hal::ECutUpdate update, Int_t collection, Int_t no) const {
186 Package* pack = GetCutCollection(update, collection);
187 TList* lista = (TList*) pack->GetObjectByName("CutMonitorList");
188 if (lista == NULL) return NULL;
189 Package* cutmon = (Package*) lista->At(no); //(HalPackage*)pack->GetObject(no);
190 return (TH1*) cutmon->GetObjectByName("Passed");
191 }
192
193 TH1* AnaFile::GetHistogramFailed(Hal::ECutUpdate update, Int_t collection, Int_t no) const {
194 Package* pack = GetCutCollection(update, collection);
195 TList* lista = (TList*) pack->GetObjectByName("CutMonitorList");
196 if (lista == NULL) return NULL;
197 Package* cutmon = (Package*) lista->At(no);
198 // HalPackage *cutmon = (HalPackage*)pack->GetObject(no);
199 return (TH1*) cutmon->GetObjectByName("Failed");
200 }
201
202 Package* AnaFile::GetPackage(TString packname) const {
203 for (int i = 0; i < fMainPackageArray->GetEntriesFast(); i++) {
204 TString name = ((TKey*) fPhysKeys->At(i))->GetName();
205 TObject* obj = fMainPackageArray->UncheckedAt(i);
206 if (obj->InheritsFrom("Hal::Package") && name == packname) { return (Package*) obj; }
207 }
208 Cout::FailSucced(Form("Package %s not found !", packname.Data()), "WARNING", kRed);
209 return NULL;
210 }
211
212 Bool_t AnaFile::SwitchPackage(TString name) {
213 Bool_t not_switched = kTRUE;
214 for (int i = 0; i < fPhysKeys->GetEntries(); i++) {
215 TKey* key = (TKey*) fPhysKeys->At(i);
216 TString key_name = key->GetName();
217 if (key_name.EqualTo(name)) {
218 fCurrentPackID = i;
219 not_switched = kFALSE;
220 }
221 }
222 if (not_switched) {
223 Cout::PrintInfo(Form("%s not found in HalPhysics", name.Data()), Hal::EInfo::kLowWarning);
224 return kFALSE;
225 }
226 return kTRUE;
227 }
228
229 Bool_t AnaFile::SwitchPackage(Int_t no) {
230 if (no < 0) return kFALSE;
231 if (no >= fMaxPackID) return kFALSE;
232 fCurrentPackID = no;
233 return kTRUE;
234 }
235
236 Bool_t AnaFile::FindCutContainer(Int_t pack_no) {
237 Package* mainpack = static_cast<Package*>(fMainPackageArray->UncheckedAt(pack_no));
238 for (int i = 0; i < mainpack->GetEntries(); i++) {
239 TObject* obj = mainpack->GetObject(i);
240 TString classname = obj->ClassName();
241 if (classname == "Hal::Package") { // possible canditate
242 Package* pack = (Package*) mainpack->GetObject(i);
243 TString pack_class_name(pack->GetName(), strlen(pack->GetName()));
244 if (pack_class_name.EqualTo("Hal::CutContainer")) {
245 fCutContainerArray->AddAtAndExpand(pack, pack_no);
246 return kTRUE;
247 }
248 }
249 }
250 return kFALSE;
251 }
252
253 Package* AnaFile::GetMetaData() const { return fMetaDataPack; }
254
255 AnaFile::~AnaFile() {
256 delete fMetaDataPack;
257 delete fMainPackageArray;
258 delete fCutContainerArray;
259 fFile->Close();
260 delete fFile;
261 }
262
263 Int_t AnaFile::GetMainObjectsNo() const { return GetMainPackage()->GetEntries(); }
264
265 void AnaFile::GetPackageList(TString filename) {
266 TFile* file = new TFile(filename);
267 TDirectory* tdir = (TDirectory*) file->Get("HalPhysics");
268 TList* list = tdir->GetListOfKeys();
269 for (int i = 0; i < list->GetEntries(); i++) {
270 TString name = ((TKey*) (list->At(i)))->GetName();
271 TObject* object = tdir->Get(name);
272 Cout::Text("List of keys ", "L");
273 if (object->InheritsFrom("Hal::Package")) {
274 Cout::Text(object->GetName(), "L");
275 } else {
276 Cout::PrintInfo(Form("%s found in HalPhysics but don't inherit from HalPackage", name.Data()), Hal::EInfo::kLowWarning);
277 }
278 }
279 file->Close();
280 }
281
282 ULong64_t AnaFile::GetPassedNo(Hal::ECutUpdate update, Int_t collection, Option_t* opt) const {
283 Package* pack = GetCutCollection(update, collection);
284 TString option = opt;
285 if (option == "fast")
286 option = "PassedFast";
287 else
288 option = "PassedSlow";
289 ParameterULong64* entries = (ParameterULong64*) pack->GetObjectByName(option);
290 return entries->GetValue();
291 }
292
293 ULong64_t AnaFile::GetFailedNo(Hal::ECutUpdate update, Int_t collection, Option_t* opt) const {
294 Package* pack = GetCutCollection(update, collection);
295 TString option = opt;
296 if (option == "fast")
297 option = "FailedFast";
298 else
299 option = "FailedSlow";
300 ParameterULong64* entries = (ParameterULong64*) pack->GetObjectByName(option);
301 return entries->GetValue();
302 }
303
304 Int_t AnaFile::GetCutsNo(Hal::ECutUpdate update, Int_t collection, TString opt) const {
305 Package* pack = GetCutCollection(update, collection);
306 TString option = opt;
307 if (option == "fast") {
308 TList* list = (TList*) pack->GetObjectByName("FastCutList");
309 if (list) return list->GetEntries();
310 } else if (option == "slow") {
311 TList* list = (TList*) pack->GetObjectByName("CutList");
312 if (list) return list->GetEntries();
313 } else {
314 Int_t total = 0;
315 TList* listf = (TList*) pack->GetObjectByName("FastCutList");
316 TList* list = (TList*) pack->GetObjectByName("CutList");
317 if (listf) total += listf->GetEntries();
318 if (list) total += list->GetEntries();
319 return total;
320 }
321 return 0;
322 }
323
324 Int_t AnaFile::GetCuMonitorsNo(Hal::ECutUpdate update, Int_t collection, TString opt) const {
325 Package* pack = GetCutCollection(update, collection);
326 TString option = opt;
327 Int_t d1, d2, d3, d4;
328 d1 = d2 = d3 = d4 = 0;
329 TList* list = (TList*) pack->GetObjectByName("CutMonitorList");
330 if (list == NULL) return 0;
331 for (int iCut = 0; iCut < list->GetEntries(); iCut++) {
332 Package* mon = (Package*) list->At(iCut);
333 if (mon->GetObjectByName("AxisZ", 0, kTRUE)) {
334 d3++;
335 } else if (mon->GetObjectByName("AxisY", 0, kTRUE)) {
336 d2++;
337 } else {
338 d1++;
339 }
340 d4++;
341 }
342 if (option == "1d") return d1;
343 if (option == "2d") return d2;
344 if (option == "3d") return d3;
345 return d4;
346 }
347
348 void AnaFile::PrintCuts(Hal::ECutUpdate update, Int_t col) const {
349 TList* list = NULL;
350 TString label = "";
351 list =
352 ((TList*) GetCutContainer()->GetObjectByName(Form("%sCutCollectionList", Hal::Std::UpdateEnumToString(update).Data())));
353 label = Form("%s Cuts", Hal::Std::UpdateEnumToString(update).Data());
354 if (list == NULL) return;
355 if (col == -1) {
356 for (int i = 0; i < list->GetEntries(); i++) {
357 Cout::Text(Form("%s Cut col: %i", label.Data(), i), "M", kCyan);
358 PrintCutsInPackage((Package*) list->At(i));
359 }
360 } else {
361 Cout::Text(Form("%s Cut col: %i", label.Data(), col), "M", kCyan);
362 PrintCutsInPackage((Package*) list->At(col));
363 }
364 }
365
366 void AnaFile::PrintCutsInPackage(Package* pack) const {
367 TList* fast_cuts = (TList*) pack->GetObjectByName("FastCutList");
368 TList* slow_cuts = (TList*) pack->GetObjectByName("CutList");
369 ParameterULong64* passedFast = (ParameterULong64*) pack->GetObjectByName("PassedFast");
370 ParameterULong64* passedSlow = (ParameterULong64*) pack->GetObjectByName("PassedSlow");
371 ParameterULong64* failedFast = (ParameterULong64*) pack->GetObjectByName("FailedFast");
372 ParameterULong64* failedSlow = (ParameterULong64*) pack->GetObjectByName("FailedSlow");
373 TString PassedFast = Hal::Std::RoundToString(passedFast->GetValue());
374 TString PassedSlow = Hal::Std::RoundToString(passedSlow->GetValue());
375 TString FailedFast = Hal::Std::RoundToString(failedFast->GetValue());
376 TString FailedSlow = Hal::Std::RoundToString(failedSlow->GetValue());
377 Cout::Database({"Type", "Failed", "Passed"});
378 Cout::Database({"Fast", PassedFast, FailedFast});
379 Cout::Database({"Slow", PassedFast, FailedSlow});
380 Cout::Text("Fast Cuts", "M", kCyan);
381 Cout::Database({"Name", "Passed %", "Param", "Min", "Max"});
382 for (int i = 0; i < fast_cuts->GetEntries(); i++)
383 PrintCut((Package*) fast_cuts->At(i));
384 Cout::Text("Slow Cuts", "M", kCyan);
385 for (int i = 0; i < slow_cuts->GetEntries(); i++)
386 PrintCut((Package*) slow_cuts->At(i));
387 }
388
389 void AnaFile::PrintCut(Package* cut) const {
390 TString name = cut->GetName();
391 Double_t passed = (Double_t)(((ParameterULong64*) cut->GetObjectByName("Passed")))->GetValue();
392 Double_t failed = (Double_t)(((ParameterULong64*) cut->GetObjectByName("Failed")))->GetValue();
393 Int_t cut_size = (((ParameterInt*) cut->GetObjectByName("CutSize")))->GetValue();
394 passed = passed / (passed + failed) * 100.0;
395 if (cut_size == 0) {
396 TString unit_name = ((ParameterString*) cut->GetObjectByName("UnitName_0i"))->GetValue();
397 TString min = Hal::Std::RoundToString(((ParameterDouble*) cut->GetObjectByName("MinCut_0"))->GetValue(), 3);
398 TString max = Hal::Std::RoundToString(((ParameterDouble*) cut->GetObjectByName("MaxCut_0"))->GetValue(), 3);
399 Cout::Database({name, Form("%4.2f", passed), unit_name, min, max});
400 return;
401 }
402 TString min, max, unit;
403
404 for (int i = 0; i < cut_size; i++) {
405 min = Form("MinCut_%i", i);
406 max = Form("MaxCut_%i", i);
407 unit = Form("UnitName_%i", i);
408 unit = ((ParameterString*) cut->GetObjectByName(unit))->GetValue();
409 min = Hal::Std::RoundToString(((ParameterDouble*) cut->GetObjectByName(min))->GetValue(), 3);
410 max = Hal::Std::RoundToString(((ParameterDouble*) cut->GetObjectByName(max))->GetValue(), 3);
411 if (i == 0) {
412 Cout::Database({name, Form("%4.2f", passed), unit, min, max});
413 } else {
414 Cout::Database({"", "", unit, min, max});
415 }
416 }
417 }
418
419 Package* AnaFile::GetCutPackage(Hal::ECutUpdate update, Int_t collection, Int_t cutNo, Option_t* opt) const {
420 TList* list = NULL;
421 TString label = "";
422 list =
423 ((TList*) GetCutContainer()->GetObjectByName(Form("%sCutCollectionList", Hal::Std::UpdateEnumToString(update).Data())));
424 label = Form("%s Cuts", Hal::Std::UpdateEnumToString(update).Data());
425 if (list == NULL) return NULL;
426 Package* cutCollection = (Package*) list->At(collection);
427 if (cutCollection == NULL) return NULL;
428 TString option = opt;
429 TList* list_fast = (TList*) cutCollection->GetObjectByName("FastCutList");
430 TList* list_slow = (TList*) cutCollection->GetObjectByName("CutList");
431 if (option == "all") {
432 if (cutNo > list_fast->GetEntries()) {
433 return (Package*) list_slow->At(cutNo - list_fast->GetEntries());
434 } else {
435 return (Package*) list_fast->At(cutNo);
436 }
437 } else if (option == "slow") {
438 return (Package*) list_slow->At(cutNo);
439 } else { // fast cuts
440 return (Package*) list_fast->At(cutNo);
441 }
442 }
443
444 Package* AnaFile::GetMainPackage() const { return static_cast<Package*>(fMainPackageArray->UncheckedAt(fCurrentPackID)); }
445
446 Package* AnaFile::GetCutContainer() const { return static_cast<Package*>(fCutContainerArray->UncheckedAt(fCurrentPackID)); }
447} // namespace Hal
Int_t GetMainObjectsNo() const
Definition AnaFile.cxx:263
void ExportHTML(TString dirname="extr_html", Int_t task_id=-1) const
Definition AnaFile.cxx:162
TObject * GetMainObject(Int_t i=0) const
Definition AnaFile.cxx:130
AnaFile(TString filename, TString packname)
Definition AnaFile.cxx:31
TH1 * GetHistogramPassed(Hal::ECutUpdate update, Int_t collection, Int_t no) const
Definition AnaFile.cxx:185
Package * GetPackage(TString name) const
Definition AnaFile.cxx:202
Package * GetMetaData() const
Definition AnaFile.cxx:253
ULong64_t GetPassedNo(Hal::ECutUpdate update, Int_t collection, Option_t *opt="slow") const
Definition AnaFile.cxx:282
void PrintCuts(Hal::ECutUpdate update=Hal::ECutUpdate::kNo, Int_t col=-1) const
Definition AnaFile.cxx:348
Int_t GetMerged() const
Definition AnaFile.cxx:153
Int_t GetCutsNo(Hal::ECutUpdate update, Int_t collection, TString opt="all") const
Definition AnaFile.cxx:304
Int_t GetCollectionsNo(Hal::ECutUpdate update) const
Definition AnaFile.cxx:172
Int_t GetCuMonitorsNo(Hal::ECutUpdate update, Int_t collection, TString opt="all") const
Definition AnaFile.cxx:324
static void GetPackageList(TString filename)
Definition AnaFile.cxx:265
TH1 * GetHistogramFailed(Hal::ECutUpdate update, Int_t collection, Int_t no) const
Definition AnaFile.cxx:193
Bool_t SwitchPackage(TString name)
Definition AnaFile.cxx:212
Package * GetCutPackage(Hal::ECutUpdate update, Int_t collection, Int_t cutNo, Option_t *type="all") const
Definition AnaFile.cxx:419
ULong64_t GetFailedNo(Hal::ECutUpdate update, Int_t collection, Option_t *opt=" slow") const
Definition AnaFile.cxx:293
static void Text(TString text, TString option="L", Color_t color=-1)
Definition Cout.cxx:92
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void Database(Int_t no...)
static void FailSucced(TString value, TString flag, Color_t color)
Definition Cout.cxx:382
TObject * GetObject(Int_t i) const
Definition Package.cxx:52
Int_t GetMerged() const
Definition Package.cxx:140
TObject * GetObjectByName(TString name, Int_t index=0, Bool_t quited=kFALSE) const
Definition Package.cxx:67
Int_t GetEntries() const
Definition Package.cxx:61
const char * GetName() const
Definition Package.cxx:167
TString GetValue() const
Definition Parameter.h:202
ULong64_t GetValue() const
Definition Parameter.h:292