]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/ZDCPEDESTALda.cxx
Protection against crash when attempting to fit empty histos
[u/mrichter/AliRoot.git] / ZDC / ZDCPEDESTALda.cxx
1 /*
2
3 This program reads the DAQ data files passed as argument using the monitoring library.
4
5
6 The program reports about its processing progress.
7
8 Messages on stdout are exported to DAQ log system.
9
10 DA for ZDC standalone pedestal runs
11
12 Contact: Chiara.Oppedisano@to.infn.it
13 Link: 
14 Run Type: STANDALONE_PEDESTAL_RUN
15 DA Type: LDC
16 Number of events needed: no constraint (tipically ~10^3)
17 Input Files:  
18 Output Files: ZDCPedestal.dat, ZDCChMapping.dat
19 Trigger Types Used: Standalone Trigger
20
21 */
22 #define PEDDATA_FILE  "ZDCPedestal.dat"
23 #define PEDHISTO_FILE "ZDCPedHisto.root"
24 #define MAPDATA_FILE  "ZDCChMapping.dat"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <Riostream.h>
29
30 // DATE
31 #include <event.h>
32 #include <monitor.h>
33 #include <daqDA.h>
34
35 //ROOT
36 #include <TROOT.h>
37 #include <TPluginManager.h>
38 #include <TH1F.h>
39 #include <TH2F.h>
40 #include <TProfile.h>
41 #include <TF1.h>
42 #include <TFile.h>
43 #include <TFitter.h>
44 #include "TMinuitMinimizer.h"
45
46 //AliRoot
47 #include <AliRawReaderDate.h>
48 #include <AliRawEventHeaderBase.h>
49 #include <AliZDCRawStream.h>
50
51
52 /* Main routine
53       Arguments: list of DATE raw data files
54 */
55 int main(int argc, char **argv) {
56   
57
58   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
59                                         "*",
60                                         "TStreamerInfo",
61                                         "RIO",
62                                         "TStreamerInfo()"); 
63
64   TMinuitMinimizer m; 
65   gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer", "Minuit","TMinuitMinimizer",
66       "Minuit", "TMinuitMinimizer(const char *)");
67   TVirtualFitter::SetDefaultFitter("Minuit");
68
69   int status = 0;
70   int const kNChannels = 24;
71
72   /* log start of process */
73   printf("\n ZDC PEDESTAL program started\n");  
74
75   /* check that we got some arguments = list of files */
76   if (argc<2){
77     printf("Wrong number of arguments\n");
78     return -1;
79   }
80
81   // --- Histograms for ADC pedestals 
82   //     [22 signal channels + 2 reference PTMs]  x 2 gain chains
83   //
84   TH1F::AddDirectory(0);
85   //
86   TH1F *hPedhg[kNChannels], *hPedOutOfTimehg[kNChannels];
87   TH2F *hPedCorrhg[kNChannels];
88   TH1F *hPedlg[kNChannels], *hPedOutOfTimelg[kNChannels];
89   TH2F *hPedCorrlg[kNChannels];
90   //
91   char namhist1hg[50], namhist2hg[50], namhist3hg[50];
92   char namhist1lg[50], namhist2lg[50], namhist3lg[50];
93   for(Int_t j=0; j<kNChannels; j++){
94      if(j<=4){ // ZNC
95        sprintf(namhist1hg,"PedZNChg_%d",j);
96        sprintf(namhist2hg,"PedZNChgOutOfTime_%d",j);
97        sprintf(namhist3hg,"PedCorrZNChg_%d",j);
98        //
99        sprintf(namhist1lg,"PedZNClg_%d",j);
100        sprintf(namhist2lg,"PedZNClgOutOfTime_%d",j);
101        sprintf(namhist3lg,"PedCorrZNClg_%d",j);
102      }
103      else if(j>=5 && j<=9){ // ZPC
104        sprintf(namhist1hg,"PedZPChg_%d",j-5);
105        sprintf(namhist2hg,"PedZPChgOutOfTime_%d",j-5);
106        sprintf(namhist3hg,"PedCorrZPChg_%d",j-5);
107        //
108        sprintf(namhist1lg,"PedZPClg_%d",j-5);
109        sprintf(namhist2lg,"PedZPClgOutOfTime_%d",j-5);
110        sprintf(namhist3lg,"PedCorrZPClg_%d",j-5);       
111      }
112      else if(j==10 || j==11){ // ZEM
113        sprintf(namhist1hg,"PedZEMhg_%d",j-10);
114        sprintf(namhist2hg,"PedZEMhgOutOfTime_%d",j-10);
115        sprintf(namhist3hg,"PedCorrZEMhg_%d",j-10);
116        //
117        sprintf(namhist1lg,"PedZEMlg_%d",j-10);
118        sprintf(namhist2lg,"PedZEMlgOutOfTime_%d",j-10);
119        sprintf(namhist3lg,"PedCorrZEMlg_%d",j-10);
120      }
121      else if(j>=12 && j<=16){ // ZNA
122        sprintf(namhist1hg,"PedZNAhg_%d",j-12);
123        sprintf(namhist2hg,"PedZNAhgOutOfTime_%d",j-12);
124        sprintf(namhist3hg,"PedCorrZNAhg_%d",j-12);
125        //
126        sprintf(namhist1lg,"PedZNAlg_%d",j-12);
127        sprintf(namhist2lg,"PedZNAlgOutOfTime_%d",j-12);
128        sprintf(namhist3lg,"PedCorrZNAlg_%d",j-12);
129      }
130      else if(j>=17 && j<=21){ // ZPA
131        sprintf(namhist1hg,"PedZPAhg_%d",j-17);
132        sprintf(namhist2hg,"PedZPAhgOutOfTime_%d",j-17);
133        sprintf(namhist3hg,"PedCorrZPAhg_%d",j-17);
134        //
135        sprintf(namhist1lg,"PedZPAlg_%d",j-17);
136        sprintf(namhist2lg,"PedZPAlgOutOfTime_%d",j-17);
137        sprintf(namhist3lg,"PedCorrZPAlg_%d",j-17);
138      }
139      else if(j==22 || j==24){ //Reference PMs
140        sprintf(namhist1hg,"PedRefhg_%d",j-22);
141        sprintf(namhist2hg,"PedRefhgOutOfTime_%d",j-22);
142        sprintf(namhist3hg,"PedCorrRefhg_%d",j-22);
143        //
144        sprintf(namhist1lg,"PedReflg_%d",j-22);
145        sprintf(namhist2lg,"PedReflgOutOfTime_%d",j-22);
146        sprintf(namhist3lg,"PedCorrReflg_%d",j-22);
147      }
148      // --- High gain chain histos
149      hPedhg[j] = new TH1F(namhist1hg, namhist1hg, 200, 0., 200.);
150      hPedOutOfTimehg[j] = new TH1F(namhist2hg, namhist2hg, 200, 0., 200.);
151      hPedCorrhg[j] = new TH2F(namhist3hg,namhist3hg,100,0.,200.,100,0.,200.);
152      // --- Low gain chain histos
153      hPedlg[j] = new TH1F(namhist1lg, namhist1lg, 100, 0., 1000.);
154      hPedOutOfTimelg[j] = new TH1F(namhist2lg, namhist2lg, 100, 0., 1000.);
155      hPedCorrlg[j] = new TH2F(namhist3lg,namhist3lg,100,0.,1000.,100,0.,1000.);
156   }
157
158
159   /* open result file */
160   FILE *fp=NULL;
161   fp=fopen("./result.txt","w");
162   if (fp==NULL) {
163     printf("Failed to open file\n");
164     return -1;
165   }
166   
167   FILE *mapFile4Shuttle;
168
169   /* report progress */
170   daqDA_progressReport(10);
171
172
173   /* init some counters */
174   int nevents_physics=0;
175   int nevents_total=0;
176
177   /* read the data files */
178   int n;
179   for(n=1;n<argc;n++){
180    
181     status=monitorSetDataSource( argv[n] );
182     if (status!=0) {
183       printf("monitorSetDataSource() failed : %s\n",monitorDecodeError(status));
184       return -1;
185     }
186
187     /* report progress */
188     /* in this example, indexed on the number of files */
189     daqDA_progressReport(10+80*n/argc);
190
191     /* read the file */
192     for(;;) {
193       struct eventHeaderStruct *event;
194       eventTypeType eventT;
195
196       /* get next event */
197       status=monitorGetEventDynamic((void **)&event);
198       if(status==MON_ERR_EOF) break; /* end of monitoring file has been reached */
199       if(status!=0) {
200         printf("monitorGetEventDynamic() failed : %s\n",monitorDecodeError(status));
201         return -1;
202       }
203
204       /* retry if got no event */
205       if(event==NULL) {
206         break;
207       }
208       
209       // Initalize raw-data reading and decoding
210       AliRawReader *reader = new AliRawReaderDate((void*)event);
211       reader->Select("ZDC");
212       // --- Reading event header
213       //UInt_t evtype = reader->GetType();
214       //printf("\n\t ZDCPEDESTALda -> ev. type %d\n",evtype);
215       //printf("\t ZDCPEDESTALda -> run # %d\n",reader->GetRunNumber());
216       //
217       AliZDCRawStream *rawStreamZDC = new AliZDCRawStream(reader);
218         
219
220       /* use event - here, just write event id to result file */
221       eventT=event->eventType;
222       
223       Int_t ich=0;
224       Int_t adcMod[2*kNChannels], adcCh[2*kNChannels], sigCode[2*kNChannels];
225       Int_t det[2*kNChannels], sec[2*kNChannels];
226       
227       if(eventT==START_OF_DATA){
228                 
229         rawStreamZDC->SetSODReading(kTRUE);
230         
231         // --------------------------------------------------------
232         // --- Writing ascii data file for the Shuttle preprocessor
233         mapFile4Shuttle = fopen(MAPDATA_FILE,"w");
234         if(!rawStreamZDC->Next()) printf(" \t No raw data found!! \n");
235         else{
236           while((rawStreamZDC->Next()) && (ich<2*kNChannels)){
237             if(rawStreamZDC->IsChMapping()){
238               adcMod[ich] = rawStreamZDC->GetADCModFromMap(ich);
239               adcCh[ich] = rawStreamZDC->GetADCChFromMap(ich);
240               sigCode[ich] = rawStreamZDC->GetADCSignFromMap(ich);
241               det[ich] = rawStreamZDC->GetDetectorFromMap(ich);
242               sec[ich] = rawStreamZDC->GetTowerFromMap(ich);
243               //
244               fprintf(mapFile4Shuttle,"\t%d\t%d\t%d\t%d\t%d\t%d\n",
245                 ich,adcMod[ich],adcCh[ich],sigCode[ich],det[ich],sec[ich]);
246               //
247               //printf("ZDCPEDESTALda.cxx -> %d mod %d ch %d, code %d det %d, sec %d\n",
248               //   ich,adcMod[ich],adcCh[ich],sigCode[ich],det[ich],sec[ich]);
249               //
250               ich++;
251             }
252           }
253         }
254         fclose(mapFile4Shuttle);
255       }// SOD event
256       
257       if(eventT==PHYSICS_EVENT){
258         // --- Reading data header
259         reader->ReadHeader();
260         const AliRawDataHeader* header = reader->GetDataHeader();
261         if(header){
262          UChar_t message = header->GetAttributes();
263          if(message & 0x20){ // PEDESTAL RUN
264             //printf("\t STANDALONE_PEDESTAL RUN raw data found\n");
265          }
266          else{
267             //printf("ZDCPEDESTALda.cxx -> NO STANDALONE_PEDESTAL RUN raw data found\n");
268             //return -1;
269          }
270         }
271         else{
272            printf("\t ATTENTION! No Raw Data Header found!!!\n");
273            return -1;
274         }
275         
276         rawStreamZDC->SetSODReading(kTRUE);
277
278         if(!rawStreamZDC->Next()) printf(" \t No raw data found!! \n"); 
279         //
280         // ----- Setting ch. mapping -----
281         for(Int_t jk=0; jk<2*kNChannels; jk++){
282           //printf("ZDCPEDESTALDA.cxx ->  ch.%d mod %d, ch %d, code %d det %d, sec %d\n",
283           //    jk,adcMod[jk],adcCh[jk],sigCode[jk],det[jk],sec[jk]);
284           rawStreamZDC->SetMapADCMod(jk, adcMod[jk]);
285           rawStreamZDC->SetMapADCCh(jk, adcCh[jk]);
286           rawStreamZDC->SetMapADCSig(jk, sigCode[jk]);
287           rawStreamZDC->SetMapDet(jk, det[jk]);
288           rawStreamZDC->SetMapTow(jk, sec[jk]);
289         }
290         //
291         Int_t iraw=0;
292         Int_t RawADChg[kNChannels], RawADCoothg[kNChannels];
293         Int_t RawADClg[kNChannels], RawADCootlg[kNChannels];
294         for(Int_t j=0; j<kNChannels; j++){
295            RawADChg[j]=0; RawADCoothg[j]=0;
296            RawADClg[j]=0; RawADCootlg[j]=0;
297         }
298         //
299         while(rawStreamZDC->Next()){
300          Int_t index=-1;
301          Int_t detector = rawStreamZDC->GetSector(0);
302          Int_t sector = rawStreamZDC->GetSector(1);
303          
304          if(rawStreamZDC->IsADCDataWord() && (detector!=-1)){
305           if(sector!=5){ // Physics signals
306             if(detector==1) index = sector; // *** ZNC
307             else if(detector==2) index = sector+5; // *** ZPC
308             else if(detector==3) index = sector+9; // *** ZEM
309             else if(detector==4) index = sector+12; // *** ZNA
310             else if(detector==5) index = sector+17; // *** ZPA
311           }
312           else{ // Reference PMs
313             index = (detector-1)/3+22;
314           }
315           //
316           if(index==-1) printf("ERROR in ZDCPEDESTALda.cxx -> det %d quad %d res %d index %d \n", 
317             detector,sector,rawStreamZDC->GetADCGain(),index);
318           
319            //
320            if(iraw<2*kNChannels){ // --- In-time pedestals (1st 48 raw data)
321             if(rawStreamZDC->GetADCGain()==0){ 
322               hPedhg[index]->Fill(rawStreamZDC->GetADCValue()); 
323               RawADChg[index] = rawStreamZDC->GetADCValue();
324               //
325               //printf("\t filling histo hPedhg[%d]\n",index);
326             }
327             else{
328               hPedlg[index]->Fill(rawStreamZDC->GetADCValue()); 
329               RawADClg[index] = rawStreamZDC->GetADCValue();
330               //
331               //printf("\t filling histo hPedlg[%d]\n",index);
332             }
333            }
334            else{  // --- Out-of-time pedestals
335             if(rawStreamZDC->GetADCGain()==0){
336               hPedOutOfTimehg[index]->Fill(rawStreamZDC->GetADCValue());
337               RawADCoothg[index] = rawStreamZDC->GetADCValue();
338               //
339               //printf("\t filling histo hPedOutOfTimehg[%d]\n",index);
340             }
341             else{
342               hPedOutOfTimelg[index]->Fill(rawStreamZDC->GetADCValue());
343               RawADCootlg[index] = rawStreamZDC->GetADCValue();
344               //
345               //printf("\t filling histo hPedOutOfTimelg[%d]\n",index);
346             }
347            }
348             iraw++;
349           }//IsADCDataWord()
350           //
351           if(iraw == 4*kNChannels){ // Last ADC channel -> Filling correlation histos
352             for(Int_t k=0; k<kNChannels; k++){
353               hPedCorrhg[k]->Fill(RawADCoothg[k], RawADChg[k]);
354               hPedCorrlg[k]->Fill(RawADCootlg[k], RawADClg[k]);
355             }
356           }
357          }
358          //
359          nevents_physics++;
360          //
361          delete reader;
362          delete rawStreamZDC;
363
364       }//(if PHYSICS_EVENT) 
365       nevents_total++;
366
367       /* free resources */
368       free(event);
369     
370     }
371   }  
372   
373   /* Analysis of the histograms */
374   //
375   FILE *fileShuttle;
376   fileShuttle = fopen(PEDDATA_FILE,"w");
377   //
378   Float_t MeanPed[2*kNChannels], MeanPedWidth[2*kNChannels], 
379         MeanPedOOT[2*kNChannels], MeanPedWidthOOT[2*kNChannels];
380   // --- In-time pedestals
381   TF1 *ADCfunchg[kNChannels];
382   for(Int_t i=0; i<kNChannels; i++){
383      if(hPedhg[i]->GetEntries() == 0){
384        printf("\n WARNING! Empty histos -> ending DA WITHOUT writing output\n\n");
385        return -1;
386      } 
387      hPedhg[i]->Fit("gaus","Q");
388      ADCfunchg[i] = hPedhg[i]->GetFunction("gaus");
389      MeanPed[i] = (Double_t) ADCfunchg[i]->GetParameter(1);
390      MeanPedWidth[i] = (Double_t)  ADCfunchg[i]->GetParameter(2);
391      fprintf(fileShuttle,"\t%f\t%f\n",MeanPed[i],MeanPedWidth[i]);
392      //printf("\t MeanPedhg[%d] = %f\n",i, MeanPed[i]);
393   }
394   TF1 *ADCfunclg[kNChannels];
395   for(Int_t i=0; i<kNChannels; i++){
396      hPedlg[i]->Fit("gaus","Q");
397      ADCfunclg[i] = hPedlg[i]->GetFunction("gaus");
398      MeanPed[i+kNChannels] = (Double_t)  ADCfunclg[i]->GetParameter(1);
399      MeanPedWidth[i+kNChannels] = (Double_t)  ADCfunclg[i]->GetParameter(2);
400      fprintf(fileShuttle,"\t%f\t%f\n",MeanPed[i+kNChannels],MeanPedWidth[i+kNChannels]);
401      //printf("\t MeanPedlg[%d] = %f\n",i+kNChannels, MeanPed[i+kNChannels]);
402   }
403   // --- Out-of-time pedestals
404   TF1 *ADCootfunchg[kNChannels];
405   for(Int_t i=0; i<kNChannels; i++){
406      if(hPedOutOfTimehg[i]->GetEntries() == 0){
407        printf("\n WARNING! Empty histos -> ending DA WITHOUT writing output\n\n");
408        return -1;
409      } 
410      hPedOutOfTimehg[i]->Fit("gaus","Q");
411      ADCootfunchg[i] = hPedOutOfTimehg[i]->GetFunction("gaus");
412      MeanPedOOT[i] = (Double_t)  ADCootfunchg[i]->GetParameter(1);
413      MeanPedWidthOOT[i] = (Double_t)  ADCootfunchg[i]->GetParameter(2);
414      fprintf(fileShuttle,"\t%f\t%f\n",MeanPedOOT[i],MeanPedWidthOOT[i]);
415      //printf("\t MeanPedOOThg[%d] = %f\n",i, MeanPedOOT[i]);
416   }
417   TF1 *ADCootfunclg[kNChannels];
418   for(Int_t i=0; i<kNChannels; i++){
419      hPedOutOfTimelg[i]->Fit("gaus","Q");
420      ADCootfunclg[i] = hPedOutOfTimelg[i]->GetFunction("gaus");
421      MeanPedOOT[i+kNChannels] = (Double_t)  ADCootfunclg[i]->GetParameter(1);
422      MeanPedWidthOOT[i+kNChannels] = (Double_t)  ADCootfunclg[i]->GetParameter(2);
423      fprintf(fileShuttle,"\t%f\t%f\n",MeanPedOOT[i+kNChannels],MeanPedWidthOOT[i+kNChannels]);
424      //printf("\t MeanPedOOTlg[%d] = %f\n",i+kNChannels, MeanPedOOT[i+kNChannels]);
425   }
426   
427   // --- Correlations
428
429 /*  Float_t CorrCoeff0[2*kNChannels], CorrCoeff1[2*kNChannels];
430   TProfile *hPedCorrProfhg[kNChannels], *hPedCorrProflg[kNChannels];
431   TF1 *ffunchg[kNChannels], *ffunclg[kNChannels];
432   char namhist4[50];
433   for(int i=0;i<kNChannels;i++) {
434      sprintf(namhist4,"ADCHRvsOOT%d_Prof",i);
435      hPedCorrProfhg[i] = hPedCorrhg[i]->ProfileX(namhist4,-1,-1,"S");
436      hPedCorrProfhg[i]->SetName(namhist4);
437      hPedCorrProfhg[i]->Fit("pol1","Q");
438      ffunchg[i] = hPedCorrProfhg[i]->GetFunction("pol1");
439      CorrCoeff0[i] = (Double_t)  ffunchg[i]->GetParameter(0);
440      CorrCoeff1[i] = (Double_t) ffunchg[i]->GetParameter(1);
441      fprintf(fileShuttle,"\t%f\t%f\n",CorrCoeff0[i],CorrCoeff1[i]);
442      //printf("\t CorrCoeff0[%d] = %f, CorrCoeff1[%d] = %f\n",i, CorrCoeff0[i], i, CorrCoeff1[i]);
443   }    
444   for(int i=0;i<kNChannels;i++) {
445      sprintf(namhist4,"ADCLRvsOOT%d_Prof",i);
446      hPedCorrProflg[i] = hPedCorrlg[i]->ProfileX(namhist4,-1,-1,"S");
447      hPedCorrProflg[i]->SetName(namhist4);
448      hPedCorrProflg[i]->Fit("pol1","Q");
449      ffunclg[i] = hPedCorrProflg[i]->GetFunction("pol1");
450      CorrCoeff0[i+kNChannels] =  (Double_t) ffunclg[i]->GetParameter(0);
451      CorrCoeff1[i+kNChannels] =  (Double_t) ffunclg[i]->GetParameter(1);
452      fprintf(fileShuttle,"\t%f\t%f\n",CorrCoeff0[i+kNChannels],CorrCoeff1[i+kNChannels]);
453      //printf("\t CorrCoeff0[%d] = %f, CorrCoeff1[%d] = %f\n",
454      //         i+kNChannels, CorrCoeff0[i+kNChannels], i+kNChannels, CorrCoeff1[i+kNChannels]);
455   }    
456 */
457   //                                                   
458   fclose(fileShuttle);
459   //
460   /* report progress */
461   daqDA_progressReport(80);
462   //
463   TFile *histofile = new TFile(PEDHISTO_FILE,"RECREATE");
464   histofile->cd();
465   for(int k=0; k<kNChannels; k++){
466      hPedhg[k]->Write();
467      hPedOutOfTimehg[k]->Write();
468      hPedCorrhg[k]->Write();
469      hPedlg[k]->Write();
470      hPedOutOfTimelg[k]->Write();
471      hPedCorrlg[k]->Write();
472   }
473   //                                                   
474   histofile->Close();
475   //
476   for(Int_t j=0; j<kNChannels; j++){
477      delete hPedhg[j];
478      delete hPedOutOfTimehg[j];
479      delete hPedCorrhg[j];
480      delete hPedlg[j];
481      delete hPedOutOfTimelg[j];
482      delete hPedCorrlg[j];
483   }
484
485   /* write report */
486   fprintf(fp,"Run #%s, received %d physics events out of %d\n",getenv("DATE_RUN_NUMBER"),nevents_physics,nevents_total);
487
488   /* close result file */
489   fclose(fp);
490   
491   /* report progress */
492   daqDA_progressReport(90);
493
494   /* store the result files on FES */
495   // [1] File with mapping
496   status = daqDA_FES_storeFile(MAPDATA_FILE,MAPDATA_FILE);
497   if(status){
498     printf("Failed to export mapping data file to DAQ FES\n");
499     return -1;
500   }
501   // [2] File with pedestal data
502   status = daqDA_FES_storeFile(PEDDATA_FILE,PEDDATA_FILE);
503   if(status){
504     printf("Failed to export pedestal data file to DAQ FES\n");
505     return -1;
506   }
507   // [3] File with pedestal histos
508   status = daqDA_FES_storeFile(PEDHISTO_FILE,PEDHISTO_FILE);
509   if(status){
510     printf("Failed to export pedestal histos file to DAQ FES\n");
511     return -1;
512   }
513   
514   /* store the result files on DB */
515   status = daqDA_DB_storeFile(PEDDATA_FILE,PEDDATA_FILE);  
516   if(status){
517     printf("Failed to store pedestal data file to DAQ DB\n");
518     return -1;
519   }
520
521   /* report progress */
522   daqDA_progressReport(100);
523
524   return status;
525 }