//////////////////////////////////// // KRATTA SIM KAP reader // for the Asy-Eos experiment // TKratSimKapReader DEFINITION // Feb 2013 // revison 02/2013 // E.d.F ver 1.0 // sebastian.kupny@uj.edu.pl // Description: // Reads simulations results for KrakowArrayPoint class used for simulations // Changes: //////////////////////////////////// #include "TKratSimKapReader.h" using std::cout; using std::endl; //______________________________________________________________________ TKratSimKapReader::TKratSimKapReader (const char* name, TString runlist, Int_t iVerbose ) : FairTask( name, iVerbose ), fCurrentFileNumberOnTheList(0), fNumberOfEventsInAnalysedFiles(0), fEntryIndex(0), fSaveOutputToTree(kFALSE) { fRunlist = runlist; fVerbose = iVerbose; fFileMainTreeName = "cbmsim"; fMCPointName = "KrakowPoint"; fMCPointCopyName = "KrakowPointClone"; fMCTrackName = "MCTrack"; fMCTrackCopyName = "MCTrackClone"; } //______________________________________________________________________ TKratSimKapReader::~TKratSimKapReader() { delete fProgressBar; } //______________________________________________________________________ void TKratSimKapReader::SetParContainers() { // Get run and runtime database FairRunAna* run = FairRunAna::Instance(); if ( ! run ) Fatal("SetParContainers", "No analysis run"); FairRuntimeDb* rtdb = run->GetRuntimeDb(); if ( ! rtdb ) Fatal("SetParContainers", "No runtime database"); } //______________________________________________________________________ Long64_t TKratSimKapReader::GetNumberOfEventsInInputFiles() { return fNumberOfEventsInAllFiles; } //______________________________________________________________________ Long64_t TKratSimKapReader::LoadFilesFromRunlist() { if(fRunlist.Length() == 0 ){ cout << "[TKratSimKapReader::Init():] Errror: File name with list of run not set or it is an empty string."<> fNFiles; Long64_t nNumberOfEventsInOneFile = 0; for( Int_t i = 0; i < fNFiles; i++) { fileMainTree = 0; fInputSimKapFileName = ""; nNumberOfEventsInOneFile = 0; inputFileWithRunList >> fInputSimKapFileName; if( fInputSimKapFileName.Length() > 0) { if ( CheckIfFileExist( fInputSimKapFileName.Data() )){ ///NOTE: If something is wrong one can remove bad file from the list. inputDstFile = new TFile( fInputSimKapFileName ); if ( inputDstFile->IsOpen() ){ fileMainTree = (TTree*) inputDstFile->Get( fFileMainTreeName.Data() ); if ( fileMainTree ){ nNumberOfEventsInOneFile = fileMainTree->GetEntries(); fListOfInputFiles.push_back( fInputSimKapFileName ); rRealNFiles++; fNumberOfEventsInAllFiles+=nNumberOfEventsInOneFile; if ( fVerbose > 1 ){ cout << "[TKratSimKapReader::Init():] " << fInputSimKapFileName << " with " << nNumberOfEventsInOneFile; cout << " entry/ies added to the list - [OK]" << endl; } }else{ if ( fVerbose > 1 ){ cout << "[TKratSimKapReader::Init():] Warning: in file " << fInputSimKapFileName; cout << " the tree " << fFileMainTreeName.Data() << " not found [BAD]" << endl; } } }else{ if ( fVerbose > 1 ) cout << "[TKratSimKapReader::Init():] Warning: " << fInputSimKapFileName << " couldn't open in root [BAD]" << endl; } inputDstFile-> Close(); delete inputDstFile; }else{ if ( fVerbose > 1 ) cout << "[TKratSimKapReader::Init():] Warning: " << fInputSimKapFileName << " not found [BAD]" << endl; } } } }else{ if ( fVerbose > 1 ) cout << "[TKratSimKapReader::Init():] Error: could not open the file: " << fRunlist << endl; } inputFileWithRunList.close(); if ( fNFiles != rRealNFiles ){ if ( fVerbose > 1 ){ cout << "[TKratSimKapReader::Init():] Warning: " << fRunlist << " contains only " << rRealNFiles; cout << " correct files what is less than " << fNFiles << " as you want! Changed " << endl; } fNFiles = rRealNFiles; } if ( fVerbose > 2 ){ cout << "[TKratSimKapReader::Init():] Files from run list " << fRunlist << " able to analyse:" << endl; for(Int_t i = 0; i< fListOfInputFiles.size(); i++) { cout << "[TKratSimKapReader::Init():] ["<< i << "] " << fListOfInputFiles[i] << endl; } } if ( fVerbose > 1 ) cout << "[TKratSimKapReader::Init():] Total number of events: " << fNumberOfEventsInAllFiles << endl; return fNumberOfEventsInAllFiles; } //______________________________________________________________________ InitStatus TKratSimKapReader::Init() { //fLogger->Info(MESSAGE_ORIGIN," TKratSimKapReader::Init()------------------Start "); //Farroot version: v-13.05 cout << "TKratSimKapReader::Init()------------------Start" << endl; /// Read input files if ( fVerbose > 0 ) cout <<"runlist= " << fRunlist << endl; LoadFilesFromRunlist(); if ( fNumberOfEventsInAllFiles < 1 ){ Fatal("Init", "No events to analyse"); } /// Configure Fairroot instances FairRootManager* ioman = FairRootManager::Instance(); if ( ! ioman ) Fatal("Init", "No FairRootManager"); fKapDataCont = new TKratSimKapReaderDataContainer(); fKapDataContClones = new TClonesArray("TKratSimKapReaderDataContainer"); fKapDataContClones = (TClonesArray*) fKapDataCont; ioman->Register( "KrattaMcSimDataCont", "Kratta MC sim data container", fKapDataCont, kTRUE); /// Create an instance of progress bar (before function GetNextDstFile) fProgressBar = new TKratProgressBar( fNumberOfEventsInAllFiles ); /// Open first file to analyse if ( fListOfInputFiles.size() > 0 ){ GetNextDstFile(); } else{ if ( fVerbose > 0 )cout << "[TKratSimKapReader::Init:] Warning: No files found which can be analysd. Finish " << endl; } /// Register objects in Fairroot manager fMCPointCopy = new TClonesArray("KrakowArrayPoint"); fMCPointCopy = (TClonesArray*) fMCPoint; ioman->Register( fMCPointCopyName.Data(), "Kratta MC sim point copy", fMCPointCopy, fSaveOutputToTree); fMCTrackCopy = new TClonesArray("R3BMCTrack"); fMCTrackCopy = (TClonesArray*) fMCTrack; ioman->Register( fMCTrackCopyName.Data(), "Kratta MC sim track copy", fMCTrackCopy, fSaveOutputToTree); cout << "TKratSimKapReader::Init()------------------End" << endl; return kSUCCESS; } //______________________________________________________________________ void TKratSimKapReader::Exec(Option_t* opt) { if ( fFileMainTree ){ if ( fEntryIndex < fNEventsInTree ){ fFileMainTree-> GetEntry( fEntryIndex ); fProgressBar->SetUpdateFrequency(100); /// Debug level 1 or higher. if ( fProgressBarEnabled && fVerbose > 0 ){ fProgressBar->UpdateAndWrite( fNumberOfEventsInAnalysedFiles + fEntryIndex ); } /// Debug level 2 or higher. if ( fVerbose > 4 ){ Int_t nPoints = fMCPoint-> GetEntries(); cout << "Evt=\t" << fEntryIndex << " with \t" << nPoints << " KrakowArrayPoint(s)" << endl; if ( fVerbose > 3 ){ for (Int_t kapi = 0; kapi < nPoints; kapi++ ) { KrakowArrayPoint *krakowArrayPoint; krakowArrayPoint = (KrakowArrayPoint*) fMCPoint->ConstructedAt(kapi); Int_t aDetId = krakowArrayPoint->GetDetectorID(); Int_t aTrackID = krakowArrayPoint->GetTrackID(); Double_t aEnergyLoss = krakowArrayPoint->GetEnergyLoss(); R3BMCTrack *track; track = (R3BMCTrack*) fMCTrack->ConstructedAt(aTrackID); //track = (R3BMCTrack*) (*fMCTrack)[aTrackID]; Int_t aPID = track->GetPdgCode(); Int_t aMother = track->GetMotherId(); Double_t aMctMass = track->GetMass(); Double_t aMctEnergy = track->GetEnergy(); cout << "Particle PID " << aPID << " hit \t Detector Id=" << aDetId << " \t TrackId=" << aTrackID << " \t EnergyLoss=" << aEnergyLoss; cout << endl; } } } /// Debug level 6 or higher. First 1000 events and every 1000th event will be written if ( fVerbose > 5 && ((fNumberOfEventsInAnalysedFiles + fEntryIndex) % 1000 == 0 || (fNumberOfEventsInAnalysedFiles + fEntryIndex) < 1000 )) { //cout << "Global Index=\t" << fNumberOfEventsInAnalysedFiles + fEntryIndex; //cout << ", File Index=\t"<< fEntryIndex << "\t" << *fKrakowArrayPoint << endl; } fEntryIndex++; }else{ ///cout << endl; fNumberOfEventsInAnalysedFiles += fEntryIndex; fEntryIndex = 0; if ( fVerbose > 1 ) cout << "[TKratSimKapReader::Exec:] No more entry in the tree. Analysed events: " << fNumberOfEventsInAnalysedFiles << ", reset index: " << fEntryIndex << endl; GetNextDstFile(); /// ---- Reinit all tasks ---- /// This part of code allows you to reinit all of tasks in this analysis. FairRunAna* runman = FairRunAna::Instance(); if ( ! runman ){ Fatal("Init", "No FairRunAna"); }else{ FairTask *tft = runman->GetMainTask(); //cout << "*tft=" << tft << endl; if ( tft != NULL ){ tft->ReInitTask(); }else{ Fatal("Init", "No MainTask"); } } /// ---- Reinit all tasks - done ---- } }else{ if ( fVerbose > 2 ) cout << "[TKratSimKapReader::Exec:] No more entry in the tree " << endl; //Finish(); FairRunAna::Instance()->TerminateRun(); ///NOTE:Using this makes that the data will be wrong written to file with no possiblilty to open then. } } //______________________________________________________________________ void TKratSimKapReader::Reset() { if ( fVerbose > 1 ) cout << "TKratSimKapReader::Reset()" << endl; // Clear the structure // cout << " -I- Digit Reset() called " << endl; // pc if (fLandDigi ) fLandDigi->Clear(); } //______________________________________________________________________ void TKratSimKapReader::Finish() { cout << endl; if ( fVerbose > 1 ) cout << "TKratSimKapReader::Finish()" << endl; /// TODO: FREE ALL USED MEMORY, FREE POINTERS, ETC. // THIS PART OF CODE CAN BE LAUNCHED MANY TIMES /// OR BETTER PUT THAT TO DESTRUCTOR ?? ///cout << "FINISH" << endl; /// YOUR NEVER RICH THIS POINT HERE BECAUSE OF TerminateRun :) } //______________________________________________________________________ void TKratSimKapReader::GetNextDstFile() { ///Close the old one if ( fInputSimKapFile ){ if ( fInputSimKapFile -> IsOpen() ){ fInputSimKapFile-> Close(); delete fInputSimKapFile; fInputSimKapFile = 0; /// necessary fFileMainTree = 0; } } ///Open next file to analyse if( fCurrentFileNumberOnTheList < fListOfInputFiles.size() ) { fInputSimKapFileName = fListOfInputFiles[ fCurrentFileNumberOnTheList ]; fInputSimKapFile = new TFile( fInputSimKapFileName ); fCurrentFileNumberOnTheList++; fFileMainTree = (TTree*) fInputSimKapFile->Get( fFileMainTreeName.Data() ); fFileMainTree -> SetBranchAddress( fMCPointName.Data() , &fMCPoint); fFileMainTree -> SetBranchAddress( fMCTrackName.Data() , &fMCTrack); fNEventsInTree = fFileMainTree->GetEntries(); std::string stlInputFile ( fInputSimKapFileName.Data()); TString fParticleStringID = "unknown"; Double_t fEkin = atof( (stlInputFile.substr( stlInputFile.find("Ekin") + 4, 4 )).c_str() ); Double_t fMom = atof( (stlInputFile.substr( stlInputFile.find("V_p") + 3, 4 )).c_str() ); /// If particle is unknown then we try get them from filename (will be with sense only for files with form: r3bsim_He3_Ekin0120MeV_p0830MeV.root /// See code below how it works: if( fParticleStringID.CompareTo("unknown") == 0 ){ Int_t particleStringLength = (stlInputFile.find("_Ekin") - (stlInputFile.find("r3bsim_") + 7)); fParticleStringID = stlInputFile.substr( stlInputFile.find("r3bsim_") + 7, particleStringLength ); } fKapDataCont->fEkin = fEkin; fKapDataCont->fMomentum = fMom; fKapDataCont->fParticle = fParticleStringID; fKapDataCont->fNEventsInFile = fNEventsInTree; if ( fVerbose > 2 ){ cout << " fEkin= " << fEkin; cout << " fMom= " << fMom; cout << " fParticleStringID= " << fParticleStringID << endl; cout << " fNEventsInTree= " << fNEventsInTree << endl; cout << " Kap.fEkin= " << fKapDataCont->fEkin; cout << " Kap.fMom= " << fKapDataCont->fMomentum; cout << " Kap.fParticleStringID= " << fKapDataCont->fParticle << endl; cout << " Kap.fNEventsInFile= " << fKapDataCont->fNEventsInFile << endl; //getchar(); } if ( fVerbose > 1 ) cout << "[TKratSimKapReader::GetNextDstFile:] Number of entries in file." << fInputSimKapFileName << ": " << fNEventsInTree << endl; } else { if ( fVerbose > 1 ) cout << "[TKratSimKapReader::GetNextDstFile:] No more files." << endl; } } //______________________________________________________________________ bool TKratSimKapReader::CheckIfFileExist(const char *filename) { bool result = false; ifstream ifile(filename); if (ifile) { result = true; } ifile.close(); return result; } //______________________________________________________________________ InitStatus TKratSimKapReader::ReInit() { if ( fVerbose > 1 ) cout << "[TKratSimKapReader::ReInit:] ReInit is calling" << endl; return kSUCCESS; } ClassImp( TKratSimKapReader )