--- /dev/null
+/*
+CPV PED DA for processing pedestal runs and producing pedestal tables for loading to CPV FEE.
+
+Contact: Sergey Evdokimov <sevdokim@cern.ch>
+Link: https://twiki.cern.ch/twiki/bin/view/ALICE/CPVda
+Reference run: 211758 (/afs/cern.ch/user/s/sevdokim/public/CPV_run211758_pedestal.raw)
+Run Type: PHYSICS
+DA Type: PED
+Number of events needed: 2000 events
+Input files: raw data file
+Output files: thr?_??.dat CpvPeds.root
+Trigger types used: PHYSICS_EVENT
+*/
+
+#include "event.h"
+#include "monitor.h"
+#include "daqDA.h"
+
+#include <Riostream.h>
+#include <stdlib.h>
+#include <fstream>
+#include <string>
+
+//AliRoot
+#include "AliPHOSCpvRawStream.h"
+#include "AliPHOSCpvPedProducer.h"
+#include "AliPHOSCpvParam.h"
+#include "AliRawReaderDate.h"
+#include "AliBitPacking.h"
+#include "TMath.h"
+
+//ROOT
+#include "TROOT.h"
+#include "TPluginManager.h"
+#include "TSAXParser.h"
+#include "TTree.h"
+
+#include "TString.h"
+#include "TFile.h"
+#include "TSystem.h"
+#include "TKey.h"
+#include "TH2S.h"
+#include "TH2F.h"
+#include "TObject.h"
+#include "TBenchmark.h"
+#include "TMath.h"
+#include "TRandom.h"
+
+
+int main( int argc, char **argv )
+{
+ gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
+ "*",
+ "TStreamerInfo",
+ "RIO",
+ "TStreamerInfo()");
+ Int_t status,print;
+ Int_t sigcut=3;
+ Bool_t turbo = kTRUE;
+
+ if (argc!=2) {
+ printf("Wrong number of arguments\n");
+ return -1;
+ }
+
+ // log start of process
+ printf("Cpv DA program started\n");
+
+ /* report progress */
+ daqDA_progressReport(0);
+
+ status=monitorSetDataSource( argv[1] );
+ if (status!=0) {
+ printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
+ return -1;
+ }
+
+ /* declare monitoring program */
+ status=monitorDeclareMp( __FILE__ );
+ if (status!=0) {
+ printf("monitorDeclareMp() failed : %s\n",monitorDecodeError(status));
+ return -1;
+ }
+
+ /* define wait event timeout - 1s max */
+ monitorSetNowait();
+ monitorSetNoWaitNetworkTimeout(1000);
+
+ /* report progress */
+ daqDA_progressReport(5);
+
+
+ // init event counter
+ Int_t iPhysEvnt=0;
+ Int_t iTotEvnt =0;
+
+ AliPHOSCpvPedProducer * pedProducer = new AliPHOSCpvPedProducer(sigcut);
+
+ // Reader
+ AliRawReader * reader;
+
+ /* report progress */
+ daqDA_progressReport(10);
+
+ /* main loop (infinite) */
+ for(;;) { // infinite loop
+ struct eventHeaderStruct *event;
+ eventTypeType eventT;
+
+ /* check shutdown condition */
+ if (daqDA_checkShutdown()) {break;}
+
+ // get next event
+ status=monitorGetEventDynamic((void **)&event);
+ if (status==MON_ERR_EOF) { // end of monitoring file has been reached
+ printf("End of monitoring file has been reached! \n");
+ break;
+ }
+
+ if (status!=0) {
+ printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
+ break;
+ }
+
+ // retry if got no event
+ if (event==NULL) continue;
+
+ // use event - here, just write event id to result file
+ eventT=event->eventType;
+ if (eventT==PHYSICS_EVENT) { //we use PHYSICS_EVENT for pedestal not CALIBRATION_EVENT???
+ iTotEvnt++;
+ reader = new AliRawReaderDate((void*)event);
+ pedProducer->LoadNewEvent(reader);
+ if(pedProducer->FillPedestal()) iPhysEvnt++;
+ delete reader;
+ } // if PHYSICS_EVENT
+
+ free(event);
+
+ /* exit when last event received, no need to wait for TERM signal */
+ if (eventT==END_OF_RUN) {
+ printf("EOR event detected\n");
+ break;
+ }
+ }
+
+ Printf(" Received %d events, %d good events",iTotEvnt,iPhysEvnt);
+ /* report progress */
+ daqDA_progressReport(80);
+
+ for(int iDDL = 0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++){
+ if(pedProducer -> CalcPedestal(iDDL)){
+ pedProducer -> WritePedFiles(iDDL);
+ //for (int iCC = 0; iCC<AliPHOSCpvParam::kNRows){
+ // status=daqDA_DB_storeFile(Form("thr%d_%02d.dat", iDDL, iCC));
+ // if(status) printf("Failed to store thr%d_%02d.dat in DAQ DB!\n",iDDL, iCC);
+ // status=daqDA_FES_storeFile(Form("thr%d_%02d.dat", iDDL, iCC));
+ // if(status) printf("Failed to export thr%d_%02d.dat to DAQ FES!\n",iDDL, iCC);
+ //}
+ }
+ }
+
+ pedProducer->WriteAllHistsToFile("CpvPeds.root");
+ status = daqDA_DB_storeFile("CpvPeds.root","CpvPeds.root");
+ if(status) printf("Failed to store CpvPeds.root in DAQ DB!\n");
+
+ /* report progress */
+ daqDA_progressReport(95);
+
+
+ return status;
+}
--- /dev/null
+#include "AliPHOSCpvPedProducer.h"
+#include "AliPHOSCpvParam.h"
+#include "AliPHOSCpvRawStream.h"
+#include <fstream>
+#include <iostream>
+#include <TTree.h>
+#include <TF1.h>
+#include <TFitResult.h>
+#include <TFitResultPtr.h>
+#include <TSystem.h>
+#include <TTimeStamp.h>
+
+#include "TFile.h"
+
+using namespace std;
+
+using std::ifstream;
+using std::ofstream;
+ClassImp(AliPHOSCpvPedProducer) ;
+
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+AliPHOSCpvPedProducer::AliPHOSCpvPedProducer(Int_t sigcut):
+ TObject(),
+ fSigCut(sigcut),
+ fTurbo(kTRUE),
+ fhErrors(0),
+ fRawStream(0)
+{
+ //
+ //constructor
+ //
+ CreateErrHist();
+} //constructor
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+AliPHOSCpvPedProducer::~AliPHOSCpvPedProducer()
+{
+ //
+ //destructor
+ //
+ for(Int_t iDDL=0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++) {//iDDL
+ delete fPedMeanMap[iDDL];
+ delete fPedSigMap [iDDL];
+ delete f1DPedMean [iDDL];
+ delete f1DPedSigma[iDDL];
+ for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++)
+ for(Int_t iY=1; iY<AliPHOSCpvParam::kPadPcY; iY++)
+ delete fPadAdc[iDDL][iX][iY];
+ }//iDDL
+
+ //delete fhErrors;
+} //destructor
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliPHOSCpvPedProducer::SetTurbo(Bool_t turbo)
+{
+ fTurbo = turbo;
+ if(fRawStream) fRawStream->SetTurbo(fTurbo);
+}
+//--------------------------------------------------------------------------------------
+Bool_t AliPHOSCpvPedProducer::LoadNewEvent(AliRawReader *& rawReader)
+{
+ if(fRawStream) delete fRawStream;
+ fRawStream = new AliPHOSCpvRawStream(rawReader);
+ if(fRawStream) {
+ fRawStream->SetTurbo(fTurbo);
+ return kTRUE;
+ }
+ fhErrors->Fill(0);
+ return kFALSE;
+}
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliPHOSCpvPedProducer::FillPedestal(Int_t abspad,Float_t q)
+{
+ //
+ //Called from the CpvdaFillPedestal() and fills the pedestal values
+ //Arguments: absolute pad number as from AliPHOSCpvParam and q-charge
+ //
+ if(q<0) {
+ AliError("Negative charge is read!!!!!!");
+ return kFALSE;
+ }
+ if(AliPHOSCpvParam::IsValidAbs(abspad) && q>0) {
+ Int_t iDDL=AliPHOSCpvParam::A2DDL(abspad),
+ iX=AliPHOSCpvParam::A2X(abspad),
+ iY=AliPHOSCpvParam::A2Y(abspad);
+ if(!fPedMeanMap[iDDL]) CreateDDLHistos(iDDL);
+ fPadAdc [iDDL][iX][iY] -> Fill(q);
+ return kTRUE;
+ }
+ return kFALSE;
+}//FillPedestal(int,float)
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliPHOSCpvPedProducer::FillPedestal()
+{
+ //
+ //Called from the Cpvda
+ //
+ while(fRawStream->Next()){
+ for(Int_t iPad=0;iPad<fRawStream->GetNPads();iPad++) {
+ Int_t charge = fRawStream->GetChargeArray()[iPad];
+ Int_t aPad = fRawStream -> GetPadArray()[iPad];
+ if(charge){
+ if(!AliPHOSCpvParam::IsValidAbs(aPad)) continue;
+ if(!FillPedestal(aPad, (Float_t)charge)) return kFALSE;
+ }
+ }
+ }
+ return kTRUE;
+}//FillPedestal(TClonesArray*)
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliPHOSCpvPedProducer::CalcPedestal(Int_t iDDL)
+{
+ //
+ //Calculate pedestal for each pad
+ //Arguments: nDDL-DDL number, nEv - number of the read events
+ //Retutns: kTRUE/kFALSE
+ //
+ //cout<<"Now we are going to calculate pedestals"<<endl;
+
+ if(fPedMeanMap[iDDL]){
+ for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++) {
+ for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
+ //cout<<"Ped["<<iX<<"]["<<iY<<"] = " << fPadAdc[iDDL][iX][iY]->GetMean()<<endl;
+ fPedMeanMap[iDDL] -> Fill(iX, iY, fPadAdc[iDDL][iX][iY]->GetMean());
+ fPedSigMap [iDDL] -> Fill(iX, iY, fPadAdc[iDDL][iX][iY]->GetRMS ());
+ f1DPedMean [iDDL] -> Fill(fPadAdc[iDDL][iX][iY]->GetMean());
+ f1DPedSigma[iDDL] -> Fill(fPadAdc[iDDL][iX][iY]->GetRMS ());
+ }
+
+ }
+ return kTRUE;
+ }
+ else return kFALSE;
+}//CaclPedestal()
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliPHOSCpvPedProducer::WriteAllHistsToFile(const char * name) const
+{
+ TFile * rootF = TFile::Open(name,"RECREATE");
+ printf("Root file created \n");
+ //rootF->cd();
+ for(Int_t iDDL=0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++) {
+ // for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++) {
+ // for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
+ // //fPadAdc[iDDL][iX][iY]->Write();
+ // }
+ // }
+ //Printf("iDDL = %d\n", iDDL);
+ if ( fPedMeanMap[iDDL])
+ rootF->WriteObject(fPedMeanMap[iDDL], Form("PedMeanMap%d",iDDL));
+ if ( fPedSigMap[iDDL])
+ rootF->WriteObject(fPedSigMap [iDDL], Form("fPedSigMap%d",iDDL));
+ if ( f1DPedMean[iDDL])
+ rootF->WriteObject(f1DPedMean [iDDL], Form("f1DPedMean%d",iDDL));
+ if ( f1DPedSigma[iDDL])
+ rootF->WriteObject(f1DPedSigma[iDDL], Form("f1DPedSig%d",iDDL));
+ //printf("Write here something \n");
+ }
+ //if(fhErrors) fhErrors -> Write();
+
+ for(Int_t iDDL=0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++)
+ for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++)
+ for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++);
+ //fPadAdc[iDDL][iX][iY]->Write();
+
+ rootF->Close();
+} //WriteAllHistsToFile()
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliPHOSCpvPedProducer::WritePedFiles(Int_t iDDL) const
+{
+ //
+ // Write pedestal files to load them to RCB card
+ // One file per each column controler
+ //
+
+ // accordance to RCB format, pedestals must be written in blocks of 64 lines (not AliPHOSCpvParam::kNPadAdd!)
+ Int_t block = 64;
+
+ //cout<<"pedestal files now will be created!"<< endl;
+
+ if(!fPedMeanMap[iDDL]) {
+ Printf("No pedestals found for DDL %d !\n");
+ return;
+ }
+
+ for(Int_t iCC=0; iCC<AliPHOSCpvParam::kNRows; iCC++) {
+ FILE * pedFile;
+ FILE* pedFileForRCB;
+ pedFile = fopen(Form("thr%d_%02d.dat",iDDL,iCC),"w");
+ if(!pedFile) {
+ Printf("AliPHOSCpvPedProducer::WritePedFiles: Error, file thr%d_%02d.dat could not be open",iDDL,iCC);
+ }
+ // create and initialize arrays for ped and sigmas
+ Int_t ped[AliPHOSCpvParam::kN3GAdd][block],
+ sig[AliPHOSCpvParam::kN3GAdd][block];
+ for(Int_t i3g=0; i3g<AliPHOSCpvParam::kN3GAdd; i3g++) {
+ for(Int_t iPad=0; iPad<block; iPad++) {
+ ped[i3g][iPad] = fMaxThr;
+ sig[i3g][iPad] = 0;
+ }
+ }
+ Int_t iXmin, iXmax;
+ AliPHOSCpvParam::GetLimOfCConX(iCC,iXmin,iXmax);
+ //cout<<iXmin<<iXmax<<endl;
+ for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
+ Int_t g3 = AliPHOSCpvParam::Y23G(iY);
+ for(Int_t iX=iXmin; iX<=iXmax; iX++) {
+ Int_t pad = AliPHOSCpvParam::XY2Pad(iX,iY);
+ ped[g3][pad] = (Int_t) fPadAdc[iDDL][iX][iY]->GetMean();
+ sig[g3][pad] = (Int_t) fPadAdc[iDDL][iX][iY]->GetRMS ();
+ //cout<< "ped is " << fPadAdc[iDDL][iX][iY]->GetMean()<<endl;
+ }
+ }
+
+ // write arrays to file
+ for(Int_t i3g=0; i3g<AliPHOSCpvParam::kN3GAdd; i3g++) {
+ for(Int_t pad=0; pad<block; pad++) {
+ // first 10 bit for pedestal
+ // last 9 bit for fSigCut*sigma
+ Int_t write,writeRCB;
+ if ((ped[i3g][pad] + fSigCut*sig[i3g][pad]) > fMaxThr)
+ write = (fMaxThr<<9) + 0;
+ else
+ write = ((ped[i3g][pad]+fSigCut * sig[i3g][pad])<<9) + fSigCut * sig[i3g][pad];
+
+ fprintf(pedFile, "0x%05x\n", write);
+ }
+ }
+ fclose(pedFile);
+ } // iCC
+} // WritePedFiles(iDDL)
+//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliPHOSCpvPedProducer::CreateErrHist()
+{
+ Int_t nErrors = AliPHOSCpvRawStream::GetNErrors();
+ const char * errNames[nErrors];
+ for(Int_t i=0; i<nErrors; i++) {
+ errNames[i] = AliPHOSCpvRawStream::GetErrName(i);
+ }
+ fhErrors = new TH1I("errorTypes","Errors occured during processing",nErrors+1,0,nErrors+1);
+ TAxis* x = fhErrors->GetXaxis();
+ x->SetBinLabel(1, "Can't get event");
+ for(Int_t i=0; i<nErrors; i++) {
+ x->SetBinLabel(i+2,errNames[i]);
+ }
+}
+//--------------------------------------------------------------------------------------
+void AliPHOSCpvPedProducer::CreateDDLHistos(Int_t iDDL)
+{
+ // creating histograms
+ fPedMeanMap[iDDL] = new TH2F(Form("hPedMeanMap%d",iDDL),"2D mean pedestal map" ,AliPHOSCpvParam::kPadPcX,0.,AliPHOSCpvParam::kPadPcX,AliPHOSCpvParam::kPadPcY,0.,AliPHOSCpvParam::kPadPcY);
+ fPedSigMap [iDDL] = new TH2F(Form("hPedSigMap%d" ,iDDL),"2D pedestal sigma map",AliPHOSCpvParam::kPadPcX,0.,AliPHOSCpvParam::kPadPcX,AliPHOSCpvParam::kPadPcY,0.,AliPHOSCpvParam::kPadPcY);
+ f1DPedMean [iDDL] = new TH1F(Form("h1DPedMean%d" ,iDDL),"1D mean pedestal map" ,5000,0,5000);
+ f1DPedSigma[iDDL] = new TH1F(Form("h1DPedSigma%d",iDDL),"1D pedestal sigma map",100 ,0,100 );
+
+ // initialization of arrays
+ int adr;
+ for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++) {
+ for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
+ adr = AliPHOSCpvParam::XY2A(iDDL,iX,iY);
+ fPadAdc[iDDL][iX][iY] = new TH1F(Form("hPad%d_%d_%d",iDDL,iX,iY),Form("Amplitudes CC=%d, 3Gass=%d, pad=%d",AliPHOSCpvParam::A2CC(adr),AliPHOSCpvParam::A23G(adr),AliPHOSCpvParam::A2Pad(adr)),5000,0,5000);
+ }//iY
+ }//iX
+
+}
--- /dev/null
+#ifndef AliPHOSCpvPedProducer_h
+#define AliPHOSCpvPedProducer_h
+
+// Authors: Mikhail.Stolpovskiy@cern.ch, Sergey.Evdokimov@cern.ch
+// The AliPHOSCpvPedProducer class calculates pedestals using AliPHOSCpvRawStream
+// Also writes pedestals to files
+// And creates a ROOT file with some histograms.
+// this class supposed to be used in Cpv DA programm
+
+#include <TH1.h>
+#include <TH2.h>
+#include <TMath.h>
+#include <TFile.h>
+#include <TString.h>
+#include <THnSparse.h>
+#include "AliPHOSCpvParam.h"
+#include "AliPHOSCpvRawStream.h"
+#include <TClonesArray.h>
+
+class TFile;
+class AliPHOSCpvPedProducer: public TObject {
+
+
+public:
+ AliPHOSCpvPedProducer(Int_t sigcut = 3);
+ virtual ~AliPHOSCpvPedProducer();
+ void SetSigCut(Int_t sigcut = 3) {fSigCut = sigcut;} //set n. of pedestal distribution sigmas used to create zero suppresion table
+ Bool_t LoadNewEvent(AliRawReader *& rawReader); // returns true, if ok
+ void SetTurbo(Bool_t turbo); // if turbo==true then do read without error checking
+ Bool_t GetTurbo() const {return fTurbo;}
+
+ Bool_t FillPedestal(Int_t pad,Float_t q); // pad - absolute pad number; q - charge of the pad
+ Bool_t FillPedestal(); // analyse event and fill pedestals
+ Bool_t CalcPedestal(Int_t iDDL); // analyse pedestals when all events processed for indicated DDL
+
+ TH2F* GetPedMeanMap(Int_t iDDL) const {return fPedMeanMap[iDDL];} //Get the pedestal mean map for a DDL to send to AMORE
+ TH2F* GetPedSigMap(Int_t iDDL) const {return fPedSigMap[iDDL];} //Get the pedestal sigma map for a DDL to send to AMORE
+ TH1F* GetPedMean(Int_t iChFee) const {return f1DPedMean[iChFee];} //Get the pedestal mean map for a FEE channel to send to AMORE
+ TH1F* GetPedSigma(Int_t iChFee) const {return f1DPedSigma[iChFee];}//Get the pedestal Sigma map for a FEE channel to send to AMORE
+ void WritePedFiles(Int_t iDDL) const; // write pedestals to load(?) to RCB for indicated DDL
+ void WriteAllHistsToFile(const char* name) const; // create and write a new root file with hists
+
+ void SetErrorsHist(TH1I * pHist) {fhErrors = new TH1I(*pHist);} //Set the histogram of errors, taken from AliPHOSCpvRawDigiProdicer
+ static Int_t GetMaxThr() {return fMaxThr;} // Get maximal threshold
+
+protected:
+ void CreateErrHist(); // initialize histogram of errors
+ void CreateDDLHistos(Int_t iDDL); // initialize histograms for pedestal calculation and representation
+
+ TH1F *fPadAdc[AliPHOSCpvParam::kNDDL][AliPHOSCpvParam::kPadPcX][AliPHOSCpvParam::kPadPcY]; //Charge distribution for pads
+ Int_t fSigCut; //n. of pedestal distribution sigmas used to create zero suppresion table
+ static const Int_t fMaxThr = 511; //maximal threshold (9 bits all with 1)
+ Bool_t fTurbo; // if true, then read without error checking
+
+ TH2F *fPedMeanMap[AliPHOSCpvParam::kNDDL]; //2D mean pedestal map to export to AMORE
+ TH2F *fPedSigMap [AliPHOSCpvParam::kNDDL]; //2D pedestal sigma map to export to AMORE
+ TH1F *f1DPedMean [AliPHOSCpvParam::kNDDL]; //1D mean pedestal map to export to AMORE
+ TH1F *f1DPedSigma[AliPHOSCpvParam::kNDDL]; //1D pedestal sigma map to export to AMORE
+ TH1I *fhErrors; //histogram of errors from AliPHOSCpvRawDigiProducer
+ AliPHOSCpvRawStream * fRawStream; //! Raw data stream
+private:
+ ClassDef(AliPHOSCpvPedProducer,1); //Cpv calibration and pedestal class
+};
+#endif