FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairWriteoutBuffer.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 /*
9  * FairWriteoutBuffer.cpp
10  *
11  * Created on: Jul 30, 2010
12  * Author: stockman
13  */
14 
15 #include "FairWriteoutBuffer.h"
16 
17 #include "FairLogger.h" // for FairLogger
18 #include "FairRootManager.h" // for FairRootManager
19 
20 #include <iostream> // for operator<<, ostream, cout, etc
21 #include <iterator> // for reverse_iterator
22 
23 FairWriteoutBuffer::FairWriteoutBuffer(TString branchName, TString className, TString folderName, Bool_t persistance)
24  : TObject()
25  , fStartTime_map()
26  , fDeadTime_map()
27  , fBranchName(branchName)
28  , fClassName(className)
29  , fTreeSave(true)
30  , fActivateBuffering(kTRUE)
31  , fVerbose(0)
32  , fLogger(FairLogger::GetLogger())
33 {
34  FairRootManager::Instance()->Register(branchName, className, folderName, persistance);
35  if (fBranchName == "" || fClassName == "") {
36  fTreeSave = false;
37  } else {
38  fTreeSave = true;
39  }
40 }
41 
43 {
44  if (fActivateBuffering) {
45  if (fVerbose > 0) {
46  std::cout << "Before removing: ";
49  }
50 
52 
53  if (fVerbose > 0) {
54  std::cout << "After removing: ";
57  }
58 
60 
61  if (fVerbose > 0) {
62  std::cout << "DeadTime map after readout time: " << time << std::endl;
64  }
65 
66  } else {
68  }
69 }
70 
72 {
74  std::vector<FairTimeStamp*> data;
75  if (fActivateBuffering) {
76  if (fVerbose > 0) {
77  std::cout << "-I- FairWriteoutBuffer::WriteOutData for time: " << time << std::endl;
78  }
79  data = GetRemoveOldData(time);
80  if (fTreeSave && data.size() > 0) {
81  TClonesArray* myArray = ioman->GetTClonesArray(fBranchName);
82  if (!myArray) {
83  std::cout << "-E- FairWriteoutBuffer::WriteOutData " << fBranchName << " array is not available!"
84  << std::endl;
85  }
86  if (fVerbose > 0) {
87  std::cout << "-I- FairWriteoutBuffer::WriteOutData size: " << data.size() << std::endl;
88  }
89  for (unsigned int i = 0; i < data.size(); i++) {
90  AddNewDataToTClonesArray(data[i]);
91  if (fVerbose > 1) {
92  std::cout << i << " : ";
93  data[i]->Print();
94  std::cout << std::endl;
95  }
96  delete data[i];
97  }
98  }
99  } else {
101  }
102 }
103 
105 {
106  double ultimateTime = 0;
107 
108  if (fStartTime_map.size() > 0) {
109  ultimateTime = fStartTime_map.rbegin()->first + 1;
111  }
112 
113  if (fDeadTime_map.size() > 0) {
114  if (fDeadTime_map.rbegin()->first > ultimateTime) {
115  ultimateTime = fDeadTime_map.rbegin()->first + 1;
116  }
117  }
118 
119  if (ultimateTime > 0) {
120  WriteOutData(ultimateTime);
121  }
122 }
123 
124 std::vector<FairTimeStamp*> FairWriteoutBuffer::GetRemoveOldData(double time)
125 {
126  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
127  std::vector<FairTimeStamp*> result;
128  for (DTMapIter it = fDeadTime_map.begin(); it != fDeadTime_map.lower_bound(time); ++it) {
129  if (fVerbose > 1) {
130  std::cout << "-I- GetRemoveOldData: DeadTime: " << it->first << " Data: " << it->second << std::endl;
131  }
132  result.push_back(it->second);
133  EraseDataFromDataMap(it->second);
134  }
135  fDeadTime_map.erase(fDeadTime_map.begin(), fDeadTime_map.lower_bound(time));
136  return result;
137 }
138 
139 std::vector<FairTimeStamp*> FairWriteoutBuffer::GetAllData()
140 {
141  return GetRemoveOldData(fDeadTime_map.rbegin()->first + 1);
142 }
143 
144 void FairWriteoutBuffer::FillNewData(FairTimeStamp* data, double startTime, double activeTime)
145 {
146  FairTimeStamp* dataClone = static_cast<FairTimeStamp*>(data->Clone());
147 
148  if (fActivateBuffering) {
149  if (fVerbose > 0) {
150  std::cout << "StartTime: " << startTime << std::endl;
151  }
152  std::pair<double, FairTimeStamp*> timeData(activeTime, dataClone);
153  fStartTime_map.insert(std::pair<double, std::pair<double, FairTimeStamp*>>(startTime, timeData));
154  } else {
155  AddNewDataToTClonesArray(dataClone);
156  delete dataClone;
157  }
158 }
159 
160 void FairWriteoutBuffer::FillDataToDeadTimeMap(FairTimeStamp* data, double activeTime, double startTime)
161 {
162  if (fActivateBuffering) {
163  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
164  // typedef std::map<FairTimeStamp, double>::iterator DataMapIter;
165  if (activeTime < 0)
166  activeTime = 0; // to avoid errors due to wrong (negative) returnvalues of overwritten modify function by
167  // subdetector groups. A negative (-1) aktiveTime already indicate empty data maps!
168  double timeOfOldData = FindTimeForData(data);
169  // PrintDeadTimeMap();
170  if (timeOfOldData > -1) { // if an older active data object is already present
171  if (fVerbose > 1) {
172  std::cout << " OldData found! " << timeOfOldData << std::endl;
173  }
174  if (fVerbose > 1) {
175  std::cout << "New Data: " << activeTime << " : " << data << std::endl;
176  }
177  double currentdeadtime = timeOfOldData;
178  FairTimeStamp* oldData;
179  bool dataFound = false;
180 
181  // PrintDeadTimeMap();
182  for (DTMapIter it = fDeadTime_map.lower_bound(currentdeadtime);
183  it != fDeadTime_map.upper_bound(currentdeadtime);
184  ++it) {
185  oldData = it->second;
186  if (fVerbose > 1) {
187  std::cout << "Check Data: " << it->first << " : " << oldData << std::endl;
188  }
189  if (oldData->equal(data)) {
190  if (fVerbose > 1) {
191  std::cout << " oldData == data " << std::endl;
192  }
193  if (fVerbose > 1) {
194  std::cout << it->first << " : " << it->second << std::endl;
195  }
196  fDeadTime_map.erase(it);
197  EraseDataFromDataMap(oldData);
198  dataFound = true;
199  break;
200  }
201  }
202 
203  if (dataFound == true) {
204  if (timeOfOldData
205  > startTime) { // if older active data can interference with the new data call modify function
206  std::vector<std::pair<double, FairTimeStamp*>> modifiedData =
207  Modify(std::pair<double, FairTimeStamp*>(currentdeadtime, oldData),
208  std::pair<double, FairTimeStamp*>(activeTime, data));
209  for (unsigned int i = 0; i < modifiedData.size(); i++) {
210  FillDataToDeadTimeMap(modifiedData[i].second,
211  modifiedData[i].first,
212  0); // startTime = 0 since the Maps are already empty and the startTime
213  // therefore doesn't matter anymore
214  if (fVerbose > 1) {
215  std::cout << i << " :Modified Data: " << modifiedData[i].first << " : "
216  << modifiedData[i].second << std::endl;
217  }
218  }
219  } else { // no interference can happen between old hit and new hit
220  AddNewDataToTClonesArray(oldData); // therefore the old hit is written out
221  fDeadTime_map.insert(
222  std::pair<double, FairTimeStamp*>(activeTime, data)); // and the new hit is stored
223  FillDataMap(data, activeTime);
224  }
225  } else {
226  std::cout << "-E- FairWriteoutBuffer::FillDataToDeadTimeMap: old data present in dataMap but not in "
227  "deadTimeMap!"
228  << std::endl;
229  }
230 
231  } else {
232  if (fVerbose > 1) {
233  std::cout << "-I- FairWriteoutBuffer::FillDataToDeadTimeMap Data Inserted: " << activeTime << " : ";
234  data->Print();
235  std::cout << std::endl;
236  }
237  fDeadTime_map.insert(std::pair<double, FairTimeStamp*>(activeTime, data));
238  FillDataMap(data, activeTime);
239  }
240  } else {
242  delete data;
243  }
244 }
245 
247 {
248  typedef std::multimap<double, std::pair<double, FairTimeStamp*>>::iterator startTimeMapIter;
249 
250  startTimeMapIter stopTime = fStartTime_map.lower_bound(time);
251  for (startTimeMapIter iter = fStartTime_map.begin(); iter != stopTime; ++iter) {
252  std::pair<double, FairTimeStamp*> data = iter->second;
253  double startTime = iter->first;
254  FillDataToDeadTimeMap(data.second, data.first, startTime);
255  }
256  fStartTime_map.erase(fStartTime_map.begin(), stopTime);
257 }
258 
260 {
261  typedef std::multimap<double, std::pair<double, FairTimeStamp*>>::iterator startTimeMapIter;
262  std::cout << "StartTimeMap: " << std::endl;
263  for (startTimeMapIter iter = fStartTime_map.begin(); iter != fStartTime_map.end(); ++iter) {
264  FairTimeStamp* data = iter->second.second;
265  std::cout << " | " << iter->first << "/" << data->GetTimeStamp();
266  }
267  std::cout << " |" << std::endl;
268 }
269 
271 {
272  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
273  std::cout << "DeadTimeMap: " << std::endl;
274  for (DTMapIter it = fDeadTime_map.begin(); it != fDeadTime_map.end(); ++it) {
275  std::cout << it->first << " / ";
276  PrintData(it->second);
277  std::cout << std::endl;
278  }
279  std::cout << std::endl;
280 }
281 
virtual void MoveDataFromStartTimeMapToDeadTimeMap(double time)
virtual void AddNewDataToTClonesArray(FairTimeStamp *data)=0
virtual void FillDataMap(FairTimeStamp *data, double activeTime)=0
add a new element in the search buffer
virtual void PrintData(FairTimeStamp *data)
virtual void FillNewData(FairTimeStamp *data, double startTime, double activeTime)
virtual std::vector< FairTimeStamp * > GetAllData()
static FairRootManager * Instance()
virtual std::vector< std::pair< double, FairTimeStamp * > > Modify(std::pair< double, FairTimeStamp * > oldData, std::pair< double, FairTimeStamp * >)
std::multimap< double, FairTimeStamp * > fDeadTime_map
ClassImp(FairEventBuilder)
virtual void WriteOutData(double time)
virtual std::vector< FairTimeStamp * > GetRemoveOldData(double time)
virtual double FindTimeForData(FairTimeStamp *data)=0
TClonesArray * GetTClonesArray(TString branchName)
virtual void EraseDataFromDataMap(FairTimeStamp *data)=0
delete the element from the search buffer (see PndSdsDigiPixelWriteoutBuffer)
virtual void PrintDeadTimeMap()
Method should be overwritten in derived classes to print the data of an object stored in the buffer...
Double_t GetTimeStamp() const
Definition: FairTimeStamp.h:44
std::multimap< double, std::pair< double, FairTimeStamp * > > fStartTime_map
void Register(const char *name, const char *Foldername, TNamed *obj, Bool_t toFile)
virtual void WriteOutDataDeadTimeMap(double time)
virtual void FillDataToDeadTimeMap(FairTimeStamp *data, double activeTime, double startTime)
virtual bool equal(FairTimeStamp *data)
Definition: FairTimeStamp.h:71
A container class to store digi data during events.
virtual void PrintStartTimeMap()
virtual void WriteOutAllData()