FairRoot/PandaRoot
PndEmcWaveformWriteoutBuffer.cxx
Go to the documentation of this file.
1 /*
2  * PndEmcWaveformWriteoutBuffer.cxx
3 */
4 #include <assert.h>
6 #include "FairRunAna.h"
7 #include "FairEventHeader.h"
8 
10 
11 #include "PndEmcWaveform.h"
12 #include <map>
13 //std::map<Int_t, Int_t> CntMap;
14 
16 }
17 
18 
19 PndEmcWaveformWriteoutBuffer::PndEmcWaveformWriteoutBuffer(TString branchName, TString folderName, Bool_t persistance): FairWriteoutBuffer(branchName, "PndEmcWaveform", folderName, persistance), fSaveToFile(false)
20 {
21 }
23  fSaveToFile = flag;
24  if(fSaveToFile){
25  fWave = new TFile("PndEmcWaveformWriteoutBuffer.root", "RECREATE");
26  tWave = new TTree("wave","wave");
27  tWave->Branch("e" ,&fEnergy ,"e/D");
28  tWave->Branch("peak" ,&fPeak ,"peak/D");
29  tWave->Branch("mod" ,&fMod ,"mod/I");
30  tWave->Branch("xcor" ,&fXcor ,"xcor/I");
31  tWave->Branch("ycor" ,&fYcor ,"ycor/I");
32  //tWave->Branch("idx" ,&fIdx ,"idx/I");
33  tWave->Branch("detid" ,&fDetID ,"detid/I");
34  tWave->Branch("dt" , &fDiffTime,"dt/D");
35  tWave->Branch("overlapt" , &fOverlap,"overlapt/D");
36  tWave->Branch("wpc" , &fPileupCount,"wpc/I");
37  }
38 }
39 
41 }
43  if(fSaveToFile){
44  TFile* oldFile = gFile;
45  gFile = fWave;
46  gFile->cd();
47  tWave->Write();
48  gFile = oldFile;
49  }
50 }
51 
53 {
54  FairRootManager* ioman = FairRootManager::Instance();
55  TClonesArray* myArray = ioman->GetTClonesArray(fBranchName);
56  if (fVerbose > 1) std::cout << "Data Inserted: " << *(PndEmcWaveform*)(data) << std::endl;
57  new ((*myArray)[myArray->GetEntries()]) PndEmcWaveform(*(PndEmcWaveform*)(data));
58 }
59 
61 {
62  std::map<PndEmcWaveform, double>::iterator it;
63  //std::map<long, double>::iterator it;
64  PndEmcWaveform& myData = *((PndEmcWaveform*)data);
65  it = fData_map.find(myData);
66  if (it == fData_map.end())
67  return -1;
68  else
69  return it->second;
70 }
71 void PndEmcWaveformWriteoutBuffer::FillDataMap(FairTimeStamp* data, double activeTime)
72 {
73  PndEmcWaveform& myData = * ((PndEmcWaveform*)data);
74  fData_map.insert(std::pair<PndEmcWaveform,double>(myData, activeTime));
75 }
77 {
78  PndEmcWaveform& myData = * ((PndEmcWaveform*)data) ;
79  if (fData_map.find(myData) != fData_map.end())
80  fData_map.erase(fData_map.find(myData));
81 }
82 std::vector<std::pair<double, FairTimeStamp*> > PndEmcWaveformWriteoutBuffer::Modify(std::pair<double, FairTimeStamp*> oldData
83  , std::pair<double, FairTimeStamp*> newData)
84 {
85  assert(oldData.second);
86  assert(newData.second);
87 
88  std::vector<std::pair<double, FairTimeStamp*> > result;
89  std::pair<double, FairTimeStamp*> singleResult;
90  /*if (newData.first > 0)
91  singleResult.first += newData.first;*/
92 
93  PndEmcWaveform* oldWave = (PndEmcWaveform*) (oldData.second);
94  PndEmcWaveform* newWave = (PndEmcWaveform*) (newData.second);
95  //(*newWave) += (*oldWave);
96  if (fVerbose>1) {
97  cout<<"PndEmcWaveformWriteoutBuffer::Modify called #"<<oldWave->GetDetectorId()<<", #"<<newWave->GetDetectorId()<<endl;
98  }
99 
100  if(oldWave->GetTimeStamp() < newWave->GetTimeStamp()){
101  (*oldWave) += (*newWave);
102  singleResult.second = oldData.second;
103  singleResult.first = oldWave->GetActiveTime();
104 
105 
106  if(fSaveToFile){
107  //fEnergy = theHit->GetEnergy();
108  //fPeak = theWaveform->Max();
109  fMod = oldWave->GetModule();
110  fDetID = oldWave->GetDetectorId();
111  fDiffTime = newWave->GetTimeStamp() - oldWave->GetTimeStamp() ;
112  fOverlap = oldWave->GetActiveTime() - newWave->GetTimeStamp();
113  PndEmcTwoCoordIndex* tci = oldWave->GetTCI();
114  fXcor = tci->XCoord();
115  fYcor = tci->YCoord();
116  fPileupCount = oldWave->GetPileupCount();
117  //fIdx = tci->Index();
118  tWave->Fill();
119  if(fPileupCount >= 1)
120  WriteToFile(oldWave);
121  }
122  }else{
123  (*newWave) += (*oldWave);
124  singleResult.second = newData.second;
125  singleResult.first = newWave->GetActiveTime();
126 
127  if(fSaveToFile){
128  fMod = newWave->GetModule();
129  fDetID = newWave->GetDetectorId();
130  fDiffTime = oldWave->GetTimeStamp() - newWave->GetTimeStamp() ;
131  fOverlap = newWave->GetActiveTime() - oldWave->GetTimeStamp();
132  PndEmcTwoCoordIndex* tci = newWave->GetTCI();
133  fXcor = tci->XCoord();
134  fYcor = tci->YCoord();
135  fPileupCount = newWave->GetPileupCount();
136  //fIdx = tci->Index();
137  tWave->Fill();
138  if(fPileupCount >= 1)
139  WriteToFile(newWave);
140  }
141  }
142 
143 
144  if (fVerbose > 0){
145  //if( CntMap[oldWave->GetDetectorId()] ++ >= 2)
146  // std::cout<<"DetectorID #"<<oldWave->GetDetectorId()<<" has "<<CntMap[oldWave->GetDetectorId()]<<" added"<<std::endl;
147  //std::cout << "Modify PndEmcWaveform at DetectorID# "<<oldWave->GetDetectorId()<<", #"<<newWave->GetDetectorId() << std::endl;
148  std::cout << "OldData: " << oldData.first << " : " << oldData.second << " NewData: " << newData.first << " : " << newData.second << std::endl;
149  std::cout << "Resulting Data: " << singleResult.first << " : " << singleResult.second << std::endl;
150  }
151 
152  result.push_back(singleResult);
153  //FillDataMap(singleResult.second, singleResult.first);
154 
155  return result;
156 
157 }
159 {
160  TGraphErrors* gr = theWaveform->ToTGraph();
161  TString name("evt");
162  Int_t evtNo = FairRunAna::Instance()->GetEventHeader()->GetMCEntryNumber();
163  name += evtNo;
164  name += "_e";
165  std::vector<Int_t> evtList = theWaveform->GetEvtList();
166  for(size_t i=0;i<evtList.size();++i){
167  name += evtList[i];
168  name += "_";
169  }
170  name += theWaveform->GetDetectorId();
171  gr->SetName(name);
172  gr->SetTitle(name);
173  TFile* oldFile = gFile;
174  gFile = fWave;
175  gFile->cd();
176  gr->Write();
177  oldFile->cd();
178  delete gr;
179 }
180 /*void PndEmcWaveformWriteoutBuffer::FillDataToDeadTimeMap(FairTimeStamp* data, double activeTime)
181  {
182  if (fActivateBuffering) {
183  typedef std::multimap<double, FairTimeStamp*>::iterator DTMapIter;
184  typedef std::map<FairTimeStamp, double>::iterator DataMapIter;
185 
186  PndEmcWaveform* emc = (PndEmcWaveform*) data;
187 
188  std::cout<<"data timestamp#"<<data->GetTimeStamp()<<", activeTime#"<<activeTime<<std::endl;
189  std::cout<<"detector ID#"<<emc->GetDetectorId()<<std::endl;
190  std::cout<<"begin FindTimeForData"<<std::endl;
191  double timeOfOldData = FindTimeForData(data);
192  std::cout<<"end FindTimeForData"<<std::endl;
193  if(timeOfOldData > -1) { //if an older active data object is already present
194  if (fVerbose > 1) {
195  std::cout << " OldData found! " << std::endl;
196  }
197  if (fVerbose > 1) {
198  std::cout << "New Data: " << activeTime << " : " << data << std::endl;
199  }
200  double currentdeadtime = timeOfOldData;
201  std::cout<<"currentdeadtime #"<<currentdeadtime<<std::endl;
202  FairTimeStamp* oldData(0);
203  bool not_equal(true);
204  for (DTMapIter it = fDeadTime_map.lower_bound(currentdeadtime); it != fDeadTime_map.upper_bound(currentdeadtime); it++) {
205  oldData = it->second;
206  if (fVerbose > 1) {
207  std::cout << "Check Data: " << it->first << " : " << oldData << std::endl;
208  PndEmcWaveform* old = (PndEmcWaveform*)oldData;
209  PndEmcWaveform* New = (PndEmcWaveform*)data;
210  std::cout << "Check Data: 2#" << old->GetDetectorId() << " : " << New->GetDetectorId() << std::endl;
211  }
212  if (oldData->equal(data)) {
213  if (fVerbose > 1) {
214  std::cout << " oldData == data " << std::endl;
215  }
216  if (fVerbose > 1) {
217  std::cout << it->first << " : " << it->second << std::endl;
218  }
219  not_equal = false;
220  std::cout<<"before fDeadTime_map.erase"<<std::endl;
221  fDeadTime_map.erase(it);
222  std::cout<<"before EraseDataFromDataMap"<<std::endl;
223  EraseDataFromDataMap(oldData);
224  std::cout<<"end EraseDataFromDataMap"<<std::endl;
225  break;
226  }
227  }
228  if(oldData == 0 || not_equal) return;
229  std::vector<std::pair<double, FairTimeStamp*> > modifiedData = Modify(std::pair<double, FairTimeStamp*>(currentdeadtime, oldData), std::pair<double, FairTimeStamp*>(-1, data));
230  for (int i = 0; i < modifiedData.size(); i++) {
231  FillDataToDeadTimeMap(modifiedData[i].second, modifiedData[i].first);
232  if (fVerbose > 1) {
233  std::cout << i << " :Modified Data: " << modifiedData[i].first << " : " << modifiedData[i].second << std::endl;
234  }
235  }
236  } else {
237  if (fVerbose > 1) {
238  std::cout << "-I- FairWriteoutBuffer::FillNewData Data Inserted: " << activeTime << " : ";
239  data->Print();
240  std::cout << std::endl;
241  }
242  fDeadTime_map.insert(std::pair<double, FairTimeStamp*>(activeTime, data));
243  std::cout<<"begin FillDataMap"<<std::endl;
244  FillDataMap(data, activeTime);
245  std::cout<<"end FillDataMap"<<std::endl;
246  }
247  } else {
248  AddNewDataToTClonesArray(data);
249  }
250  }*/
int fVerbose
Definition: poormantracks.C:24
Double_t GetActiveTime() const
Int_t i
Definition: run_full.C:25
std::map< PndEmcWaveform, double > fData_map
stores crystal index coordinates (x,y) or (theta,phi)
void WriteToFile(PndEmcWaveform *theWaveform)
PndEmcTwoCoordIndex * GetTCI() const
virtual void FillDataMap(FairTimeStamp *data, double activeTime)
const std::vector< Int_t > & GetEvtList() const
long GetDetectorId() const
TString name
virtual void EraseDataFromDataMap(FairTimeStamp *data)
represents a simulated waveform in an emc crystal
Int_t GetPileupCount() const
virtual double FindTimeForData(FairTimeStamp *data)
ClassImp(PndAnaContFact)
Short_t GetModule() const
virtual std::vector< std::pair< double, FairTimeStamp * > > Modify(std::pair< double, FairTimeStamp * > oldData, std::pair< double, FairTimeStamp * > newData)
TGraphErrors * ToTGraph() const