EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairTSBufferFunctional.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairTSBufferFunctional.cxx
2 
3 #include "FairRootManager.h"
4 #include "FairTimeStamp.h"
5 
7 
8 
9 FairTSBufferFunctional::FairTSBufferFunctional(TString branchName, TTree* sourceTree, BinaryFunctor* stopFunction, BinaryFunctor* startFunction)
10  :TObject(),
11  fOutputArray(NULL),
12  fBufferArray(NULL),
13  fInputArray(NULL),
14  fStartFunction(startFunction),
15  fStopFunction (stopFunction),
16  fBranch(NULL),
17  fBranchIndex(-1),
18  fStartIndex(-1),
19  fVerbose(0)
20 {
21  fBranch = sourceTree->GetBranch(branchName.Data());
22  if (fBranch == 0) {
23  std::cout << "-E- FairTSBufferFunctional::FairTSBufferFunctional Branch " << branchName << " does not exist!" << std::endl;
24  }
26  fInputArray = (TClonesArray*)ioman->GetObject(branchName.Data());
27  fBufferArray = new TClonesArray(fInputArray->GetClass()->GetName());
28  fOutputArray = new TClonesArray(fInputArray->GetClass()->GetName());
29 
30 }
31 
32 TClonesArray* FairTSBufferFunctional::GetData(Double_t stopParameter)
33 {
34 
35  Double_t actualTime = 0.;
36  int posBuffer = 0;
37 
38  if (fStopFunction == 0) { //no function is given ==> read in data in traditional way event by event
40  fOutputArray->AbsorbObjects((TClonesArray*)fInputArray, 0, fInputArray->GetEntriesFast() - 1);
41  return fOutputArray;
42  }
43  if (fVerbose > 1) {
44  std::cout << "-I- FairTSBufferFunctional::GetData for stopParameter: " << stopParameter << std::endl;
45  }
46 
47  //if the BufferArray is empty fill it
48  if (fBufferArray->GetEntriesFast() == 0) {
49  if (fVerbose > 1) {
50  std::cout << "-I- FairTSBufferFunctional::GetData fBufferArray is empty: Read in Data" << std::endl;
51  }
54  }
55 
56  //if the BufferArray is still empty you have reached the end of your data set
57  if (fVerbose > 1) {
58  std::cout << "-I- FairTSBufferFunctional::GetData fBufferArray->GetEntriesFast(): " << fBufferArray->GetEntriesFast() << std::endl;
59  }
60  FairTimeStamp* dataPoint = (FairTimeStamp*)fBufferArray->Last();
61  if (dataPoint == 0) {
62  if (fVerbose > 0) {
63  std::cout << "-I- FairTSBufferFunctional::GetData dataPoint is empty ==> All Data read in" << std::endl;
64  }
65  return fOutputArray;
66  }
67 
68  dataPoint = (FairTimeStamp*)fBufferArray->First();
69 
70  while (!(*fStopFunction)(dataPoint, stopParameter)) { //check if you have reached end of requested data
71  posBuffer++;
72  //if you have reached the end of the BufferArray fill it with new data from tree
73  if (posBuffer == fBufferArray->GetEntriesFast()) {
76  }
77  //if you are still at the end of the BufferArray than break (no new data in tree)
78  if (posBuffer == fBufferArray->GetEntriesFast()) {
79  break;
80  }
81  dataPoint = (FairTimeStamp*)fBufferArray->At(posBuffer);
82  if (fVerbose > 1) {
83  std::cout << posBuffer << " TimeStampData: " << dataPoint->GetTimeStamp() << std::endl;
84  }
85  }
86 
87  if (fVerbose > 1) {
88  std::cout << "-I- FairTSBufferFunctional::GetData Index for Absorb: " << posBuffer << " BufferArray size: " << fBufferArray->GetEntriesFast() << std::endl;
89  }
90  if (posBuffer < fBufferArray->GetEntriesFast() && posBuffer > 0) {
91  if (fVerbose > 1) {
92  std::cout << "-I- FairTSBufferFunctional::GetData absorb BufferArray up to posBuffer " << posBuffer << " into fOutputArray" << std::endl;
93  }
94  fOutputArray->AbsorbObjects(fBufferArray, 0, posBuffer - 1);
95  posBuffer = 0;
96  return fOutputArray;
97  }
98  if (fVerbose > 1) {
99  std::cout << "Index: " << posBuffer << " BranchIndex: " << fBranchIndex << " NBranch " << fBranch->GetEntries() << std::endl;
100  }
101 
102  if (posBuffer >= fBufferArray->GetEntriesFast() && posBuffer != 0 && fBranchIndex + 1 >= fBranch->GetEntries()) {
103  if (fVerbose > 1) {
104  std::cout << "-I- FairTSBufferFunctional::GetData end of data reached. Send the rest to the OutputArray!" << std::endl;
105  }
106  fOutputArray->AbsorbObjects(fBufferArray, 0, fBufferArray->GetEntries() - 1);
107  }
108 
109  if (fVerbose > 1) {
110  std::cout << "-I- FairTSBufferFunctional::GetData: Read in up to entry: " << fBranchIndex << " with actualTime " << actualTime << " and requested stopParameter " << stopParameter << std::endl;
111  }
112 
113  return fOutputArray;
114 }
115 
116 TClonesArray* FairTSBufferFunctional::GetData(Double_t startParameter, Double_t stopParameter)
117 {
118  if (fStartFunction != 0) {
119  fBufferArray->Clear();
120  Int_t startIndex = FindStartIndex(startParameter);
121  std::cout << "StartIndex: " << startIndex << "/" << GetBranchIndex() << std::endl;
122  if (startIndex > -1) {
124  fBufferArray->AbsorbObjects(fInputArray, startIndex, fInputArray->GetEntries() -1);
125  }
126  }
127  return GetData(stopParameter);
128 }
129 
130 Int_t FairTSBufferFunctional::FindStartIndex(Double_t startParameter)
131 {
132  FairTimeStamp* dataPoint;
133  Int_t tempIndex = fBranchIndex;
134  Bool_t runBackwards = kTRUE;
135  Int_t previousIndex = -1;
136  Int_t previousBranchIndex = -1;
137 
138  ReadInEntry(tempIndex); //< Get Data out of Tree
139 
140  while (fInputArray->GetEntries() == 0 && tempIndex > 0) { //< If the entry of the tree was empty read in previous entries until you find one which is filled
141  tempIndex--;
142  ReadInEntry(tempIndex);
143  }
144 
145  if (fInputArray->GetEntries() == 0) { //If the previous entries in the tree are also empty run in the forward direction
147  runBackwards = kFALSE;
148  }
149 
150  if (fInputArray->GetEntries() == 0) { //If there is still no data the branch is empty!
151  std::cout << "-I- FairTSBufferFunctional::FindStartIndex: All entries are empty!" << std::endl;
152  return -1;
153  }
154  fBranchIndex = tempIndex;
155  //Now we have data or FindStartIndex already returned -1
156 
157  dataPoint = (FairTimeStamp*)fInputArray->Last();
158  //std::cout << "DataPoint: " << *dataPoint << std::endl;
159  while(!(*fStartFunction)(dataPoint, startParameter)) {
160  //std::cout << "DataPoint Search Entry: " << fBranchIndex << ": " << *dataPoint << std::endl;
162  if (fInputArray->GetEntries() != 0) {
163  dataPoint = (FairTimeStamp*)fInputArray->Last();
164  } else {
165  return -1;
166  }
167  }
168 
169  //Now you have data where the last element in the array does not fit to your request
170  Int_t startPos = fInputArray->GetEntries() - 1;
171  while ((*fStartFunction)(dataPoint, startParameter)) {
172  //std::cout << "DataPoint Search in Entry: " << fBranchIndex << ": " << *dataPoint << std::endl;
173  previousIndex = startPos;
174  previousBranchIndex = fBranchIndex;
175  startPos--;
176  if (startPos == -1) {
178  startPos = fInputArray->GetEntries() - 1;
179  if (startPos < 0) {
180  if (fBranchIndex == 0) {
181  return 0;
182  }
183  return -1;
184  }
185  }
186  dataPoint = (FairTimeStamp*)fInputArray->At(startPos);
187  }
188  fBranchIndex = previousBranchIndex;
189  return previousIndex;
190 }
191 
192 
194 {
195  fInputArray->Delete();
196 
197  if (fVerbose > 1) {
198  std::cout << "-I- FairTSBufferFunctional::ReadInNextFilledEntry: Entries in InputArray " << fInputArray->GetEntriesFast() << " Branch Entries: " << fBranch->GetEntries() << std::endl;
199  }
200  while (fInputArray->GetEntriesFast() == 0 && fBranchIndex + 1 < fBranch->GetEntries()) {
201  fBranchIndex++;
203  }
204 }
205 
207 {
208  Int_t tempIndex = startEntry;
209 
210  fInputArray->Delete();
211  while (fInputArray->GetEntriesFast() == 0 && tempIndex > 0) {
212  tempIndex--;
213  ReadInEntry(tempIndex);
214  }
215  return tempIndex;
216 }
217 
219 {
220  if (fInputArray->GetEntriesFast() > 0) {
221  if (fVerbose > 1) {
222  std::cout << "-I- FairTSBufferFunctional::ReadInNextFilledEntry: Absorb InputArray into Buffer" << std::endl;
223  }
224  fBufferArray->AbsorbObjects(fInputArray, 0, fInputArray->GetEntries() - 1);
225  }
226 }
227 
229 {
230  if (fBranchIndex + 1 < fBranch->GetEntries()) {
231  fBranchIndex++;
233  }
234 }
235 
237 {
238  fInputArray->Delete();
239  if (number < fBranch->GetEntries()) {
240  fBranch->GetEntry(number);
241  for (int i = 0; i < fInputArray->GetEntriesFast(); i++) {
242  ((FairTimeStamp*) fInputArray->At(i))->SetEntryNr(FairLink(0, number, FairRootManager::Instance()->GetBranchId(fBranch->GetName()), i, 1));
243  }
244  if (fVerbose > 1)
245  std::cout
246  << "-I- FairTSBufferFunctional::ReadInEntry BranchIndex: "
247  << number << " Entries: "
248  << fInputArray->GetEntriesFast() << std::endl;
249  }
250 }
251 
253 {
254  if (fBranchIndex + 1 >= fBranch->GetEntries()) {
255  if(fBufferArray->GetEntriesFast() == 0) {
256  if (fOutputArray->GetEntriesFast() == 0) {
257  return kTRUE;
258  } else { return kFALSE; }
259  } else { return kFALSE; }
260  } else { return kFALSE; }
261 }