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