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