Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
AnalysisManager.cxx
1/*
2 * RunAna.cxx
3 *
4 * Created on: 4 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 "AnalysisManager.h"
11
12#include "DataManager.h"
13#include "IOManager.h"
14#include "MagField.h"
15
16#include "Cout.h"
17#include "Package.h"
18#include "Parameter.h"
19#include "Source.h"
20#include "Std.h"
21#include "Task.h"
22#include "TriggerTask.h"
23
24#include <cstdlib>
25
26#include <RtypesCore.h>
27#include <TFile.h>
28
29namespace Hal {
30 AnalysisManager::AnalysisManager() {}
31
33 Cout::PrintInfo("=== AnalysisManager::Init ===", EInfo::kInfo);
34 if (fSource == nullptr) exit(0);
35 Bool_t initSource = fSource->Init();
36 if (!initSource) { Cout::PrintInfo("Can't init source!", EInfo::kCriticalError); }
37 fManager = fSource->GetIOManager();
38 if (!fManager) {
39 Cout::PrintInfo("IO manager not found!", EInfo::kCriticalError);
40 exit(0);
41 }
42 if (fField == nullptr) { fField = new MagField(); }
43 fManager->SetField(fField);
44 fManager->SetOutput(fOutputFile);
45 fManager->Init();
46 DataManager* mng = DataManager::Instance();
47 mng->SetManager(fManager);
48 Cout::PrintInfo("=== Init triggers ===", EInfo::kDebugInfo);
49 for (auto task : fTriggers) {
50 Cout::PrintInfo(Form(" Init trigger %s", task->ClassName()), EInfo::kDebugInfo);
51 Task::EInitFlag stat = task->Init();
52 switch (stat) {
53 case Task::EInitFlag::kERROR: {
54 Cout::PrintInfo(Form(" Trigger %s go to passive mode", task->ClassName()), EInfo::kDebugInfo);
55 fPassiveTriggers.push_back(task);
56 } break;
57 case Task::EInitFlag::kSUCCESS: {
58 task->MarkAsActive();
59 Cout::PrintInfo(Form(" Trigger %s go to active mode", task->ClassName()), EInfo::kDebugInfo);
60 fActiveTriggers.push_back(task);
61 } break;
62 case Task::EInitFlag::kFATAL: {
63 Cout::PrintInfo(Form(" Failed to init %s", task->ClassName()), EInfo::kError);
64 exit(0);
65 } break;
66 }
67 }
68 Cout::PrintInfo("=== Init tasks ===", EInfo::kInfo);
69 for (auto task : fTasks) {
70 Task::EInitFlag stat = task->Init();
71 Cout::PrintInfo(Form(" Init task %s", task->ClassName()), EInfo::kDebugInfo);
72 switch (stat) {
73 case Task::EInitFlag::kERROR: {
74 Cout::PrintInfo(Form(" Task %s go to passive mode", task->ClassName()), EInfo::kDebugInfo);
75 fPassiveTasks.push_back(task);
76 } break;
77 case Task::EInitFlag::kSUCCESS: {
78 Cout::PrintInfo(Form(" Task %s go to active mode", task->ClassName()), EInfo::kDebugInfo);
79 fActiveTasks.push_back(task);
80 } break;
81 case Task::EInitFlag::kFATAL: {
82 Cout::PrintInfo(Form(" Failed to init %s", task->ClassName()), EInfo::kError);
83 exit(0);
84 } break;
85 }
86 }
87 if (fActiveTriggers.size() > 0) { fTriggersEnabled = kTRUE; }
88 fManager->LockUnusedBranches(); // lock unused branches by setting status to zero
89 return kTRUE;
90 }
91
92 void AnalysisManager::Run(Int_t start, Int_t end) {
93 if (start == -1 && end == -1) {
94 start = 0;
95 end = fManager->GetEntries();
96 }
97 if (start < 0) start = 0;
98 if (start > fManager->GetEntries()) { start = fManager->GetEntries(); }
99 if (end > fManager->GetEntries()) { end = fManager->GetEntries(); }
100 Cout::PrintInfo(Form("Run from %i to %i events", start, end), EInfo::kInfo);
101 Int_t percent = (end - start) / 100;
102 if (percent == 0) percent = 1;
103 for (int i = start; i < end; i++) {
104 ++fProcessedEvents;
105 if (fProgressBar) {
106 if (fProcessedEvents % percent == 1) Hal::Cout::ProgressBar(fProcessedEvents, end - start);
107 }
108 DoStep(i);
109 fManager->FillTree();
110 }
111 Finish();
112 }
113
114 void AnalysisManager::Finish() {
115 auto GoToDir = [](TString name) {
116 TDirectory* dir = (TDirectory*) gFile;
117 std::vector<TString> array = Hal::Std::ExplodeString(name, '/');
118 for (int i = 0; i < (int) array.size(); i++) {
119 TDirectory* newdir = (TDirectory*) dir->FindObject(array[i]);
120 if (!newdir) { dir->mkdir(array[i]); }
121 dir->cd(array[i]);
122 }
123 };
124
125 Package* metadata_new = new Package();
126 metadata_new->SetName("RunInfo");
127 metadata_new->AddObject(new ParameterString("Software ver", HAL_PHYSICALANALYSYS_VER));
128 metadata_new->AddObject(new ParameterString("Date", Hal::Std::GetDate(), 'f'));
129 metadata_new->AddObject(new ParameterString("Time", Hal::Std::GetTime(), 'f'));
130 metadata_new->AddObject(new ParameterUInt("Processed_events", fProcessedEvents, '+'));
131 metadata_new->AddObject(new ParameterString("Input file", DataManager::Instance()->GetSourceName(), 'f'));
132 metadata_new->AddObject(fManager->GetBranchesList());
133
134 TList* trigList = new TList();
135 trigList->SetName("EventTriggers");
136
137 for (auto probeTask : fActiveTriggers) {
138 trigList->AddLast(probeTask->Report());
139 }
140 for (auto probeTask : fPassiveTriggers) {
141 trigList->AddLast(probeTask->Report());
142 }
143 metadata_new->AddObject(trigList);
144
145 TList* list = new TList();
146 list->SetName("Tasks");
147 for (auto task : fTasks) {
148 TString name = task->ClassName();
149 TString label = "Active";
150 for (auto probeTask : fPassiveTasks) {
151 if (probeTask == task) label = "Passive";
152 }
153 list->AddLast(new ParameterString(name, label));
154 }
155 metadata_new->AddObject(list);
156
157
158 TDirectory* dir = (TDirectory*) gFile;
159
160 GoToDir("HalInfo");
161 TDirectory* metadatata = (TDirectory*) dir->Get("HalInfo");
162 if (metadatata->Get("RunInfo")) {
163 delete metadata_new;
164 } else {
165 metadata_new->Write("RunInfo");
166 }
167 gFile->cd();
168 for (auto task : fActiveTasks) {
169 task->FinishTask();
170 }
171 Cout::PrintInfo("Analysis completed", EInfo::kInfo);
172 fManager->CloseManager();
173 }
174
175 AnalysisManager::~AnalysisManager() {
176 delete fSource;
177 delete fManager;
178 for (auto task : fTasks) {
179 delete task;
180 }
181 for (auto task : fTriggers) {
182 delete task;
183 }
184 if (fField) delete fField;
185 }
186
188
190
191 void AnalysisManager::DoStep(Int_t entry) {
192 if (fTriggersEnabled) {
193 fManager->GetEntry(entry, 0);
194 for (auto task : fActiveTriggers) {
195 task->Exec("");
196 if (!task->IsEventGood()) return;
197 }
198 }
199 fManager->GetEntry(entry, 1);
200 for (auto task : fActiveTasks) {
201 task->Exec("");
202 }
203 }
204
205} // namespace Hal
void AddTrigger(TriggerTask *trigger)
void AddReader(Reader *reader)
virtual Bool_t Init()
void Run(Int_t start=-1, Int_t end=-1)
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void ProgressBar(Double_t acutal, Double_t total)
Definition Cout.cxx:229
void SetManager(IOManager *mngr)
Definition DataManager.h:65
virtual TList * GetBranchesList() const
virtual void CloseManager()
Definition IOManager.h:201
virtual void LockUnusedBranches()
Definition IOManager.h:237
virtual Int_t GetEntries() const =0
virtual void SetOutput(TString)
Definition IOManager.h:128
virtual Int_t GetEntry(Int_t i, Int_t flag)=0
virtual void SetField(MagField *field)
Definition IOManager.h:123
virtual void FillTree()=0
virtual Bool_t Init()=0
virtual IOManager * GetIOManager() const
Definition Source.cxx:29