FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairRingSorter.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 // FairRingSorter.cxx
10 // Created on: Jul 30, 2010
11 // Author: stockman
12 // -------------------------------------------------------------------------
13 
14 #include "FairRingSorter.h"
15 
16 #include "FairTimeStamp.h" // for FairTimeStamp
17 
18 FairTimeStamp* FairRingSorter::CreateElement(FairTimeStamp* data) { return static_cast<FairTimeStamp*>(data->Clone()); }
19 
20 void FairRingSorter::AddElement(FairTimeStamp* digi, double timestamp)
21 {
22  FairTimeStamp* newElement = CreateElement(digi);
23  if (timestamp < fLowerBoundPointer.second) {
24  std::cout << "-E- Timestamp " << timestamp << " below lower bound " << fLowerBoundPointer.second << std::endl;
25  newElement->Print();
26  return;
27  }
28  int index = CalcIndex(timestamp);
29 
30  if (timestamp >= fLowerBoundPointer.second + (2 * GetBufferSize())) {
31  if (fVerbose > 0) {
32  std::cout << "-I- FairRingSorterT::AddElement : Timestamp " << timestamp
33  << " larger than 2 * bufferspace: " << fLowerBoundPointer.second + GetBufferSize()
34  << " writing out " << index + 1 << std::endl;
35  }
36  WriteOutAll();
37  SetLowerBound(timestamp);
38  } else if (timestamp >= fLowerBoundPointer.second + GetBufferSize()) {
39  if (fVerbose > 0) {
40  std::cout << "-I- FairRingSorterT::AddElement :Timestamp " << timestamp
41  << " larger than bufferspace: " << fLowerBoundPointer.second + GetBufferSize() << " writing out "
42  << index + 1 << std::endl;
43  }
44  WriteOutElements(index + 1);
45  SetLowerBound(timestamp);
46  }
47  fRingBuffer[index].insert(std::pair<double, FairTimeStamp*>(timestamp, newElement));
48 }
49 
50 void FairRingSorter::SetLowerBound(double timestampOfHitToWrite)
51 {
52  int index = CalcIndex(timestampOfHitToWrite + fCellWidth);
53 
54  int cellValue = static_cast<int>(timestampOfHitToWrite / fCellWidth);
55 
56  fLowerBoundPointer.second = ((cellValue + 1) * fCellWidth) - GetBufferSize();
57  fLowerBoundPointer.first = index;
58  if (fVerbose > 0) {
59  std::cout << "-I- FairRingSorter::SetLowerBound " << index << " / " << fLowerBoundPointer.second << std::endl;
60  }
61 }
62 
64 {
65  if (fLowerBoundPointer.first >= index) {
66  for (unsigned int i = fLowerBoundPointer.first; i < fRingBuffer.size(); i++) {
67  WriteOutElement(i);
68  }
69  for (int i = 0; i < index; i++) {
70  WriteOutElement(i);
71  }
72  } else {
73  for (int i = fLowerBoundPointer.first; i < index; i++) {
74  WriteOutElement(i);
75  }
76  }
77  if (fVerbose > 1) {
78  std::cout << "-I- FairRingSorter::WriteOutElements: Size of Output-Array: " << fOutputData.size() << std::endl;
79  for (unsigned int i = 0; i < fOutputData.size(); i++) {
80  fOutputData[i]->Print();
81  std::cout << " | ";
82  }
83  std::cout << std::endl;
84  }
85 }
86 
88 {
89  std::multimap<double, FairTimeStamp*>* myDataField = &fRingBuffer.at(index);
90  std::multimap<double, FairTimeStamp*>::iterator it;
91  if (!myDataField->empty()) {
92  if (fVerbose > 1) {
93  std::cout << "-I- FairRingSorter:WriteOutElement ";
94  myDataField->begin()->second->Print();
95  std::cout << std::endl;
96  }
97  for (auto& mmi : *myDataField) {
98  fOutputData.push_back(mmi.second);
99  }
100  myDataField->clear();
101  }
102 }
103 
104 int FairRingSorter::CalcIndex(double val)
105 {
106  unsigned int index = static_cast<unsigned int>(val / fCellWidth);
107  while (index >= fRingBuffer.size()) {
108  index -= fRingBuffer.size();
109  }
110  return index;
111 }
112 
virtual void WriteOutElement(int index)
writes out the entry at the index and clears it
virtual void WriteOutElements(int index)
writes out the entries from LowerBoundPointer up to index
ClassImp(FairEventBuilder)
virtual void AddElement(FairTimeStamp *digi, double timestamp)
virtual double GetBufferSize()
virtual FairTimeStamp * CreateElement(FairTimeStamp *data)
virtual void WriteOutAll()
virtual void SetLowerBound(double timestampOfHitToWrite)