// ------------------------------------------------------------------ // ----- 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 "TofTrb3Def.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() { LOG(INFO) << "TMbsMappingTof::Init: Starting ... "<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() ); 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 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 ); 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); } // 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" ); // 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(); } // 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()); return kTRUE; } Bool_t TMbsMappingTof::ClearOutput() { fCbmTofDigiCollection->Clear("C"); return kTRUE; } // ------------------------------------------------------------------ // Mapping // ----- Bit masks ----------------------------------------------------------- const Int_t TMbsMappingTof::fgklTypeMask = ( 1 << TMbsMappingTof::fgkiTypeSize) - 1; const Int_t TMbsMappingTof::fgklBoardMask = ( 1 << TMbsMappingTof::fgkiBoardSize) - 1; const Int_t TMbsMappingTof::fgklChannelMask = ( 1 << TMbsMappingTof::fgkiChannelSize) - 1; const Int_t TMbsMappingTof::fgklEdgeMask = ( 1 << TMbsMappingTof::fgkEdgeSize) - 1; // ----------------------------------------------------------------------------- // ----- Bit shifts -------------------------------------------------------- const Int_t TMbsMappingTof::fgkiTypeOffs = 0; const Int_t TMbsMappingTof::fgkiBoardOffs = TMbsMappingTof::fgkiTypeOffs + TMbsMappingTof::fgkiTypeSize; const Int_t TMbsMappingTof::fgkiChannelOffs = TMbsMappingTof::fgkiBoardOffs + TMbsMappingTof::fgkiBoardSize; const Int_t TMbsMappingTof::fgkiEdgeOffs = TMbsMappingTof::fgkiChannelOffs + TMbsMappingTof::fgkiChannelSize; // ----------------------------------------------------------------------------- Int_t TMbsMappingTof::GetTdcUniqueId( UInt_t uType, UInt_t uBoard, UInt_t uChannel, UInt_t uEdge ) { Int_t iUniqueId = ( ( uType & fgklTypeMask ) << fgkiTypeOffs ) + ( ( uBoard & fgklBoardMask ) << fgkiBoardOffs ) + ( ( uChannel & fgklChannelMask ) << fgkiChannelOffs ) + ( ( uEdge & fgklEdgeMask ) << fgkiEdgeOffs ); return iUniqueId; } Bool_t TMbsMappingTof::MapTdcDataToDet() { if( NULL == fCalibDataCollection || NULL == fCbmTofDigiCollection ) return kFALSE; TTofCalibData * fCalibData; LOG(DEBUG)<<"TMbsMappingTof::MapTdcDataToDet => "<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 ); LOG(DEBUG1)<<"TMbsMappingTof::MapTdcDataToDet ("<< uTdc << ","<< uChan<<") => " << iTdcUId << " => " << iMappedTdcInd <GetMapping( iMappedTdcInd, uChan ); LOG(DEBUG1)<<"TMbsMappingTof::MapTdcDataToDet ("<< uTdc << ","<< uChan<<") => " << iTdcUId << " => " << iMappedTdcInd << " => " << Form(" %08x ",iChanUId) << ", Digi " << fCbmTofDigiCollection->GetEntriesFast() << "," << dTime << "," << dTot <UseDigiExp() ) new((*fCbmTofDigiCollection)[ fCbmTofDigiCollection->GetEntriesFast() ]) CbmTofDigiExp( iChanUId, dTime, dTot ); else new((*fCbmTofDigiCollection)[ fCbmTofDigiCollection->GetEntriesFast() ]) CbmTofDigi( iChanUId, dTime, dTot ); } // if( 0xFFFFFFFF != fMbsMappingPar->GetMapping( iMappedTdcInd, uChan ) ) else LOG(WARNING)<<"TMbsMappingTof::MapTdcDataToDet: Valid Hit on inactive tdc channel (" << uTdc << ","<< uChan<<") " < Incomplete calibrated data found" <<" => Maybe you are using the wrong Tot building mode? fuEdge = "<GetEntriesFast() ; iDataIndex++ ) // Time sort digis fCbmTofDigiCollection->Sort(); }