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