// ------------------------------------------------------------------ // ----- TMbsMappingTof ----- // ----- Created 09/06/2013 by P.-A. Loizeau ----- // ------------------------------------------------------------------ #include "TMbsMappingTof.h" // General Unpack headers #include "TMbsUnpackTofPar.h" // ToF specific headers #include "TMbsMappingTofPar.h" #include "TMbsCalibTofPar.h" #include "TofTdcDef.h" #include "TofCaenDef.h" #include "TofVftxDef.h" #include "TofTrbTdcDef.h" #include "TofGet4Def.h" #include "TTofTdcBoard.h" #include "TTofTdcData.h" #include "TTofCalibData.h" #include "CbmTofAddress.h" #include "CbmTofDigiExp.h" #include "CbmTofDigi.h" #include "TTofTriglogBoard.h" // FAIR headers #include "FairLogger.h" #include "FairRunAna.h" #include "FairRuntimeDb.h" #include "FairRootManager.h" // ROOT headers #include "TClonesArray.h" #include "TH2.h" #include "TH1.h" #include "TROOT.h" #include "TTimeStamp.h" // C++ headers #include TMbsMappingTof::TMbsMappingTof() : FairTask("MbsCalibTof"), fMbsUnpackPar(NULL), fMbsCalibPar(NULL), fMbsMappingPar(NULL), fCalibDataCollection(NULL), fCbmTofDigiCollection(NULL) { } TMbsMappingTof::TMbsMappingTof(const char* name, Int_t mode, Int_t verbose) : FairTask(name, verbose), fMbsUnpackPar(0), fMbsCalibPar(NULL), fMbsMappingPar(NULL), fCalibDataCollection(NULL), fCbmTofDigiCollection(NULL) { } TMbsMappingTof::~TMbsMappingTof() { DeleteHistograms(); LOG(INFO)<<"**** TMbsMappingTof: Delete instance "<printParams(); } InitStatus TMbsMappingTof::Init() { if( kFALSE == InitParameters() ) return kFATAL; fMbsMappingPar->printParams(); if( kFALSE == CreateHistogramms() ) return kFATAL; if( kFALSE == RegisterInput() ) return kFATAL; // Create the proper output objects for each detector if( kFALSE == RegisterOutput() ) return kFATAL; return kSUCCESS; } void TMbsMappingTof::Exec(Option_t* option) { ClearOutput(); // Event rejection // E.g.: pulser event used to Empty the VFTX buffers if( 1 == fMbsUnpackPar->OnlyOneTriglog() && -1 < fMbsUnpackPar->GetTriggerToReject() ) { // if( NULL == fTriglogBoardCollection ) // return kFALSE; TTofTriglogBoard * xTriglogBoard = (TTofTriglogBoard*) fTriglogBoardCollection->At(0); // Always only 1 TRIGLOG board! // if( NULL == xTriglogBoard ) // return kFALSE; // if( 1 == ( 0x1 & ( (xTriglogBoard->GetTriggPatt()) >> (fMbsUnpackPar->GetTriggerToReject()) ) ) ) // Reject selected trigger only when alone (accept when both not rejected trigger and rejected trigger are present) if( (xTriglogBoard->GetTriggPatt()) == ( 1 << (fMbsUnpackPar->GetTriggerToReject()) ) ) // Jump this event ! return; } // if trigger rejection enabled and trigger board enabled MapTdcDataToDet(); FillHistograms(); } void TMbsMappingTof::Finish() { WriteHistogramms(); } // ------------------------------------------------------------------ Bool_t TMbsMappingTof::InitParameters() { // Get Base Container FairRun* ana = FairRun::Instance(); FairRuntimeDb* rtdb=ana->GetRuntimeDb(); // Unpack parameter fMbsUnpackPar = (TMbsUnpackTofPar*) (rtdb->getContainer("TMbsUnpackTofPar")); if( 0 == fMbsUnpackPar ) { LOG(ERROR)<<"TMbsMappingTof::InitParameters => Could not obtain the TMbsUnpackTofPar "<getContainer("TMbsCalibTofPar")); if( 0 == fMbsCalibPar ) { LOG(ERROR)<<"TMbsMappingTof::InitParameters => Could not obtain the TMbsCalibTofPar "<getContainer("TMbsMappingTofPar")); if( 0 == fMbsMappingPar ) { LOG(ERROR)<<"TMbsMappingTof::InitParameters => Could not obtain the TMbsMappingTofPar "<initContainers( ana->GetRunId() ); return kTRUE; } // ------------------------------------------------------------------ Bool_t TMbsMappingTof::CreateHistogramms() { TDirectory * oldir = gDirectory; // <= To prevent histos from being sucked in by the param file of the TRootManager! gROOT->cd(); // <= To prevent histos from being sucked in by the param file of the TRootManager ! if( 0 < fMbsMappingPar->GetNbMappedDet() ) { fhDetChEvtMul.resize( fMbsMappingPar->GetNbMappedDet() ); fhDetChTotMul.resize( fMbsMappingPar->GetNbMappedDet() ); fhDetChHitsPerEvt.resize( 2*fMbsMappingPar->GetNbMappedDet() ); fhDetChCoincSides.resize( fMbsMappingPar->GetNbMappedDet() ); fhDetChCoincLeft.resize( fMbsMappingPar->GetNbMappedDet() ); fhDetChCoincRight.resize( fMbsMappingPar->GetNbMappedDet() ); fviNbHitInThisEvent.resize( fMbsMappingPar->GetNbMappedDet() ); for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) { Int_t iUniqueId = fMbsMappingPar->GetMappedDetUId( iDetIndx ); Int_t iSmType = CbmTofAddress::GetSmType( iUniqueId ); Int_t iSmId = CbmTofAddress::GetSmId( iUniqueId ); Int_t iRpcId = CbmTofAddress::GetRpcId( iUniqueId ); fhDetChEvtMul[iDetIndx] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_ch_evt_mul", iSmType, iSmId, iRpcId ), Form("Number of events with hits for each channel of Rpc #%03d in Sm %03d of type %d; Channel []; Side []; Events with hits[]", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), 2, 0, 2 ); fhDetChTotMul[iDetIndx] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_ch_tot_mul", iSmType, iSmId, iRpcId ), Form("Number of hits for each channel of Rpc #%03d in Sm %03d of type %d; Channel []; Side []; Hits []", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), 2, 0, 2 ); fhDetChHitsPerEvt[2*iDetIndx] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_ch_left_hitsperevt", iSmType, iSmId, iRpcId ), Form("Number of hits per event for the left side each channel of Rpc #%03d in Sm %03d of type %d; Channel []; Nb hits per event []; Events []", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), 10, 0, 10 ); fhDetChHitsPerEvt[2*iDetIndx + 1] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_ch_right_hitsperevt", iSmType, iSmId, iRpcId ), Form("Number of hits per event for the right side each channel of Rpc #%03d in Sm %03d of type %d; Channel []; Nb hits per event []; Events []", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), 10, 0, 10 ); fhDetChCoincSides[iDetIndx] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_coinc_sides", iSmType, iSmId, iRpcId ), Form("Number of events with hits for both channels of Rpc #%03d in Sm %03d of type %d; Channel left []; Channel right []; Events with hits in both[]", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType)); fhDetChCoincLeft[iDetIndx] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_coinc_left", iSmType, iSmId, iRpcId ), Form("Number of events with hits for both channels of Rpc #%03d in Sm %03d of type %d; Channel left 1 []; Channel left 2 []; Events with hits in both[]", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType)); fhDetChCoincRight[iDetIndx] = new TH2I( Form("tof_ty%01d_sm%03d_rpc%03d_coinc_right", iSmType, iSmId, iRpcId ), Form("Number of events with hits for both channels of Rpc #%03d in Sm %03d of type %d; Channel right 1 []; Channel right 2 []; Events with hits in both[]", iRpcId, iSmId, iSmType ), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType), fMbsMappingPar->GetSmTypeNbCh(iSmType), 0, fMbsMappingPar->GetSmTypeNbCh(iSmType)); fviNbHitInThisEvent[iDetIndx].resize( 2*fMbsMappingPar->GetSmTypeNbCh(iSmType), 0 ); } // for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) } // if( 0 < fMbsMappingPar->GetNbMappedDet() ) gDirectory->cd( oldir->GetPath() ); // <= To prevent histos from being sucked in by the param file of the TRootManager! return kTRUE; } Bool_t TMbsMappingTof::FillHistograms() { if( 0 != FairRunAna::Instance() ) { // Get Base Container <- Needed because currently RunOnLmd runs 1 more time after setting end of file flag!!!!! // Should not be a problem when using the FAIRROOT way of reading MBS event instead of the custom "Go4 like" way FairRunAna* ana = FairRunAna::Instance(); // <- Needed because currently RunOnLmd runs 1 more time after setting end of file flag!!!!!! // Needed because currently RunOnLmd runs 1 more time after setting end of file flag!!!!!! if( kTRUE == ana->GetLMDProcessingStatus() ) return kFALSE; } // if( 0 != FairRunAna::Instance() ) if( 0 < fMbsMappingPar->GetNbMappedDet() ) { for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) std::fill( fviNbHitInThisEvent[iDetIndx].begin(), fviNbHitInThisEvent[iDetIndx].end(), 0 ); Int_t iAddress = 0xFFFFFFFF; Int_t iMappedDet = -1; Int_t iSmType = -1; Int_t iChan = -1; Int_t iSide = -1; // loop over Mapped Data for( Int_t iDigiIndex = 0; iDigiIndex < fCbmTofDigiCollection->GetEntriesFast() ; iDigiIndex++ ) { if( kTRUE == fMbsMappingPar->UseDigiExp() ) { CbmTofDigiExp * xDigiPtr = (CbmTofDigiExp *)fCbmTofDigiCollection->At( iDigiIndex ); iAddress = xDigiPtr->GetAddress(); iMappedDet = fMbsMappingPar->GetMappedDetInd( iAddress ); if( -1 < iMappedDet ) { iSmType = xDigiPtr->GetType(); iChan = xDigiPtr->GetChannel(); iSide = xDigiPtr->GetSide(); fviNbHitInThisEvent[iMappedDet][ iSide*fMbsMappingPar->GetSmTypeNbCh(iSmType) + iChan] ++; fhDetChTotMul[iMappedDet]->Fill( iChan, iSide); } // if( -1 < iMappedDet ) } // if( kTRUE == fMbsMappingPar->UseDigiExp() ) else { CbmTofDigi * xDigiPtr = (CbmTofDigi *)fCbmTofDigiCollection->At( iDigiIndex ); iAddress = xDigiPtr->GetAddress(); iMappedDet = fMbsMappingPar->GetMappedDetInd( iAddress ); if( -1 < iMappedDet ) { iSmType = xDigiPtr->GetType(); iChan = xDigiPtr->GetChannel(); iSide = xDigiPtr->GetSide(); fviNbHitInThisEvent[iMappedDet][ iSide*fMbsMappingPar->GetSmTypeNbCh(iSmType) + iChan] ++; fhDetChTotMul[iMappedDet]->Fill( iChan, iSide); } // if( -1 < iMappedDet ) } // else of if( kTRUE == fMbsMappingPar->UseDigiExp() ) } // for( Int_t iDigiIndex = 0; iDigiIndex < fCbmTofDigiCollection->GetEntriesFast() ; iDigiIndex++ ) for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) { Int_t iUniqueId = fMbsMappingPar->GetMappedDetUId( iDetIndx ); iSmType = CbmTofAddress::GetSmType( iUniqueId ); for( Int_t iChIndx = 0; iChIndx < fMbsMappingPar->GetSmTypeNbCh(iSmType); iChIndx++) { // Hits per events fhDetChHitsPerEvt[2*iDetIndx]->Fill( iChIndx, fviNbHitInThisEvent[iDetIndx][ iChIndx ]); fhDetChHitsPerEvt[2*iDetIndx+1]->Fill( iChIndx, fviNbHitInThisEvent[iDetIndx][ fMbsMappingPar->GetSmTypeNbCh(iSmType) +iChIndx ]); // Events with at least one hit if( 0 < fviNbHitInThisEvent[iDetIndx][ iChIndx ] ) fhDetChEvtMul[iDetIndx]->Fill( iChIndx, 0); if( 0 < fviNbHitInThisEvent[iDetIndx][ fMbsMappingPar->GetSmTypeNbCh(iSmType) + iChIndx ] ) fhDetChEvtMul[iDetIndx]->Fill( iChIndx, 1); // Events with at least one hit in both channels = coincidences for( Int_t iChIndx2 = 0 ; iChIndx2 < fMbsMappingPar->GetSmTypeNbCh(iSmType); iChIndx2++) { if( 0 < fviNbHitInThisEvent[iDetIndx][ iChIndx ] && 0 < fviNbHitInThisEvent[iDetIndx][ fMbsMappingPar->GetSmTypeNbCh(iSmType) + iChIndx2 ]) fhDetChCoincSides[iDetIndx]->Fill( iChIndx, iChIndx2 ); if( iChIndx < iChIndx2 && 0 < fviNbHitInThisEvent[iDetIndx][ iChIndx ] && 0 < fviNbHitInThisEvent[iDetIndx][ iChIndx2 ]) fhDetChCoincLeft[iDetIndx]->Fill( iChIndx, iChIndx2 ); if( iChIndx < iChIndx2 && 0 < fviNbHitInThisEvent[iDetIndx][ fMbsMappingPar->GetSmTypeNbCh(iSmType) + iChIndx ] && 0 < fviNbHitInThisEvent[iDetIndx][ fMbsMappingPar->GetSmTypeNbCh(iSmType) + iChIndx2 ]) fhDetChCoincRight[iDetIndx]->Fill( iChIndx, iChIndx2 ); } // for( Int_t iChIndx2 = iChIndx ; iChIndx2 < fMbsMappingPar->GetSmTypeNbCh(iSmType); iChIndx2++) } // for all Det/Ch pairs } // for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) } // if( 0 < fMbsMappingPar->GetNbMappedDet() ) return kTRUE; } void TMbsMappingTof::WriteHistogramms() { TDirectory * oldir = gDirectory; TFile *fHist = new TFile("./tofMbsMap.hst.root","RECREATE"); // TDirectory* inDir = (TDirectory *)fHist; if( 0 < fMbsMappingPar->GetNbMappedDet() ) { // create a subdirectory for each histogram type in this file TDirectory *cdMapEvt = fHist->mkdir( "Map_Evt" ); TDirectory *cdMapTot = fHist->mkdir( "Map_Total" ); TDirectory *cdMapHitsPerEvt = fHist->mkdir( "Map_HitsPerEvt" ); TDirectory *cdMapCoincSides = fHist->mkdir( "Map_coincSide" ); TDirectory *cdMapCoincLeft = fHist->mkdir( "Map_coincLeft" ); TDirectory *cdMapCoincRight = fHist->mkdir( "Map_coincRight" ); // loop over all mapped Detectors for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) { // Nb Events with hits cdMapEvt->cd(); fhDetChEvtMul[iDetIndx]->Write(); // Total Nb hits per channel/side cdMapTot->cd(); fhDetChTotMul[iDetIndx]->Write(); // Hits per events cdMapHitsPerEvt->cd(); fhDetChHitsPerEvt[2*iDetIndx]->Write(); fhDetChHitsPerEvt[2*iDetIndx+1]->Write(); cdMapCoincSides->cd(); fhDetChCoincSides[iDetIndx]->Write(); cdMapCoincLeft->cd(); fhDetChCoincLeft[iDetIndx]->Write(); cdMapCoincRight->cd(); fhDetChCoincRight[iDetIndx]->Write(); } // for( Int_t iDetIndx = 0; iDetIndx < fMbsMappingPar->GetNbMappedDet(); iDetIndx++ ) } // if( 0 < fMbsMappingPar->GetNbMappedDet() ) gDirectory->cd( oldir->GetPath() ); fHist->Close(); } void TMbsMappingTof::DeleteHistograms() { } // ------------------------------------------------------------------ Bool_t TMbsMappingTof::RegisterInput() { FairRootManager* rootMgr = FairRootManager::Instance(); if( 0 < fMbsUnpackPar->GetNbActiveBoards() ) { fCalibDataCollection = (TClonesArray*) rootMgr->GetObject("TofCalibData"); if( NULL == fCalibDataCollection) { LOG(ERROR)<<"TMbsMappingTof::RegisterInput => Could not get the TofCalibData TClonesArray!!!"<GetNbActiveBoards() ) // Event rejection // E.g.: pulser event used to Empty the VFTX buffers if( 1 == fMbsUnpackPar->OnlyOneTriglog() && -1 < fMbsUnpackPar->GetTriggerToReject() ) { LOG(INFO)<<"TMbsMappingTof::RegisterInput => Load the TofTriglog TClonesArray for trigger rejection!!!"<GetObject("TofTriglog"); if( NULL == fTriglogBoardCollection) { LOG(ERROR)<<"TMbsMappingTof::RegisterInput => Could not get the TofTriglog TClonesArray!!!"<UseDigiExp() ) fCbmTofDigiCollection = new TClonesArray("CbmTofDigiExp"); else fCbmTofDigiCollection = new TClonesArray("CbmTofDigi"); // rootMgr->Register("CbmTofDigi","Tof",fCbmTofDigiCollection, kTRUE); rootMgr->Register( "CbmTofDigi","Tof",fCbmTofDigiCollection, fMbsUnpackPar->WriteDataInCbmOut() || fbSaveMappedDigis ); return kTRUE; } Bool_t TMbsMappingTof::ClearOutput() { fCbmTofDigiCollection->Clear("C"); return kTRUE; } void TMbsMappingTof::SetSaveDigis( Bool_t bSaveDigis ) { fbSaveMappedDigis = bSaveDigis; LOG(INFO)<<"TMbsMappingTof => Enable the saving of mapped digis in analysis output file" < "<GetEntriesFast() <<" data calibrated to be mapped in this event!"<GetEntriesFast() ; iDataIndex++ ) { fCalibData = (TTofCalibData *)fCalibDataCollection->At( iDataIndex ); UInt_t uType = fCalibData->GetType(); UInt_t uTdc = fCalibData->GetBoard(); UInt_t uChan = fCalibData->GetChannel(); Double_t dTime = fCalibData->GetTime(); Double_t dTot = fCalibData->GetTot(); UInt_t uEdge = fCalibData->GetEdge(); Int_t iTdcUId = GetTdcUniqueId( uType, uTdc ); Int_t iMappedTdcInd = fMbsMappingPar->GetMappedTdcInd( iTdcUId ); // First Check if this TDC is in the mapping and if the calibrated hit is complete if( -1 < iMappedTdcInd && 2 == uEdge ) { Int_t iChanUId = fMbsMappingPar->GetMapping( iMappedTdcInd, uChan ); if( 0xFFFFFFFF != iChanUId ) { // Create Digi and store it if( kTRUE == fMbsMappingPar->UseDigiExp() ) new((*fCbmTofDigiCollection)[ fCbmTofDigiCollection->GetEntriesFast() ]) CbmTofDigiExp( iChanUId, dTime, dTot ); else new((*fCbmTofDigiCollection)[ fCbmTofDigiCollection->GetEntriesFast() ]) CbmTofDigi( iChanUId, dTime, dTot ); } // if( 0xFFFFFFFF != fMbsMappingPar->GetMapping( iMappedTdcInd, uChan ) ) } // if( -1 < iMappedTdcInd && 2 == uEdge ) else if( uEdge < 2) LOG(WARNING)<<"TMbsCalibTof::MapTdcDataToDet => Incomplete calibrated data found" <<" => Maybe you are using the wrong Tot building mode? fuEdge = "<GetEntriesFast() ; iDataIndex++ ) // Time sort digis fCbmTofDigiCollection->Sort(); }