Previous commit had the bad side-effect of changing the behaviour of Raw QA to comput...
[u/mrichter/AliRoot.git] / MUON / AliMUONGain.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        
4 * Author: The ALICE Off-line Project.                                    * 
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 #include "AliMUONGain.h"
19 #include "AliMUONPedestal.h"
20 #include "AliMUONErrorCounter.h"
21 #include "AliMUON2DMap.h"
22 #include "AliMUONVCalibParam.h"
23 #include "AliMUONVStore.h"
24 #include "AliMpIntPair.h"
25
26 #include <TString.h>
27 #include <THashList.h>
28 #include <TTimeStamp.h>
29 #include <TTree.h>
30 #include <TFile.h>
31 #include <TF1.h>
32 #include <TGraphErrors.h>
33 #include <TMath.h>
34 #include <Riostream.h>
35
36 #include <sstream>
37
38 #define  NFITPARAMS 4
39
40 //-----------------------------------------------------------------------------
41 /// \class AliMUONGain
42 ///
43 /// Implementation of calibration computing
44 ///
45 /// add
46 /// 
47 ///
48 /// \author Alberto Baldisseri, JL Charvet 
49 //-----------------------------------------------------------------------------
50
51
52
53
54 // functions
55
56
57 //______________________________________________________________________________
58 Double_t funcLin (const Double_t *x, const Double_t *par)
59 {
60   /// Linear function
61   return par[0] + par[1]*x[0];
62 }
63
64 //______________________________________________________________________________
65 Double_t funcParabolic (const Double_t *x, const Double_t *par)
66 {
67   /// Parabolic function
68   return par[0]*x[0]*x[0];
69 }
70
71 //______________________________________________________________________________
72 Double_t funcCalib (const Double_t *x, const Double_t *par)  
73 {
74   /// Calibration function
75   Double_t xLim= par[3];
76
77   if(x[0] <= xLim) return par[0] + par[1]*x[0];
78
79   Double_t yLim = par[0]+ par[1]*xLim;
80   return yLim + par[1]*(x[0] - xLim) + par[2]*(x[0] - xLim)*(x[0] - xLim);
81 }
82
83
84 /// \cond CLASSIMP
85 ClassImp(AliMUONGain)
86 /// \endcond
87
88 //______________________________________________________________________________
89 AliMUONGain::AliMUONGain()
90 : AliMUONPedestal(),
91 fInjCharge(0), 
92 fnInit(1),
93 fnEntries(11),
94 fnbpf1(6),
95 fPrintLevel(0), 
96 fPlotLevel(0)
97 {
98 /// Default constructor
99
100   sprintf(fRootDataFileName," "); //Gain
101 }
102 //  AliMUONPedestal& operator=(const AliMUONPedestal& other); Copy ctor
103   
104 //______________________________________________________________________________
105 AliMUONGain::~AliMUONGain()
106 {
107 /// Destructor
108 }
109
110 //______________________________________________________________________________
111 TString AliMUONGain::WriteDummyHeader(void) 
112 {
113 ///
114
115   ostringstream stream;
116   stream<<"//DUMMY FILE (to prevent Shuttle failure)"<< endl;
117   stream<<"//================================================" << endl;
118   stream<<"//       MUONTRKda: Calibration run  " << endl;
119   stream<<"//================================================" << endl;
120   stream<<"//   * Run           : " << fRunNumber << endl; 
121   stream<<"//   * Date          : " << fDate->AsString("l") <<endl;
122   stream<<"//   * DAC           : " << fInjCharge << endl;
123   stream<<"//-------------------------------------------------" << endl;
124
125   return TString(stream.str().c_str());
126 }
127
128 //______________________________________________________________________________
129 void AliMUONGain::MakePedStoreForGain(TString shuttleFile)
130 {
131 ///
132
133   ofstream fileout;
134   TString tempstring;
135   Char_t flatFile[256]="";
136   TString outputFile;
137   
138   // Store pedestal map in root file
139   TTree* tree = 0x0;
140
141   // write dummy ascii file -> Shuttle
142   if(fIndex<fnEntries)
143     {  
144       //     fileout.open(shuttleFile.Data());
145       //     tempstring = WriteDummyHeader();
146       //     fileout << tempstring;
147       FILE *pfilew=0;
148       pfilew = fopen (shuttleFile,"w");
149
150       fprintf(pfilew,"//DUMMY FILE (to prevent Shuttle failure)\n");
151       fprintf(pfilew,"//================================================\n");
152       fprintf(pfilew,"//       MUONTRKda: Calibration run  \n");
153       fprintf(pfilew,"//=================================================\n");
154       fprintf(pfilew,"//   * Run           : %d \n",fRunNumber); 
155       fprintf(pfilew,"//   * Date          : %s \n",fDate->AsString("l"));
156       fprintf(pfilew,"//   * DAC           : %d \n",fInjCharge);
157       fprintf(pfilew,"//-------------------------------------------------\n");
158       fclose(pfilew);
159     }
160
161
162
163   Finalize();
164   MakeControlHistos();
165   if(fPrintLevel>0)
166     {
167       // compute and store mean DAC values (like pedestals)
168       sprintf(flatFile,"%s.ped",fprefixDA);
169       outputFile=flatFile;
170       cout << "\n" << fprefixDA << " : Flat file  generated  : " << flatFile << "\n";
171       if (!outputFile.IsNull())  
172       {
173         ofstream out(outputFile.Data());
174         MakeASCIIoutput(out);
175         out.close();
176       }      
177     }
178
179   TString mode("UPDATE");
180
181   if (fIndex==1) {
182     mode = "RECREATE";
183   }
184   TFile* histoFile = new TFile(fRootDataFileName, mode.Data(), "MUON Tracking Gains");
185
186   // second argument should be the injected charge, taken from config crocus file
187   // put also info about run number could be usefull
188   AliMpIntPair* pair   = new AliMpIntPair(fRunNumber,fInjCharge );
189
190   if (mode.CompareTo("UPDATE") == 0) {
191     tree = (TTree*)histoFile->Get("t");
192     tree->SetBranchAddress("run",&pair);
193     tree->SetBranchAddress("ped",&fPedestalStore);
194
195   } else {
196     tree = new TTree("t","Pedestal tree");
197     tree->Branch("run", "AliMpIntPair",&pair);
198     tree->Branch("ped", "AliMUON2DMap",&fPedestalStore);
199     tree->SetBranchAddress("run",&pair);
200     tree->SetBranchAddress("ped",&fPedestalStore);
201
202   }
203
204   tree->Fill();
205   tree->Write("t", TObject::kOverwrite); // overwrite the tree
206   histoFile->Close();
207
208   delete pair;
209 }
210
211 //______________________________________________________________________________
212 TString AliMUONGain::WriteGainHeader(Int_t nInit, Int_t nEntries, Int_t nbpf2, Int_t *numrun, Double_t *injCharge) 
213 {
214 ///
215
216   ostringstream stream;
217
218
219   stream<<"//================================================" << endl;
220   stream<<"//  Calibration file calculated by MUONTRKda" << endl;
221   stream<<"//=================================================" << endl;
222   stream<<"//   * Run           : " << fRunNumber << endl; 
223   stream<<"//   * Date          : " << fDate->AsString("l") <<endl;
224   stream<<"//   * Statictics    : " << fNEvents << endl;
225   stream<<"//   * # of MANUS    : " << fNManu << endl;
226   stream<<"//   * # of channels : " << fNChannel << endl;
227   stream<<"//-------------------------------------------------" << endl;
228
229   if(nInit==0)
230     stream<<"//  "<< nEntries <<" DAC values  fit: "<< fnbpf1 << " pts (1st order) " << nbpf2 << " pts (2nd order)" << endl;
231   if(nInit==1)
232     stream<<"//  "<< nEntries <<" DAC values  fit: "<< fnbpf1 << " pts (1st order) " << nbpf2 << " pts (2nd order) DAC=0 excluded" << endl;
233
234   stream<<"//   RUN     DAC   " << endl;
235   stream<<"//-----------------" << endl;
236   for (Int_t i = 0; i < nEntries; ++i) {
237         stream<<Form("//   %d    %5.0f \n",numrun[i],injCharge[i]);
238   }
239   stream<<"//=======================================" << endl;
240   stream<<"// BP MANU CH.   a1      a2     thres. q " << endl;
241   stream<<"//=======================================" << endl;
242
243   return TString(stream.str().c_str());
244 }
245
246 //______________________________________________________________________________
247 TString AliMUONGain::WriteGainData(Int_t BP, Int_t Manu, Int_t ch, Double_t p1, Double_t p2, Int_t threshold, Int_t q) 
248 {
249 ///
250
251   ostringstream stream("");
252   stream << Form("%4i %5i %2i %7.4f %10.3e %4i %2x\n",BP,Manu,ch,p1,p2,threshold,q);
253   return TString(stream.str().c_str());
254
255 }
256
257 //_______________________________________________________________________________
258 void AliMUONGain::MakeGainStore(TString shuttleFile)
259 {
260   /// Store gains in ASCII files
261   ofstream fileout;
262   ofstream filcouc;
263   TString tempstring;   
264   Char_t filename[256]; 
265
266   Double_t goodA1Min =  0.5;
267   Double_t goodA1Max =  2.;
268   //     Double_t goodA1Min =  0.7;
269   //     Double_t goodA1Max =  1.7;
270   Double_t goodA2Min = -0.5E-03;
271   Double_t goodA2Max =  1.E-03;
272   // Table for uncalibrated  buspatches and manus
273   THashList* uncalBuspatchManuTable = new THashList(1000,2);
274
275   Int_t numrun[11];
276
277   // open file MUONTRKda_gain_data.root
278   // read again the pedestal for the calibration runs (11 runs)
279   // need the injection charge from config file (to be done)
280   // For each channel make a TGraphErrors (mean, sigma) vs injected charge
281   // Fit with a polynomial fct
282   // store the result in a flat file.
283
284   TFile*  histoFile = new TFile(fRootDataFileName);
285
286   AliMUON2DMap* map[11];
287   AliMUONVCalibParam* ped[11];
288   AliMpIntPair* run[11];
289
290   //read back from root file
291   TTree* tree = (TTree*)histoFile->Get("t");
292   Int_t nEntries = tree->GetEntries();
293
294   Int_t nbpf2 = nEntries - (fnInit + fnbpf1) + 1; // nb pts used for 2nd order fit
295
296   // read back info
297   for (Int_t i = 0; i < nEntries; ++i) {
298     map[i] = 0x0;
299     run[i] = 0x0;
300     tree->SetBranchAddress("ped",&map[i]);
301     tree->SetBranchAddress("run",&run[i]);
302     tree->GetEvent(i);
303     //        std::cout << map[i] << " " << run[i] << std::endl;
304   }
305   // RunNumber extracted from Root data fil
306   if(fIndex==0)fRunNumber=(UInt_t)run[nEntries-1]->GetFirst();
307   //     sscanf(getenv("DATE_RUN_NUMBER"),"%d",&gAliRunNumber);
308
309   Double_t pedMean[11];
310   Double_t pedSigma[11];
311   for ( Int_t i=0 ; i<11 ; i++) {pedMean[i]=0.;pedSigma[i]=1.;};
312   Double_t injCharge[11];
313   Double_t injChargeErr[11];
314   for ( Int_t i=0 ; i<11 ; i++) {injCharge[i]=0.;injChargeErr[i]=1.;};
315
316   // some print
317   cout<<"\n ********  MUONTRKda for Gain computing (Last Run = " << fRunNumber << ") ********\n" << endl;
318   cout<<" * Date          : " << fDate->AsString("l") << "\n" << endl;
319   cout << " Entries = " << nEntries << " DAC values \n" << endl; 
320   for (Int_t i = 0; i < nEntries; ++i) {
321     cout<< " Run = " << run[i]->GetFirst() << "    DAC = " << run[i]->GetSecond() << endl;
322     numrun[i] = run[i]->GetFirst();
323     injCharge[i] = run[i]->GetSecond();
324     injChargeErr[i] = 0.01*injCharge[i];
325     if(injChargeErr[i] <= 1.) injChargeErr[i]=1.;
326   }
327   cout << "" << endl;
328
329   //  print out in .log file
330
331   (*fFilcout)<<"\n\n//=================================================" << endl;
332   (*fFilcout)<<"//    MUONTRKda: Gain Computing  Run = " << fRunNumber << endl;
333   (*fFilcout)<<"//    RootDataFile  = "<< fRootDataFileName << endl;
334   (*fFilcout)<<"//=================================================" << endl;
335   (*fFilcout)<<"//* Date          : " << fDate->AsString("l") << "\n" << endl;
336
337
338
339   // why 2 files ? (Ch. F.)  => second file contains detailed results
340     FILE *pfilen = 0;
341     if(fPrintLevel>1)
342       {
343         sprintf(filename,"%s.param",fprefixDA);
344         cout << " Second fit parameter file        = " << filename << "\n";
345         pfilen = fopen (filename,"w");
346
347         fprintf(pfilen,"//===================================================================\n");
348         fprintf(pfilen,"//  BP MANU CH. par[0]     [1]     [2]     [3]      xlim          P(chi2) p1        P(chi2)2  p2\n");
349         fprintf(pfilen,"//===================================================================\n");
350         fprintf(pfilen,"//   * Run           : %d \n",fRunNumber); 
351         fprintf(pfilen,"//===================================================================\n");
352       }
353
354
355   // file outputs for gain
356
357   ofstream pfilew;
358   pfilew.open(shuttleFile.Data());
359   // Write Header Data of the .par file
360   pfilew << WriteGainHeader(fnInit,nEntries,nbpf2,numrun,injCharge);
361
362   // print mean and sigma values in file
363   FILE *pfilep = 0;
364   if(fPrintLevel>1)
365     {
366       sprintf(filename,"%s.peak",fprefixDA);
367       cout << " File containing Peak mean values = " << filename << "\n";
368       pfilep = fopen (filename,"w");
369
370       fprintf(pfilep,"//==============================================================================================================================\n");
371       fprintf(pfilep,"//   * Run           : %d \n",fRunNumber); 
372       fprintf(pfilep,"//==============================================================================================================================\n");
373       fprintf(pfilep,"// BP  MANU  CH.    Ped.     <0>      <1>      <2>      <3>      <4>      <5>      <6>      <7>      <8>      <9>     <10> \n"); 
374       fprintf(pfilep,"//==============================================================================================================================\n");
375       fprintf(pfilep,"//                 DAC= %9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f  fC\n",injCharge[0],injCharge[1],injCharge[2],injCharge[3],injCharge[4],injCharge[5],injCharge[6],injCharge[7],injCharge[8],injCharge[9],injCharge[10]);
376       fprintf(pfilep,"//                      %9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f\n",injChargeErr[0],injChargeErr[1],injChargeErr[2],injChargeErr[3],injChargeErr[4],injChargeErr[5],injChargeErr[6],injChargeErr[7],injChargeErr[8],injChargeErr[9],injChargeErr[10]);
377       fprintf(pfilep,"//==============================================================================================================================\n");
378     }
379
380   Double_t chi2    = 0.;
381   Double_t chi2P2  = 0.;
382   Double_t prChi2  = 0; 
383   Double_t prChi2P2 =0;
384   Double_t a0=0.,a1=1.,a2=0.;
385   Int_t busPatchId ;
386   Int_t manuId     ;
387   Int_t channelId ;
388   Int_t threshold = 0;
389   Int_t q = 0;
390   Int_t p1 =0;
391   Int_t p2 =0;
392   Double_t gain=0; 
393   Double_t capa=0.2; // internal capacitor (pF)
394
395   //  plot out 
396
397   TFile* gainFile = 0x0;
398   TTree* tg = 0x0;
399   if(fPlotLevel>0)
400     {
401       sprintf(fHistoFileName,"%s.root",fprefixDA);
402      gainFile = new TFile(fHistoFileName,"RECREATE","MUON Tracking gains");
403       tg = new TTree("tg","TTree avec class Manu_DiMu");
404
405       tg->Branch("bp",&busPatchId, "busPatchId/I");
406       tg->Branch("manu",&manuId, "manuId/I");
407       tg->Branch("channel",&channelId, "channelId/I");
408
409       tg->Branch("a0",&a0, "a0/D");
410       tg->Branch("a1",&a1, "a1/D");
411       tg->Branch("a2",&a2, "a2/D");
412       tg->Branch("Pchi2",&prChi2, "prChi2/D");
413       tg->Branch("Pchi2_2",&prChi2P2, "prChi2P2/D");
414       tg->Branch("Threshold",&threshold, "threshold/I");
415       tg->Branch("q",&q, "q/I");
416       tg->Branch("p1",&p1, "p1/I");
417       tg->Branch("p2",&p2, "p2/I");
418       tg->Branch("gain",&gain, "gain/D");
419     }
420
421   char graphName[256];
422
423   // iterates over the first pedestal run
424   TIter next(map[0]->CreateIterator());
425   AliMUONVCalibParam* p;
426
427   Int_t    nmanu         = 0;
428   Int_t    nGoodChannel   = 0;
429   Int_t    nBadChannel   = 0;
430   Int_t    noFitChannel   = 0;
431   Int_t    nplot=0;
432   Double_t sumProbChi2   = 0.;
433   Double_t sumA1         = 0.;
434   Double_t sumProbChi2P2 = 0.;
435   Double_t sumA2         = 0.;
436
437   Double_t x[11], xErr[11], y[11], yErr[11];
438   Double_t xp[11], xpErr[11], yp[11], ypErr[11];
439
440   Int_t uncalcountertotal=0 ;
441
442   while ( ( p = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
443     {
444       ped[0]  = p;
445
446       busPatchId = p->ID0();
447       manuId     = p->ID1();
448
449       // read back pedestal from the other runs for the given (bupatch, manu)
450       for (Int_t i = 1; i < nEntries; ++i) {
451         ped[i] = static_cast<AliMUONVCalibParam*>(map[i]->FindObject(busPatchId, manuId));
452       }
453
454       // compute for each channel the gain parameters
455       for ( channelId = 0; channelId < ped[0]->Size() ; ++channelId ) 
456         {
457
458           Int_t n = 0;
459           for (Int_t i = 0; i < nEntries; ++i) {
460
461             if (!ped[i]) continue; //shouldn't happen.
462             pedMean[i]      = ped[i]->ValueAsDouble(channelId, 0);
463             pedSigma[i]     = ped[i]->ValueAsDouble(channelId, 1);
464
465             if (pedMean[i] < 0) continue; // not connected
466
467             if (pedSigma[i] <= 0) pedSigma[i] = 1.; // should not happen.
468             n++;
469           }
470
471
472           // print_peak_mean_values
473           if(fPrintLevel>1)
474             {
475
476               fprintf(pfilep,"%4i%5i%5i%10.3f",busPatchId,manuId,channelId,0.);
477               fprintf(pfilep,"%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f%9.1f \n",pedMean[0],pedMean[1],pedMean[2],pedMean[3],pedMean[4],pedMean[5],pedMean[6],pedMean[7],pedMean[8],pedMean[9],pedMean[10]);
478               fprintf(pfilep,"                   sig= %9.3f%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f%9.3f \n",pedSigma[0],pedSigma[1],pedSigma[2],pedSigma[3],pedSigma[4],pedSigma[5],pedSigma[6],pedSigma[7],pedSigma[8],pedSigma[9],pedSigma[10]);
479             }
480
481           // makegain 
482
483
484           // Fit Method:  Linear fit over gAlinbpf1 points + parabolic fit  over nbpf2  points) 
485           // nInit=1 : 1st pt DAC=0 excluded
486
487           // 1. - linear fit over gAlinbpf1 points
488
489           Double_t par[4] = {0.,0.5,0.,kADCMax};
490           Int_t nbs   = nEntries - fnInit;
491           if(nbs < fnbpf1)fnbpf1=nbs;
492
493           Int_t fitproceed=1;
494           for (Int_t j = 0; j < nbs; ++j)
495             {
496               Int_t k = j + fnInit;
497               x[j]    = pedMean[k];
498               if(x[j]==0. || x[j]== kADCMax)fitproceed=0;
499               xErr[j] = pedSigma[k];
500               y[j]    = injCharge[k];
501               yErr[j] = injChargeErr[k];
502
503             }
504
505           TGraphErrors *graphErr;
506           if(!fitproceed) { p1=0; p2=0; noFitChannel++;}
507
508           if(fitproceed)
509             {
510                       
511               TF1 *f1 = new TF1("f1",funcLin,0.,kADCMax,2);
512               graphErr = new TGraphErrors(fnbpf1, x, y, xErr, yErr);
513
514               f1->SetParameters(0,0);
515
516               graphErr->Fit("f1","RQ");
517
518               chi2 = f1->GetChisquare();
519               f1->GetParameters(par);
520
521               delete graphErr;
522               graphErr=0;
523               delete f1;
524
525               prChi2 = TMath::Prob(chi2, fnbpf1 - 2);
526
527               Double_t xLim = pedMean[fnInit + fnbpf1 - 1];
528               Double_t yLim = par[0]+par[1] * xLim;
529
530               a0 = par[0];
531               a1 = par[1];
532
533               // 2. - Translation : new origin (xLim, yLim) + parabolic fit over nbf2 points
534
535               if(nbpf2 > 1)
536                 {
537                   for (Int_t j = 0; j < nbpf2; j++)
538                     {
539                       Int_t k  = j + (fnInit + fnbpf1) - 1;
540                       xp[j]    = pedMean[k] - xLim;
541                       xpErr[j] = pedSigma[k];
542
543                       yp[j]    = injCharge[k] - yLim - par[1]*xp[j];
544                       ypErr[j] = injChargeErr[k];
545                     }
546
547                   TF1 *f2 = new TF1("f2",funcParabolic,0.,kADCMax,1);
548                   graphErr = new TGraphErrors(nbpf2, xp, yp, xpErr, ypErr);
549
550                   graphErr->Fit(f2,"RQ");
551                   chi2P2 = f2->GetChisquare();
552                   f2->GetParameters(par);
553
554                   delete graphErr;
555                   graphErr=0;
556                   delete f2;
557
558                   prChi2P2 = TMath::Prob(chi2P2, nbpf2-1);
559                   a2 = par[0];
560                 }
561
562               par[0] = a0;
563               par[1] = a1;
564               par[2] = a2;
565               par[3] = xLim;
566
567               if(prChi2>0.999999)prChi2=0.999999 ; if(prChi2P2>0.999999)prChi2P2=0.9999999; // avoiding Pr(Chi2)=1 value
568               p1 = TMath::Nint(floor(prChi2*15))+1;    // round down value : floor(2.8)=2.
569               p2 = TMath::Nint(floor(prChi2P2*15))+1;
570               q  = p1*16 + p2;  // fit quality 
571
572               Double_t x0 = -par[0]/par[1]; // value of x corresponding to à 0 fC 
573               threshold = TMath::Nint(ceil(par[3]-x0)); // linear if x < threshold
574
575               if(fPrintLevel>1)
576                 {
577                   fprintf(pfilen,"%4i %4i %2i",busPatchId,manuId,channelId);
578                   fprintf(pfilen," %6.2f %6.4f %10.3e %4.2f %4i          %8.6f %8.6f   %x          %8.6f  %8.6f   %x\n",
579                           par[0], par[1], par[2], par[3], threshold, prChi2, floor(prChi2*15), p1,  prChi2P2, floor(prChi2P2*15),p2);
580                 }
581
582
583               // tests
584               if(par[1]< goodA1Min ||  par[1]> goodA1Max) p1=0;
585               if(par[2]< goodA2Min ||  par[2]> goodA2Max) p2=0;
586
587             } // fitproceed
588
589           if(fitproceed && p1>0 && p2>0) 
590             {
591               nGoodChannel++;
592               sumProbChi2   += prChi2;
593               sumA1         += par[1];
594               sumProbChi2P2   += prChi2P2;
595               sumA2         += par[2];
596             }
597           else // bad calibration
598             {
599               nBadChannel++;
600               q=0;  
601               par[1]=0.5; a1=0.5; p1=0;
602               par[2]=0.;  a2=0.;  p2=0;
603               threshold=kADCMax;        
604
605               // bad calibration counter
606               char bpmanuname[256];
607               AliMUONErrorCounter* uncalcounter;
608
609               sprintf(bpmanuname,"bp%dmanu%d",busPatchId,manuId);
610               if (!(uncalcounter = (AliMUONErrorCounter*)uncalBuspatchManuTable->FindObject(bpmanuname)))
611                 {
612                   // New buspatch_manu name
613                   uncalcounter= new AliMUONErrorCounter (busPatchId,manuId);
614                   uncalcounter->SetName(bpmanuname);
615                   uncalBuspatchManuTable->Add(uncalcounter);
616                 }
617               else
618                 {
619                   // Existing buspatch_manu name
620                   uncalcounter->Increment();
621                 }
622               //                            uncalcounter->Print_uncal()
623               uncalcountertotal ++;
624             }
625           gain=1./(par[1]*capa); // mv/fC
626
627           if(fPlotLevel>0)
628             {if(fPlotLevel>1)
629                 {
630                   //                  if(q==0  and  nplot < 100)
631                   //      if(p1>1 && p2==0  and  nplot < 100)
632                             if(p1>10 && p2>10  and  nplot < 100)
633                   //    if(p1>=1 and p1<=2  and  nplot < 100)
634 //                if((p1==1 || p2==1) and  nplot < 100)
635                     {
636                       nplot++;
637                       //              cout << " nplot = " << nplot << endl;
638                       TF1 *f2Calib = new TF1("f2Calib",funcCalib,0.,kADCMax,NFITPARAMS);
639
640                       graphErr = new TGraphErrors(nEntries,pedMean,injCharge,pedSigma,injChargeErr);
641
642                       sprintf(graphName,"BusPatch_%d_Manu_%d_Ch_%d",busPatchId, manuId,channelId);
643
644                       graphErr->SetTitle(graphName);
645                       graphErr->SetMarkerColor(3);
646                       graphErr->SetMarkerStyle(12);
647                       graphErr->Write(graphName);
648
649                       sprintf(graphName,"f2_BusPatch_%d_Manu_%d_Ch_%d",busPatchId, manuId,channelId);
650                       f2Calib->SetTitle(graphName);
651                       f2Calib->SetLineColor(4);
652                       f2Calib->SetParameters(par);
653                       f2Calib->Write(graphName);
654
655                       delete graphErr;
656                       graphErr=0;
657                       delete f2Calib;
658                     }
659                 }
660
661
662               tg->Fill();
663             }
664
665
666           pfilew << WriteGainData(busPatchId,manuId,channelId,par[1],par[2],threshold,q);
667         }
668       nmanu++;
669       if(nmanu % 500 == 0)std::cout << " Nb manu = " << nmanu << std::endl;
670     }
671
672   //      print in logfile
673   if (uncalBuspatchManuTable->GetSize())
674     {
675       uncalBuspatchManuTable->Sort();  // use compare
676       TIterator* iter = uncalBuspatchManuTable->MakeIterator();
677       AliMUONErrorCounter* uncalcounter;
678       (*fFilcout) << "\n List of problematic BusPatch and Manu " << endl;
679       (*fFilcout) << " ========================================" << endl;
680       (*fFilcout) << "        BP       Manu        Nb Channel  " << endl ;
681       (*fFilcout) << " ========================================" << endl;
682       while((uncalcounter = (AliMUONErrorCounter*) iter->Next()))
683         {
684           (*fFilcout) << "\t" << uncalcounter->BusPatch() << "\t " << uncalcounter->ManuId() << "\t\t"   << uncalcounter->Events() << endl;
685         }
686       (*fFilcout) << " ========================================" << endl;
687
688       (*fFilcout) << " Number of bad calibrated Manu    = " << uncalBuspatchManuTable->GetSize() << endl ;
689       (*fFilcout) << " Number of bad calibrated channel = " << uncalcountertotal << endl;
690         
691     }
692
693
694   (*fFilcout) << "\n Nb of channels in raw data = " << nmanu*64 << " (" << nmanu << " Manu)" <<  endl;
695   (*fFilcout) << " Nb of calibrated channel   = " << nGoodChannel << " (" << goodA1Min << "<a1<" << goodA1Max 
696               << " and " << goodA2Min << "<a2<" << goodA2Max << ") " << endl;
697   (*fFilcout) << " Nb of uncalibrated channel = " << nBadChannel << " (" << noFitChannel << " unfitted)" << endl;
698
699   cout << "\n Nb of channels in raw data = " << nmanu*64 << " (" << nmanu << " Manu)" <<  endl;
700   cout << " Nb of calibrated channel   = " << nGoodChannel << " (" << goodA1Min << "<a1<" << goodA1Max 
701        << " and " << goodA2Min << "<a2<" << goodA2Max << ") " << endl;
702   cout << " Nb of uncalibrated channel = " << nBadChannel << " (" << noFitChannel << " unfitted)" << endl;
703
704   Double_t meanA1         = sumA1/(nGoodChannel);
705   Double_t meanProbChi2   = sumProbChi2/(nGoodChannel);
706   Double_t meanA2         = sumA2/(nGoodChannel);
707   Double_t meanProbChi2P2 = sumProbChi2P2/(nGoodChannel);
708
709   Double_t capaManu = 0.2; // pF
710   (*fFilcout) << "\n linear fit   : <a1> = " << meanA1 << "\t  <gain>  = " <<  1./(meanA1*capaManu) 
711               << " mV/fC (capa= " << capaManu << " pF)" << endl;
712   (*fFilcout) <<   "        Prob(chi2)>  = " <<  meanProbChi2 << endl;
713   (*fFilcout) << "\n parabolic fit: <a2> = " << meanA2  << endl;
714   (*fFilcout) <<   "        Prob(chi2)>  = " <<  meanProbChi2P2 << "\n" << endl;
715
716   cout << "\n  <gain>  = " <<  1./(meanA1*capaManu) 
717        << " mV/fC (capa= " << capaManu << " pF)" 
718        <<  "  Prob(chi2)>  = " <<  meanProbChi2 << endl;
719   
720   pfilew.close();
721
722   if(fPlotLevel>0){tg->Write();histoFile->Close();}
723   if(fPrintLevel>1){fclose(pfilep); fclose(pfilen);}
724 }