//////////////////////////////////// // KRATTA Task merger // for the Asy-Eos experiment // TKratCalibMerger DEFINITION // Mar 2013 // revison 03/2013 // E.d.F ver 1.0 // sebastian.kupny@uj.edu.pl // Changes: //////////////////////////////////// #include "TKratCalibMerger.h" using namespace std; //______________________________________________________________________ TKratCalibMerger::TKratCalibMerger ( ) : FairTask() { } //______________________________________________________________________ TKratCalibMerger::TKratCalibMerger (const char* name, TString runlist, TString pathToCalib, Int_t iVerbose ) : FairTask(name, iVerbose), //fCalibFilesMap(0), // ??? fRunlist(""), fPathToKrattaCalibFiles(""), fInputCalibFile(0), fFileMainTree(0), fFileMainTreeName(""), fNEventsInTree(0), fEntryIndex(0), fMBSTS(0), fRootMBSTSCopy(0), fMbsInfo(0), fMbsInfoClone(0), fTRootKRATEventFromFile(0), fTRootKRATEventFromFileName(""), fTRootKRATEventFromFileCopy(0), fTRootKRATEventFromFileCopyName(""), fMbsRunNo(0), fMbsEventNo(0), fMbsTimeStamp(0), fKrattaCalibRunNo(0), fKrattaCalibEventNo(0), fKrattaCalibTimeStamp(0), fPrintCamacAndCalibTimeStamps(0), fSaveOutputToTree(kFALSE) { fFileMainTreeName = "cbmsim"; fTRootKRATEventFromFileName = "KRATTAEVENT"; fTRootKRATEventFromFileCopyName = "KRATTAEVENT_CLONE"; fMbstsContInName = "MBSTSCLONE"; fMbsinfoContInName = "MBSINFO"; fRunlist = TString ( runlist ); fPathToKrattaCalibFiles = TString ( pathToCalib ); } //______________________________________________________________________ TKratCalibMerger::~TKratCalibMerger() { } //______________________________________________________________________ void TKratCalibMerger::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"); } //______________________________________________________________________ InitStatus TKratCalibMerger::Init() { //fLogger->Info(MESSAGE_ORIGIN," TKratCalibMerger::Init()------------------Start "); //Farroot version: v-13.05 cout << "TKratCalibMerger::Init()------------------Start " << endl; /// Configure Fairroot instances FairRootManager* ioman = FairRootManager::Instance(); if ( ! ioman ) Fatal("Init", "No FairRootManager"); /// Prepare input data if ( fVerbose > 1 ){ cout << "[TKratCalibMerger::Init():] Runlist=" << fRunlist << endl; cout << "[TKratCalibMerger::Init():] PathToKrattaCalibFiles=" << fPathToKrattaCalibFiles << endl; } LoadCalibFilesFromRunlist(); ///Get access to fairroot data source fRootMBSTSCopy = (TClonesArray*) ioman->GetObject( fMbstsContInName ); fMBSTS = (TRootTS*)fRootMBSTSCopy; fMbsInfoClone = (TClonesArray*) ioman->GetObject( fMbsinfoContInName ); fMbsInfo = (TMbsInfo*)fMbsInfoClone; /// Check input data containers: if (fRootMBSTSCopy==NULL){ cout << "[TKratCalibMerger::Init:] Error: Couldn't find necessary input "; cout << "data container with name: " << fMbstsContInName << " (of class TRootTS)."; cout << " It will breaks the analysis." << endl; getchar(); } if (fMbsInfoClone==NULL){ cout << "[TKratCalibMerger::Init:] Error: Couldn't find necessary input "; cout << "data container with name: " << fMbsinfoContInName << " (of class TMbsInfo)."; cout << " It will breaks the analysis." << endl; getchar(); } fTRootKRATEventFromFile = new TRootKRATEvent(); fTRootKRATEventFromFileCopy = new TClonesArray("fTRootKRATEvent"); fTRootKRATEventFromFileCopy = (TClonesArray*) fTRootKRATEventFromFile; ioman->Register( fTRootKRATEventFromFileCopyName.Data(), "Kratta TRootKRATEvent Copy", fTRootKRATEventFromFileCopy, fSaveOutputToTree); cout << "TKratCalibMerger::Init()------------------End " << endl; return kSUCCESS; } //______________________________________________________________________ void TKratCalibMerger::PrintTimeStamps() const { if ( fPrintCamacAndCalibTimeStamps != 2){ cout << "[TKratCalibMerger:] "; cout << "CAMAC:\t["; cout << " RUN=" << fMbsRunNo; cout << ". EVT=" << fMbsEventNo; cout << ". TS MI=" << fMbsTimeStamp; cout << ". TS RT=" << (unsigned long int) fMBSTS->TSM*65535+fMBSTS->TSS; cout << "]\t"; cout << "KRATTA.CALIB:\t["; cout << " RUN=" << fTRootKRATEventFromFile->Run; cout << ". EVT=" << fTRootKRATEventFromFile->Evt; //cout << ". TS=" << fTRootKRATEventFromFile->tms; cout << ". fEntryIndex=" << fEntryIndex; cout << "]" << endl; }else{ cout << "[TKratCalibMerger:] " << endl; cout << "CAMAC:\t\t["; cout << " RUN=" << fMbsRunNo; cout << ". EVT=" << fMbsEventNo; cout << ". TS MI=" << fMbsTimeStamp; cout << ". TS RT=" << (unsigned long int) fMBSTS->TSM*65535+fMBSTS->TSS; cout << "]\t"; cout << endl; cout << "KRATTA.CALIB:\t["; cout << " RUN=" << fTRootKRATEventFromFile->Run; cout << ". EVT=" << fTRootKRATEventFromFile->Evt; //cout << ". TS =" << fTRootKRATEventFromFile->tms; cout << ". fEntryIndex=" << fEntryIndex; cout << "]" << endl; } } //______________________________________________________________________ void TKratCalibMerger::Exec(Option_t* opt) { fMbsRunNo = fMbsInfo->GetRunNumber(); fMbsEventNo = fMbsInfo->GetCamacEventNumber(); ///fMbsTimeStamp = fMbsInfo->GetTimeStamp(); ///SEEMS TO BE NOT FILLED fMbsTimeStamp = fMBSTS->TSM*65535+fMBSTS->TSS; //cout << "fKrattaCalibRunNo = " << fKrattaCalibRunNo << ", fMbsRunNo=" << fMbsRunNo << endl; //getchar(); if (fKrattaCalibRunNo != fMbsRunNo ) /// THIS IS CALLED ONLY WHEN NEW RUN NUMBER HAS CHANGED { if ( fVerbose > 1 ) cout << "[TKratCalibMerger::Exec():] Run number has changed: before= " << fKrattaCalibRunNo << " now: " << fMbsRunNo << " - Loading new file " << endl; LoadFileWithRunNumber( fMbsRunNo ); fKrattaCalibRunNo = fMbsRunNo; /// TO AVOID SITUATION THAT IF THERE IS NO CALIB THIS WILL BE CALLED AFTER EVERY EVENT if ( fVerbose > 2 ){ PrintTimeStamps(); } if ( fFileMainTree ){ fFileMainTree-> GetEntry( 1 ); // Because GetEntry(0) is empty, why? I don't know. fKrattaCalibRunNo = fTRootKRATEventFromFile->Run; fKrattaCalibEventNo = fTRootKRATEventFromFile->Evt; //fKrattaCalibTimeStamp = fTRootKRATEventFromFile->tms; fTRootKRATEventFromFileCopy->Clear(); } //return; /// SKIP THIS EVENT ONE BECAUSE OF SOME STRENGTH BEHAVIOUR } if ( fFileMainTree ){ if ( fEntryIndex < fNEventsInTree ){ /// CHECK IF EVENTS ARE THE NOT THE SAME (THEN SYNCHRHONIZATION IS NEEDED) if ( fMbsEventNo != fKrattaCalibEventNo //|| fMbsTimeStamp != fKrattaCalibTimeStamp ) { ///SYNCHRHONIZATION NEEDED if ( fMbsEventNo > fKrattaCalibEventNo //&& fMbsTimeStamp > fKrattaCalibTimeStamp ) { //cout << "SYNCRHONIZATION NEEDED - COND 1" << endl; if ( fVerbose > 7 ) cout << "[TKratMerger::Exec] Synchronization - Cond. 1" << endl; int skipedKrattaEvents = 0; do{ fFileMainTree-> GetEntry( fEntryIndex ); fKrattaCalibRunNo = fTRootKRATEventFromFile->Run; fKrattaCalibEventNo = fTRootKRATEventFromFile->Evt; //fKrattaCalibTimeStamp = fTRootKRATEventFromFile->tms; skipedKrattaEvents++; fEntryIndex++; }while ( fMbsEventNo > fKrattaCalibEventNo && //fMbsTimeStamp > fKrattaCalibTimeStamp && fEntryIndex < fNEventsInTree - 1 ); if ( fVerbose > 2 && skipedKrattaEvents > 2 ){ cout << "[TKratCalibMerger::Exec] Warning: Jumped over skipedKrattaEvents=" << skipedKrattaEvents << " events " << endl; } } else{ //cout << "SYNCRHONIZATION NEEDED - COND 2" << endl; if ( fVerbose > 7 ) cout << "[TKratMerger::Exec] Synchronization - Cond. 2" << endl; } } /// SYNCHRHONIZATION TEST if ( fMbsRunNo == fTRootKRATEventFromFile->Run && fMbsEventNo == fTRootKRATEventFromFile->Evt //&& fMbsTimeStamp == fTRootKRATEventFromFile->tms ) { // Events have the same number and timestamp //cout << "Same " << endl; //cout << *fTRootKRATEventFromFile << endl; //fKrattaCalibRunNo = fTRootKRATEventFromFile->run; //fKrattaCalibEventNo = fTRootKRATEventFromFile->evt; //fKrattaCalibTimeStamp = fTRootKRATEventFromFile->tms; }else { ///CLEAR THE fTRootKRATEventFromFile structure if possible or use another one /// Eventually use fFileMainTree-> GetEntry( fEntryIndex ); or not use if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::Exec] Warning: Events not same:" << endl; PrintTimeStamps(); if ( fVerbose > 9 ){ cout << "Press any key to continue... " << flush; getchar(); } } // //cout << *fTRootKRATEventFromFile << endl; } /// NOTE: If no one of the above warnings apear /// then here events must be synchronized. if ( fPrintCamacAndCalibTimeStamps > 0 ) { PrintTimeStamps(); } }else{ /// when in KRATTA CALIB (root file) is less events than is in LMD (why?) if ( fVerbose > 2 ) { cout << "[TKratCalibMerger::Exec] Warning: There is no more KRATTA calib. events"; cout << " - no event " << fMbsRunNo << ":" << fMbsEventNo << endl; } fTRootKRATEventFromFileCopy->Clear(); } }else { /// when there is no proper KRATTA DST (root file) (why?) if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::Exec] Warning: The pointer fFileMainTree is not initialized"; cout << " - no proper root file with KRATTA calib. events corresponds to run " << fMbsRunNo; cout << endl; } fTRootKRATEventFromFileCopy->Clear(); } } //______________________________________________________________________ Long64_t TKratCalibMerger::LoadCalibFilesFromRunlist( void ) { if(fRunlist.Length() == 0 ){ cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Error: File name with list of run not set or it is an empty string."<> rDeclaratedNFiles; Long64_t nNumberOfEventsInOneFile = 0; for( Int_t i = 0; i < rDeclaratedNFiles; i++) { fileMainTree = 0; nNumberOfEventsInOneFile = 0; inputFileWithRunList >> inputLmdFileName; if( inputLmdFileName.Length() > 0) { if ( CheckIfFileExist( inputLmdFileName.Data() )){ inputCalibFileName = Form( "%s/%s", fPathToKrattaCalibFiles.Data(), GetCalibFileNameFromFileName( inputLmdFileName ).Data() ); runNumber = GetRunNumberFromFileName( inputLmdFileName ); if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Info: GetCalibFileNameFromFileName(): " << inputCalibFileName << endl; cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Info: GetRunNumberFromFileName(): " << runNumber << endl; } ///NOTE: If something is wrong one can remove bad file from the list. inputCalibFile = new TFile( inputCalibFileName ); if ( inputCalibFile->IsOpen() ){ fileMainTree = (TTree*) inputCalibFile->Get( fFileMainTreeName.Data() ); if ( fileMainTree ){ nNumberOfEventsInOneFile = fileMainTree->GetEntries(); ///fListOfInputFiles.push_back( inputCalibFileName ); fCalibFilesMap[runNumber] = inputCalibFileName; rRealNFiles++; numberOfEventsInAllFiles+=nNumberOfEventsInOneFile; if ( fVerbose > 1 ){ cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] " << inputCalibFileName << " with " << nNumberOfEventsInOneFile; cout << " entry/ies added to the list - [OK]" << endl; } }else{ if ( fVerbose > 1 ){ cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Warning: in file " << inputCalibFileName; cout << " the tree " << fFileMainTreeName.Data() << " not found [BAD]" << endl; } } }else{ if ( fVerbose > 1 ) cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Warning: " << inputCalibFileName << " couldn't open in root [BAD]" << endl; } inputCalibFile-> Close(); delete inputCalibFile; }else{ if ( fVerbose > 1 ) cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Warning: " << inputCalibFileName << " not found [BAD]" << endl; } } } }else{ if ( fVerbose > 1 ) cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Error: could not open the file: " << fRunlist << endl; } inputFileWithRunList.close(); if ( rDeclaratedNFiles != rRealNFiles ){ if ( fVerbose > 1 ){ cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Warning: " << fRunlist << " contains only " << rRealNFiles; cout << " correct files what is less than " << rDeclaratedNFiles << " as you want! Changed " << endl; } rDeclaratedNFiles = rRealNFiles; } if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Files from run list " << fRunlist << " able to analyse:" << endl; PrintLoadedCalibFiles(); } if ( fVerbose > 1 ) cout << "[TKratCalibMerger::LoadCalibFilesFromRunlist():] Total number of events: " << numberOfEventsInAllFiles << endl; return numberOfEventsInAllFiles; } //______________________________________________________________________ void TKratCalibMerger::PrintLoadedCalibFiles() { std::map::iterator it; if ( fCalibFilesMap.size() > 0 ){ cout << "[TKratCalibMerger::PrintLoadedCalibFiles():] Loaded CALIB files:" << endl; for ( it=fCalibFilesMap.begin(); it!=fCalibFilesMap.end(); ++it) std::cout << it->first << " <=> " << it->second << endl; }else{ cout << "[TKratCalibMerger::PrintLoadedCalibFiles():] No files were loaded:" << endl; } if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::PrintLoadedCalibFiles():] fCalibFilesMap.end()->second = " << fCalibFilesMap.end()->second << endl; } } //______________________________________________________________________ TString TKratCalibMerger::GetCalibFileNameFromFileName( TString A_string ) { TString filename = ""; if ( A_string == NULL ){ return filename; ///Throw exception or print warning } if ( A_string.Length() == 0 ){ return filename; ///Throw exception or print warning } string stlString( A_string.Data() ); string fileExtension = ".lmd"; unsigned PositionOfTheFileExtensionInFileName = stlString.find( fileExtension ); unsigned PositionOfTheFile = stlString.find_last_of("/"); string strFileNameWithoutExtension = stlString.substr( PositionOfTheFile + 1, PositionOfTheFileExtensionInFileName - (PositionOfTheFile + 1)); filename = Form ("%s_kratcalib.root", strFileNameWithoutExtension.c_str() ); return filename; } //______________________________________________________________________ Int_t TKratCalibMerger::GetRunNumberFromFileName( TString A_string ) { /// This function extract the run number from the file name. /// The file name pattern is like the follow: /PATH/PREFIX_XXXX.lmd /// And we want extract the XXXX number int runNumber = 0; if ( A_string == NULL){ return runNumber; ///Throw exception or print warning } if ( A_string.Length() == 0){ return runNumber; ///Throw exception or print warning } string stlString( A_string.Data() ); string fileExtension = ".lmd"; int PositionOfTheFileExtensionInFileName = stlString.find( fileExtension ); if (PositionOfTheFileExtensionInFileName < 4 ){ return runNumber; } /// (pos -4, 4) because the run number should consist four digits. string strRunNumber = stlString.substr(PositionOfTheFileExtensionInFileName - 4, 4 ); if (strRunNumber != NULL ) { runNumber = atoi( strRunNumber.c_str() ); if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::GetRunNumberFromFileName:] Extracted from file name " << A_string << " the run number: " << runNumber << endl; } }else{ /// cout << " Warning: Couldn't extract run number from the file name" << endl; /// Throw exception or print warning } return runNumber; } //______________________________________________________________________ Int_t TKratCalibMerger::LoadFileWithRunNumber( Int_t RunNo ) { ///Close the old one Int_t eventsInTree = 0; if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::LoadFileWithRunNumber:] Needed file for run number: " << RunNo << endl; } if ( fInputCalibFile ){ if ( fInputCalibFile -> IsOpen() ){ fInputCalibFile-> Close(); delete fInputCalibFile; fInputCalibFile = 0; /// necessary fFileMainTree = 0; /// necessary } } ///Open next file to analyse TString calibFileName = ""; fEntryIndex = 0; calibFileName = fCalibFilesMap.find( RunNo )->second ; if ( fVerbose > 2 ){ cout << "[TKratCalibMerger::LoadFileWithRunNumber:] CalibFilesMap.find( " << RunNo << " )->second == " << fCalibFilesMap.find( RunNo )->second << endl; cout << "[TKratCalibMerger::LoadFileWithRunNumber:] CalibFileName = " << calibFileName << endl; } if ( calibFileName.Length() > 0 && calibFileName.CompareTo( fCalibFilesMap.end()->second ) != 0 ){ fInputCalibFile = new TFile( calibFileName.Data() ); fFileMainTree = (TTree*) fInputCalibFile->Get( fFileMainTreeName.Data() ); fFileMainTree -> SetBranchAddress( fTRootKRATEventFromFileName.Data() , &fTRootKRATEventFromFile); eventsInTree = fFileMainTree->GetEntries(); if(fVerbose > 1 && eventsInTree > 0){ fFileMainTree->GetEntry(0); cout << "Tree->GetEntry(0): " << fTRootKRATEventFromFile->Run << " " << fTRootKRATEventFromFile->Evt << endl; fFileMainTree->GetEntry(1); cout << "Tree->GetEntry(1)" << fTRootKRATEventFromFile->Run << " " << fTRootKRATEventFromFile->Evt << endl; fFileMainTree->GetEntry(2); cout << "Tree->GetEntry(2)" << fTRootKRATEventFromFile->Run << " " << fTRootKRATEventFromFile->Evt << endl; } fNEventsInTree = eventsInTree; if ( fVerbose > 0 ) cout << "[TKratCalibMerger::LoadFileWithRunNumber:] File " << calibFileName << " opened with " << eventsInTree << " entries." << endl; }else{ if ( fVerbose > 0 ) cout << "[TKratCalibMerger::LoadFileWithRunNumber:] Unable to find KRATTA CALIB file for run " << RunNo << ". No KRATTA data will be stored for this run." << endl; fTRootKRATEventFromFileCopy->Clear(); /// ON CAN CLEAR HERE THE CLONES ARRAY WHICH IS SAVED TO OUTPUT } return eventsInTree; } //______________________________________________________________________ bool TKratCalibMerger::CheckIfFileExist(const char *filename) { bool result = false; ifstream ifile(filename); if (ifile) { result = true; } ifile.close(); return result; } //______________________________________________________________________ void TKratCalibMerger::Reset() { } //______________________________________________________________________ void TKratCalibMerger::Finish() { } ClassImp( TKratCalibMerger )