FairRoot/PandaRoot
PndGemEventBuilderOnTracks.cxx
Go to the documentation of this file.
1 /* $Id: */
2 
3 // -------------------------------------------------------------------------
4 // ----- PndGemEventBuilderOnTracks source file -----
5 // ----- Created 22/08/2013 by R. Karabowicz -----
6 // -------------------------------------------------------------------------
7 
18 
19 #include "PndTrack.h"
20 
21 #include "PndDetectorList.h"
22 #include "FairRecoEventHeader.h"
23 
24 #include "FairRootManager.h"
25 #include "FairRunAna.h"
26 #include "FairRuntimeDb.h"
27 
28 #include "TClonesArray.h"
29 #include "TMath.h"
30 #include "TRandom2.h"
31 
32 #include <iomanip>
33 
34 using std::cout;
35 using std::cerr;
36 using std::endl;
37 using std::flush;
38 using std::fixed;
39 using std::right;
40 using std::left;
41 using std::setw;
42 using std::setprecision;
43 using std::set;
44 using std::map;
45 
46 using std::sort;
47 
48 bool CompareRecoEvents(RecoEvent ev1, RecoEvent ev2) { return (ev1.meanTime<ev2.meanTime); }
49 
50 // ----- Default constructor ------------------------------------------
52  : FairEventBuilder("GEMEvTrack","PndTrack","GEM",kTRUE),
53  fGemTracks (NULL),
54  fTNofEvents (0),
55  fTNofTracks (0),
56  fTNofRecoEvents(0),
57  fExecTime (0.),
58  fGemTrackDelay (0.)
59 {
60 }
61 // -------------------------------------------------------------------------
62 
63 
64 
65 // ----- Constructor with name -----------------------------------------
67  : FairEventBuilder("GEMEvTrack","PndTrack","GEM",kTRUE),
68  fGemTracks (NULL),
69  fTNofEvents (0),
70  fTNofTracks (0),
71  fTNofRecoEvents(0),
72  fExecTime (0.),
73  fGemTrackDelay (0.)
74 {
75  SetBuilderName(name);
77 }
78 // -------------------------------------------------------------------------
79 
80 
81 
82 // ----- Destructor ----------------------------------------------------
84 }
85 // -------------------------------------------------------------------------
86 
87 
88 
89 // ----- Public method Exec --------------------------------------------
90 std::vector<std::pair<double, FairRecoEventHeader*> > PndGemEventBuilderOnTracks::FindEvents() {
91  if ( fVerbose )
92  cout << endl << "======== PndGemEventBuilderOnTracks::FindEvents(Event = " << fTNofEvents << " ) ====================" << endl;
93 
94  fTimer.Start();
95 
96  fRecoEvents.clear();
97 
98  fTNofTracks += fGemTracks->GetEntries();
99 
100  PndTrack* tempTrack = NULL;
101 
102  if ( fVerbose )
103  cout << "There are " << fGemTracks->GetEntries() << " tracks." << endl;
104 
105  for ( Int_t itrack = 0 ; itrack < fGemTracks->GetEntries() ; itrack++ ) {
106  tempTrack = (PndTrack*)fGemTracks->At(itrack);
107 
108  if ( fVerbose > 1 ) {
109  cout << " " << fTNofEvents << " . " << itrack << " @ " << tempTrack->GetTimeStamp() << "ns" << endl
110  << " trying to match to one of " << fRecoEvents.size() << " reco events" << endl;
111  }
112 
113  Int_t revNo = CompareTrackToPreviousEvents(itrack,tempTrack);
114 
115  FillNewData(tempTrack,
116  tempTrack->GetTimeStamp(),
117  tempTrack->GetTimeStamp());
118 
119  }
120 
121  fTNofEvents += 1;
122 
123  fTNofRecoEvents += fRecoEvents.size();
124 
125  std::sort(fRecoEvents.begin(),fRecoEvents.end(),CompareRecoEvents);
126 
127  std::vector<std::pair<double, FairRecoEventHeader*> > result;
128  std::pair<double,FairRecoEventHeader*> singleResult;
129 
130  if ( fVerbose )
131  cout << "There are " << fRecoEvents.size() << " events." << endl;
132 
133  for ( Int_t irev = 0 ; irev < fRecoEvents.size() ; irev++ ) {
134  FairRecoEventHeader recoEvent;
135  recoEvent.SetEventTime(fRecoEvents[irev].meanTime-fGemTrackDelay,5.);
136  recoEvent.SetIdentifier(GetIdentifier());
137  SetMaxAllowedTime(recoEvent.GetEventTime());
138 
139  fEvent_map[recoEvent] = recoEvent.GetEventTime();
140 
141  singleResult.first = recoEvent.GetEventTime();
142  singleResult.second = (FairRecoEventHeader*)&fEvent_map.rbegin()->first;
143 
144  if ( fVerbose )
145  cout << "FOUND EVENT @ " << singleResult.first << endl;
146 
147  result.push_back(singleResult);
148  }
149 
150  fExecTime += fTimer.RealTime();
151  fTimer.Stop();
152 
153  return result;
154 }
155 // -------------------------------------------------------------------------
156 
157 // -------------------------------------------------------------------------
159  cout << " print " << endl;
160  cout << " fEventMap has " << fEvent_map.size() << " entries" << endl;
161  std::map<FairRecoEventHeader,double>::iterator iter;
162  for ( iter = fEvent_map.begin() ; iter != fEvent_map.end() ; iter++ ) {
163  cout << " // for time " << iter->second << " got " << iter->first.GetEventTime() << " at an address " << &iter->first << endl;
164  }
165  cout << "-------" << endl;
166 }
167 // -------------------------------------------------------------------------
168 
169 // -------------------------------------------------------------------------
170 void PndGemEventBuilderOnTracks::StoreEventData(FairRecoEventHeader* recoEvent) {
171  typedef std::multimap<double, std::pair<double, FairTimeStamp*> >::iterator startTimeMapIter;
172 
173  if ( fVerbose ) {
174  cout << "* " << fData_map.size() << " in fData_map "
175  << "* " << fStartTime_map.size() << " in fStartTime_map "
176  << " TO PUT IN EVENT AT " << recoEvent->GetEventTime() << endl;
177  cout << "CHECK TRACKS WITH TimeStamps:" << endl;
178  for (startTimeMapIter jter = fStartTime_map.begin() ; jter != fStartTime_map.end(); ++jter) {
179  std::pair<double, FairTimeStamp*> datb = jter->second;
180  cout << "@@@ " << datb.second->GetTimeStamp() << endl;
181  }
182  }
183 
184  startTimeMapIter iter = fStartTime_map.begin();
185  while ( iter != fStartTime_map.end() ) {
186  std::pair<double, FairTimeStamp*> data = iter->second;
187  if ( fVerbose ) {
188  cout << " ---> check data at " << data.second->GetTimeStamp()
189  << " /// " << data.second->GetTimeStamp()-recoEvent->GetEventTime()-fGemTrackDelay << endl;
190  }
191  if ( TMath::Abs(data.second->GetTimeStamp()-recoEvent->GetEventTime()-fGemTrackDelay) < 3. ) {
192  if ( fVerbose ) cout << "WILL ADD THIS ONE" << endl;
193  FillDataToDeadTimeMap(data.second, data.first);
194 
195  startTimeMapIter save = iter;
196  save++;
197  fStartTime_map.erase(iter);
198  iter = save;
199  }
200  else
201  iter++;
202  }
203 }
204 
205 std::vector<std::pair<double, PndTrack*> > PndGemEventBuilderOnTracks::Modify(std::pair<double, PndTrack*> oldData, std::pair<double, PndTrack*> newData)
206 {
207  std::vector<std::pair<double, PndTrack*> > result;
208  result.push_back(newData);
209  return result;
210 }
211 
213 {
214  FairRootManager* ioman = FairRootManager::Instance();
215  TClonesArray* myArray = ioman->GetTClonesArray(fBranchName);
216  if (fVerbose > 1) std::cout << "Data Inserted: " << *(PndTrack*)(data) << std::endl;
217  new ((*myArray)[myArray->GetEntries()]) PndTrack(*(PndTrack*)(data));
218 }
219 
221 {
222  std::map<PndTrack, double>::iterator it;
223  PndTrack myData = *(PndTrack*)data;
224  it = fData_map.find(myData);
225  if (it == fData_map.end())
226  return -1;
227  else
228  return it->second;
229 }
230 void PndGemEventBuilderOnTracks::FillDataMap(FairTimeStamp* data, double activeTime)
231 {
232  PndTrack myData = *(PndTrack*)data;
233  fData_map[myData] = activeTime;
234 }
235 
237 {
238  PndTrack myData = *(PndTrack*)data;
239  if (fData_map.find(myData) != fData_map.end())
240  fData_map.erase(fData_map.find(myData));
241 }
242 
243 // ----- Private method CompareTrackToPreviousEvents -------------------
245  Double_t trackTime = tempTrack->GetTimeStamp();
246  Int_t recoEvent = -1;
247  for ( Int_t irev = 0 ; irev < fRecoEvents.size() ; irev++ ) {
248  RecoEvent iterREV = fRecoEvents[irev];
249 
250  // SHOULD BE SOMETHING MORE SERIOUS THAN "2."
251  // 26.08 - just checked the reconstructed track time difference,
252  // got a distribution with sigma of 9.49795e-01
253  // Setting to 3.0
254  if ( TMath::Abs(trackTime-iterREV.meanTime) < 3. ) {
255  if ( fVerbose && recoEvent != -1 ) {
256  cout << "track @ " << trackTime << " matches to two reco events: " << irev << " and " << recoEvent << endl;
257  }
258  else {
259  recoEvent = irev;
260  }
261  }
262  }
263 
264  // matching event found
265  if ( recoEvent != -1 ) {
266  fRecoEvents[recoEvent].meanTime = (fRecoEvents[recoEvent].meanTime*fRecoEvents[recoEvent].nofTracks+trackTime)/((Double_t)(fRecoEvents[recoEvent].nofTracks+1));
267  fRecoEvents[recoEvent].nofTracks = fRecoEvents[recoEvent].nofTracks+1;
268  fRecoEvents[recoEvent].trackIndex.push_back(trackId);
269  }
270  else {
271  RecoEvent tempREV;
272  tempREV.meanTime = trackTime;
273  tempREV.nofTracks = 1;
274  tempREV.trackIndex.push_back(trackId);
275 
276  fRecoEvents.push_back(tempREV);
277  }
278 
279  return recoEvent;
280 }
281 // -------------------------------------------------------------------------
282 
283 // ----- Private method SetParContainers -------------------------------
285 
286  // Get run and runtime database
287  FairRunAna* run = FairRunAna::Instance();
288  if ( ! run ) Fatal("SetParContainers", "No analysis run");
289 
290  FairRuntimeDb* db = run->GetRuntimeDb();
291  if ( ! db ) Fatal("SetParContainers", "No runtime database");
292 }
293 // -------------------------------------------------------------------------
294 
295 
296 
297 
298 // ----- Private method Init -------------------------------------------
300 
301  // Get input array
302  FairRootManager* ioman = FairRootManager::Instance();
303  if ( ! ioman ) Fatal("Init", "No FairRootManager");
304 
305  fGemTracks = (TClonesArray*) ioman->GetObject("GEMTrack");
306 
307  // fGemOutTracks = new TClonesArray("PndTrack",100);
308  // ioman->Register("GEMTrackQQQ", "Gem Tracks", fGemOutTracks, kTRUE);
309  // ioman->RegisterWriteoutBuffer("GEMDigi", fDataBuffer);
310  // fDataBuffer->ActivateBuffering(fTimeOrderedDigi);
311 
312  fGemTrackDelay = 4.96229;
313 
314  return kTRUE;
315 }
316 // -------------------------------------------------------------------------
317 
318 
319 
320 
321 // ----- Private method ReInit -----------------------------------------
323 
324  // Create sectorwise digi sets
325  // MakeSets();
326 
327  return kTRUE;
328 }
329 // -------------------------------------------------------------------------
330 
331 
332 
333 // ----- Public method Finish ------------------------------------------
335 
336  cout << "-------------------- " << GetBuilderName() << " : Summary -----------------------" << endl;
337  cout << " Events: " << setw(10) << fTNofEvents << endl;
338  cout << " Tracks: " << setw(10) << fTNofTracks << " ( " << (Double_t)fTNofTracks /((Double_t)fTNofEvents) << " per event )" << endl;
339  cout << " Reco Events: " << setw(10) << fTNofRecoEvents << endl;
340  cout << " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " << endl;
341  cout << " >>> EB >>> all time = " << fExecTime << "s" << endl;
342  cout << "---------------------------------------------------------------------" << endl;
343 }
344 // -------------------------------------------------------------------------
345 
346 
348 
int fVerbose
Definition: poormantracks.C:24
Int_t run
Definition: autocutx.C:47
void FillDataMap(FairTimeStamp *data, double activeTime)
PndTransMap * map
Definition: sim_emc_apd.C:99
void EraseDataFromDataMap(FairTimeStamp *data)
std::vector< std::pair< double, FairRecoEventHeader * > > FindEvents()
static T Abs(const T &x)
Definition: PndCAMath.h:39
std::map< FairRecoEventHeader, double > fEvent_map
Double_t
std::vector< Int_t > trackIndex
std::vector< std::pair< double, PndTrack * > > Modify(std::pair< double, PndTrack * > oldData, std::pair< double, PndTrack * > newData)
double FindTimeForData(FairTimeStamp *data)
void AddNewDataToTClonesArray(FairTimeStamp *data)
std::vector< RecoEvent > fRecoEvents
bool CompareRecoEvents(RecoEvent ev1, RecoEvent ev2)
TString name
Int_t CompareTrackToPreviousEvents(Int_t trackId, PndTrack *tempTrack)
ClassImp(PndAnaContFact)
Int_t iVerbose
virtual void StoreEventData(FairRecoEventHeader *recoEvent)
std::map< PndTrack, double > fData_map