Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
MemoryMapManager.cxx
1/*
2 * MemoryMapManager.cxx
3 *
4 * Created on: 28-10-2013
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "MemoryMapManager.h"
11
12#include "Cout.h"
13#include "CutCollection.h"
14#include "CutContainer.h"
15#include "DataFormatManager.h"
16#include "DataManager.h"
17#include "Link.h"
18#include "Std.h"
19#include "Track.h"
20
21#include <TMathBase.h>
22#include <TString.h>
23#include <iostream>
24
25
26namespace Hal {
28 fUseCompression(kFALSE),
29 fDirectAcces(kFALSE),
30 fTrackMapSize(4000),
31 fFormatID(0),
32 fEventCollectionsNo(1),
33 fTrackCollectionsNo(1),
34 fMixSize(1),
35 fEventToTrackNo(0),
36 fCounter(nullptr),
37 fTotalTracks(nullptr),
38 fReadyToMix(nullptr),
39 fEvents(nullptr),
40 fInterface(nullptr),
41 fMaxTrackCollectionNo(0),
42 fCurrentEvent(nullptr),
43 fTrackMap(nullptr),
44 fTrackCounter(nullptr) {}
45
47 fEventCollectionsNo = cont->GetEventCollectionsNo();
48 fTrackCollectionsNo = cont->GetTrackCollectionsNo();
49 fEventToTrackNo = new Int_t[fEventCollectionsNo];
50 /*\
51 * this stuff is needed to check if all track collections belong to given
52 * event collections have some particles to avoid e.g. mixing empty kaon
53 * events with full pion events in non-id femto this checking is provide if
54 * call update counters (event_collection, kTRUE)
55 */
56 for (int i = 0; i < fEventCollectionsNo; i++) {
57 fEventToTrackNo[i] = cont->GetEventCollection(i)->GetNextNo();
58 fMaxTrackCollectionNo = TMath::Max(fEventToTrackNo[i], fMaxTrackCollectionNo);
59 }
60 }
61
62 void MemoryMapManager::BufferEvent(Int_t collection) {
63 if (fUseCompression) {
64 CalculateCompressedMap(collection);
65 Event* uevent = fEvents[collection]->At(fCounter[collection]);
66 uevent->Build(fCurrentEvent, fCompression);
67 // we need to rebuild links
68 for (int iTrack = 0; iTrack < uevent->GetTotalTrackNo(); iTrack++) {
69 Track* track = uevent->GetTrack(iTrack);
70 track->TranslateLinks(fCompression);
71 // track->SetThisID(iTrack);
72 }
73 fTotalTracks[collection] = uevent->GetTotalTrackNo();
74 } else {
75 Event* uevent = fEvents[collection]->At(fCounter[collection]);
76 uevent->Build(fCurrentEvent);
77 fTotalTracks[collection] = uevent->GetTotalTrackNo();
78 }
79#ifdef MEMORYMAMANGER_COMPRESSION_DEBUG_
80 std::cout << "MEMORY MANAGER DEBUG MAPS" << std::endl;
81 std::cout << "ORYGINAL EVENT" << std::endl;
82 Event* temp = GetTemporaryEvent();
83 int total = temp->GetTotalTrackNo();
84 if (total > 50) total = 50;
85 for (int i = 0; i < total; i++) {
86 Track* tr = temp->GetTrack(i);
87 std::cout << tr->GetId() << " " << tr->GetLinks()->GetLinks(0) << std::endl;
88 }
89 std::cout << "COPIED EVENT" << std::endl;
90 temp = fEvents[collection]->At(fCounter[collection]);
91 total = temp->GetTotalTrackNo();
92 if (total > 50) total = 50;
93 for (int i = 0; i < total; i++) {
94 Track* tr = temp->GetTrack(i);
95 std::cout << tr->GetId() << " " << tr->GetLinks()->GetLinks(0) << std::endl;
96 }
97 std::cout << "==============" << std::endl;
98#endif
99 }
100 //-----------------------------
101 void MemoryMapManager::ReloadMap(Int_t size) {
102 fTrackMap->MakeBigger(-1, -1, -1, size);
103 fTrackMapSize = size;
104#ifdef HAL_DEBUG
105 Cout::PrintInfo("Reloading maps", EInfo::kDebugInfo);
106#endif
107 }
108
109 void MemoryMapManager::SetMixSize(Int_t mix_size) { fMixSize = mix_size; }
110
111 void MemoryMapManager::ManualUpdate(Event* event) { event->Update(fInterface); }
112
114 if (!fDirectAcces) fCurrentEvent->Update(fInterface);
115 if (fCurrentEvent->GetTotalTrackNo() > fTrackMapSize) {
116 for (int i = 0; i < fEventCollectionsNo; i++) {
117 ReloadMap(fCurrentEvent->GetTotalTrackNo() * 1.2);
118 }
119 }
120 return fCurrentEvent;
121 }
122
123 void MemoryMapManager::Init(Int_t event_factor, Int_t task_id, Bool_t compress, std::vector<TString> direct) {
124 if (direct.size() > 0) {
125 fDirectAcces = kTRUE;
126 } else {
127 fDirectAcces = kFALSE;
128 }
129 fFormatID = task_id;
130 fUseCompression = compress;
131 if (event_factor > 1) {
132 Int_t* old_map = fEventToTrackNo;
133 Int_t realEventCol = fEventCollectionsNo;
134 fEventCollectionsNo = fEventCollectionsNo * event_factor;
135 fEventToTrackNo = new Int_t[fEventCollectionsNo];
136 for (int i = 0; i < realEventCol; i++) {
137 for (int j = 0; j < event_factor; j++) {
138 fEventToTrackNo[j + i * event_factor] = old_map[i];
139 }
140 fMaxTrackCollectionNo = TMath::Max(fEventToTrackNo[i], fMaxTrackCollectionNo);
141 }
142 delete[] old_map;
143 }
144
145 fEvents = new EventArray*[fEventCollectionsNo];
146 for (Int_t i = 0; i < fEventCollectionsNo; i++) {
147 fEvents[i] = new EventArray(fFormatID, fMixSize);
148 }
149
151 if (fDirectAcces == kFALSE) {
152 fCurrentEvent = dataManager->GetNewEvent(fFormatID, EFormatDepth::kNonBuffered);
153 fInterface = fCurrentEvent->CreateInterface();
154 fInterface->ConnectToTree(Hal::EventInterface::eMode::kRead);
155 } else {
156 for (auto brName : direct) {
157 Event* ev = dynamic_cast<Event*>(DataManager::Instance()->GetObject(brName));
158 if (ev) {
159 fCurrentEvent = ev;
160 break;
161 }
162 }
163 }
164 fTotalTracks = new Int_t[fEventCollectionsNo];
165 fTrackMap = new Array_4<Int_t>();
166 fTrackCounter = new Array_3<Int_t>();
167 fTrackCounter->MakeBigger(fEventCollectionsNo, fTrackCollectionsNo, fMixSize); // TODO swap order
168 fTrackMap->MakeBigger(fEventCollectionsNo, fTrackCollectionsNo, fMixSize, fTrackMapSize);
169 fReadyToMix = new Bool_t[fEventCollectionsNo];
170 for (int i = 0; i < fEventCollectionsNo; i++) {
171 fReadyToMix[i] = kFALSE;
172 }
173 fCounter = new Int_t[fEventCollectionsNo];
174 for (int i = 0; i < fEventCollectionsNo; i++) {
175 fCounter[i] = -1;
176 }
177 }
178
179 Int_t MemoryMapManager::GetTracksNo(Int_t event_collection, Int_t track_collection) const {
180 return fTrackCounter->Get(event_collection, track_collection, fCounter[event_collection]);
181 // return fTrackCounter[event_collection][track_collection][
182 // fCounter[event_collection] ];
183 }
184
185 Int_t MemoryMapManager::GetRawTracksNo(Int_t event_collection) const { return fTotalTracks[event_collection]; }
186
187 Bool_t MemoryMapManager::IsReadyToMixing(Int_t collection) const { return fReadyToMix[collection]; }
188
190 fCounter[collection] = -1;
191 for (int i = 0; i < fEventToTrackNo[collection]; i++) {
192 for (int j = 0; j < fMixSize; j++) {
193 fTrackCounter->Set(collection, i, j, 0);
194 }
195 }
196 for (Int_t i = 0; i < fMixSize; i++)
197 fEvents[collection]->At(i)->Clear("C");
198 fReadyToMix[collection] = kFALSE;
199 }
200
201 void MemoryMapManager::AddTrackToMapTrack(Int_t event_collection, Int_t track_collection, Int_t index) {
202 Int_t counter = fCounter[event_collection];
203 fTrackMap->Set(event_collection,
204 track_collection,
205 counter,
206 fTrackCounter->IncrementAfter(event_collection, track_collection, counter),
207 index);
208 }
209
210 void MemoryMapManager::ResetTrackMaps(Int_t event_collection, Int_t counter) {
211 for (int i = 0; i < fTrackCollectionsNo; i++)
212 fTrackCounter->Set(event_collection, i, counter, 0);
213 }
214
215 void MemoryMapManager::ResetTrackMaps(Int_t event_collection, Int_t track_collection, Int_t counter) {
216 // fTrackCounter[event_collection][track_collection][counter]=0;
217 fTrackCounter->Set(event_collection, track_collection, counter, 0);
218 }
219
220 Int_t MemoryMapManager::GetTracksNo(Int_t event_collection, Int_t track_collection, Int_t counter) const {
221 // return fTrackCounter[event_collection][track_collection][counter];
222 return fTrackCounter->Get(event_collection, track_collection, counter);
223 }
224
225 Int_t MemoryMapManager::GetTrackCollectionsNo(Int_t event_collection) const { return fEventToTrackNo[event_collection]; }
226
227 Int_t MemoryMapManager::GetTemporaryTotalTracksNo() const { return fCurrentEvent->GetTotalTrackNo(); }
228
229 Event* MemoryMapManager::GetEvent(Int_t collection) const { return (Event*) (fEvents[collection]->At(fCounter[collection])); }
230
231 Track* MemoryMapManager::GetTrack(Int_t event_collection, Int_t track_collection, Int_t index) const {
232 //(fEvents[event_collection]->At(fCounter[event_collection])
233 //)->GetTrack(track,fTrackMap[event_collection][track_collection][fCounter[event_collection]][index]
234 //);
235 return (fEvents[event_collection]->At(fCounter[event_collection]))
236 ->GetTrack(fTrackMap->Get(event_collection, track_collection, fCounter[event_collection], index));
237 }
238
239 Track* MemoryMapManager::GetRawTrack(Int_t event_collection, Int_t index) const {
240 return (fEvents[event_collection]->At(fCounter[event_collection]))->GetTrack(index);
241 }
242
243 Event* MemoryMapManager::GetEvent(Int_t collection, Int_t counter) const { return (Event*) (fEvents[collection]->At(counter)); }
244
245 Track* MemoryMapManager::GetTrack(Int_t event_collection, Int_t track_collection, Int_t counter, Int_t index) const {
246 return (fEvents[event_collection]->At(counter))->GetTrack(fTrackMap->Get(event_collection, track_collection, counter, index));
247 }
248
249 Track* MemoryMapManager::GetRawTrack(Int_t event_collection, Int_t counter, Int_t index) const {
250 return (fEvents[event_collection]->At(counter))->GetTrack(index);
251 }
252
254 Event* ptr = NULL;
255 Track* ptr1 = NULL;
256 for (int ev_trig = 0; ev_trig < fEventCollectionsNo; ev_trig++) {
257 for (int count = 0; count < fMixSize; count++) {
258 // if(i!=2) continue;
259 std::cout << Form("Event %d collection %d", count, ev_trig) << std::endl;
260 ptr = GetEvent(ev_trig, count);
261 ptr->Print();
262
263 for (int tr_trig = 0; tr_trig < fTrackCollectionsNo; tr_trig++) {
264 std::cout << Form("buffered %i", GetTracksNo(ev_trig, tr_trig, count)) << std::endl;
265 for (int k = 0; k < GetTracksNo(ev_trig, tr_trig, count); k++) {
266 // for(int k=0;k<ptr->GetTotalTrackNo();k++){
267 // GetBufferedRawTrack(ptr1,j,i,k);
268 Int_t pos = fTrackMap->Get(ev_trig, tr_trig, count, k);
269 // Int_t pos = fTrackMap[ev_trig][tr_trig][count][k];
270 ptr1 = GetRawTrack(ev_trig, count, pos);
271 std::cout << Form("%i val %i", k, pos) << std::endl;
272 ptr1->Print();
273 ptr1 = GetTrack(ev_trig, tr_trig, count, k);
274 ptr1->Print();
275 }
276 }
277 }
278 }
279 }
280
281 void MemoryMapManager::RejectLastEvent(Int_t event_collection) {
282 for (int i = 0; i < fTrackCollectionsNo; i++)
283 fTrackCounter->Set(event_collection, i, fCounter[event_collection], 0);
284 --fCounter[event_collection];
285 }
286
287 Int_t MemoryMapManager::GetCounter(Int_t event_collection) const { return fCounter[event_collection]; }
288
290 // need to be called before next Exec() or lead to memory leak
291 fCurrentEvent->Clear();
292 }
293
294 void MemoryMapManager::CalculateCompressedMap(Int_t event_collection) {
295 int total_tracks = fCurrentEvent->GetTotalTrackNo();
296 int collections = fEventToTrackNo[event_collection];
297
298 Int_t counter = fCounter[event_collection];
299 fCompression.Reset(total_tracks);
300
301 std::vector<int> links;
302 links.resize(fCurrentEvent->GetMaxExpectedLinks());
303 // index global map - if fIndexMap[i=>0 need to store particle
304 for (int iTrackCollection = 0; iTrackCollection < collections; iTrackCollection++) {
305 for (int j = 0; j < GetTracksNo(event_collection, iTrackCollection); j++) {
306 Int_t index = fTrackMap->Get(event_collection, iTrackCollection, counter, j);
307 Track* track = fCurrentEvent->GetTrack(index);
308 Int_t size = track->GetLinksFast(links, kTRUE);
309 for (int iLink = 0; iLink < size; iLink++) {
310 fCompression.MarkAsGood(links[iLink]);
311 }
312 }
313 }
314 fCompression.Recalculate();
315 for (int iTrackCollection = 0; iTrackCollection < collections; iTrackCollection++) {
316 auto submap = fTrackMap->At(event_collection)->At(iTrackCollection)->At(counter);
317 for (int iTrack = 0; iTrack < GetTracksNo(event_collection, iTrackCollection); iTrack++) {
318 Int_t old_index = submap->Get(iTrack);
319 submap->Set(iTrack, fCompression.GetNewIndex(old_index));
320 }
321 }
322
323#ifdef MAPMANAGER_DEBUG
324 std::cout << " collections at" << counter << std::endl;
325 for (int i = 0; i < collections; i++) {
326 std::cout << "Collection " << i << " end = " << End[i] << std::endl;
327 for (int j = 0; j < fTrackCounter->Get(event_collection, i, counter); j++) {
328 std::cout << Form(" %4d", fTrackMap->Get(event_collection, i, counter, j));
329 }
330 std::cout << std::endl;
331 }
332 std::cout << "compression" << std::endl;
333#endif
334
335#ifdef MAPMANAGER_DEBUG
336 std::cout << "SUMM" << std::endl;
337 for (int i = 0; i < fSumMapSize; i++) {
338 // std::cout<<Form(" %4d",fTrackMap->Get(event_collection,0,counter,i));
339 }
340 std::cout << std::endl;
341 for (int i = 0; i < fSumMapSize; i++) {
342 std::cout << Form(" %4d", fSumMap[i]);
343 }
344 std::cout << std::endl;
345#endif
346 }
347
348 void MemoryMapManager::PrepareMaps(Int_t collection) {
349 Int_t dif = fMixSize - fCounter[collection];
350 switch (dif) {
351 case 1: fCounter[collection] = -1; break;
352 case 2: fReadyToMix[collection] = kTRUE; break;
353 default: break;
354 }
355 ++fCounter[collection];
356 ResetTrackMaps(collection, fCounter[collection]);
357 }
358
359 MemoryMapManager::~MemoryMapManager() {
360 if (fEvents) {
361 for (int i = 0; i < fEventCollectionsNo; i++)
362 delete fEvents[i];
363 delete[] fEvents;
364 }
365 delete fTrackCounter;
366 delete fTrackMap;
367 delete[] fReadyToMix;
368 delete[] fTotalTracks;
369 delete[] fEventToTrackNo;
370 delete[] fCounter;
371 if (fInterface) delete fInterface;
372 }
373
374 EventArray::EventArray(Int_t task_id, Int_t size) : fSize(size) {
376 fArray = new Event*[fSize];
377 for (int i = 0; i < fSize; i++) {
378 fArray[i] = dataManager->GetNewEvent(task_id, EFormatDepth::kBuffered);
379 }
380 }
381
382 Track* MemoryMapManager::GetTemporaryTrack(Int_t event_collection, Int_t track_collection, Int_t index) const {
383 return fCurrentEvent->GetTrack(fTrackMap->Get(event_collection, track_collection, fCounter[event_collection], index));
384 }
385} // namespace Hal
Array_1< T > * At(const Int_t i)
Definition Array.h:159
Array_2< T > * At(Int_t i) const
Definition Array.h:228
void Set(Int_t A, Int_t B, Int_t C, T val)
Definition Array.h:265
void MakeBigger(Int_t sizeA, Int_t sizeB, Int_t sizeC)
Definition Array.cxx:263
T IncrementAfter(Int_t A, Int_t B, Int_t C)
Definition Array.h:256
T Get(Int_t A, Int_t B, Int_t C) const
Definition Array.h:248
void Set(Int_t A, Int_t B, Int_t C, Int_t D, T val)
Definition Array.h:345
Array_3< T > * At(Int_t i) const
Definition Array.h:301
T Get(Int_t A, Int_t B, Int_t C, Int_t D) const
Definition Array.h:327
void MakeBigger(Int_t sizeA, Int_t sizeB, Int_t sizeC, Int_t sizeD)
Definition Array.cxx:326
Int_t GetNewIndex(Int_t old_index) const
void MarkAsGood(Int_t old_index)
void Reset(Int_t size)
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
Int_t GetNextNo() const
Int_t GetEventCollectionsNo() const
CutCollection * GetEventCollection(Int_t collection) const
Int_t GetTrackCollectionsNo() const
static DataFormatManager * Instance()
Event * GetNewEvent(Int_t task_id, EFormatDepth format_depth=EFormatDepth::kAll) const
EventArray(Int_t task_id, Int_t size=1)
Event * At(const Int_t size)
void Build(Event *event, const CompressionMap &map)
Definition Event.cxx:96
virtual void Update(EventInterface *interface)
Definition Event.cxx:157
Int_t GetTotalTrackNo() const
Definition Event.h:236
virtual EventInterface * CreateInterface() const =0
virtual void Print(Option_t *opt="") const
Definition Event.cxx:91
virtual void Clear(Option_t *opt=" ")
Definition Event.cxx:82
virtual Int_t GetMaxExpectedLinks() const
Definition Event.h:330
Track * GetTrack(Int_t i) const
Definition Event.h:208
void Init(Int_t event_factor, Int_t task_id, Bool_t compress, std::vector< TString > direct)
Int_t GetTracksNo(Int_t event_collection, Int_t track_collection) const
Track * GetRawTrack(Int_t event_collection, Int_t index) const
void AddTrackToMapTrack(Int_t event_collection, Int_t track_collection, Int_t index)
void SetMixSize(Int_t mix_size)
Event * GetEvent(Int_t collection) const
Int_t GetTemporaryTotalTracksNo() const
void RejectLastEvent(Int_t event_collection)
void BufferEvent(Int_t collection)
Bool_t IsReadyToMixing(Int_t collection) const
Track * GetTrack(Int_t event_collection, Int_t track_collection, Int_t index) const
Int_t GetRawTracksNo(Int_t event_collection) const
void ManualUpdate(Event *event)
void PrepareMaps(Int_t collection)
void ResetEventCollection(Int_t collection)
Track * GetTemporaryTrack(Int_t event_collection, Int_t track_collection, Int_t index) const
virtual void Print(Option_t *option="") const
Definition Track.cxx:26
void TranslateLinks(const CompressionMap &map)
Definition Track.cxx:129
std::vector< int > GetLinks() const
Definition Track.cxx:78
virtual Int_t GetLinksFast(std::vector< int > &vec, Bool_t fast=kTRUE) const
Definition Track.cxx:84