// $Id: AliHLTTPCCAPerformance.cxx,v 1.13 2010/09/01 10:38:27 ikulakov Exp $ // ************************************************************************** // This file is property of and copyright by the ALICE HLT Project * // ALICE Experiment at CERN, All rights reserved. * // * // Primary Authors: Sergey Gorbunov * // Ivan Kisel * // for The ALICE HLT Project. * // * // Developed by: Igor Kulakov * // Maksym Zyzak * // * // Permission to use, copy, modify and distribute this software and its * // documentation strictly for non-commercial purposes is hereby granted * // without fee, provided that the above copyright notice appears in all * // copies and that both the copyright notice and this permission notice * // appear in the supporting documentation. The authors make no claims * // about the suitability of this software for any purpose. It is * // provided "as is" without express or implied warranty. * // * //*************************************************************************** #ifdef DO_TPCCATRACKER_EFF_PERFORMANCE #include "AliHLTTPCCAPerformance.h" #include "AliHLTTPCCounters.h" #include "AliHLTTPCPerformanceBase.h" #include "AliHLTTPCCAGlobalPerformance.h" #include "AliHLTTPCCAParam.h" #include "AliHLTTPCCAGBHit.h" #include "AliHLTTPCCAMCTrack.h" #ifndef HLTCA_STANDALONE #include "AliHLTTPCCAMCPoint.h" #endif #include "AliHLTTPCCAGBTrack.h" #include "AliHLTTPCCAGBTracker.h" #include "AliHLTTPCCADisplay.h" #include "TRandom3.h" //dbg #include "TMath.h" #include "TROOT.h" #include "Riostream.h" #include "TFile.h" #include "TH1.h" #include "TH2.h" #include "TProfile.h" #include "TStyle.h" AliHLTTPCCAPerformance::AliHLTTPCCAPerformance() { static bool first_call = true; if (first_call){ typedef TSubPerformance TSP; /// Just define here all sub-performances /// TSP(new __ClassName__ , __Name__ ), #ifndef STAR_STANDALONE const int NSPerfo = 1; #else // HLTCA_STANDALONE const int NSPerfo = 1; #endif // HLTCA_STANDALONE const TSP perfos[NSPerfo] = { TSP(new AliHLTTPCCAGlobalPerformance, "Global Performance"), }; subPerformances.resize(NSPerfo); for (int iP = 0; iP < NSPerfo; iP++){ subPerformances[iP] = perfos[iP]; } } first_call = false; //* constructor } AliHLTTPCCAPerformance::~AliHLTTPCCAPerformance() { //* destructor fHitLabels.Resize( 0 ); fMCTracks.Resize( 0 ); for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ // TODO: why we can't do this in subPerformances.~destructor() ?? if (subPerformances[iPerf].perf) delete subPerformances[iPerf].perf; } } AliHLTTPCCAPerformance &AliHLTTPCCAPerformance::Instance() { // reference to static object static AliHLTTPCCAPerformance gAliHLTTPCCAPerformance; return gAliHLTTPCCAPerformance; } bool AliHLTTPCCAPerformance::SetNewEvent(AliHLTTPCCAGBTracker* const tracker, string mcTracksFile, string mcPointsFile) { fTracker = tracker; // Read MC info from file FILE *file = std::fopen( mcTracksFile.data(), "rb" ); if ( !file ) { return false; } ReadMCEvent( file ); fclose( file ); file = std::fopen( mcPointsFile.data(), "rb" ); if ( !file ) { return false; } ReadLocalMCPoints( file ); fclose( file ); return true; } // void AliHLTTPCCAPerformance::SetNewEvent void AliHLTTPCCAPerformance::InitSubPerformances() { // Init subperformances static bool first_call = true; for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ subPerformances[iPerf]->SetNewEvent(fTracker, &fHitLabels, &fMCTracks, &fLocalMCPoints); } if (first_call) CreateHistos(); first_call = false; } // void AliHLTTPCCAPerformance::InitSubPerformances void AliHLTTPCCAPerformance::CreateHistos() { for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ if(!(subPerformances[iPerf]->IsHistoCreated())) subPerformances[iPerf]->CreateHistos(subPerformances[iPerf].name, fOutputFile); } } bool AliHLTTPCCAPerformance::CreateHistos(string name) { unsigned i = 0; for( ; i < (subPerformances.size()) && (subPerformances[i].name != name); i++); if(!(subPerformances[i]->IsHistoCreated()) && i != subPerformances.size()) subPerformances[i]->CreateHistos(subPerformances[i].name, fOutputFile); if ( i == subPerformances.size() ) return 0; return 1; } void AliHLTTPCCAPerformance::WriteDir2Current( TObject *obj ) { //* recursive function to write down all created in the file histos if ( !obj->IsFolder() ) obj->Write(); else { TDirectory *cur = gDirectory; ((TDirectory*)obj)->cd(); TList *listSub = ( ( TDirectory* )obj )->GetList(); TIter it( listSub ); while ( TObject *obj1 = it() ) WriteDir2Current( obj1 ); cur->cd(); } } void AliHLTTPCCAPerformance::WriteHistos() { if(fOutputFile) WriteDir2Current(fOutputFile); } void AliHLTTPCCAPerformance::ExecPerformance() { fStatNEvents++; for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ if(subPerformances[iPerf].IsGlobalPerf) subPerformances[iPerf]->Exec(0); } #if 1 // current event efficiencies for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ cout << endl << " ---- " << subPerformances[iPerf].name << " event " << fStatNEvents << " ---- "<< endl; subPerformances[iPerf]->PrintEfficiency(); } cout << endl << " ============================== " << endl; #endif //0 for (unsigned int iPerf = 0; iPerf < subPerformances.size(); iPerf++){ cout << endl << " ---- " << subPerformances[iPerf].name << " " << fStatNEvents << " events Statistic ---- " << endl; if(subPerformances[iPerf].IsGlobalPerf) subPerformances[iPerf]->PrintEfficiencyStatistic(); }; } AliHLTTPCPerformanceBase* AliHLTTPCCAPerformance::GetSubPerformance(string name) { unsigned i = 0; for( ; (i < subPerformances.size()) && (subPerformances[i].name != name); i++); //ASSERT ( i != subPerformances.size() , " Incorrect name of subPerformance used."); if ( i == subPerformances.size() ) return 0; return subPerformances[i].perf; } // AliHLTTPCPerformanceBase* AliHLTTPCCAPerformance::GetSubPerformance(string name) /// -------------------- Read\write MC information --------------------- void AliHLTTPCCAPerformance::WriteMCEvent( FILE *file ) const { // write MC information to the file int n = fMCTracks.Size(); std::fwrite( &n, sizeof( int ), 1, file ); n = fHitLabels.Size(); std::fwrite( &n, sizeof( int ), 1, file ); std::fwrite( fMCTracks.Data(), sizeof( AliHLTTPCCAMCTrack ), fMCTracks.Size(), file ); std::fwrite( fHitLabels.Data(), sizeof( AliHLTTPCCAHitLabel ), fHitLabels.Size(), file ); } void AliHLTTPCCAPerformance::ReadMCEvent( FILE *file ) { // read mc info from the file int read; int n; read = std::fread( &n, sizeof( int ), 1, file ); assert( read == 1 ); fMCTracks.Resize( n ); read = std::fread( &n, sizeof( int ), 1, file ); assert( read == 1 ); fHitLabels.Resize( n ); read = std::fread( fMCTracks.Data(), sizeof( AliHLTTPCCAMCTrack ), fMCTracks.Size(), file ); assert( read == fMCTracks.Size() ); read = std::fread( fHitLabels.Data(), sizeof( AliHLTTPCCAHitLabel ), fHitLabels.Size(), file ); assert( read == fHitLabels.Size() ); UNUSED_PARAM1(read); // for (int i = 0; i < fMCTracks.Size(); i++){ // AliHLTTPCCAMCTrack& mc = fMCTracks[i]; // std::cout << mc.PDG() << " "; // std::cout << std::endl; // for (int iPar = 0; iPar < 7; iPar++) // std::cout << mc.Par(iPar) << " "; // std::cout << std::endl; // for (int iPar = 0; iPar < 7; iPar++) // std::cout << mc.TPCPar(iPar) << " "; // std::cout << std::endl; // // std::cout << mc.P() << " "; // }; // for (int i = 0; i < fHitLabels.Size(); i++){ // AliHLTTPCCAHitLabel& l = fHitLabels[i]; // for (int iPar = 0; iPar < 3; iPar++) // std::cout << l.fLab[iPar] << " "; // std::cout << std::endl; // }; } void AliHLTTPCCAPerformance::ReadLocalMCPoints( FILE *file ) { int read; int n; read = std::fread( &n, sizeof( int ), 1, file ); assert( read == 1 ); fLocalMCPoints.Resize( n ); read = std::fread( fLocalMCPoints.Data(), sizeof( AliHLTTPCCALocalMCPoint ), fLocalMCPoints.Size(), file ); assert( read == fLocalMCPoints.Size() ); UNUSED_PARAM1(read); } // void AliHLTTPCCAPerformance::ReadLocalMCPoints( FILE *file ) void AliHLTTPCCAPerformance::SetMCTracks(vector& mcTracks) { const int N = mcTracks.size(); fMCTracks.Resize(N); for(int i = 0; i < N; i++){ fMCTracks[i] = mcTracks[i]; } } void AliHLTTPCCAPerformance::SetMCPoints(vector& mcPoints) { const int N = mcPoints.size(); fLocalMCPoints.Resize(N); for(int i = 0; i < N; i++){ fLocalMCPoints[i] = mcPoints[i]; } } void AliHLTTPCCAPerformance::SetHitLabels(vector& hitLabels) { const int N = hitLabels.size(); fHitLabels.Resize(N); for(int i = 0; i < N; i++){ fHitLabels[i] = hitLabels[i]; } } void AliHLTTPCCAPerformance::SaveDataInFiles(string prefix) const { { ofstream ofile((prefix+"hitLabels.data").data(),ios::out|ios::app); const int Size = fHitLabels.Size(); ofile << Size << std::endl; for (int i = 0; i < fHitLabels.Size(); i++){ const AliHLTTPCCAHitLabel &l = fHitLabels[i]; ofile << l; } ofile.close(); } { ofstream ofile((prefix+"MCTracks.data").data(),ios::out|ios::app); const int Size = fMCTracks.Size(); ofile << Size << std::endl; for (int i = 0; i < fMCTracks.Size(); i++){ const AliHLTTPCCAMCTrack &l = fMCTracks[i]; ofile << l; } ofile.close(); } { ofstream ofile((prefix+"MCPoints.data").data(),ios::out|ios::app); const int Size = fLocalMCPoints.Size(); ofile << Size << std::endl; for (int i = 0; i < fLocalMCPoints.Size(); i++){ const AliHLTTPCCALocalMCPoint &l = fLocalMCPoints[i]; ofile << l; } ofile.close(); } } bool AliHLTTPCCAPerformance::ReadDataFromFiles(string prefix) { { ifstream ifile((prefix+"hitLabels.data").data()); if ( !ifile.is_open() ) return 0; int Size; ifile >> Size; fHitLabels.Resize(Size); for (int i = 0; i < Size; i++){ AliHLTTPCCAHitLabel &l = fHitLabels[i]; ifile >> l; } ifile.close(); } { ifstream ifile((prefix+"MCTracks.data").data()); if ( !ifile.is_open() ) return 0; int Size; ifile >> Size; fMCTracks.Resize(Size); for (int i = 0; i < Size; i++){ AliHLTTPCCAMCTrack &l = fMCTracks[i]; ifile >> l; } ifile.close(); } { ifstream ifile((prefix+"MCPoints.data").data()); if ( !ifile.is_open() ) return 0; int Size; ifile >> Size; fLocalMCPoints.Resize(Size); for (int i = 0; i < Size; i++){ AliHLTTPCCALocalMCPoint &l = fLocalMCPoints[i]; ifile >> l; } ifile.close(); } // calculate needed additional info { const int NMCTracks = fMCTracks.Size(); // NMCRows for (int i = 0; i < NMCTracks; ++i ){ AliHLTTPCCAMCTrack& t = fMCTracks[i]; vector rows; rows.resize( AliHLTTPCCAParameters::MaxNStations, 0 ); for (int j = 0, iP = t.FirstMCPointID(); j < t.NMCPoints(); iP++, j++){ rows[fLocalMCPoints[iP].IRow()]++; } int nRows = 0; for (unsigned int j = 0 ; j < rows.size(); j++) if( rows[j] > 0 ) nRows++; t.SetNMCRows( nRows ); } // NHitRows vector< int > nmchits(NMCTracks, 0); AliHLTResizableArray& hits = const_cast(fTracker)->fHits; for (int i = 0; i < hits.Size(); i++) { int id = hits[i].ID(); for ( int il = 0; il < 3; il++ ) { int trackId = HitLabel(id).fLab[il]; if(trackId < 0) continue; nmchits[trackId]++; } } for (int i = 0; i < NMCTracks; ++i ){ AliHLTTPCCAMCTrack& t = fMCTracks[i]; t.SetNHitRows( nmchits[i] ); } // Angle, HitErrors for ( int ih = 0; ih < hits.Size(); ih++ ) { AliHLTTPCCAGBHit &hit = hits[ih]; const AliHLTTPCCAHitLabel &l = (fHitLabels)[hit.ID()]; const int iMC = l.fLab[0]; if( iMC<0 ) continue; AliHLTTPCCAMCTrack &mc = (fMCTracks)[iMC]; int MCindex = -1; int nFirstMC = mc.FirstMCPointID(); int nMCPoints = mc.NMCPoints(); AliHLTTPCCALocalMCPoint *points = &((fLocalMCPoints).Data()[nFirstMC]); for(int iMCPoint=0; iMCPoint& hits = const_cast(fTracker)->fHits; std::sort( hits.Data(), hits.Data() + const_cast(fTracker)->NHits(), AliHLTTPCCAGBHit::Compare ); // has an influence on the procedure. CHECKME why? TRandom3 rand; { int nHits = fTracker->NHits(); for ( int ih = 0; ih < nHits; ih++ ) { AliHLTTPCCAGBHit &hit = hits[ih]; const AliHLTTPCCAHitLabel &l = (fHitLabels)[hit.ID()]; const int iMC = l.fLab[0]; if( iMC<0 ) continue; AliHLTTPCCAMCTrack &mc = (fMCTracks)[iMC]; int MCindex = -1; int nFirstMC = mc.FirstMCPointID(); int nMCPoints = mc.NMCPoints(); AliHLTTPCCALocalMCPoint *points = &((fLocalMCPoints).Data()[nFirstMC]); for(int iMCPoint=0; iMCPoint& hits = const_cast(fTracker)->fHits; const int NHits = fLocalMCPoints.Size(); const_cast(fTracker)->fNHits = NHits; // hits.Clear(); hits.Resize(NHits); fHitLabels.Resize(NHits); AliHLTResizableArray &fStrips = const_cast(fTracker)->fFStrips; // create a virtual strip for each hit (currently strips position is not used for ITS, so don't have to fill them) AliHLTResizableArray &bStrips = const_cast(fTracker)->fBStrips; // create a virtual strip for each hit fStrips.Resize(NHits); bStrips.Resize(NHits); TRandom3 rand; for ( int ih = 0; ih < NHits; ih++ ) { AliHLTTPCCALocalMCPoint &mcP = fLocalMCPoints[ih]; AliHLTTPCCAGBHit &hit = hits[ih]; hit.SetID(ih); AliHLTTPCCAHitLabel &l = fHitLabels[ih]; l.fLab[0] = mcP.TrackI(); l.fLab[1] = -1; l.fLab[2] = -1; // get errors double mcEx = mcP.Px(); double mcEy = mcP.Py(); double mcEz = mcP.Pz(); double mcEt = TMath::Sqrt( mcEx * mcEx + mcEy * mcEy ); hit.SetErr2Y(0.01); hit.SetErr2Z(0.01); sfloat_v Err2Y = hit.Err2Y(), Err2Z = hit.Err2Z(); const AliHLTTPCCAParam par; TrackParamVector t; t.SetZ( mcP.Z() ); t.SetSinPhi(mcEy / mcEt); t.SetDzDs(mcEz / mcEt); double mcX = mcP.X(); double mcY = mcP.Y(); double mcZ = mcP.Z(); hit.SetIRow( mcP.IRow() ); hit.SetAngle( mcP.Angle() ); double mcXTmp = mcX*cos( hit.Angle() ) + mcY*sin( hit.Angle() ); double mcYTmp = -mcX*sin( hit.Angle() ) + mcY*cos( hit.Angle() ); mcYTmp -= sqrt(Err2Y[0])*rand.Gaus(); mcZ -= sqrt(Err2Z[0])*rand.Gaus(); mcX = mcXTmp*cos( hit.Angle() ) - mcYTmp*sin( hit.Angle() ); mcY = mcXTmp*sin( hit.Angle() ) + mcYTmp*cos( hit.Angle() ); hit.SetX( mcX ); hit.SetY( mcY ); hit.SetZ( mcZ ); hit.SetFStripP( &fStrips[ih] ); hit.SetBStripP( &bStrips[ih] ); } } // Match hits with closest MCPoint void AliHLTTPCCAPerformance::RematchHits(){ AliHLTResizableArray& hits = const_cast(fTracker)->fHits; const int NHits = fTracker->NHits(); for ( int ih = 0; ih < NHits; ih++ ) { AliHLTTPCCAGBHit &hit = hits[ih]; AliHLTTPCCAHitLabel &l = fHitLabels[ih]; int MCindex = -1; float R2 = 10e30; for(int iMCPoint=0; iMCPoint < fLocalMCPoints.Size(); iMCPoint++) { if(fLocalMCPoints[iMCPoint].IRow() != hit.IRow()) continue; const float dY = hit.Y() - fLocalMCPoints[iMCPoint].Y(); const float dZ = hit.Z() - fLocalMCPoints[iMCPoint].Z(); const float r2 = dY*dY + dZ*dZ; if( r2 < R2 ) { MCindex = iMCPoint; R2 = r2; } } if(MCindex == -1) { l.fLab[0] = -1; l.fLab[1] = -1; l.fLab[2] = -1; continue; } l.fLab[0] = fLocalMCPoints[MCindex].TrackI(); l.fLab[1] = -1; l.fLab[2] = -1; } } #endif //DO_TPCCATRACKER_EFF_PERFORMANCE