FairRoot/PandaRoot
PndCAPerformance.cxx
Go to the documentation of this file.
1 // $Id: PndCAPerformance.cxx,v 1.13 2010/09/01 10:38:27 ikulakov Exp $
2 // **************************************************************************
3 // This file is property of and copyright by the ALICE HLT Project *
4 // ALICE Experiment at CERN, All rights reserved. *
5 // *
6 // Primary Authors: Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de> *
7 // Ivan Kisel <kisel@kip.uni-heidelberg.de> *
8 // for The ALICE HLT Project. *
9 // *
10 // Developed by: Igor Kulakov <I.Kulakov@gsi.de> *
11 // Maksym Zyzak <M.Zyzak@gsi.de> *
12 // *
13 // Permission to use, copy, modify and distribute this software and its *
14 // documentation strictly for non-commercial purposes is hereby granted *
15 // without fee, provided that the above copyright notice appears in all *
16 // copies and that both the copyright notice and this permission notice *
17 // appear in the supporting documentation. The authors make no claims *
18 // about the suitability of this software for any purpose. It is *
19 // provided "as is" without express or implied warranty. *
20 // *
21 //***************************************************************************
22 #ifdef DO_TPCCATRACKER_EFF_PERFORMANCE
23 
24 #include "PndCAPerformance.h"
25 
26 #include "PndCACounters.h"
27 #include "PndCAPerformanceBase.h"
28 #include "PndCAGlobalPerformance.h"
29 #include "PndCAParam.h"
30 
31 #include "PndCAGBHit.h"
32 #include "PndCAMCTrack.h"
33 #ifndef HLTCA_STANDALONE
34 #include "PndCAMCPoint.h"
35 #endif
36 #include "PndCAGBTrack.h"
37 #include "PndCAGBTracker.h"
38 
39 //#include "PndCADisplay.h"
40 
41 #include "PndCAParameters.h"
42 
43 #include "TRandom3.h" //dbg
44 #include "TMath.h"
45 #include "TROOT.h"
46 #include "Riostream.h"
47 #include "TFile.h"
48 #include "TH1.h"
49 #include "TH2.h"
50 #include "TProfile.h"
51 #include "TStyle.h"
52 
53 PndCAPerformance::PndCAPerformance()
54 {
55  static bool first_call = true;
56 
57  if (first_call){
58  typedef TSubPerformance TSP;
59 
62  const int NSPerfo = 1;
63  const TSP perfos[NSPerfo] = {
64  TSP(new PndCAGlobalPerformance, "Global Performance"),
65  };
66 
67  subPerformances.resize(NSPerfo);
68  for (int iP = 0; iP < NSPerfo; iP++){
69  subPerformances[iP] = perfos[iP];
70  }
71  }
72  first_call = false;
73  //* constructor
74 }
75 
76 PndCAPerformance::~PndCAPerformance()
77 {
78  //* destructor
79  fHitLabels.clear();
80  fMCTracks.clear();
81  for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ // TODO: why we can't do this in subPerformances.~destructor() ??
82  if (subPerformances[iPerf].perf) delete subPerformances[iPerf].perf;
83  }
84 }
85 
86 PndCAPerformance &PndCAPerformance::Instance()
87 {
88  // reference to static object
89  static PndCAPerformance gPndCAPerformance;
90  return gPndCAPerformance;
91 }
92 
93 bool PndCAPerformance::SetNewEvent(PndCAGBTracker* const tracker, string mcTracksFile, string mcPointsFile)
94 {
95  fTracker = tracker;
96  // Read MC info from file
97  FILE *file = std::fopen( mcTracksFile.data(), "rb" );
98  if ( !file ) {
99  return false;
100  }
101  ReadMCEvent( file );
102  fclose( file );
103 
104  file = std::fopen( mcPointsFile.data(), "rb" );
105  if ( !file ) {
106  return false;
107  }
108  ReadLocalMCPoints( file );
109  fclose( file );
110 
111  return true;
112 } // void PndCAPerformance::SetNewEvent
113 
114 void PndCAPerformance::InitSubPerformances()
115 {
116  // Init subperformances
117  static bool first_call = true;
118 
119  for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){
120  subPerformances[iPerf]->SetNewEvent(fTracker, &fHitLabels, &fMCTracks, &fLocalMCPoints);
121  }
122 
123  if (first_call) CreateHistos();
124 
125  first_call = false;
126 } // void PndCAPerformance::InitSubPerformances
127 
128 void PndCAPerformance::CreateHistos()
129 {
130  for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){
131  if(!(subPerformances[iPerf]->IsHistoCreated()))
132  subPerformances[iPerf]->CreateHistos(subPerformances[iPerf].name, fOutputFile);
133  }
134 }
135 
136 bool PndCAPerformance::CreateHistos(string name)
137 {
138  unsigned i = 0;
139  for( ; i < (subPerformances.size()) && (subPerformances[i].name != name); i++);
140  if(!(subPerformances[i]->IsHistoCreated()) && i != subPerformances.size())
141  subPerformances[i]->CreateHistos(subPerformances[i].name, fOutputFile);
142  if ( i == subPerformances.size() ) return 0;
143  return 1;
144 }
145 
146 
147 void PndCAPerformance::WriteDir2Current( TObject *obj )
148 {
149  //* recursive function to write down all created in the file histos
150  if ( !obj->IsFolder() ) obj->Write();
151  else {
152  TDirectory *cur = gDirectory;
153  ((TDirectory*)obj)->cd();
154  TList *listSub = ( ( TDirectory* )obj )->GetList();
155  TIter it( listSub );
156  while ( TObject *obj1 = it() ) WriteDir2Current( obj1 );
157  cur->cd();
158  }
159 }
160 
161 
162 void PndCAPerformance::WriteHistos()
163 {
164  if(fOutputFile) WriteDir2Current(fOutputFile);
165 }
166 
167 void PndCAPerformance::ExecPerformance()
168 {
169  fStatNEvents++;
170 
171  for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){
172  if(subPerformances[iPerf].IsGlobalPerf) subPerformances[iPerf]->Exec(0);
173  }
174 #if 1 // current event efficiencies
175  for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){
176  cout << endl
177  << " ---- " << subPerformances[iPerf].name << " event " << fStatNEvents << " ---- "<< endl;
178  subPerformances[iPerf]->PrintEfficiency();
179  }
180  cout << endl << " ============================== " << endl;
181 #endif //0
182  for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){
183  cout << endl
184  << " ---- " << subPerformances[iPerf].name << " " << fStatNEvents << " events Statistic ---- " << endl;
185  if(subPerformances[iPerf].IsGlobalPerf) subPerformances[iPerf]->PrintEfficiencyStatistic();
186  };
187 
188 }
189 
190 PndCAPerformanceBase* PndCAPerformance::GetSubPerformance(string name)
191 {
192  unsigned i = 0;
193  for( ; (i < subPerformances.size()) && (subPerformances[i].name != name); i++);
194  //ASSERT ( i != subPerformances.size() , " Incorrect name of subPerformance used.");
195  if ( i == subPerformances.size() ) return 0;
196  return subPerformances[i].perf;
197 } // PndCAPerformanceBase* PndCAPerformance::GetSubPerformance(string name)
198 
199 
201 void PndCAPerformance::WriteMCEvent( FILE *file ) const
202 {
203  // write MC information to the file
204  int n = fMCTracks.size();
205  std::fwrite( &n, sizeof( int ), 1, file );
206  n = fHitLabels.size();
207  std::fwrite( &n, sizeof( int ), 1, file );
208  std::fwrite( &fMCTracks[0], sizeof( PndCAMCTrack ), fMCTracks.size(), file );
209  std::fwrite( &fHitLabels[0], sizeof( PndCAHitLabel ), fHitLabels.size(), file );
210 }
211 
212 void PndCAPerformance::ReadMCEvent( FILE *file )
213 {
214  // read mc info from the file
215  int read;
216  int n;
217 
218  read = std::fread( &n, sizeof( int ), 1, file );
219  assert( read == 1 );
220  fMCTracks.resize( n );
221  read = std::fread( &n, sizeof( int ), 1, file );
222  assert( read == 1 );
223  fHitLabels.resize( n );
224 
225  read = std::fread( &fMCTracks[0], sizeof( PndCAMCTrack ), fMCTracks.size(), file );
226  assert( read == (int) fMCTracks.size() );
227 
228  read = std::fread( &fHitLabels[0], sizeof( PndCAHitLabel ), fHitLabels.size(), file );
229  assert( read == (int) fHitLabels.size() );
230  UNUSED_PARAM1(read);
231 // for (int i = 0; i < fMCTracks.size(); i++){
232 // PndCAMCTrack& mc = fMCTracks[i];
233 // std::cout << mc.PDG() << " ";
234 // std::cout << std::endl;
235 // for (int iPar = 0; iPar < 7; iPar++)
236 // std::cout << mc.Par(iPar) << " ";
237 // std::cout << std::endl;
238 // for (int iPar = 0; iPar < 7; iPar++)
239 // std::cout << mc.TPCPar(iPar) << " ";
240 // std::cout << std::endl;
241 // // std::cout << mc.P() << " ";
242 // };
243 // for (int i = 0; i < fHitLabels.size(); i++){
244 // PndCAHitLabel& l = fHitLabels[i];
245 // for (int iPar = 0; iPar < 3; iPar++)
246 // std::cout << l.fLab[iPar] << " ";
247 // std::cout << std::endl;
248 // };
249 }
250 
251 void PndCAPerformance::ReadLocalMCPoints( FILE *file )
252 {
253 
254  int read;
255  int n;
256 
257  read = std::fread( &n, sizeof( int ), 1, file );
258  assert( read == 1 );
259  fLocalMCPoints.resize( n );
260 
261  read = std::fread( &fLocalMCPoints[0], sizeof( PndCALocalMCPoint ), fLocalMCPoints.size(), file );
262  assert( read == (int) fLocalMCPoints.size() );
263  UNUSED_PARAM1(read);
264 } // void PndCAPerformance::ReadLocalMCPoints( FILE *file )
265 
266 
267 void PndCAPerformance::SetMCTracks(vector<PndCAMCTrack>& mcTracks)
268 {
269  const int N = mcTracks.size();
270  fMCTracks.resize(N);
271  for(int i = 0; i < N; i++){
272  fMCTracks[i] = mcTracks[i];
273  }
274 }
275 
276 void PndCAPerformance::SetMCPoints(vector<PndCALocalMCPoint>& mcPoints)
277 {
278  const int N = mcPoints.size();
279  fLocalMCPoints.resize(N);
280  for(int i = 0; i < N; i++){
281  fLocalMCPoints[i] = mcPoints[i];
282  }
283 }
284 
285 void PndCAPerformance::SetHitLabels(vector<PndCAHitLabel>& hitLabels)
286 {
287  const int N = hitLabels.size();
288  fHitLabels.resize(N);
289  for(int i = 0; i < N; i++){
290  fHitLabels[i] = hitLabels[i];
291  }
292 }
293 
294 void PndCAPerformance::SaveDataInFiles(string prefix) const
295 {
296 
297  {
298  std::ofstream ofile((prefix+"hitLabels.data").data(),ios::out|ios::app);
299  const int Size = fHitLabels.size();
300  ofile << Size << std::endl;
301  for (unsigned int i = 0; i < fHitLabels.size(); i++){
302  const PndCAHitLabel &l = fHitLabels[i];
303  ofile << l;
304  }
305  ofile.close();
306  }
307 
308  {
309  std::ofstream ofile((prefix+"MCTracks.data").data(),ios::out|ios::app);
310  const int Size = fMCTracks.size();
311  ofile << Size << std::endl;
312  for (unsigned int i = 0; i < fMCTracks.size(); i++){
313  const PndCAMCTrack &l = fMCTracks[i];
314  ofile << l;
315  }
316  ofile.close();
317  }
318 
319  {
320  std::ofstream ofile((prefix+"MCPoints.data").data(),ios::out|ios::app);
321  const int Size = fLocalMCPoints.size();
322  ofile << Size << std::endl;
323  for (unsigned int i = 0; i < fLocalMCPoints.size(); i++){
324  const PndCALocalMCPoint &l = fLocalMCPoints[i];
325  ofile << l;
326  }
327  ofile.close();
328  }
329 }
330 
331 bool PndCAPerformance::ReadDataFromFiles(string prefix)
332 {
333 
334  {
335  std::ifstream ifile((prefix+"hitLabels.data").data());
336  if ( !ifile.is_open() ) return 0;
337  int Size;
338  ifile >> Size;
339  fHitLabels.resize(Size);
340  for (int i = 0; i < Size; i++){
341  PndCAHitLabel &l = fHitLabels[i];
342  ifile >> l;
343  }
344  ifile.close();
345  }
346 
347 
348  {
349  std::ifstream ifile((prefix+"MCTracks.data").data());
350  if ( !ifile.is_open() ) return 0;
351  int Size;
352  ifile >> Size;
353  fMCTracks.resize(Size);
354  for (int i = 0; i < Size; i++){
355  PndCAMCTrack &l = fMCTracks[i];
356  ifile >> l;
357  }
358  ifile.close();
359  }
360 
361 
362  {
363  std::ifstream ifile((prefix+"MCPoints.data").data());
364  if ( !ifile.is_open() ) return 0;
365  int Size;
366  ifile >> Size;
367  fLocalMCPoints.resize(Size);
368  for (int i = 0; i < Size; i++){
369  PndCALocalMCPoint &l = fLocalMCPoints[i];
370  ifile >> l;
371  if( l.IRow() >= PndCAParameters::MaxNStations ){
372  cout<<"wrong mc point station number: "<<(int) l.IRow()<<" out of "<<PndCAParameters::MaxNStations<<endl;
373  l.SetIRow( PndCAParameters::MaxNStations-1);
374  }
375  }
376  ifile.close();
377  }
378 
379 
380  // calculate needed additional info
381  {
382  const int NMCTracks = fMCTracks.size();
383  // NMCRows
384  for (int i = 0; i < NMCTracks; ++i ){
385  PndCAMCTrack& t = fMCTracks[i];
386  vector<int> rows;
387  rows.resize( PndCAParameters::MaxNStations, 0 );
388  for (int j = 0, iP = t.FirstMCPointID(); j < t.NMCPoints(); iP++, j++){
389  rows[fLocalMCPoints[iP].IRow()]++;
390  }
391  int nRows = 0;
392 
393  int nMCContRows = 0;
394  int istaold = -2, ncont=0;
395  for (unsigned int j = 0 ; j < rows.size(); j++) {
396  if( rows[j] > 0 ) {
397  nRows++;
398 
399  if( istaold == static_cast<int>(j)-1 ) {
400  ncont++;
401  }
402  else {
403  nMCContRows = (nMCContRows > ncont) ? nMCContRows : ncont;
404  ncont = 1;
405  }
406  istaold = j;
407  }
408  }
409  nMCContRows = (nMCContRows > ncont) ? nMCContRows : ncont;
410 
411  t.SetNMCRows( nRows );
412  t.SetNMCContRows( nMCContRows );
413  }
414 
415  // NHitRows
416 
417  vector<int> zero(PndCAParameters::MaxNStations, 0);
418  vector< vector<int> > nmchits(NMCTracks,zero);
419  vector<PndCAGBHit>& hits = const_cast<PndCAGBTracker *>(fTracker)->fHits;
420 
421  for ( int i = 0; i < fTracker->NHits(); i++) {
422  int id = hits[i].ID();
423  for ( int il = 0; il < 3; il++ ) {
424  int trackId = HitLabel(id).fLab[il];
425  if(trackId < 0) continue;
426  ASSERT(trackId < NMCTracks, "Incorrect MCPoints of MCTracks file. " << trackId << " < " << NMCTracks );
427  if( hits[i].IRow() >= PndCAParameters::MaxNStations ){
428  cout<<"wrong hit station number: "<<(int) hits[i].IRow()<<" out of "<<PndCAParameters::MaxNStations<<endl;
429  }
430  assert( hits[i].IRow() < PndCAParameters::MaxNStations );
431  nmchits[trackId][hits[i].IRow()]++;
432  }
433  }
434 
435  for (int i = 0; i < NMCTracks; ++i ){
436  PndCAMCTrack& t = fMCTracks[i];
437  int nRows = 0;
438 
439  int nHitContRows = 0;
440  int istaold = -1, ncont=0;
441  for (int j = 0; j < PndCAParameters::MaxNStations; ++j ) {
442  if ( nmchits[i][j] ) {
443  nRows++;
444 
445  if( istaold == j-1 ) {
446  ncont++;
447  }
448  else {
449  nHitContRows = (nHitContRows > ncont) ? nHitContRows : ncont;
450  ncont = 1;
451  }
452  istaold = j;
453  }
454  }
455  nHitContRows = (nHitContRows > ncont) ? nHitContRows : ncont;
456 
457  t.SetNHitRows( nRows );
458  t.SetNHitContRows( nHitContRows );
459  }
460 
461  // PV
462  fPV[0] = fPV[1] = fPV[2] = 0;
463  for (int i = 0; i < NMCTracks; ++i ){
464  PndCAMCTrack& t = fMCTracks[i];
465  if (t.MotherId() > 0) continue;
466  fPV[0] = t.X();
467  fPV[1] = t.Y();
468  fPV[2] = t.Z();
469  break;
470  }
471 
472  }
473 
474  return 1;
475 }
476 
477 
478 //#include "PndCADisplay.h"
479 
480 
481 struct Vector3 {
482  Vector3( const float& x, const float& y, const float& z):fx(x),fy(y),fz(z){};
483  float X() const { return fx; }
484  float Y() const { return fy; }
485  float Z() const { return fz; }
486  private:
487  float fx,fy,fz;
488 };
489 
490 
491 bool operator<(const Vector3& a, const Vector3& b) {
492  if (a.X() != b.X())
493  return a.X() < b.X();
494  else
495  if (a.Y() != b.Y())
496  return a.Y() < b.Y();
497  else
498  return a.Z() < b.Z();
499 }
500 
501  // Rid of hits with same wires positions
502 void PndCAPerformance::CombineHits(){
503 
504  vector<PndCAGBHit>& hits = const_cast<PndCAGBTracker *>(fTracker)->fHits;
505  //unsigned int nHits = fTracker->NHits();
506  int nLabels = fHitLabels.size();
507 
508  int nHitsNew=0;
509  for( unsigned int ih = 0; ih < hits.size(); ih++ ){
510  PndCAGBHit &hi = hits[ih];
511  int jh=0;
512  for( ; jh<nHitsNew; jh++ ){
513  PndCAGBHit &hj = hits[jh];
514  if( hi.IRow() != hj.IRow() ) continue;
515  double dx = hi.GlobalX() - hj.GlobalX();
516  double dy = hi.GlobalY() - hj.GlobalY();
517  double dz = hi.Z() - hj.Z();
518  if( dx*dx+dy*dy+dz*dz > 1.e-8 ) continue;
519  break;
520  }
521  PndCAGBHit &hj = hits[jh];
522  if( jh==nHitsNew ){ // store hit
523  hj = hi;
524  nHitsNew++;
525  continue;
526  }
527  // merge
529  cout<<"Pile up in MVD, something is wrong"<<endl;
530  exit(0);
531  }
532  bool mergeLabels = ( hj.ID() < nLabels && hi.ID()<nLabels );
533  PndCAHitLabel *lto = 0, *lfrom = 0;
534 
535  if( mergeLabels ){
536  lto = &fHitLabels[hj.ID()];
537  lfrom = &fHitLabels[hi.ID()];
538  }
539 
540  if( hi.R() < hj.R() ){ // merge hits
541  if( mergeLabels ){
542  lto = &fHitLabels[hi.ID()];
543  lfrom = &fHitLabels[hj.ID()];
544  }
545  hj = hi;
546  }
547  if( mergeLabels ){ // merge labels
548  int k = 0;
549  for( ; k < 3 && lto->fLab[k] >=0; k++ );
550  if ( k>=3 ) continue; // no space to store new label
551  for( int a=0; a<3; a++ ){
552  int lab = lfrom->fLab[a];
553  if( lab<0 ) continue;
554  bool store = true;
555  for( int b=0; b<k; b++ ) if( lto->fLab[b]==lab ) store=false;
556  if( store ) lto->fLab[k] = lab;
557  }
558  }
559  }
560 
561  const_cast<PndCAGBTracker *>(fTracker)->fNHits = nHitsNew;
562  hits.resize(nHitsNew);
563 }
564 
565 
566 
567 #endif //DO_TPCCATRACKER_EFF_PERFORMANCE
float GlobalY() const
Definition: PndCAGBHit.h:43
double dy
float Z() const
Definition: PndCAGBHit.h:44
int IRow() const
Definition: PndCAGBHit.h:57
float GlobalX() const
Definition: PndCAGBHit.h:42
Int_t i
Definition: run_full.C:25
TTree * b
TFile * file
exit(0)
int n
float Y() const
Definition: PndCAMCTrack.h:41
int NMCPoints() const
Definition: PndCAMCTrack.h:54
double Y
Definition: anaLmdDigi.C:68
void SetNMCRows(int v)
Definition: PndCAMCTrack.h:78
int MotherId() const
Definition: PndCAMCTrack.h:35
Int_t a
Definition: anaLmdDigi.C:126
int FirstMCPointID() const
Definition: PndCAMCTrack.h:55
float Z() const
Definition: PndCAMCTrack.h:42
Double_t z
vector< FitStore > store(3000)
TFile * out
Definition: reco_muo.C:20
void SetNMCContRows(int v)
Definition: PndCAMCTrack.h:80
TString name
double dx
double X
Definition: anaLmdDigi.C:68
Double_t x
void SetNHitRows(int v)
Definition: PndCAMCTrack.h:79
#define ASSERT(v, msg)
Definition: PndCADef.h:56
void SetNHitContRows(int v)
Definition: PndCAMCTrack.h:81
double Z
Definition: anaLmdDigi.C:68
float R() const
Definition: PndCAGBHit.h:60
TTree * t
Definition: bump_analys.C:13
CbmHit * hits[nHits]
Definition: RiemannTest.C:19
Double_t y
friend F32vec4 operator<(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:75
int ID() const
Definition: PndCAGBHit.h:58
float X() const
Definition: PndCAMCTrack.h:40