]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - T0/AliT0CalibTimeEq.cxx
removed separate histograms for Calib and physics data
[u/mrichter/AliRoot.git] / T0 / AliT0CalibTimeEq.cxx
index 0ad81ca166b81bc4af1b8b4225754b653f284ee2..6e9cd34ed008898d0b17072876b9a99c977a8012 100644 (file)
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "AliT0CalibTimeEq.h"
-
+#include "AliLog.h"
 #include <TFile.h>
 #include <TMath.h>
 #include <TF1.h>
 #include <TSpectrum.h>
 #include <TProfile.h>
-#include "iostream.h"
+#include <iostream>
 
 ClassImp(AliT0CalibTimeEq)
 
 //________________________________________________________________
-  AliT0CalibTimeEq::AliT0CalibTimeEq():TNamed()
+  AliT0CalibTimeEq::AliT0CalibTimeEq():TNamed(),
+                                      fMeanVertex(0),        
+                                      fRmsVertex(0)      
 {
   //
+
 }
 
 //________________________________________________________________
-AliT0CalibTimeEq::AliT0CalibTimeEq(const char* name):TNamed()
+AliT0CalibTimeEq::AliT0CalibTimeEq(const char* name):TNamed(),
+                                      fMeanVertex(0),        
+                                      fRmsVertex(0)      
 {
   //constructor
 
@@ -51,7 +56,9 @@ AliT0CalibTimeEq::AliT0CalibTimeEq(const char* name):TNamed()
 }
 
 //________________________________________________________________
-AliT0CalibTimeEq::AliT0CalibTimeEq(const AliT0CalibTimeEq& calibda):TNamed(calibda)            
+AliT0CalibTimeEq::AliT0CalibTimeEq(const AliT0CalibTimeEq& calibda):TNamed(calibda),           
+                                      fMeanVertex(0),        
+                                      fRmsVertex(0)      
 {
 // copy constructor
   SetName(calibda.GetName());
@@ -94,78 +101,227 @@ void  AliT0CalibTimeEq::Print(Option_t*) const
   printf("\n   ----    PM Arrays       ----\n\n");
   printf(" Time delay CFD \n");
   for (Int_t i=0; i<24; i++) printf(" CFD  %f ",fTimeEq[i]);
+  printf("\n Mean Vertex %f \n", fMeanVertex);
 } 
 
 
 //________________________________________________________________
-void AliT0CalibTimeEq::ComputeOnlineParams(const char* filePhys)
+Bool_t AliT0CalibTimeEq::ComputeOnlineParams(const char* filePhys)
 {
   // compute online equalized time
-//  Int_t npeaks = 20;
-//  Double_t sigma = 4.;
-
-  TFile *gFile = TFile::Open(filePhys);
-//  Bool_t down=false;
-//  Int_t index[20];
-  Char_t buf1[15];
-//  Char_t temp[10];
-//  Float_t p[24][3]={0.,0.,0.};
-  for (Int_t i=0; i<24; i++)
-  {
-    sprintf(buf1,"CFD1-CFD%d",i+1);
-    TH1F *cfd = (TH1F*) gFile->Get(buf1);
-    //    printf(" i = %d buf1 = %s\n", i, buf1);
-    Double_t mean=cfd->GetMean();
-    SetTimeEq(i,mean);
-    delete cfd;
+  Float_t meandiff, sigmadiff, meanver, meancfdtime, sigmacfdtime;
+  meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
+  Double_t rmsver=0;
+  Int_t nent=0;
+  Bool_t ok=false;
+  gFile = TFile::Open(filePhys);
+  if(!gFile) {
+    AliError("No input PHYS data found ");
   }
-    /*
-
-    TSpectrum *s = new TSpectrum(2*npeaks,1.);
-    printf(" buf1 = %s cfd = %x\n", buf1, cfd);
-    Int_t nfound = s->Search(cfd,sigma,"goff",0.2);
-    printf(" nfound = %d\n", nfound);
-    if(nfound!=0)
+  else
     {
-      Float_t *xpeak = s->GetPositionX();
-      TMath::Sort(nfound, xpeak, index,down);
-      Float_t xp = xpeak[index[0]];
-      Float_t hmax = xp+3*sigma;
-      Float_t hmin = xp-3*sigma;
-      cfd->GetXaxis()->SetRange((Int_t)hmin-20,(Int_t)hmax+20);
-      TF1 *g1 = new TF1("g1", "gaus", hmin, hmax);
-      cfd->Fit("g1","IRQN");
-      for(Int_t j =0; j<3; j++)
-      {
-        p[i][j] = g1->GetParameter(j);
-       //      SetCFDvalue(i, j, p[i][j]);
+      ok=true;
+      for (Int_t i=0; i<24; i++)
+       {
+         meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
+         TH1F *cfd = (TH1F*) gFile->Get(Form("CFD1minCFD%d",i+1));
+         TH1F *cfdtime = (TH1F*) gFile->Get(Form("CFD%d",i+1));
+         if(!cfd) {
+           AliWarning(Form("no Diff histograms collected by PHYS DA for channel %i", i));
+           ok=false;
+           return ok;
+         }
+         if(!cfdtime) {
+           AliWarning(Form("no CFD histograms collected by PHYS DA for channel %i", i));
+           ok=false;
+           return ok;
+         }
+         if(cfd) {
+           nent = Int_t(cfd->GetEntries());
+           if(nent>500)  { 
+             if(cfd->GetRMS()>1.5 )
+               GetMeanAndSigma(cfd, meandiff, sigmadiff);
+             if(cfd->GetRMS()<=1.5) 
+               {
+                 meandiff = cfd->GetMean();
+                 sigmadiff=cfd->GetRMS();
+               }
+             Int_t   maxBin = cfd->GetMaximumBin(); 
+             Double_t  meanEstimate = cfd->GetBinCenter( maxBin); 
+             if(TMath::Abs(meanEstimate - meandiff) > 20 ) meandiff = meanEstimate; 
+           }
+           else 
+             {
+               ok=false;
+               AliWarning(Form(" Not  enouph data in PMT %i- PMT1:  %i ", i, nent));
+             }
+         }
+           //      printf(" i = %d buf1 = %s\n", i, buf1);
+         if(cfdtime) {
+           nent = Int_t(cfdtime->GetEntries());
+           if(nent > 500 )  { //!!!!!!!!!!
+             if(cfdtime->GetRMS()>1.5 )
+               GetMeanAndSigma(cfdtime,meancfdtime, sigmacfdtime);
+             if(cfdtime->GetRMS()<=1.5) 
+               {
+                 meancfdtime = cfdtime->GetMean();
+                 sigmacfdtime = cfdtime->GetRMS();
+               }
+             Int_t   maxBin = cfdtime->GetMaximumBin(); 
+             Double_t  meanEstimate = cfdtime->GetBinCenter( maxBin); 
+             if(TMath::Abs(meanEstimate - meancfdtime) > 20 ) meancfdtime = meanEstimate; 
+           }
+           else 
+             {
+               ok=false;
+               AliWarning(Form(" Not  enouph data in PMT in CFD peak %i - %i ", i, nent));
+             }
+         }
+         SetTimeEq(i,meandiff);
+         SetTimeEqRms(i,sigmadiff);
+         SetCFDvalue(i,0,meancfdtime);
+         if (cfd) delete cfd;
+         if (cfdtime) delete cfdtime;
+         
+       }
+      TH1F *ver = (TH1F*) gFile->Get("hVertex");
+      if(!ver) AliWarning("no T0 histogram collected by PHYS DA ");
+      if(ver) {
+       meanver = ver->GetMean();
+       rmsver = ver->GetRMS();
       }
-        SetTimeEq(i,(p[i][2]));
-       cout<<" mean "<<p[i][2]<<endl;
-    */
-      /*
-      SetCFDvalue(i, 3, hmin);
-      SetCFDvalue(i, 4, hmax);
-
-      if (i<12)
-      {
-        SetTimeEq(i,(p[i][2]-p[0][2]));
-      }
-      else
-      {
-       SetTimeEq(i,(p[i][2]-p[12][2]));
-      }        
+      SetMeanVertex(meanver);
+      SetRmsVertex(rmsver);
+      
+      gFile->Close();
+      delete gFile;
       
-    } 
+    }
+    return ok; 
+}
+
+//________________________________________________________________
+  Bool_t AliT0CalibTimeEq::ComputeOfflineParams(const char* filePhys, Float_t *timecdb, Float_t *cfdvalue, Int_t badpmt)
+{
+  // compute offline equalized time
+  Float_t meandiff, sigmadiff, meanver, meancfdtime, sigmacfdtime;
+  meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
+  Int_t nent=0;
+  Bool_t ok=false;
+  TH1F *cfddiff = NULL; 
+  TH1F *cfdtime = NULL;
+  TObjArray * TzeroObj = NULL;
+
+  gFile = TFile::Open(filePhys);
+  if(!gFile) {
+    AliError("No input PHYS data found ");
+    return ok;
   }
-      */  
-   gFile->Close();
-   delete gFile;
-   printf("\n\n");
-   for(int j=0;j<24;j++)
-   {
-                printf("fTimeEq[%d]=%f\n",j,fTimeEq[j]);
+  else
+    {
+      meandiff = sigmadiff =  meanver = meancfdtime = sigmacfdtime =0;
+      ok=true;
+      TDirectory *dr = (TDirectory*) gFile->Get("T0Calib");
+      /*      if (!dr ) { AliError("No input T0calib found "); 
+       ok=false;
+       return ok;
+       } */
+      if (dr)   TzeroObj = (TObjArray*) dr->Get("T0Calib");
+
+      for (Int_t i=0; i<24; i++)
+       {
+         if (i != badpmt) {
+           if(TzeroObj) {
+             cfddiff = (TH1F*)TzeroObj->FindObject(Form("CFD1minCFD%d",i+1));
+             cfdtime = (TH1F*)TzeroObj->FindObject(Form("CFD1minCFD%d",i+1));
+           }
+           else
+             {
+               cfddiff = (TH1F*)gFile->Get(Form("CFD1minCFD%d",i+1));
+               cfdtime = (TH1F*)gFile->Get(Form("CFD1minCFD%d",i+1));
+             }
+           if(!cfddiff || !cfdtime) {
+             AliWarning(Form("no  histograms collected by pass0 for channel %i", i));
+             ok=false;
+             delete gFile;
+             return ok;
+           }
+           if(cfddiff) {
+           nent = Int_t(cfddiff->GetEntries());
+           if(nent>500 )  { //!!!!!
+             if(cfddiff->GetRMS()>1.5 )
+               GetMeanAndSigma(cfddiff, meandiff, sigmadiff);
+             if(cfddiff->GetRMS()<=1.5) 
+               {
+                 meandiff = cfddiff->GetMean();
+                 sigmadiff = cfddiff->GetRMS();
+               }
+               Int_t   maxBin = cfddiff->GetMaximumBin(); 
+               Double_t  meanEstimate = cfddiff->GetBinCenter( maxBin); 
+               if(TMath::Abs(meanEstimate - meandiff) > 20 ) meandiff = meanEstimate; 
+             
+           }
+           else 
+             {
+               ok=false;
+               AliWarning(Form(" Not  enouph data in PMT %i- PMT1:  %i ", i, nent));
+             }
+         }         
+         
+         if(cfdtime) {
+           nent = Int_t(cfdtime->GetEntries());
+           if(nent>500 )  { //!!!!!
+             if(cfdtime->GetRMS()>1.5 )
+               GetMeanAndSigma(cfdtime,meancfdtime, sigmacfdtime);
+             if(cfdtime->GetRMS()<=1.5) 
+               {
+                 meancfdtime = cfdtime->GetMean();
+                 sigmacfdtime=cfdtime->GetRMS();
+               }
+               Int_t   maxBin = cfdtime->GetMaximumBin(); 
+               Double_t  meanEstimate = cfdtime->GetBinCenter( maxBin); 
+               if(TMath::Abs(meanEstimate - meancfdtime) > 20 ) meancfdtime = meanEstimate; 
+           }
+           else 
+             {
+               ok=false;
+               AliWarning(Form(" Not  enouph data in PMT in CFD peak %i - %i ", i, nent));
+             }
+         }
+         
+         SetTimeEq(i,timecdb[i] + meandiff);
+         SetTimeEqRms(i,sigmadiff);
+         SetCFDvalue(i,0,cfdvalue[i] );
+         if (cfddiff) cfddiff->Reset();
+         if (cfdtime) cfdtime->Reset();
+         } //bad pmt
+       }
+      
+      gFile->Close();
+      delete gFile;
+
+    }
+    return ok; 
    }
+
+//________________________________________________________________________
+void AliT0CalibTimeEq::GetMeanAndSigma(TH1F* hist,  Float_t &mean, Float_t &sigma) {
+  
+  const double window = 2.;  //fit window 
+  
+  double meanEstimate, sigmaEstimate; 
+  int maxBin;
+  maxBin        =  hist->GetMaximumBin(); //position of maximum
+  meanEstimate  =  hist->GetBinCenter( maxBin); // mean of gaussian sitting in maximum
+  sigmaEstimate = hist->GetRMS();
+  TF1* fit= new TF1("fit","gaus", meanEstimate - window*sigmaEstimate, meanEstimate + window*sigmaEstimate);
+  fit->SetParameters(hist->GetBinContent(maxBin), meanEstimate, sigmaEstimate);
+  hist->Fit("fit","RQ","Q");
+
+  mean  = (Float_t) fit->GetParameter(1);
+  sigma = (Float_t) fit->GetParameter(2);
+
+  delete fit;
 }