]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - T0/AliT0QAChecker.cxx
Upadte from Taku
[u/mrichter/AliRoot.git] / T0 / AliT0QAChecker.cxx
index e7ab24d5c0542a91e387df179276d40546a819d2..5a4b80911a1fd8b7f0399f4590e9fdedc1c3cba8 100644 (file)
 #include <Riostream.h>
 #include <TClass.h>
 #include <TH1F.h> 
+#include <TF1.h> 
+#include <TFitResultPtr.h>
 #include <TH2.h> 
 #include <TIterator.h> 
 #include <TKey.h> 
 #include <TFile.h> 
 #include <TMath.h>
 #include <TString.h>
+#include <TPaveText.h>
 
 // --- Standard library ---
 
 #include "AliT0QAChecker.h"
 
 ClassImp(AliT0QAChecker)
+//____________________________________________________________________________
+AliT0QAChecker::AliT0QAChecker() :
+AliQACheckerBase("T0","T0 Quality Assurance Checker")
 
-//__________________________________________________________________
-Double_t * AliT0QAChecker::Check(AliQAv1::ALITASK_t index, TObjArray ** list)
 {
+  // Standard constructor
+
+}
+
+//____________________________________________________________________________
+AliT0QAChecker::AliT0QAChecker(const AliT0QAChecker& qac):
+  AliQACheckerBase(qac.GetName(), qac.GetTitle()) 
+{
+  // copy constructor
+  AliError("Copy should not be used with this class\n");
+}
+//____________________________________________________________________________
+AliT0QAChecker& AliT0QAChecker::operator=(const AliT0QAChecker& qac){
+  // assignment operator
+  this->~AliT0QAChecker();
+  new(this)AliT0QAChecker(qac);
+  return *this;
+}
+
+
+//____________________________________________________________________________
+AliT0QAChecker::~AliT0QAChecker(){
+  // destructor
 
+}
+
+//__________________________________________________________________
+void AliT0QAChecker::Check(Double_t *  test, AliQAv1::ALITASK_t index, TObjArray ** list, const AliDetectorRecoParam * /*recoParam*/)
+{
   // Super-basic check on the QA histograms on the input list:
   // look whether they are empty!
-  
-  Double_t * test = new Double_t[AliRecoParam::kNSpecies] ; 
-  
-  char * detOCDBDir = Form("T0/%s/%s", AliQAv1::GetRefOCDBDirName(), AliQAv1::GetRefDataDirName()) ; 
+    
+ char * detOCDBDir = Form("T0/%s/%s", AliQAv1::GetRefOCDBDirName(), AliQAv1::GetRefDataDirName()) ; 
 
   AliCDBEntry *QARefRec = AliQAManager::QAManager()->Get(detOCDBDir);
   //  QARefRec->Dump();
   if( !QARefRec){
     AliInfo("QA reference data NOT retrieved for Reconstruction check. No T0 reference distribution");
   }
-  
-   for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) 
-    test[specie]    = 10.0 ; 
-
-  Double_t nent[500];
-  TString hname[500];
-  const char *cname;
-  memset(nent,0,500*sizeof(Double_t));
-  Double_t w[500];
-  memset(w,1,500*sizeof(Double_t));
-  TH2 *fhRecDiff[3];  
-  TH1 *fhRawEff[500];
-  TH2 *fhRawTime[500];
-  TH1 *fhESD[2];
-  TH2 *fhHits[10];
-  
-  for (Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
-    //  TString dataType = AliQAv1::GetAliTaskName(index);
-    if (list[specie]->GetEntries() == 0){
-      test[specie] = 1. ; // nothing to check
+
+    
+  for(Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++){ 
+    test[specie]    = 1.0; //FK//  initiate qa flag for the whole set of histograms as good 
+  }
+
+
+  for(Int_t specie = 0 ; specie < AliRecoParam::kNSpecies ; specie++) {
+    if(!(AliQAv1::Instance()->IsEventSpecieSet(specie) && list[specie]) || list[specie]->GetEntries() == 0) {
+      continue;
     }
-    else {
-      TH1 * hdata ;
-      TH2 * h ;
-      for (Int_t ir=0; ir<list[specie]->GetEntries(); ir++) {
-      
-       //hits
-
-       if(index == AliQAv1::kSIM && AliQAv1::GetTaskName(AliQAv1::kHITS)){
-         h =  (TH2*) list[specie]->UncheckedAt(ir);
-         cname = h->GetName();
-         hname[ir] = cname;
-         fhHits[ir] = h;
-       }
-
-       //raw
-      if(index == AliQAv1::kRAW ){
-        if(ir > 204 && ir<208 ) {
-          hdata = (TH1*) list[specie]->UncheckedAt(ir);
-          if(hdata) {
-            cname = hdata->GetName();
-            hname[ir] = cname;
-            fhRawEff[ir] = hdata;
-          }
-        }
-       if((ir>207 && ir<210)  && specie == 4) {
-         h =  (TH2*) list[specie]->UncheckedAt(ir);
-         if(h) {
-           cname = h->GetName();
-           hname[ir] = cname;
-           fhRawTime[ir] = h;
-          AliDebug(AliQAv1::GetQADebugLevel(), Form("count %i %s ",ir, hname[ir].Data()) );
-         }
-       }
+
+    if(index == AliQAv1::kRAW){
+
+      if(AliRecoParam::ConvertIndex(specie) == AliRecoParam::kCalib){//      if (index == AliQAv1::kRAW )
+        //check laser data efficiencies   
+        Double_t qaFlag = CheckLaser(list[specie]);
+        if(qaFlag < test[specie]) test[specie] = qaFlag;
       }
-      //rec
-      if(index == AliQAv1::kREC){
-        h = (TH2*) list[specie]->UncheckedAt(ir);
-        if(h) {
-          cname = h->GetName();
-          hname[ir] = cname;
-           fhRecDiff[ir] = h;
-        }
+
+      if(AliRecoParam::ConvertIndex(specie) == AliRecoParam::kCalib   ||
+         AliRecoParam::ConvertIndex(specie) == AliRecoParam::kHighMult ||
+         AliRecoParam::ConvertIndex(specie) == AliRecoParam::kLowMult){ 
+         //AliRecoParam::ConvertIndex(specie) == AliRecoParam::kDefault ||
+         
+        //check BCID   
+        Double_t qaFlag = CheckBCID(list[specie]);
+        if(qaFlag < test[specie]) test[specie] = qaFlag;
       }
-      //esd
-      if(index ==  AliQAv1::kESD){
-        hdata = (TH1*) list[specie]->UncheckedAt(ir);
-        if(hdata){
-          fhESD[ir] = hdata;
-          AliDebug(AliQAv1::GetQADebugLevel(), Form("count %i %s ",ir, hname[ir].Data()) );
-        }
+
+      if(AliRecoParam::ConvertIndex(specie) == AliRecoParam::kHighMult ||
+        AliRecoParam::ConvertIndex(specie) == AliRecoParam::kLowMult){ 
+        //AliRecoParam::ConvertIndex(specie) == AliRecoParam::kDefault ||
+        //check physics 
+        Double_t qaFlag = CheckRaw(list[specie]);
+        if(qaFlag < test[specie]) test[specie] = qaFlag;
       }
     }
 
+    if(index == AliQAv1::kESD && AliRecoParam::Convert(specie) != AliRecoParam::kCalib){
+      test[specie] = CheckESD(list[specie]);
+    } 
+  }
+}
 
+//--------------------------------------------------------------------------
+Double_t AliT0QAChecker::CheckLaser(TObjArray *listrec) const {
+   
+  TH1 *hdata; 
+  TH1 *fhRawEff[10];
+  Int_t nEffHistos=0;
 
-    //raw data
+  //thresholds for warning and error on efficiencies
+  Float_t thrWarning = 0.5; //FK//  warning level
+  Float_t thrError   = 0.2; //FK//  error level
 
-      if (index == AliQAv1::kRAW && specie == 4){
-       test[specie] = CheckRaw(list[specie],dynamic_cast<TObjArray*>(dynamic_cast<TList *>(QARefRec->GetObject())->First()));
-      }
-      
-      if(index == AliQAv1::kREC){
-        //rec points
-        for (Int_t icase=0; icase<2; icase++) {
-          for (Int_t idet=0; idet<24; idet++) {
-            Double_t mean = fhRecDiff[icase]->
-            ProjectionY(Form("%s_py", fhRecDiff[icase]->GetName()),
-                        idet,idet+1)->GetMean();
-            Double_t rms= fhRecDiff[icase]->
-            ProjectionY(Form("%s_py", fhRecDiff[icase]->GetName()),
-                        idet,idet+1)->GetRMS();
-            AliDebug(AliQAv1::GetQADebugLevel(), Form("name %s icase %i idet %i mean %f, rms %f\n",
-                         fhRecDiff[icase]->GetName(), icase, idet, mean,rms)); 
-                 
-            if(TMath::Abs(mean) >1.5 || rms >1){
-              AliDebug(AliQAv1::GetQADebugLevel(), Form(" calibration is nor perfect; test=%f", test)) ;
-              test[specie]=0.25;
-            }
-            if(mean>3 || rms >5) {
-              test[specie] = 0.1;
-              AliDebug(AliQAv1::GetQADebugLevel(), Form(" wrong calibration test=%f", test[specie])) ;
-            } 
-          }
-        }       
-      }
-      if (index == AliQAv1::kESD) {
-        //ESD
-        for (Int_t icase=0; icase<2; icase++) {
-          Double_t rmsVertex = fhESD[icase]->GetRMS();
-          Double_t meanVertex = fhESD[icase]->GetMean();
-          test[specie]=1;
-          AliDebug(AliQAv1::GetQADebugLevel(), Form("numentries %d meanVertex %f rmsVertex %f", fhESD[icase]->GetEntries(), meanVertex, rmsVertex));
-          if (TMath::Abs(rmsVertex)>3) {
-            test[specie]=0.25;
-            AliDebug(AliQAv1::GetQADebugLevel(), Form("Vertex position resolution not good  , rms= %f test=%f",
-                            rmsVertex, test[specie])) ; 
-          }
-          if (TMath::Abs(meanVertex)>3) {
-            test[specie]=0.25;
-            AliDebug(AliQAv1::GetQADebugLevel(), Form("Vertex position bad calibrated  , Mean= %f test=%f",
-                            meanVertex, test[specie])) ; 
-          }
+  const int kNumberOfHistos = 3; 
+  Int_t consecutiveHistoNumber[kNumberOfHistos] = { 207, 208, 209};  //Checked histos   fhCDFeff, hEffLED, hEffQTC
+  Int_t qualityFlag[kNumberOfHistos]; //quality flag for a given histogram
+
+  for(Int_t ir=0; ir<kNumberOfHistos; ir++){
+    qualityFlag[ir] = kT0Info; //init quality flag for a given histogram
+
+    hdata = (TH1*) listrec->UncheckedAt(consecutiveHistoNumber[ir]);
+    if(hdata){
+      fhRawEff[nEffHistos] = hdata;
+      nEffHistos++;
+    }
+  }
+     
+  TLine linelowyellow(0, thrWarning, 24, thrWarning);    
+  linelowyellow.SetLineColor(5);
+  linelowyellow.SetLineStyle(3);
+  linelowyellow.SetLineWidth(4);
+  TLine linelowred(0, thrError, 24, thrError);    
+  linelowred.SetLineColor(2);
+  linelowred.SetLineStyle(3);
+  linelowred.SetLineWidth(4);
+
+  Bool_t bEffHistosNotEmpty = kFALSE; //check if all histograms have some counts
+  for(Int_t ih = 0; ih < nEffHistos; ih++){
+     
+    EraseOldMessages((TH1*) fhRawEff[ih]);// clean objects added at previous checks
+    fhRawEff[ih]->SetLineWidth(2);
+    fhRawEff[ih]->SetMaximum(2.);
+    fhRawEff[ih]->SetMinimum(0.);
+    fhRawEff[ih]->GetListOfFunctions()->Add((TLine*)linelowyellow.Clone());
+    fhRawEff[ih]->GetListOfFunctions()->Add((TLine*)linelowred.Clone());
+
+    if(fhRawEff[ih]->Integral()>0) bEffHistosNotEmpty = kTRUE; //this histo does have some counts in it
+
+    Int_t nbins= fhRawEff[ih]->GetNbinsX();
+    for(Int_t ib=1; ib<=nbins; ib++){ //loop over bins and check if the efficiency is above level
+        
+      Float_t chcont = fhRawEff[ih]->GetBinContent(ib);
+      if(chcont < thrWarning && qualityFlag[ih] > kT0Error  ) qualityFlag[ih] = kT0Warning;//Warning level
+      if(chcont < thrError)  qualityFlag[ih] = kT0Error;//Error level
+    }
+   
+    if(qualityFlag[ih] == kT0Info ){
+      AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 efficiency  %s  is good", fhRawEff[ih]->GetName() ));
+    }else if(qualityFlag[ih] == kT0Warning){ 
+      AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 efficiency  %s  is not so good", fhRawEff[ih]->GetName() ));
+    }else if(qualityFlag[ih] == kT0Error){
+      AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 efficiency  %s  is not good", fhRawEff[ih]->GetName() ));
+    }
+  }
+
+  //executive summary
+  int lowestQualityFlag = (int) kT0Info;
+  for(Int_t ih = 0; ih < nEffHistos; ih++){
+
+    if(!bEffHistosNotEmpty){ //all laser efficiency plots are empty
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("1) T0 is in BEAMTUNIG: empty plots are ok")); 
+      text.AddText(Form("2) T0 is in READY: check calibriation trigger")); 
+      text.AddText(Form("if also physics data are empty report"));
+      text.AddText(Form("readout problem to the T0 on-call expert")); 
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      fhRawEff[ih]->GetListOfFunctions()->Add((TPaveText*)text.Clone());             
+    }
+    if( qualityFlag[ih] <lowestQualityFlag )  lowestQualityFlag = qualityFlag[ih];
+  }
+   
+  return ConvertQualityFlagToDouble(lowestQualityFlag); 
+}
+//--------------------------------------------------------------------------
+Double_t AliT0QAChecker::CheckBCID(TObjArray *listrec) const {
+   
+  Int_t qualityFlagBCID = kT0Info; //init quality flag for a given histogram; 
+
+  TH2F *hBCID = (TH2F*) listrec->UncheckedAt(224); //BCID versus TRM  BCID
+
+     
+  // clean objects added at previous checks
+  EraseOldMessages((TH1*)hBCID);
+
+  if(hBCID->Integral()>0){
+    //BCID does have some counts in it
+
+    Int_t nbinsX = hBCID->GetNbinsX();
+    Int_t nbinsY = hBCID->GetNbinsY();
+    double entriesOnDiagonal  = 0; //count diagonal and off diagonal entries
+    double entriesOffDiagonal = 0;
+    Int_t offset = 37; 
+    Int_t period = 3564; 
+
+    for(Int_t itrm=1; itrm<=nbinsX; itrm++){ //BCID TRM
+      for(Int_t ibcid=1; ibcid<=nbinsY; ibcid++){ //BCID
+        if(TMath::Abs(itrm-ibcid)<6 || TMath::Abs( (itrm+offset)%period - ibcid)<2 ){ 
+           entriesOnDiagonal  += hBCID->GetBinContent(itrm,ibcid); //On  Diagonal
+        }else{
+         entriesOffDiagonal += hBCID->GetBinContent(itrm,ibcid); //Off Diagonal
         }
       }
-    } //  if (list->GetEntries() != 0
-    AliDebug(AliQAv1::GetQADebugLevel(), Form("Test Result = %f", test[specie])) ;
-  } 
+    }
+    if(entriesOnDiagonal<1 || entriesOffDiagonal>20){
+      qualityFlagBCID = kT0Warning; //no entries on diagonal
+      AliDebug(AliQAv1::GetQADebugLevel(), Form("T0   %s is not diagonal", hBCID->GetName() ));
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("Check if entries are on a diagonal.")); 
+      text.AddText(Form("Report readout problem to the T0 on-call expert")); 
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      hBCID->GetListOfFunctions()->Add((TPaveText*)text.Clone());            
+    }
+  }else{ //BCID empty
+
+    qualityFlagBCID = kT0Error;
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 :  %s has NO entries", hBCID->GetName() ));
+
+    TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+    text.AddText(Form("NO ENTRIES!!!")); 
+    text.AddText(Form("If T0 is READY report")); 
+    text.AddText(Form("readout problem to the T0 on-call expert"));
+    text.SetBorderSize(0);
+    text.SetFillStyle(0);
+    hBCID->GetListOfFunctions()->Add((TPaveText*)text.Clone());              
+  }
+
+  //executive summary
+  int lowestQualityFlag = (int) qualityFlagBCID;
+
+  return ConvertQualityFlagToDouble(lowestQualityFlag); 
   
-  return test ;
 }
 
 //--------------------------------------------------------------------------
-Double_t AliT0QAChecker::CheckRaw(TObjArray *listrec , TObjArray *listref) const
-{
+Double_t AliT0QAChecker::CheckRaw(TObjArray *listrec) const {
+   
+
+  Int_t qualityFlagTrigger = kT0Info; //init quality flag for a given histogram; 
+
+  TH1F *hTrigger = (TH1F*) listrec->UncheckedAt(169);//hRawTrigger 
+
+     
+  // clean objects added at previous checks
+  EraseOldMessages((TH1*) hTrigger); 
+
+  if(hTrigger->Integral()>0){
+    //trigger plot does have some counts in it
+    //are Mean, ORA and ORC not empty?  
+    if( hTrigger->GetBinContent(1)<0.001 || hTrigger->GetBinContent(3)<0.001 || hTrigger->GetBinContent(4)<0.001){
+      qualityFlagTrigger = kT0Error; //no entries on diagonal
+      AliDebug(AliQAv1::GetQADebugLevel(), Form("T0: too little ORA and ORC in  %s", hTrigger->GetName() ));
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("Check ORA and ORC")); 
+      text.AddText(Form("Report problem to the T0 on-call expert")); 
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      hTrigger->GetListOfFunctions()->Add((TPaveText*)text.Clone());         
+    }
+
+  }else{ //Trigger histo empty
+
+    qualityFlagTrigger = kT0Error;
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 histogram  %s has NO entries", hTrigger->GetName() ));
+
+    TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+    text.AddText(Form("NO ENTRIES!!!")); 
+    text.AddText(Form("If T0 is READY report")); 
+    text.AddText(Form("readout problem to the T0 on-call expert")); 
+    text.SetBorderSize(0);
+    text.SetFillStyle(0);
+    hTrigger->GetListOfFunctions()->Add((TPaveText*)text.Clone());           
+
+  }
+
+  //--------- timing plots ---------
+  Int_t qualityFlagMeanBest = kT0Info; //init quality flag for a given histogram;
+  TH1F *hMeanBest    = (TH1F*) listrec->UncheckedAt(223);  //fhMeanBest  time
+  EraseOldMessages((TH1*) hMeanBest); 
+
+  if(hMeanBest->Integral()<1){
+    qualityFlagMeanBest = kT0Error;
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 histogram  %s has NO entries", hMeanBest->GetName() ));
+
+    TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+    text.AddText(Form("NO ENTRIES!!!")); 
+    text.AddText(Form("If T0 is READY and beam is on report")); 
+    text.AddText(Form("the problem to the T0 on-call expert"));
+    text.SetBorderSize(0);
+    text.SetFillStyle(0);
+    hMeanBest->GetListOfFunctions()->Add((TPaveText*)text.Clone());          
+  }
+
+
+
+
+  //----------- vetext plots ---------
+
+  Int_t qualityFlagVetrexFirst = kT0Info; //init quality flag for a given histogram;
+  Int_t qualityFlagVetrexBest  = kT0Info; //init quality flag for a given histogram;
+  TH1F *hVertexFirst     = (TH1F*) listrec->UncheckedAt(225);//fhVertex1st
+  TH1F *hVertexBest      = (TH1F*) listrec->UncheckedAt(226);//fhVertexBest
+  TH1F *hOrCminOrATvdcOn = (TH1F*) listrec->UncheckedAt(217);//vertex with TVDC on
+
+  // clean objects added at previous checks
+  EraseOldMessages((TH1*) hVertexFirst); 
+  EraseOldMessages((TH1*) hVertexBest); 
+  float errorLevelDifference   = 400; 
+  float warningLevelDifference = 200; 
+
+  if(hVertexFirst->Integral()>0 && hOrCminOrATvdcOn->Integral()>0){
+    Float_t thr = 0.05 * hVertexFirst->GetBinContent(hVertexFirst->GetMaximumBin());
+     
+    float meanVertexFirst = GetMeanAboveThreshold( hVertexFirst, thr);
+    float meanVertexTVDC  = GetMeanAboveThreshold( hOrCminOrATvdcOn,thr);
+    float diff = TMath::Abs(meanVertexFirst - meanVertexTVDC);
+    if(diff > errorLevelDifference){
+      qualityFlagVetrexFirst = kT0Warning; //init quality flag for a given histogram;
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("large diff. between TVDC vertex")); 
+      text.AddText(Form("and the first vertex."));
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      hVertexFirst->GetListOfFunctions()->Add((TPaveText*)text.Clone());             
+
+    }else if(diff > warningLevelDifference){
+      qualityFlagVetrexFirst = kT0Warning; //init quality flag for a given histogram;
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("large diff. between TVDC vertex")); 
+      text.AddText(Form("and the first vertex."));
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      hVertexFirst->GetListOfFunctions()->Add((TPaveText*)text.Clone());             
+
+    }
+  }else{
+    qualityFlagVetrexFirst = kT0Error;
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 histogram  %s has NO entries", hVertexFirst->GetName() ));
+
+    TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+    text.AddText(Form("NO ENTRIES!!!")); 
+    text.AddText(Form("If T0 is READY and beam is on report")); 
+    text.AddText(Form("the problem to the T0 on-call expert"));
+    text.SetBorderSize(0);
+    text.SetFillStyle(0);
+    hVertexFirst->GetListOfFunctions()->Add((TPaveText*)text.Clone());       
+  }
+
+  if(hVertexBest->Integral()>0 && hOrCminOrATvdcOn->Integral()>0){
+
+    Float_t thr = 0.05 * hVertexBest->GetBinContent(hVertexBest->GetMaximumBin());
+    float meanVertexBest  = GetMeanAboveThreshold(hVertexBest,thr);
+    float meanVertexTVDC =  GetMeanAboveThreshold(hOrCminOrATvdcOn,thr);
+    float diff = TMath::Abs(meanVertexBest - meanVertexTVDC);
+    if(diff > errorLevelDifference){
+      qualityFlagVetrexBest = kT0Warning; //init quality flag for a given histogram;
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("Large. diff. between TVDC vertex")); 
+      text.AddText(Form("and the best vertex."));
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      hVertexBest->GetListOfFunctions()->Add((TPaveText*)text.Clone());              
+
+    }else if(diff > warningLevelDifference){
+      qualityFlagVetrexBest = kT0Warning; //init quality flag for a given histogram;
+
+      TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+      text.AddText(Form("Large. diff. between TVDC vertex")); 
+      text.AddText(Form("and the best vertex."));
+      text.SetBorderSize(0);
+      text.SetFillStyle(0);
+      hVertexBest->GetListOfFunctions()->Add((TPaveText*)text.Clone());              
+    }
+  }else{
+    qualityFlagVetrexBest = kT0Error;
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("T0 histogram  %s has NO entries", hVertexBest->GetName() ));
+
+    TPaveText text(0.20,0.50,0.99,0.99,"NDC");   
+    text.AddText(Form("NO ENTRIES!!!")); 
+    text.AddText(Form("If T0 is READY and beam is on report")); 
+    text.AddText(Form("the problem to the T0 on-call expert"));
+    text.SetBorderSize(0);
+    text.SetFillStyle(0);
+    hVertexBest->GetListOfFunctions()->Add((TPaveText*)text.Clone());        
+  }
+
+
+  //executive summary
+  int lowestQualityFlag = (int) qualityFlagTrigger;
+  if(qualityFlagVetrexBest<lowestQualityFlag)  lowestQualityFlag = qualityFlagVetrexBest;
+  if(qualityFlagVetrexFirst<lowestQualityFlag) lowestQualityFlag = qualityFlagVetrexFirst;
+  if(qualityFlagMeanBest <lowestQualityFlag)   lowestQualityFlag = qualityFlagMeanBest; 
   
-  TH1 *fhRawEff;
-  TH1 *fhRawRef;
-  TH2 *fhRawRec2d;
-  TH2 *fhTime;
-
-  TIter next(listref) ;
-  Int_t counter=0;
-  Float_t refmean[50][25]; 
-  Float_t refrms[50][25]; 
+
+  return ConvertQualityFlagToDouble(lowestQualityFlag); 
+  
+}
+
+//--------------------------------------------------------------------------
+Double_t AliT0QAChecker::CheckESD(TObjArray *listrec ) const
+{
   Float_t checkr = 0;
+  TH1 *fhESD;
+  fhESD  = (TH1*) listrec->UncheckedAt(2);
+  if(fhESD){
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("count %s ", fhESD->GetName()) );
+     TF1 *f1 = new TF1("f1","gaus",-1,1);
+    fhESD->Fit("f1","R","Q", -1,1);
+    Double_t par[3];
+    f1->GetParameters(&par[0]);
+    
+    TPaveText text(0.30,0.50,0.99,0.99,"NDC");
+    
+    text.AddText(Form("T0 RUN %d ",AliCDBManager::Instance()->GetRun()));
+    
+    AliDebug(AliQAv1::GetQADebugLevel(), Form("numentries %d mean %f  #sigma %f", (int)fhESD->GetEntries(),par[1], par[2]));
+    
+    
+    if (par[2] > 0.07 && par[2] < 1.) {
+      checkr=0.5;
+       text.AddText(Form("not good resolution :\n %f ns\n", par[2] ));
+       text.SetFillColor(5);
+       printf("T0 detector resolution is not good enouph: %f ns\n",par[2] );
+    }
+    if(TMath::Abs(par[1])>0.05) {
+      checkr = 0.5;
+      text.AddText(Form(" Check clock shift on %f ns", par[1]));
+      text.SetFillColor(5);
+    }
+    if (par[2] >  1. || TMath::Abs(par[1])>0.1) {
+      checkr = 0.25;
+      text.AddText(Form(" Bad resolution:\n mean %f ns sigma %f ns", par[1], par[2]));
+      text.SetFillColor(2);
+      { // RS Clean previous additions
+       TList* lstF = fhESD->GetListOfFunctions();
+       if (lstF) {
+         TObject *stats = lstF->FindObject("stats");
+         lstF->Remove(stats);
+         TObject *obj;
+         while ((obj = lstF->First())) {
+           while(lstF->Remove(obj)) { }
+           delete obj;
+         }
+         if (stats) lstF->Add(stats);
+       } 
+      }
+      fhESD->GetListOfFunctions()->Add((TPaveText*)text.Clone());      
+      AliDebug(AliQAv1::GetQADebugLevel(),
+              Form("Please, check calibration: shift= %f resolution %f test=%f\n",
+                   par[1], par[2], checkr) ) ; 
+    }
+  }
+  else
+    {
+      AliDebug(AliQAv1::GetQADebugLevel(),
+              Form("No ESD QA histogram found, nothing to check"));
+       checkr=0;
+    }
   
-  Int_t nref = listref->GetEntries(); 
-  Int_t nrec = listrec->GetEntries(); 
   
-  for (Int_t iii=4; iii<6; iii++){
-    fhRawRec2d =(TH2*) listref->At(iii); 
-    for (Int_t idet=1; idet<25; idet++) {
-      
-      refmean[iii-4][idet] = fhRawRec2d->      
-       ProjectionY(Form("%s_py_%i_%i",                                                              fhRawRec2d ->GetName(), idet,iii-4),
-                   idet,idet+1)->GetMean();
-      
-      refrms[iii-4][idet] = fhRawRec2d->
-       ProjectionY(Form("%s_py%i_%i", 
-                        fhRawRec2d ->GetName(), idet,iii-4),
-                   idet,idet+1)->GetRMS();
-      
-      }
+  return checkr;
+}
+
+
+//--------------------------------------------------------------------------
+void AliT0QAChecker::EraseOldMessages(TH1* h) const 
+{
+  //erase the old captions 
+  TList* lstF = h->GetListOfFunctions();
+  if(lstF){  
+     TObject *stats = lstF->FindObject("stats");
+     lstF->Remove(stats);
+     TObject *obj;
+     while ((obj = lstF->First())) {
+       while(lstF->Remove(obj)) { }
+         delete obj;
+    }
+    if (stats) lstF->Add(stats);
   }
+}
+//--------------------------------------------------------------------------
+Double_t AliT0QAChecker::ConvertQualityFlagToDouble(int qualityFlag) const 
+{
+  //covert quality flag to double
+  Double_t checkr=1.0;
 
-  
-  TString nameDev[2] = {"CDF", "LED"};
-  for (Int_t icase=208; icase<210; icase++) {
-    fhTime = (TH2*) listrec->At(icase);
-    for (Int_t idet=1; idet<25; idet++) {
-      Double_t binmean = fhTime->
-       ProjectionY(Form("%s_py_%i_%i",                                                              fhTime ->GetName(), idet,icase),
-                   idet,idet+1)->GetMean();
-      Double_t rms= fhTime ->
-       ProjectionY(Form("%s_py%i_%i", 
-                        fhTime ->GetName(), idet,icase),
-                   idet,idet+1)->GetRMS();
-      Double_t diffmean = binmean-refmean[icase-208][idet];
-      
-      if (TMath::Abs(diffmean) < 2 ) {
-       checkr = 1;
-       //      printf(" Laser calibration signal sits on its place %f for PMT %s %i : check = %f\n",  diffmean, nameDev[icase-208].Data() ,idet, checkr);
-       AliDebug(AliQAv1::GetQADebugLevel(),
-                Form(" Laser calibration signal sits on its place %f for PMT %s %i : check = %f\n",  diffmean, nameDev[icase-208].Data(),idet, checkr)) ; 
-     }
-     if (TMath::Abs(diffmean) <= 5 && TMath::Abs(diffmean) >= 2 ){
-       checkr = 0.5;
-       // printf(" Laser calibration signal shifted by  %f ps for PMT %s %i : check = %f\n",  diffmean*24.4, nameDev[icase-208].Data(),idet, checkr);
-       AliDebug(AliQAv1::GetQADebugLevel(),
-               Form(" Laser calibration signal shifted by  %f ps (%f channels) for PMT %s %i : check = %f\n",  diffmean*24.4 ,diffmean , nameDev[icase-208],idet, checkr)) ; 
-      }
-     if (TMath::Abs(diffmean) > 5) { 
-       checkr = 0.25;
-       //   printf(" Big problems :laser calibration signal shifted by  %f ps (%f channels) for PMT %s %i : check = %f\n",  diffmean*24.4, diffmean, nameDev[icase-208].Data(),idet, checkr);
-       AliDebug(AliQAv1::GetQADebugLevel(),
-               Form(" Big problems :laser calibration signal shifted by  %f ps (%f channels) for PMT %s %i : check = %i\n",  diffmean*24.4, diffmean, nameDev[icase-208].Data(),idet, checkr)) ; 
-       
-     }
-     
-   }
- }
+  switch ( qualityFlag ){
+    case kT0Info:
+        checkr = 1.0; break;
+    case kT0Warning:
+        checkr = 0.75; break;
+    case kT0Error:
+          checkr = 0.25; break;
+    case kT0Fatal:
+        checkr = -1.0; break;
+    default:
+         AliError("Invalid ecc value. FIXME !");
+         checkr = 0.25; break;
+  };
 
-  return checkr;
+  return checkr; 
 }
+//--------------------------------------------------------------------------
+Float_t AliT0QAChecker::GetMeanAboveThreshold(TH1F* hV, Float_t thr) const{
+  //caculate mean value of histo bins above threshold
+  Int_t nBins = hV->GetNbinsX();
+  Int_t nBinsAboveThr = 0; 
+  Float_t sum = 0;
 
+  for(Int_t ib=1;ib<=nBins;ib++){
+    Float_t val = hV->GetBinContent(ib);
+    if(val<thr){
+      sum+=val;
+      nBinsAboveThr++; 
+    }
+  }
+
+  if(nBinsAboveThr>0) return sum/nBinsAboveThr;
+  else return hV->GetMean();
+}