Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
EventAna.cxx
1/*
2 * EventAna.cxx
3 *
4 * Created on: 05-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 "EventAna.h"
11#include "MemoryMapManager.h"
12
13#include "Cout.h"
14#include "Cut.h"
15#include "CutContainer.h"
16#include "CutsAndMonitors.h"
17#include "DataFormatManager.h"
18#include "DataManager.h"
19#include "Event.h"
20#include "EventVirtualCut.h"
21#include "Helix.h"
22#include "MagField.h"
23#include "Package.h"
24#include "Parameter.h"
25#include "StdString.h"
26
27#include <TCollection.h>
28#include <TDatabasePDG.h>
29#include <TFile.h>
30#include <TList.h>
31#include <TNamed.h>
32#include <TObjString.h>
33#include <vector>
34
35
36namespace Hal {
37
38 EventAna::EventAna(ECutUpdate tiers) :
39 Task(),
40 fProcessedEvents(0),
41 fMixSize(1),
42 fEventCollectionsNo(0),
43 fCurrentEventCollectionID(0),
44 fTiers(tiers),
45 fPDG(nullptr),
46 fCutContainer(NULL),
47 fMemoryMap(nullptr),
48 fCurrentEvent(nullptr),
49 fComment(""),
50 fInit(kFALSE),
51 fInChain(kFALSE),
52 fTaskID(0),
53 fInFileName(""),
54 fDataFormatManager(nullptr) {
56 fDataFormatManager = DataFormatManager::Instance();
60 AddTags("ana");
61#ifdef _HAL_CLEAR_BUFFER_
62 fIsLastTask = kFALSE;
63#endif
64 }
65
66 Task::EInitFlag EventAna::Init() {
67#ifdef _HAL_CLEAR_BUFFER_
68 // TList* list = FairRunAna::Instance()->GetMainTask()->GetListOfTasks();
69 // if (this == list->At(list->GetEntries() - 1)) { fIsLastTask = kTRUE; }
70#endif
71 /* trying to set magfield */
72 fPDG = TDatabasePDG::Instance();
73
74 if (TESTBIT(fFormatOption, eBitFormat::kChecking) == kTRUE) {
75#ifdef HAL_DEBUG
76 Cout::PrintInfo("Format checking", EInfo::kDebugInfo);
77#endif
78 if (CheckFormat() == Task::EInitFlag::kFATAL) {
79 Cout::PrintInfo("Failed to check format", EInfo::kError);
80 return Task::EInitFlag::kFATAL;
81 }
82 }
83 Task::EInitFlag stat = InitCutContainer();
84 if (stat == Task::EInitFlag::kFATAL) {
85 Cout::PrintInfo("Failed to init cut container", EInfo::kError);
86 return Task::EInitFlag::kFATAL;
87 }
90 if (!this->InheritsFrom("Hal::TrackAna") && !this->InheritsFrom("Hal::MultiTrackAna")) {
91 AddTags("eventana");
92 AddTags(DataFormatManager::Instance()->GetFormatName(this->fTaskID));
93 }
94 fInit = kTRUE;
95 return Task::EInitFlag::kSUCCESS;
96 }
97
98#ifdef _HAL_CLEAR_BUFFER_
99 void EventAna::ClearMemory() {
100 if (fIsLastTask) { fMemoryMap->ClearEvent(); }
101 }
102#endif
103
104 void EventAna::Exec(Option_t* /*opt*/) {
109 }
110#ifdef _HAL_CLEAR_BUFFER_
111 ClearMemory();
112#endif
113 }
114
115 void EventAna::SetOption(Option_t* opt) {
116 TString option = opt;
117 if (option.Contains("+")) {
118 std::vector<TString> arr = Hal::Std::ExplodeString(option, '+');
119 for (int i = 0; i < (int) arr.size(); i++) {
120 SetOption(arr[i]);
121 }
122 } else {
123 Cout::PrintInfo(Form("Invalid SetOption %s for class %s", option.Data(), this->ClassName()), EInfo::kLowWarning);
124 }
125 }
126
127 Task::EInitFlag EventAna::InitCutContainer() {
128 if (fCutContainer == NULL) {
129#ifdef HAL_DEBUG
130 Cout::PrintInfo("Null Cut Container, building ...", EInfo::kDebugInfo);
131#endif
133 }
134#ifdef HAL_DEBUG
135 Cout::PrintInfo("Checking Cut Containers ...", EInfo::kDebugInfo);
136#endif
138 for (int i = 0; i < fCutContainer->GetSize(); i++) {
139 CutCollection* empty_cont;
140 switch (i) {
141 case 0: {
142 for (int j = 0; j < fCutContainer->GetEventCollectionsNo(); j++) {
143 empty_cont = fCutContainer->GetEventCollection(j);
144 if (empty_cont == NULL) {
145 Cout::PrintInfo("NULL subcontainer for events", EInfo::kError);
146 return Task::EInitFlag::kFATAL;
147 }
148 }
149 } break;
150 case 1: {
151 for (int j = 0; j < fCutContainer->GetTrackCollectionsNo(); j++) {
152 empty_cont = fCutContainer->GetTrackCollection(j);
153 if (empty_cont == NULL) {
154 Cout::PrintInfo("NULL subcontainer for tracks", EInfo::kError);
155 return Task::EInitFlag::kFATAL;
156 }
157 }
158 } break;
159 case 2: {
160 for (int j = 0; j < fCutContainer->GetTwoTrackCollectionsNo(); j++) {
161 empty_cont = fCutContainer->GetTwoTrackCollection(j);
162 if (empty_cont == NULL) {
163 Cout::PrintInfo("NULL subcontainer for two-tracks", EInfo::kError);
164 return Task::EInitFlag::kFATAL;
165 }
166 }
167 } break;
168 case 3: {
169 for (int j = 0; j < fCutContainer->GetTwoTrackCollectionsBackgroundNo(); j++) {
171 if (empty_cont == NULL) {
172 Cout::PrintInfo("NULL subcontainer for background pairs", EInfo::kError);
173 return Task::EInitFlag::kFATAL;
174 }
175 }
176 } break;
177 }
178 }
179#ifdef HAL_DEBUG
180 Cout::PrintInfo("Linking collections", EInfo::kDebugInfo);
181#endif
183#ifdef HAL_DEBUG
184 Cout::PrintInfo("Initialization cut container", EInfo::kDebugInfo);
185#endif
187 return Task::EInitFlag::kSUCCESS;
188 }
189
191
194 // adding virtual cuts to push min-bias analysis
195 EventVirtualCut virtE;
196 virtE.SetCollectionID(0);
197 fCutContainer->AddCut(virtE);
198 }
199
202#ifdef HAL_DEBUG
203 Cout::PrintInfo("Adding virtual event cut", EInfo::kDebugInfo);
204#endif
205 EventVirtualCut eventCut;
206 fCutContainer->AddCut(eventCut, "fast");
207 }
208 }
209
210 void EventAna::SetFormat(Event* format, EFormatDepth format_depth) {
212 dataFormat->SetFormat(format, GetTaskID(), format_depth);
213 SetFormatOption(EFormatOption::kNoReaderAcces);
214 }
215
217 Task(),
218 fFormatOption(ana.fFormatOption),
219 fProcessedEvents(ana.fProcessedEvents),
220 fMixSize(ana.fMixSize),
221 fEventCollectionsNo(ana.fEventCollectionsNo),
222 fTiers(ana.fTiers),
223 fPDG(ana.fPDG),
224 fCutContainer(NULL),
225 fMemoryMap(NULL),
226 fCurrentEvent(NULL),
227 fComment(""),
228 fInit(kFALSE),
229 fInChain(ana.fInChain),
230 fInFileName(ana.fInFileName),
231 fDataFormatManager(ana.fDataFormatManager) {
232 if (ana.fInit) { Cout::PrintInfo("This object has been initialized, this may result in crash", EInfo::kWarning); }
233 if (ana.fCutContainer) { fCutContainer = new CutContainer(*ana.fCutContainer); }
235 fTaskID = fDataFormatManager->RegisterFormat();
236 if (fDataFormatManager->GetFormat(ana.fTaskID)) { SetFormat(fDataFormatManager->GetFormat(ana.fTaskID)->GetNewEvent()); }
237 }
238
239 void EventAna::AddToAnaMetadata(Package* main_pack, TObject* obj) const {
240 if (main_pack == NULL) {
241 Cout::PrintInfo("cannot call AddToMetadata if main_pack argument is NULL", EInfo::kLowWarning);
242 return;
243 }
244 Package* metadata_pack = (Package*) main_pack->GetObjectByName("Metadata");
245 if (metadata_pack == NULL) {
246 Cout::PrintInfo("cannot add object in AddToMetadata package don't contain any Metadata object", EInfo::kLowWarning);
247 return;
248 }
249 metadata_pack->AddObject(obj);
250 }
251
253 fCurrentEvent = nullptr;
254 if (fCutContainer) delete fCutContainer;
255 if (fMemoryMap) delete fMemoryMap;
256 fDataFormatManager = nullptr;
257 }
258
260 Package* pack = new Package(this);
261 pack->SetComment(fComment);
262 Package* ana_metadata = new Package();
263 ana_metadata->SetName("Metadata");
264 ana_metadata->AddObject(new ParameterString("Analysis Name", this->ClassName()));
266 ana_metadata->AddObject(new ParameterString("DataType", dataManager->GetFormatName(GetTaskID(), EFormatDepth::kNonBuffered)));
267 const Event* event_nonbufferend = dataManager->GetFormat(GetTaskID(), EFormatDepth::kNonBuffered);
268 ana_metadata->AddObject(event_nonbufferend->Report());
269 if (dataManager->FormatExist(GetTaskID(), EFormatDepth::kBuffered)) {
270 const Event* event_bufferend = dataManager->GetFormat(GetTaskID(), EFormatDepth::kBuffered);
271 ana_metadata->AddObject(
272 new ParameterString("DataTypeBuf", dataManager->GetFormatName(GetTaskID(), EFormatDepth::kBuffered)));
273 ana_metadata->AddObject(event_bufferend->Report());
274 } else {
275 const Event* event_bufferend = dataManager->GetFormat(GetTaskID(), EFormatDepth::kNonBuffered);
276 ana_metadata->AddObject(
277 new ParameterString("DataTypeBuf", dataManager->GetFormatName(GetTaskID(), EFormatDepth::kNonBuffered)));
278 ana_metadata->AddObject(event_bufferend->Report());
279 }
280 TList* tagList = new TList();
281 tagList->SetName("Tags");
282 for (auto str : fTagList) {
283 tagList->AddLast(new TObjString(str));
284 }
285 ana_metadata->AddObject(tagList);
286 pack->AddObject(ana_metadata);
288 return pack;
289 }
290
292 Package* pack = Report();
293 GoToDir("HalPhysics");
294 pack->Write(Form("AnaPackage_%i", GetTaskID()));
295 if (pack) { delete pack; }
296 gFile->cd();
297 Cout::PrintInfo(Form("%s done, writing results", this->ClassName()), EInfo::kDebugInfo);
298 // DataManager::Instance()->GetManager()->Fill();
299 // DataManager::Instance()->GetManager()->GetOutFile()->WriteKeys();
300 }
301
302 void EventAna::SetComment(TString comment) { fComment = comment; }
303
304 void EventAna::AddTags(TString tag) {
305 if (tag.BeginsWith(" ")) {
306 AddTags(tag(1, tag.Length()));
307 } else if (tag.Contains(" ")) {
308 Int_t begin = tag.First(' ');
309 Int_t lenght = tag.Length();
310 TString first_part(tag(0, begin));
311 AddTags(first_part);
312 TString second_part(tag(begin + 1, lenght));
313 AddTags(second_part);
314 } else {
315 for (auto str : fTagList) {
316 if (str.EqualTo(tag)) return;
317 }
318 fTagList.push_back(tag);
319 }
320 }
321
322 void EventAna::AddCut(const Cut& cut, Option_t* opt) {
323 if (fCutContainer == NULL) { fCutContainer = new CutContainer(fTiers); }
324 fCutContainer->AddCut(cut, opt);
325 }
326
327 void EventAna::AddCutMonitor(const CutMonitor& mon, Option_t* opt) {
328 if (fCutContainer == NULL) { fCutContainer = new CutContainer(fTiers); }
329 fCutContainer->AddMonitor(mon, opt);
330 }
331
333 switch (option) {
335 SETBIT(fFormatOption, eBitFormat::kDirectAcesss); // direct access
336 CLRBIT(fFormatOption, eBitFormat::kReader); // disable reader
337 CLRBIT(fFormatOption, eBitFormat::kSource); // disable source (probably is not stored by direct access class
338 } break;
340 CLRBIT(fFormatOption, eBitFormat::kDirectAcesss); // no direct access
341 } break;
342 case EFormatOption::kNoReaderAcces: {
343 CLRBIT(fFormatOption, eBitFormat::kReader); // reader disabled access
344 } break;
346 SETBIT(fFormatOption, eBitFormat::kCompression);
347 } break;
349 CLRBIT(fFormatOption, eBitFormat::kCompression);
350 } break;
352 SETBIT(fFormatOption, eBitFormat::kSource);
353 } break;
355 CLRBIT(fFormatOption, eBitFormat::kSource);
356 } break;
358 SETBIT(fFormatOption, eBitFormat::kReader);
359 CLRBIT(fFormatOption, eBitFormat::kDirectAcesss);
360 } break;
362 CLRBIT(fFormatOption, eBitFormat::kDirectAcesss); // no direct access
363 CLRBIT(fFormatOption, eBitFormat::kReader); // no reader
364 } break;
366 CLRBIT(fFormatOption, eBitFormat::kChecking);
367 } break;
368 };
369 }
370
371 Task::EInitFlag EventAna::CheckFormat() {
373 DataManager* datamanager = DataManager::Instance();
374 SetInputFileName(DataManager::Instance()->GetSourceName());
375 if (formatManager->GetFormat(GetTaskID(), EFormatDepth::kNonBuffered) == nullptr) {
376 Cout::PrintInfo("Format is not set, switching to reader", EInfo::kError);
378 }
379 if (TESTBIT(fFormatOption, eBitFormat::kReader)) { // use reader data
380 Event* event = nullptr;
381 event = dynamic_cast<Hal::Event*>(datamanager->GetObject("HalEvent."));
382 if (event) {
383 Cout::PrintInfo("L1 format from reader has been found", EInfo::kInfo);
384 formatManager->SetFormat(event->GetNewEvent(), GetTaskID(), EFormatDepth::kNonBuffered, kTRUE);
385 if (formatManager->GetFormat(GetTaskID(), EFormatDepth::kBuffered) == nullptr) {
386 formatManager->SetFormat(event->GetNewEvent(), GetTaskID(), EFormatDepth::kBuffered, kTRUE);
387 }
388 return Task::EInitFlag::kSUCCESS;
389 }
390 } else {
391 if (TESTBIT(fFormatOption, eBitFormat::kDirectAcesss)) { // direct access to data by classname.
392 TString branchName = formatManager->GetFormat(GetTaskID(), EFormatDepth::kNonBuffered)->ClassName();
393 std::vector<TString> patterns;
394 patterns.push_back(branchName);
395 patterns.push_back(branchName + ".");
396 Bool_t foundFormat = kFALSE;
397 for (auto name : patterns) {
398 Hal::Event* event = dynamic_cast<Hal::Event*>(DataManager::Instance()->GetObject(name));
399 if (event) {
400 foundFormat = kTRUE;
401 formatManager->SetFormat(event->GetNewEvent(), GetTaskID(), EFormatDepth::kNonBuffered, kTRUE);
402 if (formatManager->GetFormat(GetTaskID(), EFormatDepth::kBuffered) == nullptr) {
403 formatManager->SetFormat(event->GetNewEvent(), GetTaskID(), EFormatDepth::kBuffered, kTRUE);
404 }
405 break;
406 }
407 }
408 if (!foundFormat) {
409 Cout::PrintInfo(Form("Could not find direct access format %s", branchName.Data()), EInfo::kError);
410 return Task::EInitFlag::kFATAL;
411 }
412 } else { // not direct access nor reader, standard way
413 Bool_t exist = formatManager->GetFormat(GetTaskID(), EFormatDepth::kNonBuffered)->ExistInTree();
414 if (!exist) {
415 Cout::PrintInfo("The format is present but does not exist in the tree!", EInfo::kError);
416 return Task::EInitFlag::kFATAL;
417 }
418 }
419 }
420 return Task::EInitFlag::kSUCCESS;
421 }
422
426#ifdef HAL_DEBUG
427 Cout::PrintInfo("Initialization MemoryMap", EInfo::kDebugInfo);
428#endif
429 std::vector<TString> brName;
430 if (TESTBIT(fFormatOption, eBitFormat::kReader)) {
431 brName.push_back("HalEvent.");
432 } else if (TESTBIT(fFormatOption, eBitFormat::kDirectAcesss)) {
433 TString evName = DataFormatManager::Instance()->GetFormat(GetTaskID())->ClassName();
434 brName.push_back(Form("%s.", evName.Data()));
435 brName.push_back(evName);
436 }
437 fMemoryMap->Init(1, GetTaskID(), TESTBIT(fFormatOption, eBitFormat::kCompression), brName);
438 }
439
441 for (int iCut = 0; iCut < monCuts.GetNCuts(); iCut++) {
442 AddCut(*monCuts.GetCut(iCut), monCuts.GetCutOption(iCut));
443 }
444 for (int iMon = 0; iMon < monCuts.GetNCutMonitors(); iMon++) {
445 AddCutMonitor(*monCuts.GetMonitor(iMon), monCuts.GetCutMonitorOption(iMon));
446 }
447 }
448
450 if (this->fTiers != other.fTiers) {
451 Cout::PrintInfo(Form("Cannot assign %s to %s, different tiers no", this->ClassName(), other.ClassName()), EInfo::kError);
452 return *this;
453 }
454 if (this != &other) {
457 fMixSize = other.fMixSize;
460 fPDG = other.fPDG;
461 if (fCutContainer) {
462 delete fCutContainer;
463 fCutContainer = nullptr;
464 }
465 if (fMemoryMap) {
466 delete fMemoryMap;
467 fMemoryMap = nullptr;
468 }
469 if (other.fCutContainer) fCutContainer = (CutContainer*) other.fCutContainer->Clone();
470 if (other.fMemoryMap) fMemoryMap = (MemoryMapManager*) other.fMemoryMap->Clone();
473 if (manager->FormatExist(other.fTaskID)) { SetFormat(manager->GetNewEvent(other.fTaskID)); }
474 fCurrentEvent = nullptr;
475 fComment = other.fComment;
476 fInit = other.fInit;
477 fInChain = other.fInChain;
478 fTagList = other.fTagList;
479 fInFileName = other.fInFileName;
480 fDataFormatManager = other.fDataFormatManager;
481 if (fInit == kTRUE) { Cout::PrintInfo(Form("Copying initialized task %s", this->ClassName()), EInfo::kWarning); }
482 }
483 return *this;
484 }
485} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
CutCollection * GetTwoTrackBackgroundCollection(Int_t collection) const
Bool_t PassEvent(Event *event, const Int_t collection)
Int_t GetSize() const
Int_t GetTwoTrackCollectionsBackgroundNo() const
CutCollection * GetTwoTrackCollection(Int_t collection) const
virtual Package * Report() const
Int_t GetTwoTrackCollectionsNo() const
void AddMonitor(const CutMonitor &monitor, Option_t *opt=" ")
void AddCut(const Cut &cut, Option_t *opt=" ")
void Init(const Int_t task_id=0)
Int_t GetEventCollectionsNo() const
CutCollection * GetTrackCollection(Int_t collection) const
CutCollection * GetEventCollection(Int_t collection) const
Int_t GetTrackCollectionsNo() const
Definition Cut.h:40
void SetCollectionID(Int_t i)
Definition Cut.h:247
Int_t GetNCutMonitors() const
TString GetCutOption(Int_t i) const
const CutMonitor * GetMonitor(Int_t i) const
const Cut * GetCut(Int_t i) const
TString GetCutMonitorOption(Int_t i) const
const Event * GetFormat(Int_t task_id, EFormatDepth format_depth=EFormatDepth::kAll) const
void SetFormat(Event *format, Int_t task_id, EFormatDepth depth=EFormatDepth::kAll, Bool_t silent=kFALSE)
TString GetFormatName(Int_t task_id, EFormatDepth format_depth=EFormatDepth::kAll) const
static DataFormatManager * Instance()
Event * GetNewEvent(Int_t task_id, EFormatDepth format_depth=EFormatDepth::kAll) const
Bool_t FormatExist(Int_t task_id, EFormatDepth format_depth=EFormatDepth::kAll) const
Int_t fFormatOption
Definition EventAna.h:48
CutContainer * fCutContainer
Definition EventAna.h:78
virtual Task::EInitFlag CheckFormat()
Definition EventAna.cxx:371
Int_t fEventCollectionsNo
Definition EventAna.h:61
Int_t GetTaskID() const
Definition EventAna.h:155
Int_t fInit
Definition EventAna.h:94
virtual void LinkCollections()
Definition EventAna.cxx:190
Task::EInitFlag InitCutContainer()
Definition EventAna.cxx:127
virtual void AddCutMonitor(const CutMonitor &mon, Option_t *opt="")
Definition EventAna.cxx:327
virtual void CheckCutContainerCollections()
Definition EventAna.cxx:200
virtual void SetOption(Option_t *opt)
Definition EventAna.cxx:115
Int_t fMixSize
Definition EventAna.h:57
void SetInputFileName(TString name)
Definition EventAna.h:104
Event * fCurrentEvent
Definition EventAna.h:86
virtual void InitNewCutContainer()
Definition EventAna.cxx:192
void SetFormatOption(EFormatOption option)
Definition EventAna.cxx:332
void AddToAnaMetadata(Package *main_pack, TObject *obj) const
Definition EventAna.cxx:239
virtual void InitMemoryMap()
Definition EventAna.cxx:423
virtual void SetComment(TString comment)
Definition EventAna.cxx:302
UInt_t fProcessedEvents
Definition EventAna.h:53
MemoryMapManager * fMemoryMap
Definition EventAna.h:82
const ECutUpdate fTiers
Definition EventAna.h:70
EventAna & operator=(const EventAna &other)
Definition EventAna.cxx:449
TDatabasePDG * fPDG
Definition EventAna.h:74
virtual void AddCut(const Cut &cut, Option_t *opt="")
Definition EventAna.cxx:322
virtual Package * Report() const
Definition EventAna.cxx:259
virtual void FinishTask()
Definition EventAna.cxx:291
virtual ~EventAna()
Definition EventAna.cxx:252
Int_t fCurrentEventCollectionID
Definition EventAna.h:65
virtual void Exec(Option_t *opt)
Definition EventAna.cxx:104
virtual void AddCutsAndMonitors(const CutsAndMonitors &monCuts)
Definition EventAna.cxx:440
virtual void SetFormat(Event *format, EFormatDepth depth=EFormatDepth::kAll)
Definition EventAna.cxx:210
TString fComment
Definition EventAna.h:90
virtual void ProcessEvent()
Definition EventAna.h:163
virtual Task::EInitFlag Init()
Definition EventAna.cxx:66
virtual void AddTags(TString tag)
Definition EventAna.cxx:304
virtual Package * Report() const
Definition Event.cxx:252
virtual Bool_t ExistInTree() const
Definition Event.h:218
void Init(Int_t event_factor, Int_t task_id, Bool_t compress, std::vector< TString > direct)
void SetMixSize(Int_t mix_size)
TObject * GetObjectByName(TString name, Int_t index=0, Bool_t quited=kFALSE) const
Definition Package.cxx:67
void SetComment(TString name)
Definition Package.cxx:65
void SetName(TString name)
Definition Package.cxx:298
void AddObject(TObject *object)
Definition Package.cxx:209
void GoToDir(TString name)
Definition Task.cxx:28