]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONGain.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / MUON / AliMUONGain.cxx
index a03a29074f85fcfb017315afa1cf31cfb738e9df..1a7c0c960e2d72cad40e2b030443df67884b7984 100644 (file)
 #include <Riostream.h>
 
 #include <sstream>
-//
-//AMORE
-//
-#ifdef ALI_AMORE
-#include <AmoreDA.h>
-#include "TSystem.h"
-#endif
+#include <cstdio>
 
 #define  NFITPARAMS 4
 
 
 // functions
 
-
-//______________________________________________________________________________
-Double_t funcLin (const Double_t *x, const Double_t *par)
-{
-  /// Linear function
-  return par[0] + par[1]*x[0];
-}
-
-//______________________________________________________________________________
-Double_t funcParabolic (const Double_t *x, const Double_t *par)
-{
-  /// Parabolic function
-  return par[0]*x[0]*x[0];
-}
-
-//______________________________________________________________________________
-Double_t funcCalib (const Double_t *x, const Double_t *par)  
-{
-  /// Calibration function
-  Double_t xLim= par[3];
-
-  if(x[0] <= xLim) return par[0] + par[1]*x[0];
-
-  Double_t yLim = par[0]+ par[1]*xLim;
-  return yLim + par[1]*(x[0] - xLim) + par[2]*(x[0] - xLim)*(x[0] - xLim);
+namespace {
+  
+  //______________________________________________________________________________
+  Double_t funcLin (const Double_t *x, const Double_t *par)
+  {
+    /// Linear function
+    return par[0] + par[1]*x[0];
+  }
+  
+  //______________________________________________________________________________
+  Double_t funcParabolic (const Double_t *x, const Double_t *par)
+  {
+    /// Parabolic function
+    return par[0]*x[0]*x[0];
+  }
+  
+  //______________________________________________________________________________
+  Double_t funcCalib (const Double_t *x, const Double_t *par)  
+  {
+    /// Calibration function
+    Double_t xLim= par[3];
+    
+    if(x[0] <= xLim) return par[0] + par[1]*x[0];
+    
+    Double_t yLim = par[0]+ par[1]*xLim;
+    return yLim + par[1]*(x[0] - xLim) + par[2]*(x[0] - xLim)*(x[0] - xLim);
+  }
+  
 }
 
-
+using std::endl;
+using std::cout;
+using std::istringstream;
+using std::ostringstream;
 /// \cond CLASSIMP
 ClassImp(AliMUONGain)
 /// \endcond
@@ -96,6 +96,7 @@ ClassImp(AliMUONGain)
 AliMUONGain::AliMUONGain()
 : AliMUONPedestal(),
 fInjCharge(0), 
+fRootDataFileName(),
 fnInit(1),
 fnEntries(11),
 fnbpf1(6),
@@ -104,9 +105,7 @@ fPlotLevel(0)
 {
 /// Default constructor
 
-  sprintf(fRootDataFileName," "); //Gain
 }
-//  AliMUONPedestal& operator=(const AliMUONPedestal& other); Copy ctor
   
 //______________________________________________________________________________
 AliMUONGain::~AliMUONGain()
@@ -122,7 +121,7 @@ TString AliMUONGain::WriteDummyHeader(void)
   ostringstream stream;
   stream<<"//DUMMY FILE (to prevent Shuttle failure)"<< endl;
   stream<<"//================================================" << endl;
-  stream<<"//       MUONTRKda: Calibration run  " << endl;
+  stream<<"//       MUONTRKGAINda: Calibration run  " << endl;
   stream<<"//================================================" << endl;
   stream<<"//   * Run           : " << fRunNumber << endl; 
   stream<<"//   * Date          : " << fDate->AsString("l") <<endl;
@@ -135,11 +134,11 @@ TString AliMUONGain::WriteDummyHeader(void)
 //______________________________________________________________________________
 void AliMUONGain::MakePedStoreForGain(TString shuttleFile)
 {
-///
+/// Store Pedmean and sigma to pedestal-like ascii file
 
   ofstream fileout;
   TString tempstring;
-  Char_t flatFile[256]="";
+  TString flatFile;
   TString outputFile;
   
   // Store pedestal map in root file
@@ -148,15 +147,12 @@ void AliMUONGain::MakePedStoreForGain(TString shuttleFile)
   // write dummy ascii file -> Shuttle
   if(fIndex<fnEntries)
     {  
-      //     fileout.open(shuttleFile.Data());
-      //     tempstring = WriteDummyHeader();
-      //     fileout << tempstring;
       FILE *pfilew=0;
       pfilew = fopen (shuttleFile,"w");
 
       fprintf(pfilew,"//DUMMY FILE (to prevent Shuttle failure)\n");
       fprintf(pfilew,"//================================================\n");
-      fprintf(pfilew,"//       MUONTRKda: Calibration run  \n");
+      fprintf(pfilew,"//       MUONTRKGAINda: Calibration run  \n");
       fprintf(pfilew,"//=================================================\n");
       fprintf(pfilew,"//   * Run           : %d \n",fRunNumber); 
       fprintf(pfilew,"//   * Date          : %s \n",fDate->AsString("l"));
@@ -167,23 +163,28 @@ void AliMUONGain::MakePedStoreForGain(TString shuttleFile)
 
 
 
+  Finalize();
+  MakeControlHistos();
   if(fPrintLevel>0)
     {
       // compute and store mean DAC values (like pedestals)
-      sprintf(flatFile,"%s_%d.ped",fprefixDA,fRunNumber);
+      flatFile = Form("%s.ped",fPrefixDA.Data());
       outputFile=flatFile;
-      cout << "\n" << fprefixDA << " : Flat file  generated  : " << flatFile << "\n";
-      MakePedStore(outputFile);
+      cout << "\n" << fPrefixLDC.Data() << " : Flat file  generated  : " << flatFile.Data() << "\n";
+      if (!outputFile.IsNull())  
+      {
+        ofstream out(outputFile.Data());
+       MakeASCIIoutput(out);
+       out.close();
+      }      
     }
-  else
-    MakePedStore("");
 
   TString mode("UPDATE");
 
   if (fIndex==1) {
     mode = "RECREATE";
   }
-  TFile* histoFile = new TFile(fRootDataFileName, mode.Data(), "MUON Tracking Gains");
+  TFile* histoFile = new TFile(fRootDataFileName.Data(), mode.Data(), "MUON Tracking Gains");
 
   // second argument should be the injected charge, taken from config crocus file
   // put also info about run number could be usefull
@@ -213,25 +214,29 @@ void AliMUONGain::MakePedStoreForGain(TString shuttleFile)
 //______________________________________________________________________________
 TString AliMUONGain::WriteGainHeader(Int_t nInit, Int_t nEntries, Int_t nbpf2, Int_t *numrun, Double_t *injCharge) 
 {
-///
+/// Header of the calibration output file
 
   ostringstream stream;
 
 
-  stream<<"//================================================" << endl;
-  stream<<"//  Calibration file calculated by MUONTRKda" << endl;
-  stream<<"//=================================================" << endl;
+  stream<<"//=======================================================" << endl;
+  stream<<"//      Calibration file calculated by " << fPrefixDA.Data() <<endl;
+  stream<<"//=======================================================" << endl;
   stream<<"//   * Run           : " << fRunNumber << endl; 
   stream<<"//   * Date          : " << fDate->AsString("l") <<endl;
   stream<<"//   * Statictics    : " << fNEvents << endl;
-  stream<<"//   * # of MANUS    : " << fNManu << endl;
+  if(fConfig)
+  stream<<"//   * # of MANUS    : " << fNManuConfig << " read in the Det. config. " << endl;
+  stream<<"//   * # of MANUS    : " << fNManu << " read in raw data " << endl;
+  stream<<"//   * # of MANUS    : " << fNChannel/64 << " written in calibration file " << endl;
   stream<<"//   * # of channels : " << fNChannel << endl;
-  stream<<"//-------------------------------------------------" << endl;
+  stream<<"//-------------------------------------------------------" << endl;
 
   if(nInit==0)
     stream<<"//  "<< nEntries <<" DAC values  fit: "<< fnbpf1 << " pts (1st order) " << nbpf2 << " pts (2nd order)" << endl;
   if(nInit==1)
     stream<<"//  "<< nEntries <<" DAC values  fit: "<< fnbpf1 << " pts (1st order) " << nbpf2 << " pts (2nd order) DAC=0 excluded" << endl;
+  stream<<"//   *  nInit = " << nInit << "  *  f1nbp = " << fnbpf1 << "  *  f2nbp = " <<  nbpf2 << endl; 
 
   stream<<"//   RUN     DAC   " << endl;
   stream<<"//-----------------" << endl;
@@ -248,7 +253,7 @@ TString AliMUONGain::WriteGainHeader(Int_t nInit, Int_t nEntries, Int_t nbpf2, I
 //______________________________________________________________________________
 TString AliMUONGain::WriteGainData(Int_t BP, Int_t Manu, Int_t ch, Double_t p1, Double_t p2, Int_t threshold, Int_t q) 
 {
-///
+/// Write calibration parameters per channel
 
   ostringstream stream("");
   stream << Form("%4i %5i %2i %7.4f %10.3e %4i %2x\n",BP,Manu,ch,p1,p2,threshold,q);
@@ -256,21 +261,23 @@ TString AliMUONGain::WriteGainData(Int_t BP, Int_t Manu, Int_t ch, Double_t p1,
 
 }
 
-//__________
+//_______________________________________________________________________________
 void AliMUONGain::MakeGainStore(TString shuttleFile)
 {
+  Int_t status=0;
   /// Store gains in ASCII files
   ofstream fileout;
   ofstream filcouc;
   TString tempstring;  
-  Char_t filename[256]; 
+  TString filename; 
+  char* detail;
 
   Double_t goodA1Min =  0.5;
   Double_t goodA1Max =  2.;
-  //     Double_t goodA1Min =  0.7;
-  //     Double_t goodA1Max =  1.7;
-  Double_t goodA2Min = -0.5E-03;
-  Double_t goodA2Max =  1.E-03;
+//   Double_t goodA2Min = -0.5E-03;
+//   Double_t goodA2Max =  1.E-03;
+  Double_t goodA2Min = -0.5E-01; // changed 28/10/2009 (JLC) <=> enlarged condition on a2
+  Double_t goodA2Max =  1.E-01;
   // Table for uncalibrated  buspatches and manus
   THashList* uncalBuspatchManuTable = new THashList(1000,2);
 
@@ -283,7 +290,8 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
   // Fit with a polynomial fct
   // store the result in a flat file.
 
-  TFile*  histoFile = new TFile(fRootDataFileName);
+  if(fIndex==0)cout << " Root data file = " << fRootDataFileName.Data() << endl;  
+  TFile*  histoFile = new TFile(fRootDataFileName.Data());
 
   AliMUON2DMap* map[11];
   AliMUONVCalibParam* ped[11];
@@ -315,36 +323,33 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
   Double_t injChargeErr[11];
   for ( Int_t i=0 ; i<11 ; i++) {injCharge[i]=0.;injChargeErr[i]=1.;};
 
-  // some print
-  cout<<"\n ********  MUONTRKda for Gain computing (Last Run = " << fRunNumber << ") ********\n" << endl;
-  cout<<" * Date          : " << fDate->AsString("l") << "\n" << endl;
-  cout << " Entries = " << nEntries << " DAC values \n" << endl; 
+  //  print out in .log file
+  detail=Form("\n%s : ------  MUONTRKGAINda for Gain computing (Last Run = %d) ------\n",fPrefixLDC.Data(),fRunNumber); printf("%s",detail);
+  (*fFilcout)<<"\n\n//=================================================" << endl;
+  (*fFilcout)<<"//    MUONTRKGAINda: Gain Computing  Run = " << fRunNumber << endl;
+  (*fFilcout)<<"//    RootDataFile  = "<< fRootDataFileName.Data() << endl;
+  (*fFilcout)<<"//=================================================" << endl;
+  (*fFilcout)<<"//* Date          : " << fDate->AsString("l") << "\n" << endl;
+
+  (*fFilcout) << " Entries = " << nEntries << " DAC values \n" << endl; 
   for (Int_t i = 0; i < nEntries; ++i) {
-    cout<< " Run = " << run[i]->GetFirst() << "    DAC = " << run[i]->GetSecond() << endl;
+    (*fFilcout) << " Run = " << run[i]->GetFirst() << "    DAC = " << run[i]->GetSecond() << endl;
     numrun[i] = run[i]->GetFirst();
     injCharge[i] = run[i]->GetSecond();
     injChargeErr[i] = 0.01*injCharge[i];
     if(injChargeErr[i] <= 1.) injChargeErr[i]=1.;
   }
-  cout << "" << endl;
-
-  //  print out in .log file
-
-  (*fFilcout)<<"\n\n//=================================================" << endl;
-  (*fFilcout)<<"//    MUONTRKda: Gain Computing  Run = " << fRunNumber << endl;
-  (*fFilcout)<<"//    RootDataFile  = "<< fRootDataFileName << endl;
-  (*fFilcout)<<"//=================================================" << endl;
-  (*fFilcout)<<"//* Date          : " << fDate->AsString("l") << "\n" << endl;
-
+  detail=Form("%s : .... Fitting .... \n",fPrefixLDC.Data()); printf("%s",detail);
 
 
   // why 2 files ? (Ch. F.)  => second file contains detailed results
     FILE *pfilen = 0;
     if(fPrintLevel>1)
       {
-        sprintf(filename,"%s_%d.param",fprefixDA,fRunNumber);
-        cout << " Second fit parameter file        = " << filename << "\n";
-        pfilen = fopen (filename,"w");
+        filename=Form("%s.param",fPrefixDA.Data());
+       detail=Form("%s : Second fit parameter file        = %s\n",fPrefixLDC.Data(),filename.Data()); printf("%s",detail);
+       //       cout << " Second fit parameter file        = " << filename.Data() << "\n";
+        pfilen = fopen (filename.Data(),"w");
 
         fprintf(pfilen,"//===================================================================\n");
         fprintf(pfilen,"//  BP MANU CH. par[0]     [1]     [2]     [3]      xlim          P(chi2) p1        P(chi2)2  p2\n");
@@ -354,24 +359,20 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
       }
 
 
-
-
-  //  Write Ascii file -> Shuttle
+  // file outputs for gain
 
   ofstream pfilew;
   pfilew.open(shuttleFile.Data());
+  // Write Header Data of the .par file
   pfilew << WriteGainHeader(fnInit,nEntries,nbpf2,numrun,injCharge);
-#ifdef ALI_AMORE
-  ostringstream stringout; // String to be sent to AMORE_DB
-  stringout << WriteGainHeader(fnInit,nEntries,nbpf2,numrun,injCharge);
-#endif
 
   // print mean and sigma values in file
   FILE *pfilep = 0;
   if(fPrintLevel>1)
     {
-      sprintf(filename,"%s_%d.peak",fprefixDA,fRunNumber);
-      cout << " File containing Peak mean values = " << filename << "\n";
+      filename=Form("%s.peak",fPrefixDA.Data());
+      detail=Form("%s : File containing Peak mean values = %s\n",fPrefixLDC.Data(),filename.Data()); printf("%s",detail);
+      //      cout << " File containing Peak mean values = " << filename << "\n";
       pfilep = fopen (filename,"w");
 
       fprintf(pfilep,"//==============================================================================================================================\n");
@@ -396,17 +397,16 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
   Int_t q = 0;
   Int_t p1 =0;
   Int_t p2 =0;
-  Double_t gain=0; 
+  Double_t gain=10.; // max value (= bad value)
   Double_t capa=0.2; // internal capacitor (pF)
 
   //  plot out 
 
-  TFile* gainFile = 0x0;
   TTree* tg = 0x0;
   if(fPlotLevel>0)
     {
-      sprintf(fHistoFileName,"%s_%d.root",fprefixDA,fRunNumber);
-     gainFile = new TFile(fHistoFileName,"RECREATE","MUON Tracking gains");
+      fHistoFileName=Form("%s.root",fPrefixDA.Data());
+      new TFile(fHistoFileName.Data(),"RECREATE","MUON Tracking gains");
       tg = new TTree("tg","TTree avec class Manu_DiMu");
 
       tg->Branch("bp",&busPatchId, "busPatchId/I");
@@ -445,10 +445,12 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
   Double_t xp[11], xpErr[11], yp[11], ypErr[11];
 
   Int_t uncalcountertotal=0 ;
+  Int_t unparabolicfit=0;
 
   while ( ( p = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
     {
       ped[0]  = p;
+      unparabolicfit=0;
 
       busPatchId = p->ID0();
       manuId     = p->ID1();
@@ -475,7 +477,6 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
            n++;
          }
 
-
          // print_peak_mean_values
          if(fPrintLevel>1)
            {
@@ -485,24 +486,30 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
              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]);
            }
 
-         // makegain 
-
-
+         // makegain
          // Fit Method:  Linear fit over gAlinbpf1 points + parabolic fit  over nbpf2  points) 
          // nInit=1 : 1st pt DAC=0 excluded
 
          // 1. - linear fit over gAlinbpf1 points
 
-         Double_t par[4] = {0.,0.5,0.,kADCMax};
+         Double_t par[4] = {0.,0.5,0.,ADCMax()};
          Int_t nbs   = nEntries - fnInit;
          if(nbs < fnbpf1)fnbpf1=nbs;
 
          Int_t fitproceed=1;
+         Int_t nbpf2Dynamic=nbpf2;
+         Int_t adcLimit=4090; // when RMS < 0.5 (in other cases mean values forced to 4095, see DA_PED)
          for (Int_t j = 0; j < nbs; ++j)
            {
              Int_t k = j + fnInit;
              x[j]    = pedMean[k];
-             if(x[j]==0.)fitproceed=0;
+             if(x[j]<=0.){fitproceed=0; break;}
+             //              if(x[j]>= ADCMax())
+             if(x[j]>= adcLimit)
+               {
+                 if(j < nbs-1){fitproceed=0; break;}
+                 else { nbpf2Dynamic=nbpf2-1; break;}
+               }
              xErr[j] = pedSigma[k];
              y[j]    = injCharge[k];
              yErr[j] = injChargeErr[k];
@@ -515,7 +522,7 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
          if(fitproceed)
            {
                      
-             TF1 *f1 = new TF1("f1",funcLin,0.,kADCMax,2);
+             TF1 *f1 = new TF1("f1",funcLin,0.,ADCMax(),2);
              graphErr = new TGraphErrors(fnbpf1, x, y, xErr, yErr);
 
              f1->SetParameters(0,0);
@@ -538,10 +545,10 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
              a1 = par[1];
 
              // 2. - Translation : new origin (xLim, yLim) + parabolic fit over nbf2 points
-
-             if(nbpf2 > 1)
+             //checking:         if(busPatchId ==1841 && manuId==4)nbpf2Dynamic=2;
+             if(nbpf2Dynamic > 2)
                {
-                 for (Int_t j = 0; j < nbpf2; j++)
+                 for (Int_t j = 0; j < nbpf2Dynamic; j++)
                    {
                      Int_t k  = j + (fnInit + fnbpf1) - 1;
                      xp[j]    = pedMean[k] - xLim;
@@ -551,8 +558,8 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
                      ypErr[j] = injChargeErr[k];
                    }
 
-                 TF1 *f2 = new TF1("f2",funcParabolic,0.,kADCMax,1);
-                 graphErr = new TGraphErrors(nbpf2, xp, yp, xpErr, ypErr);
+                 TF1 *f2 = new TF1("f2",funcParabolic,0.,ADCMax(),1);
+                 graphErr = new TGraphErrors(nbpf2Dynamic, xp, yp, xpErr, ypErr);
 
                  graphErr->Fit(f2,"RQ");
                  chi2P2 = f2->GetChisquare();
@@ -562,9 +569,16 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
                  graphErr=0;
                  delete f2;
 
-                 prChi2P2 = TMath::Prob(chi2P2, nbpf2-1);
+                 prChi2P2 = TMath::Prob(chi2P2, nbpf2Dynamic-1);
                  a2 = par[0];
                }
+             else 
+               { 
+                 unparabolicfit++;
+                 (*fFilcout) << " Warning : BP = " << busPatchId << " Manu = " << manuId <<  " Channel = " << channelId <<": parabolic fit not possible (nbpf2=" <<  nbpf2Dynamic  << ") => a2=0 and linear fit OK" << std::endl;
+                 if(unparabolicfit==1) std::cout << " Warning : BP = " << busPatchId << " Manu = " << manuId <<  ": no parabolic fit for some channels (nbpf2=" <<  nbpf2Dynamic  << "), linear fit is OK (see .log for details)" << std::endl;
+                 a2=0. ; prChi2P2=0. ;
+               }
 
              par[0] = a0;
              par[1] = a1;
@@ -607,13 +621,13 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
              q=0;  
              par[1]=0.5; a1=0.5; p1=0;
              par[2]=0.;  a2=0.;  p2=0;
-             threshold=kADCMax;        
+             threshold=ADCMax();       
 
              // bad calibration counter
              char bpmanuname[256];
              AliMUONErrorCounter* uncalcounter;
 
-             sprintf(bpmanuname,"bp%dmanu%d",busPatchId,manuId);
+             snprintf(bpmanuname,256,"bp%dmanu%d",busPatchId,manuId);
              if (!(uncalcounter = (AliMUONErrorCounter*)uncalBuspatchManuTable->FindObject(bpmanuname)))
                {
                  // New buspatch_manu name
@@ -636,24 +650,25 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
                {
                  //                  if(q==0  and  nplot < 100)
                  //      if(p1>1 && p2==0  and  nplot < 100)
-                           if(p1>10 && p2>10  and  nplot < 100)
+                 //                        if(p1>10 && p2>10  and  nplot < 100)
                  //    if(p1>=1 and p1<=2  and  nplot < 100)
 //               if((p1==1 || p2==1) and  nplot < 100)
+                           if(nbpf2Dynamic<nbpf2  and  nplot < 100)
                    {
                      nplot++;
                      //              cout << " nplot = " << nplot << endl;
-                     TF1 *f2Calib = new TF1("f2Calib",funcCalib,0.,kADCMax,NFITPARAMS);
+                     TF1 *f2Calib = new TF1("f2Calib",funcCalib,0.,ADCMax(),NFITPARAMS);
 
                      graphErr = new TGraphErrors(nEntries,pedMean,injCharge,pedSigma,injChargeErr);
 
-                     sprintf(graphName,"BusPatch_%d_Manu_%d_Ch_%d",busPatchId, manuId,channelId);
+                     snprintf(graphName,256,"BusPatch_%d_Manu_%d_Ch_%d",busPatchId, manuId,channelId);
 
                      graphErr->SetTitle(graphName);
                      graphErr->SetMarkerColor(3);
                      graphErr->SetMarkerStyle(12);
                      graphErr->Write(graphName);
 
-                     sprintf(graphName,"f2_BusPatch_%d_Manu_%d_Ch_%d",busPatchId, manuId,channelId);
+                     snprintf(graphName,256,"f2_BusPatch_%d_Manu_%d_Ch_%d",busPatchId, manuId,channelId);
                      f2Calib->SetTitle(graphName);
                      f2Calib->SetLineColor(4);
                      f2Calib->SetParameters(par);
@@ -664,21 +679,13 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
                      delete f2Calib;
                    }
                }
-
-
              tg->Fill();
            }
-
-
-         tempstring = WriteGainData(busPatchId,manuId,channelId,par[1],par[2],threshold,q);
-         pfilew << tempstring;
-#ifdef ALI_AMORE
-         stringout << tempstring;
-#endif
-
+         pfilew << WriteGainData(busPatchId,manuId,channelId,par[1],par[2],threshold,q);
        }
       nmanu++;
-      if(nmanu % 500 == 0)std::cout << " Nb manu = " << nmanu << std::endl;
+      Int_t step=500;
+      if(nmanu % step == 0)printf("%s : Nb manu = %d\n",fPrefixLDC.Data(),nmanu);
     }
 
   //      print in logfile
@@ -701,56 +708,43 @@ void AliMUONGain::MakeGainStore(TString shuttleFile)
       (*fFilcout) << " Number of bad calibrated channel = " << uncalcountertotal << endl;
        
     }
+  if(nmanu && nGoodChannel) 
+    {
+      Double_t ratio_limit=0.25;
+      Double_t ratio=float (nBadChannel)/float (nmanu*64);
+
+      detail=Form("\n%s : Nb of channels in raw data = %d (%d Manu)",fPrefixLDC.Data(),nmanu*64,nmanu); 
+      (*fFilcout) << detail ; printf("%s",detail);
+      detail=Form("\n%s : Nb of calibrated channel   = %d  (%4.2f <a1< %4.2f and %4.2f <a2< %4.2f)",fPrefixLDC.Data(),nGoodChannel,goodA1Min,goodA1Max, goodA2Min,goodA2Max);
+      (*fFilcout) << detail ; printf("%s",detail);
+      detail=Form("\n%s : Nb of uncalibrated channel = %d  [%6.4f] (%d unfitted channels) ",fPrefixLDC.Data(),nBadChannel,ratio,noFitChannel);
+      (*fFilcout) << detail ; printf("%s",detail);
+
+      if(ratio > ratio_limit) { status=-1;
+       detail=Form("\n%s : !!!!! WARNING : Nb of uncalibrated channels very large : %6.4f > %6.4f (status= %d) ",fPrefixLDC.Data(),ratio,ratio_limit,status);
+       (*fFilcout) << detail ; printf("%s",detail); 
+      }
 
-
-  (*fFilcout) << "\n Nb of channels in raw data = " << nmanu*64 << " (" << nmanu << " Manu)" <<  endl;
-  (*fFilcout) << " Nb of calibrated channel   = " << nGoodChannel << " (" << goodA1Min << "<a1<" << goodA1Max 
-             << " and " << goodA2Min << "<a2<" << goodA2Max << ") " << endl;
-  (*fFilcout) << " Nb of uncalibrated channel = " << nBadChannel << " (" << noFitChannel << " unfitted)" << endl;
-
-  cout << "\n Nb of channels in raw data = " << nmanu*64 << " (" << nmanu << " Manu)" <<  endl;
-  cout << " Nb of calibrated channel   = " << nGoodChannel << " (" << goodA1Min << "<a1<" << goodA1Max 
-       << " and " << goodA2Min << "<a2<" << goodA2Max << ") " << endl;
-  cout << " Nb of uncalibrated channel = " << nBadChannel << " (" << noFitChannel << " unfitted)" << endl;
-
-  Double_t meanA1         = sumA1/(nGoodChannel);
-  Double_t meanProbChi2   = sumProbChi2/(nGoodChannel);
-  Double_t meanA2         = sumA2/(nGoodChannel);
-  Double_t meanProbChi2P2 = sumProbChi2P2/(nGoodChannel);
-
-  Double_t capaManu = 0.2; // pF
-  (*fFilcout) << "\n linear fit   : <a1> = " << meanA1 << "\t  <gain>  = " <<  1./(meanA1*capaManu) 
-             << " mV/fC (capa= " << capaManu << " pF)" << endl;
-  (*fFilcout) <<   "        Prob(chi2)>  = " <<  meanProbChi2 << endl;
-  (*fFilcout) << "\n parabolic fit: <a2> = " << meanA2  << endl;
-  (*fFilcout) <<   "        Prob(chi2)>  = " <<  meanProbChi2P2 << "\n" << endl;
-
-  cout << "\n  <gain>  = " <<  1./(meanA1*capaManu) 
-       << " mV/fC (capa= " << capaManu << " pF)" 
-       <<  "  Prob(chi2)>  = " <<  meanProbChi2 << endl;
-  
-  // file outputs for gain
-
+      Double_t meanA1         = sumA1/(nGoodChannel);
+      Double_t meanProbChi2   = sumProbChi2/(nGoodChannel);
+      Double_t meanA2         = sumA2/(nGoodChannel);
+      Double_t meanProbChi2P2 = sumProbChi2P2/(nGoodChannel);
+      Double_t capaManu = 0.2; // pF
+      (*fFilcout) << "\n linear fit   : <a1> = " << meanA1 << "     Prob(chi2)>  = " <<  meanProbChi2 <<  "    <gain>  = " <<  1./(meanA1*capaManu) 
+                 << " mV/fC (capa= " << capaManu << " pF)" << endl;
+      (*fFilcout)   <<" parabolic fit: <a2> = " << meanA2  << "    Prob(chi2)>  = " <<  meanProbChi2P2 << "\n" <<  endl;
+      detail=Form("\n%s : <gain>  = %7.5f  mV/fC (capa= %3.1f pF)  Prob(chi2) = %5.3f\n" ,fPrefixLDC.Data(),1./(meanA1*capaManu),capaManu,meanProbChi2);
+      printf("%s",detail);
+    }
+  else 
+    { status=-1;
+      detail=Form("\n%s : !!!!! ERROR :  Nb of Manu = %d or Nb calibrated channel = %d !!!!! (status= %d)\n",fPrefixLDC.Data(),nmanu,nGoodChannel,status);
+      (*fFilcout) << detail ; printf("%s",detail);
+    }
   pfilew.close();
-#ifdef ALI_AMORE
-  //
-  //Send objects to the AMORE DB
-  //
-  const char *role=gSystem->Getenv("AMORE_DA_NAME");
-  if ( role ){
-    amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
-//  TObjString peddata(stringout.str().c_str());
-    TObjString gaindata(stringout.str().c_str());
-    Int_t status =0;
-    status = amoreDA.Send("Pedestals",&gaindata);
-    if ( status )
-      cout << "Warning: Failed to write Pedestals in the AMORE database : " << status << endl;
-  } 
-  else {
-    cout << "Warning: environment variable 'AMORE_DA_NAME' not set. Cannot write to the AMORE database" << endl;
-  }
-#endif
 
   if(fPlotLevel>0){tg->Write();histoFile->Close();}
   if(fPrintLevel>1){fclose(pfilep); fclose(pfilen);}
+  SetStatusDA(status);
 }