PHOS: CPV PED DA added
[u/mrichter/AliRoot.git] / PHOS / PHOSbase / AliPHOSCpvPedProducer.cxx
1 #include "AliPHOSCpvPedProducer.h"
2 #include "AliPHOSCpvParam.h"
3 #include "AliPHOSCpvRawStream.h"
4 #include <fstream>
5 #include <iostream>
6 #include <TTree.h>
7 #include <TF1.h>
8 #include <TFitResult.h>
9 #include <TFitResultPtr.h>
10 #include <TSystem.h>
11 #include <TTimeStamp.h>
12
13 #include "TFile.h"
14
15 using namespace std;
16
17 using std::ifstream;
18 using std::ofstream;
19 ClassImp(AliPHOSCpvPedProducer) ;
20
21 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 AliPHOSCpvPedProducer::AliPHOSCpvPedProducer(Int_t sigcut):
23   TObject(),
24   fSigCut(sigcut),
25   fTurbo(kTRUE),
26   fhErrors(0),
27   fRawStream(0)
28 {
29   //
30   //constructor
31   //
32   CreateErrHist();
33 }  //constructor
34 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
35 AliPHOSCpvPedProducer::~AliPHOSCpvPedProducer()
36 {
37   //
38   //destructor
39   //
40   for(Int_t iDDL=0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++) {//iDDL
41     delete fPedMeanMap[iDDL];
42     delete fPedSigMap [iDDL];
43     delete f1DPedMean [iDDL];
44     delete f1DPedSigma[iDDL];
45     for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++)
46       for(Int_t iY=1; iY<AliPHOSCpvParam::kPadPcY; iY++)
47         delete fPadAdc[iDDL][iX][iY];
48   }//iDDL
49
50   //delete fhErrors;
51 }  //destructor
52 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53 void AliPHOSCpvPedProducer::SetTurbo(Bool_t turbo)
54 {
55   fTurbo = turbo;
56   if(fRawStream) fRawStream->SetTurbo(fTurbo);
57 }
58 //--------------------------------------------------------------------------------------
59 Bool_t AliPHOSCpvPedProducer::LoadNewEvent(AliRawReader *& rawReader)
60 {
61   if(fRawStream) delete fRawStream;
62   fRawStream = new AliPHOSCpvRawStream(rawReader);
63   if(fRawStream) {
64     fRawStream->SetTurbo(fTurbo);
65     return kTRUE;
66   }
67   fhErrors->Fill(0);
68   return kFALSE;
69 }
70 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
71 Bool_t AliPHOSCpvPedProducer::FillPedestal(Int_t abspad,Float_t q)
72 {
73   //
74   //Called from the CpvdaFillPedestal() and fills the pedestal values
75   //Arguments: absolute pad number as from AliPHOSCpvParam and q-charge
76   //
77   if(q<0) {
78    AliError("Negative charge is read!!!!!!");
79    return kFALSE;
80   }
81   if(AliPHOSCpvParam::IsValidAbs(abspad) && q>0) {
82     Int_t iDDL=AliPHOSCpvParam::A2DDL(abspad),
83             iX=AliPHOSCpvParam::A2X(abspad),
84             iY=AliPHOSCpvParam::A2Y(abspad);
85     if(!fPedMeanMap[iDDL]) CreateDDLHistos(iDDL);
86     fPadAdc [iDDL][iX][iY] -> Fill(q);
87     return kTRUE;
88   }
89   return kFALSE;
90 }//FillPedestal(int,float)
91 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
92 Bool_t AliPHOSCpvPedProducer::FillPedestal()
93 {
94   //
95   //Called from the Cpvda
96   //
97   while(fRawStream->Next()){
98     for(Int_t iPad=0;iPad<fRawStream->GetNPads();iPad++) {
99       Int_t charge = fRawStream->GetChargeArray()[iPad];
100       Int_t aPad = fRawStream -> GetPadArray()[iPad];
101       if(charge){
102         if(!AliPHOSCpvParam::IsValidAbs(aPad)) continue;
103         if(!FillPedestal(aPad, (Float_t)charge)) return kFALSE;
104       }
105     }
106   }
107   return kTRUE;
108 }//FillPedestal(TClonesArray*)
109 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
110 Bool_t AliPHOSCpvPedProducer::CalcPedestal(Int_t iDDL)
111 {
112   //
113   //Calculate pedestal for each pad
114   //Arguments: nDDL-DDL number, nEv - number of the read events
115   //Retutns: kTRUE/kFALSE
116   //
117   //cout<<"Now we are going to calculate pedestals"<<endl;
118
119   if(fPedMeanMap[iDDL]){
120     for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++) {
121       for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
122         //cout<<"Ped["<<iX<<"]["<<iY<<"] = " << fPadAdc[iDDL][iX][iY]->GetMean()<<endl;
123         fPedMeanMap[iDDL] -> Fill(iX, iY, fPadAdc[iDDL][iX][iY]->GetMean());
124         fPedSigMap [iDDL] -> Fill(iX, iY, fPadAdc[iDDL][iX][iY]->GetRMS ());
125         f1DPedMean [iDDL] -> Fill(fPadAdc[iDDL][iX][iY]->GetMean());
126         f1DPedSigma[iDDL] -> Fill(fPadAdc[iDDL][iX][iY]->GetRMS ());
127       }
128
129     }
130     return kTRUE;
131   }
132   else return kFALSE;
133 }//CaclPedestal()
134 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135 void AliPHOSCpvPedProducer::WriteAllHistsToFile(const char * name) const
136 {
137   TFile * rootF = TFile::Open(name,"RECREATE");
138   printf("Root file created \n");
139   //rootF->cd();
140   for(Int_t iDDL=0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++) {
141     // for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++) {
142     //   for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
143     //  //fPadAdc[iDDL][iX][iY]->Write();
144     //   }
145     // }
146         //Printf("iDDL = %d\n", iDDL);
147     if ( fPedMeanMap[iDDL])
148       rootF->WriteObject(fPedMeanMap[iDDL], Form("PedMeanMap%d",iDDL));
149     if ( fPedSigMap[iDDL])
150       rootF->WriteObject(fPedSigMap [iDDL], Form("fPedSigMap%d",iDDL));
151     if ( f1DPedMean[iDDL])
152       rootF->WriteObject(f1DPedMean [iDDL], Form("f1DPedMean%d",iDDL));
153     if ( f1DPedSigma[iDDL])
154       rootF->WriteObject(f1DPedSigma[iDDL], Form("f1DPedSig%d",iDDL));
155     //printf("Write here something \n");
156   }
157   //if(fhErrors) fhErrors -> Write();
158
159   for(Int_t iDDL=0; iDDL<AliPHOSCpvParam::kNDDL; iDDL++)
160     for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++)
161       for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++);
162   //fPadAdc[iDDL][iX][iY]->Write();
163
164   rootF->Close();
165 } //WriteAllHistsToFile()
166 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
167 void AliPHOSCpvPedProducer::WritePedFiles(Int_t iDDL) const
168 {
169   //
170   // Write pedestal files to load them to RCB card
171   // One file per each column controler
172   //
173
174   // accordance to RCB format, pedestals must be written in blocks of 64 lines (not AliPHOSCpvParam::kNPadAdd!)
175   Int_t block = 64;
176
177   //cout<<"pedestal files now will be created!"<< endl;
178
179   if(!fPedMeanMap[iDDL]) {
180     Printf("No pedestals found for DDL %d !\n");
181     return;
182   }
183
184   for(Int_t iCC=0; iCC<AliPHOSCpvParam::kNRows; iCC++) {
185     FILE * pedFile;
186     FILE* pedFileForRCB;
187     pedFile = fopen(Form("thr%d_%02d.dat",iDDL,iCC),"w");
188     if(!pedFile) {
189       Printf("AliPHOSCpvPedProducer::WritePedFiles: Error, file thr%d_%02d.dat could not be open",iDDL,iCC);
190     }
191     // create and initialize arrays for ped and sigmas
192     Int_t ped[AliPHOSCpvParam::kN3GAdd][block],
193       sig[AliPHOSCpvParam::kN3GAdd][block];
194     for(Int_t i3g=0; i3g<AliPHOSCpvParam::kN3GAdd; i3g++) {
195       for(Int_t iPad=0; iPad<block; iPad++) {
196         ped[i3g][iPad] = fMaxThr;
197         sig[i3g][iPad] = 0;
198       }
199     }
200     Int_t iXmin, iXmax;
201     AliPHOSCpvParam::GetLimOfCConX(iCC,iXmin,iXmax);
202     //cout<<iXmin<<iXmax<<endl;
203     for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
204       Int_t g3 = AliPHOSCpvParam::Y23G(iY);
205       for(Int_t iX=iXmin; iX<=iXmax; iX++) {
206         Int_t pad = AliPHOSCpvParam::XY2Pad(iX,iY);
207         ped[g3][pad] = (Int_t) fPadAdc[iDDL][iX][iY]->GetMean();
208         sig[g3][pad] = (Int_t) fPadAdc[iDDL][iX][iY]->GetRMS ();
209         //cout<< "ped is " << fPadAdc[iDDL][iX][iY]->GetMean()<<endl;
210       }
211     }
212
213     // write arrays to file
214     for(Int_t i3g=0; i3g<AliPHOSCpvParam::kN3GAdd; i3g++) {
215       for(Int_t pad=0; pad<block; pad++) {
216         // first 10 bit for pedestal
217         // last 9 bit for fSigCut*sigma
218         Int_t write,writeRCB;
219         if ((ped[i3g][pad] + fSigCut*sig[i3g][pad]) > fMaxThr)
220           write = (fMaxThr<<9) + 0;
221         else
222           write = ((ped[i3g][pad]+fSigCut * sig[i3g][pad])<<9) + fSigCut * sig[i3g][pad];
223
224         fprintf(pedFile, "0x%05x\n", write);
225       }
226     }
227     fclose(pedFile);
228   } // iCC
229 } // WritePedFiles(iDDL)
230 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
231 void AliPHOSCpvPedProducer::CreateErrHist()
232 {
233   Int_t nErrors = AliPHOSCpvRawStream::GetNErrors();
234   const char * errNames[nErrors];
235   for(Int_t i=0; i<nErrors; i++) {
236     errNames[i] = AliPHOSCpvRawStream::GetErrName(i);
237   }
238   fhErrors = new TH1I("errorTypes","Errors occured during processing",nErrors+1,0,nErrors+1);
239   TAxis* x = fhErrors->GetXaxis();
240   x->SetBinLabel(1, "Can't get event");
241   for(Int_t i=0; i<nErrors; i++) {
242     x->SetBinLabel(i+2,errNames[i]);
243   }
244 }
245 //--------------------------------------------------------------------------------------
246 void AliPHOSCpvPedProducer::CreateDDLHistos(Int_t iDDL)
247 {
248   // creating histograms
249   fPedMeanMap[iDDL] = new TH2F(Form("hPedMeanMap%d",iDDL),"2D mean pedestal map" ,AliPHOSCpvParam::kPadPcX,0.,AliPHOSCpvParam::kPadPcX,AliPHOSCpvParam::kPadPcY,0.,AliPHOSCpvParam::kPadPcY);
250   fPedSigMap [iDDL] = new TH2F(Form("hPedSigMap%d" ,iDDL),"2D pedestal sigma map",AliPHOSCpvParam::kPadPcX,0.,AliPHOSCpvParam::kPadPcX,AliPHOSCpvParam::kPadPcY,0.,AliPHOSCpvParam::kPadPcY);
251   f1DPedMean [iDDL] = new TH1F(Form("h1DPedMean%d" ,iDDL),"1D mean pedestal map" ,5000,0,5000);
252   f1DPedSigma[iDDL] = new TH1F(Form("h1DPedSigma%d",iDDL),"1D pedestal sigma map",100 ,0,100 );
253
254   // initialization of arrays
255   int adr;
256   for(Int_t iX=0; iX<AliPHOSCpvParam::kPadPcX; iX++) {
257     for(Int_t iY=0; iY<AliPHOSCpvParam::kPadPcY; iY++) {
258       adr = AliPHOSCpvParam::XY2A(iDDL,iX,iY);
259       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);
260     }//iY
261   }//iX
262
263 }