EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairWriteoutBuffer.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairWriteoutBuffer.cxx
1 /*
2  * FairWriteoutBuffer.cpp
3  *
4  * Created on: Jul 30, 2010
5  * Author: stockman
6  */
7 
8 #include "FairWriteoutBuffer.h"
9 #include "FairRootManager.h"
10 #include "FairLogger.h"
11 #include "TClonesArray.h"
12 
13 #include <iostream>
14 //_____________________________________________________________________________
15 FairWriteoutBuffer::FairWriteoutBuffer(TString branchName, TString className, TString folderName, Bool_t persistance)
16  : TObject(),
17  fStartTime_map(),
18  fDeadTime_map(),
19  fBranchName(branchName),
20  fClassName(className),
21  fTreeSave(true),
22  fActivateBuffering(kTRUE),
23  fVerbose(0),
24  fLogger(FairLogger::GetLogger())
25 {
26  FairRootManager::Instance()->Register(branchName, className, folderName, persistance);
27  if (fBranchName == "" || fClassName == "") {
28  fTreeSave = false;
29  } else {
30  fTreeSave = true;
31  }
32 }
33 //_____________________________________________________________________________
34 
36 {
37  if (fActivateBuffering) {
38  if (fVerbose > 0) {
39  std::cout << "Before removing: ";
42  }
43 
45 
46  if (fVerbose > 0) {
47  std::cout << "After removing: ";
50  }
51 
53 
54  if (fVerbose > 0) {
55  std::cout << "DeadTime map after readout time: " << time << std::endl;
57  }
58 
59  } else {
61  }
62 
63 }
64 //_____________________________________________________________________________
65 
67 {
69  std::vector<FairTimeStamp*> data;
70  if (fActivateBuffering) {
71  if (fVerbose > 0) {
72  std::cout << "-I- FairWriteoutBuffer::WriteOutData for time: " << time << std::endl;
73  }
74  data = GetRemoveOldData(time);
75  if (fTreeSave && data.size() > 0) {
76  TClonesArray* myArray = ioman->GetTClonesArray(fBranchName);
77  if (!myArray) {
78  std::cout << "-E- FairWriteoutBuffer::WriteOutData " << fBranchName << " array is not available!" << std::endl;
79  }
80  if (fVerbose > 0) {
81  std::cout << "-I- FairWriteoutBuffer::WriteOutData size: " << data.size() << std::endl;
82  }
83  for (int i = 0; i < data.size(); i++) {
84  AddNewDataToTClonesArray(data[i]);
85  if (fVerbose > 1) {
86  std::cout << i << " : ";
87  data[i]->Print();
88  std::cout << std::endl;
89  }
90  delete data[i];
91  }
92  }
93  } else {
95  }
96 }
97 //_____________________________________________________________________________
98 
100 {
101  double ultimateTime = 0;
102 
103  if (fStartTime_map.size() > 0) {
104  ultimateTime = fStartTime_map.rbegin()->first + 1;
106  }
107 
108  if (fDeadTime_map.size() > 0) {
109  if (fDeadTime_map.rbegin()->first > ultimateTime) {
110  ultimateTime = fDeadTime_map.rbegin()->first + 1;
111  }
112  }
113 
114  if (ultimateTime > 0) {
115  WriteOutData(ultimateTime);
116  }
117 }
118 //_____________________________________________________________________________
119 
120 std::vector<FairTimeStamp*> FairWriteoutBuffer::GetRemoveOldData(double time)
121 {
122  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
123  std::vector<FairTimeStamp*> result;
124  for(DTMapIter it = fDeadTime_map.begin(); it != fDeadTime_map.lower_bound(time); it++) {
125  if (fVerbose > 1) {
126  std::cout << "-I- GetRemoveOldData: DeadTime: " << it->first << " Data: " << it->second << std::endl;
127  }
128  result.push_back(it->second);
129  EraseDataFromDataMap(it->second);
130  }
131  fDeadTime_map.erase(fDeadTime_map.begin(), fDeadTime_map.lower_bound(time));
132  return result;
133 }
134 //_____________________________________________________________________________
135 
136 std::vector<FairTimeStamp*> FairWriteoutBuffer::GetAllData()
137 {
138  return GetRemoveOldData(fDeadTime_map.rbegin()->first + 1);
139 }
140 
141 void FairWriteoutBuffer::FillNewData(FairTimeStamp* data, double startTime, double activeTime)
142 {
143  FairTimeStamp* dataClone = static_cast<FairTimeStamp*>(data->Clone());
144 
145  if (fActivateBuffering) {
146  if (fVerbose > 0) {
147  std::cout << "StartTime: " << startTime << std::endl;
148  }
149  std::pair<double, FairTimeStamp*> timeData(activeTime, dataClone);
150  fStartTime_map.insert(std::pair<double, std::pair<double, FairTimeStamp*> >(startTime, timeData));
151  } else {
152  AddNewDataToTClonesArray(dataClone);
153  delete dataClone;
154  }
155 
156 }
157 //_____________________________________________________________________________
158 
160 {
161  if (fActivateBuffering) {
162  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
163  typedef std::map<FairTimeStamp, double>::iterator DataMapIter;
164 
165  double timeOfOldData = FindTimeForData(data);
166  // PrintDeadTimeMap();
167  if(timeOfOldData > -1) { //if an older active data object is already present
168  if (fVerbose > 1) {
169  std::cout << " OldData found! " << timeOfOldData << std::endl;
170  }
171  if (fVerbose > 1) {
172  std::cout << "New Data: " << activeTime << " : " << data << std::endl;
173  }
174  double currentdeadtime = timeOfOldData;
175  FairTimeStamp* oldData;
176  bool dataFound = false;
177 
178  // PrintDeadTimeMap();
179  for (DTMapIter it = fDeadTime_map.lower_bound(currentdeadtime); it != fDeadTime_map.upper_bound(currentdeadtime); it++) {
180  oldData = it->second;
181  if (fVerbose > 1) {
182  std::cout << "Check Data: " << it->first << " : " << oldData << std::endl;
183  }
184  if (oldData->equal(data)) {
185  if (fVerbose > 1) {
186  std::cout << " oldData == data " << std::endl;
187  }
188  if (fVerbose > 1) {
189  std::cout << it->first << " : " << it->second << std::endl;
190  }
191  fDeadTime_map.erase(it);
192  EraseDataFromDataMap(oldData);
193  dataFound = true;
194  break;
195  }
196  }
197 
198  if (dataFound == true) {
199  std::vector<std::pair<double, FairTimeStamp*> > modifiedData = Modify(std::pair<double, FairTimeStamp*>(currentdeadtime, oldData), std::pair<double, FairTimeStamp*>(-1, data));
200  for (int i = 0; i < modifiedData.size(); i++) {
201  FillDataToDeadTimeMap(modifiedData[i].second, modifiedData[i].first);
202  if (fVerbose > 1) {
203  std::cout << i << " :Modified Data: " << modifiedData[i].first << " : " << modifiedData[i].second << std::endl;
204  }
205  }
206  } else {
207  std::cout << "-E- FairWriteoutBuffer::FillDataToDeadTimeMap: old data present in dataMap but not in deadTimeMap!" << std::endl;
208  }
209 
210  } else {
211  if (fVerbose > 1) {
212  std::cout << "-I- FairWriteoutBuffer::FillDataToDeadTimeMap Data Inserted: " << activeTime << " : ";
213  data->Print();
214  std::cout << std::endl;
215  }
216  fDeadTime_map.insert(std::pair<double, FairTimeStamp*>(activeTime, data));
217  FillDataMap(data, activeTime);
218  }
219  } else {
221  delete data;
222  }
223 }
224 //_____________________________________________________________________________
225 
227 {
228  typedef std::multimap<double, std::pair<double, FairTimeStamp*> >::iterator startTimeMapIter;
229 
230  startTimeMapIter stopTime = fStartTime_map.lower_bound(time);
231  for (startTimeMapIter iter = fStartTime_map.begin(); iter != stopTime; iter++) {
232  std::pair<double, FairTimeStamp*> data = iter->second;
233  FillDataToDeadTimeMap(data.second, data.first);
234  }
235  fStartTime_map.erase(fStartTime_map.begin(), stopTime);
236 }
237 //_____________________________________________________________________________
238 
240 {
241  typedef std::multimap<double, std::pair<double, FairTimeStamp*> >::iterator startTimeMapIter;
242  std::cout << "StartTimeMap: " << std::endl;
243  for (startTimeMapIter iter = fStartTime_map.begin(); iter != fStartTime_map.end(); iter++) {
244  FairTimeStamp* data = iter->second.second;
245  std::cout << " | " << iter->first << "/" << data->GetTimeStamp();
246  }
247  std::cout << " |" << std::endl;
248 }
249 //_____________________________________________________________________________
251 {
252  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
253  std::cout << "DeadTimeMap: " << std::endl;
254  for (DTMapIter it = fDeadTime_map.begin(); it != fDeadTime_map.end(); it++) {
255  std::cout << it->first << " / ";
256  PrintData(it->second);
257  std::cout << std::endl;
258  }
259  std::cout << std::endl;
260 }
261 //_____________________________________________________________________________
262 
263