added TOF calibration library and code for pass0/passX calibration
authorrpreghen <rpreghen@f7af4fe6-9843-0410-8265-dc069ae4e863>
Wed, 16 Mar 2011 14:26:00 +0000 (14:26 +0000)
committerrpreghen <rpreghen@f7af4fe6-9843-0410-8265-dc069ae4e863>
Wed, 16 Mar 2011 14:26:00 +0000 (14:26 +0000)
TOF/AddTOFAnalysisTaskCalibPass0.C [new file with mode: 0644]
TOF/AliTOFAnalysisTaskCalibPass0.cxx [new file with mode: 0644]
TOF/AliTOFAnalysisTaskCalibPass0.h [new file with mode: 0644]
TOF/CMakelibTOFcalib.pkg [new file with mode: 0644]
TOF/TOFCalibPass0.C [new file with mode: 0644]
TOF/TOFcalibLinkDef.h [new file with mode: 0644]

diff --git a/TOF/AddTOFAnalysisTaskCalibPass0.C b/TOF/AddTOFAnalysisTaskCalibPass0.C
new file mode 100644 (file)
index 0000000..86bcaf5
--- /dev/null
@@ -0,0 +1,69 @@
+AliTOFAnalysisTaskCalibPass0 *
+AddTOFAnalysisTaskCalibPass0()
+{
+
+  /* check analysis manager */
+  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
+  if (!mgr) {
+    Error("AddAnalysisTaskEventTime", "cannot get analysis manager");
+    return NULL;
+  }
+
+  /* check input event handler */
+  if (!mgr->GetInputEventHandler()) {
+    Error("AddAnalysisTaskEventTime", "cannot get input event handler");
+    return NULL;
+  }
+  
+  /* check input data type */
+  TString str = mgr->GetInputEventHandler()->GetDataType();
+  if (str.CompareTo("ESD")) {
+    Error("AddAnalysisTaskEventTime", "input data type is not \"ESD\"");
+    return NULL;
+  }
+
+  /* get common input data container */
+  AliAnalysisDataContainer *inputc = mgr->GetCommonInputContainer();
+  if (!inputc) {
+    Error("AddAnalysisTaskEventTime", "cannot get common input container");
+    return NULL;
+  }
+  
+  /* create output data container */
+  AliAnalysisDataContainer *outputc1 = mgr->CreateContainer("Histos", TList::Class(), AliAnalysisManager::kOutputContainer, "TOFCalibPass0.root");
+  if (!outputc1) {
+    Error("AddAnalysisTaskEventTime", "cannot create output container \"Histos\"");
+    return NULL;
+  }
+
+  /*  create task and connect input/output */
+  AliTOFAnalysisTaskCalibPass0 *task = new AliTOFAnalysisTaskCalibPass0();
+  mgr->ConnectInput(task, 0, inputc);
+  mgr->ConnectOutput(task, 1, outputc1);
+
+  /* setup task */
+  task->SetEventSelectionFlag(kFALSE);
+  task->SetVertexSelectionFlag(kTRUE);
+  task->SetVertexCut(25.0);
+  /* setup TOF calib */
+  task->GetTOFcalib()->SetRemoveMeanT0(kFALSE);
+  task->GetTOFcalib()->SetCalibrateTOFsignal(kTRUE);
+  task->GetTOFcalib()->SetCorrectTExp(kFALSE);
+  /* setup track cuts */
+  AliESDtrackCuts *trackCuts = task->GetTrackCuts();
+  trackCuts->SetPtRange(0.5, 10.);
+  trackCuts->SetEtaRange(-1.0, 1.0);
+  trackCuts->SetRequireITSRefit(kTRUE);
+  trackCuts->SetMinNClustersITS(1);
+  trackCuts->SetRequireTPCRefit(kTRUE);
+  trackCuts->SetMinNClustersTPC(70);
+  trackCuts->SetMaxChi2PerClusterTPC(4.);
+  trackCuts->SetAcceptKinkDaughters(kFALSE);
+  trackCuts->SetMaxDCAToVertexZ(3.2);
+  trackCuts->SetMaxDCAToVertexXY(2.4);
+  trackCuts->SetDCAToVertex2D(kTRUE);
+
+  /* return task */
+  return task;
+
+}
diff --git a/TOF/AliTOFAnalysisTaskCalibPass0.cxx b/TOF/AliTOFAnalysisTaskCalibPass0.cxx
new file mode 100644 (file)
index 0000000..d49b800
--- /dev/null
@@ -0,0 +1,691 @@
+/**************************************************************************
+ * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ *                                                                        *
+ * Author: The ALICE Off-line Project.                                    *
+ * Contributors are mentioned in the code where appropriate.              *
+ *                                                                        *
+ * Permission to use, copy, modify and distribute this software and its   *
+ * documentation strictly for non-commercial purposes is hereby granted   *
+ * without fee, provided that the above copyright notice appears in all   *
+ * copies and that both the copyright notice and this permission notice   *
+ * appear in the supporting documentation. The authors make no claims     *
+ * about the suitability of this software for any purpose. It is          *
+ * provided "as is" without express or implied warranty.                  *
+***************************************************************************/
+
+///////////////////////////////////////////////////////////////
+//                                                           //
+//   This class provides TOF pass0/passX calibration tools   //
+//                                                           //
+///////////////////////////////////////////////////////////////
+
+#include "AliTOFAnalysisTaskCalibPass0.h"
+#include "AliESDtrack.h"
+#include "AliESDEvent.h"
+#include "AliPhysicsSelection.h"
+#include "AliESDtrackCuts.h"
+#include "AliCDBManager.h"
+#include "AliGRPManager.h"
+#include "AliGRPObject.h"
+#include "AliTOFcalib.h"
+#include "TList.h"
+#include "TH2F.h"
+#include "TFile.h"
+#include "TH1D.h"
+#include "TObjArray.h"
+#include "TString.h"
+#include "TObjString.h"
+#include "TROOT.h"
+#include "TF1.h"
+#include "TTime.h"
+#include "TGrid.h"
+#include "TTimeStamp.h"
+#include "AliTOFRunParams.h"
+#include "AliCDBStorage.h"
+#include "AliCDBId.h"
+#include "AliCDBMetaData.h"
+#include "TSystem.h"
+#include "AliLog.h"
+
+ClassImp(AliTOFAnalysisTaskCalibPass0)
+  
+//_______________________________________________________
+
+const Int_t AliTOFAnalysisTaskCalibPass0::fgkMaxNumberOfPoints = 10000; // max number of points
+const Double_t AliTOFAnalysisTaskCalibPass0::fgkMinVertexIntegral = 1000.;
+const Double_t AliTOFAnalysisTaskCalibPass0::fgkMinDeltatIntegral = 20000.;
+
+//_______________________________________________________
+  
+AliTOFAnalysisTaskCalibPass0::AliTOFAnalysisTaskCalibPass0() :
+  AliAnalysisTaskSE("TOFCalib-Pass0"),
+  fInitFlag(kFALSE),
+  fEventSelectionFlag(kFALSE),
+  fVertexSelectionFlag(kFALSE),
+  fVertexCut(10.),
+  fRunNumber(0),
+  fESDEvent(NULL),
+  fEventCuts(new AliPhysicsSelection()),
+  fTrackCuts(new AliESDtrackCuts()),
+  fStartTime(0),
+  fEndTime(0),
+  fEventTime(0),
+  fElapsedTime(0),
+  fkVertex(NULL),
+  fGRPManager(new AliGRPManager()),
+  fkGRPObject(NULL),
+  fTOFcalib(new AliTOFcalib()),
+  fHistoList(new TList()),
+  fHistoVertexTimestamp(NULL),
+  fHistoDeltatTimestamp(NULL),
+  fHistoDeltazEta(NULL),
+  fHistoDeltazCosTheta(NULL),
+  fHistoAcceptedTracksEtaPt(NULL),
+  fHistoMatchedTracksEtaPt(NULL)
+{
+  /* 
+   * default constructor 
+   */
+
+  /* define output */
+  fHistoList->SetOwner(kTRUE);
+  DefineOutput(1, TList::Class());
+}
+
+//_______________________________________________________
+
+AliTOFAnalysisTaskCalibPass0::~AliTOFAnalysisTaskCalibPass0()
+{
+  /*
+   * default destructor
+   */
+
+  if (fHistoList) delete fHistoList;
+}
+
+//_______________________________________________________
+
+Bool_t
+AliTOFAnalysisTaskCalibPass0::InitRun()
+{
+  /*
+   * init run
+   */
+
+  /* get ESD event */
+  fESDEvent = dynamic_cast<AliESDEvent *>(InputEvent());
+  if (!fESDEvent) {
+    AliError("cannot get ESD event");
+    return kFALSE;
+  }
+  /* get run number */
+  Int_t runNb = fESDEvent->GetRunNumber();
+  /* check run already initialized */
+  if (fInitFlag && fRunNumber == runNb) return kTRUE;
+  if (fInitFlag && fRunNumber != runNb) {
+    AliFatal(Form("run number has changed within same job and this is not allowed: %d -> %d", fRunNumber, runNb));
+    return kTRUE;
+  }
+  fInitFlag = kFALSE;
+  /* init cdb if not yet done */
+  AliCDBManager *cdb = AliCDBManager::Instance();
+  if (!cdb->IsDefaultStorageSet())
+    cdb->SetDefaultStorage("raw://");
+  if (cdb->GetRun() < 0)
+    cdb->SetRun(runNb);
+    /* init TOF calib */
+  if (!fTOFcalib->Init()) {
+    AliError("cannot init TOF calib");
+    return kFALSE;
+  }
+  /* get GRP data */
+  if (!fGRPManager->ReadGRPEntry()) {
+    AliError("error while reading \"GRPEntry\" from OCDB");
+    return kFALSE;
+  }
+  fkGRPObject = fGRPManager->GetGRPData();
+  if (!fkGRPObject) {
+    AliError("cannot get \"GRPData\" from GRP manager");
+    return kFALSE;
+  }
+  fStartTime = fkGRPObject->GetTimeStart();
+  fEndTime = fkGRPObject->GetTimeEnd();
+  AliInfo(Form("got \"GRPData\": startTime=%d, endTime=%d", fStartTime, fEndTime));
+  
+  AliInfo(Form("initialized for run %d", runNb));
+  fInitFlag = kTRUE;
+  fRunNumber = runNb;
+  
+  /* set histo title with run-number and start-time */
+  fHistoVertexTimestamp->SetTitle(Form("run: %d, startTimestamp: %u", fRunNumber, fStartTime));
+  fHistoDeltatTimestamp->SetTitle(Form("run: %d, startTimestamp: %u", fRunNumber, fStartTime));
+  fHistoDeltazEta->SetTitle(Form("run: %d, startTimestamp: %u", fRunNumber, fStartTime));
+  fHistoDeltazCosTheta->SetTitle(Form("run: %d, startTimestamp: %u", fRunNumber, fStartTime));
+  fHistoAcceptedTracksEtaPt->SetTitle(Form("run: %d, startTimestamp: %u", fRunNumber, fStartTime));
+  fHistoMatchedTracksEtaPt->SetTitle(Form("run: %d, startTimestamp: %u", fRunNumber, fStartTime));
+  
+  return kTRUE;
+}
+
+//_______________________________________________________
+
+Bool_t
+AliTOFAnalysisTaskCalibPass0::InitEvent()
+{
+  /*
+   * init event
+   */
+
+  /* get ESD event */
+  fESDEvent = dynamic_cast<AliESDEvent *>(InputEvent());
+  if (!fESDEvent) return kFALSE;
+  /* set event time and elapsed time */
+  fEventTime = fESDEvent->GetTimeStamp();
+  fElapsedTime = fESDEvent->GetTimeStamp() - fStartTime;
+  /* event selection */
+  if (fEventSelectionFlag && !fEventCuts->IsCollisionCandidate(fESDEvent)) return kFALSE;
+  /* vertex selection */
+  fkVertex = fESDEvent->GetPrimaryVertexTracks();
+  if (fVertexSelectionFlag && fkVertex->GetNContributors() < 1) {
+    fkVertex = fESDEvent->GetPrimaryVertexSPD();
+    if (fkVertex->GetNContributors() < 1) return kFALSE;
+  }
+  if (fVertexSelectionFlag && TMath::Abs(fkVertex->GetZ()) > fVertexCut) return kFALSE;
+  /* calibrate ESD if requested */
+  fTOFcalib->CalibrateESD(fESDEvent);
+
+  return kTRUE;
+}
+
+//_______________________________________________________
+
+Bool_t
+AliTOFAnalysisTaskCalibPass0::HasTOFMeasurement(const AliESDtrack *track) const
+{
+  /*
+   * has TOF measurement
+   */
+
+  /* check TOF status flags */
+  if (!(track->GetStatus() & AliESDtrack::kTOFout) ||
+      !(track->GetStatus() & AliESDtrack::kTIME)) return kFALSE;
+  /* check integrated length */
+  if (track->GetIntegratedLength() < 350.) return kFALSE;
+
+  /* TOF measurement ok */
+  return kTRUE;
+}
+
+//_______________________________________________________
+
+void
+AliTOFAnalysisTaskCalibPass0::UserCreateOutputObjects()
+{
+  /*
+   * user create output objects
+   */
+
+  /* time binning */
+  Int_t timeBins = 288;
+  Float_t timeMin = 0.;
+  Float_t timeMax = 24. * 3600.;
+  /* vertex binning */
+  Int_t vertexBins = 200;
+  Float_t vertexMin = -50.;
+  Float_t vertexMax = 50.;
+  /* deltat binning */
+  Int_t deltatBins = 2000;
+  Float_t deltatMin = -24400.;
+  Float_t deltatMax = 24400.;
+  /* deltaz binning */
+  Int_t deltazBins = 200;
+  Float_t deltazMin = -10.;
+  Float_t deltazMax = 10.;
+  /* eta binning */
+  Int_t etaBins = 200;
+  Float_t etaMin = -1.;
+  Float_t etaMax = 1.;
+  /* p binning */
+  Int_t pBins = 500;
+  Float_t pMin = 0.;
+  Float_t pMax = 5.;
+  
+  fHistoVertexTimestamp = new TH2F("hHistoVertexTimestamp", "Vertex position;elapsed time (s);z (cm);", timeBins, timeMin, timeMax, vertexBins, vertexMin, vertexMax);
+  fHistoList->Add(fHistoVertexTimestamp);
+
+  fHistoDeltatTimestamp = new TH2F("hHistoDeltatTimestamp", "Global time shift (T0-fill);elapsed time (s);t - t_{exp}^{(#pi)} (ps);", timeBins, timeMin, timeMax, deltatBins, deltatMin, deltatMax);
+  fHistoList->Add(fHistoDeltatTimestamp);
+
+  fHistoDeltazEta = new TH2F("hHistoDeltazEta", "Matching residuals (longitudinal);#eta;#Deltaz (cm);", etaBins, etaMin, etaMax, deltazBins, deltazMin, deltazMax);
+  fHistoList->Add(fHistoDeltazEta);
+
+  fHistoDeltazCosTheta = new TH2F("hHistoDeltazCosTheta", "Matching residuals (longitudinal);cos #theta;#Deltaz (cm);", etaBins, etaMin, etaMax, deltazBins, deltazMin, deltazMax);
+  fHistoList->Add(fHistoDeltazCosTheta);
+
+  fHistoAcceptedTracksEtaPt = new TH2F("hHistoAcceptedTracksEtaPt", "Accepted tracks;#eta;#p_{T} (GeV/c);", etaBins, etaMin, etaMax, pBins, pMin, pMax);
+  fHistoList->Add(fHistoAcceptedTracksEtaPt);
+
+  fHistoMatchedTracksEtaPt = new TH2F("hHistoMatchedTracksEtaPt", "Matched tracks;#eta;p_{T} (GeV/c);", etaBins, etaMin, etaMax, pBins, pMin, pMax);
+  fHistoList->Add(fHistoMatchedTracksEtaPt);
+
+  /* post data */
+  PostData(1, fHistoList);
+}
+
+//_______________________________________________________
+
+void
+AliTOFAnalysisTaskCalibPass0::UserExec(Option_t *)
+{
+  /*
+   * user exec
+   */
+
+  /* init run */
+  if (!InitRun()) return;
+  /* init event */
+  if (!InitEvent()) return;
+
+  /*** ACCEPTED EVENT ***/
+  
+  /* fill vertex histo */
+  fHistoVertexTimestamp->Fill(fElapsedTime, fkVertex->GetZ());
+
+  /* loop over ESD tracks */
+  Int_t nTracks = fESDEvent->GetNumberOfTracks();
+  AliESDtrack *track;
+  Double_t eta, costheta, pt, time, timei[AliPID::kSPECIES], deltat, deltaz;
+  for (Int_t itrk = 0; itrk < nTracks; itrk++) {
+    /* get track */
+    track = fESDEvent->GetTrack(itrk);
+    if (!track) continue;
+    /* check accept track */
+    if (!fTrackCuts->AcceptTrack(track)) continue;
+    /* get track info */
+    eta = track->Eta();
+    costheta = TMath::Cos(track->Theta());
+    pt = track->Pt();
+    /* fill accepted tracks histo */
+    fHistoAcceptedTracksEtaPt->Fill(eta, pt);
+    /* check TOF measurement */
+    if (!HasTOFMeasurement(track)) continue;
+
+    /*** ACCEPTED TRACK WITH TOF MEASUREMENT ***/
+
+    /* fill matched tracks histo */
+    fHistoMatchedTracksEtaPt->Fill(eta, pt);
+    /* get TOF info */
+    time = track->GetTOFsignal();
+    track->GetIntegratedTimes(timei);
+    deltat = time - timei[AliPID::kPion];
+    deltaz = track->GetTOFsignalDz();
+    
+    /* fill histos */
+    fHistoDeltatTimestamp->Fill(fElapsedTime, deltat);
+    fHistoDeltazEta->Fill(eta, deltaz);
+    fHistoDeltazCosTheta->Fill(costheta, deltaz);
+    
+  } /* end of loop over ESD tracks */
+
+  /* post data */
+  PostData(1, fHistoList);
+  
+}
+
+//_______________________________________________________
+
+Bool_t
+AliTOFAnalysisTaskCalibPass0::ProcessOutput(const Char_t *filename, const Char_t *dbString)
+{
+  /*
+   * process output
+   */
+
+  /* open file */
+  TFile *file = TFile::Open(filename);
+  if (!file || !file->IsOpen()) {
+    AliError(Form("cannot open output file %s", filename));
+    return kFALSE;
+  }
+  /* get histo list */
+  TList *list = (TList *)file->Get("Histos");
+  if (!list) {
+    AliError(Form("cannot get \"Histos\" list from file %s", filename));
+    return kFALSE;
+  }
+  /* get histos */
+  TH2F *histoVertexTimestamp = (TH2F *)list->FindObject("hHistoVertexTimestamp");
+  if (!histoVertexTimestamp) {
+    AliError(Form("cannot get \"hHistoVertexTimestamp\" object from file %s", filename));
+    return kFALSE;
+  }
+  TH2F *histoDeltatTimestamp = (TH2F *)list->FindObject("hHistoDeltatTimestamp");
+  if (!histoDeltatTimestamp) {
+    AliError(Form("cannot get \"hHistoDeltatTimestamp\" object from file %s", filename));
+    return kFALSE;
+  }
+  TH2F *histoDeltazEta = (TH2F *)list->FindObject("hHistoDeltazEta");
+  if (!histoDeltazEta) {
+    AliError(Form("cannot get \"hHistoDeltazEta\" object from file %s", filename));
+    return kFALSE;
+  }
+  TH2F *histoDeltazCosTheta = (TH2F *)list->FindObject("hHistoDeltazCosTheta");
+  if (!histoDeltazCosTheta) {
+    AliError(Form("cannot get \"hHistoDeltazCosTheta\" object from file %s", filename));
+    return kFALSE;
+  }
+  TH2F *histoAcceptedTracksEtaPt = (TH2F *)list->FindObject("hHistoAcceptedTracksEtaPt");
+  if (!histoAcceptedTracksEtaPt) {
+    AliError(Form("cannot get \"hHistoAccptedTracksEtaPt\" object from file %s", filename));
+    return kFALSE;
+  }
+  TH2F *histoMatchedTracksEtaPt = (TH2F *)list->FindObject("hHistoMatchedTracksEtaPt");
+  if (!histoMatchedTracksEtaPt) {
+    AliError(Form("cannot get \"hHistoMatchedTracksEtaPt\" object from file %s", filename));
+    return kFALSE;
+  }
+  /* check matching performance */
+  if (!CheckMatchingPerformance(histoDeltazEta, histoAcceptedTracksEtaPt, histoMatchedTracksEtaPt)) {
+    AliError("error while checking matching efficiency");
+    return kFALSE;
+  }
+  /* calibrate and store */
+  if (!CalibrateAndStore(histoVertexTimestamp, histoDeltatTimestamp, dbString)) {
+    AliError("error while calibrating and storing");
+    return kFALSE;
+  }
+
+  /* success */
+  return kTRUE;
+}
+
+//_______________________________________________________
+
+Bool_t
+AliTOFAnalysisTaskCalibPass0::CheckMatchingPerformance(const TH2F *histoDeltazCosTheta, const TH2F *histoAcceptedTracksEtaPt, const TH2F *histoMatchedTracksEtaPt) const
+{
+  /*
+   * check matching performance
+   */
+
+  /* check pointers */
+  if (!histoDeltazCosTheta || !histoAcceptedTracksEtaPt || !histoMatchedTracksEtaPt)
+    return kFALSE;
+  /* dummy for the time being */
+  return kTRUE;
+}
+
+//_______________________________________________________
+
+Bool_t
+AliTOFAnalysisTaskCalibPass0::CalibrateAndStore(TH2F *histoVertexTimestamp, TH2F *histoDeltatTimestamp, const Char_t *dbString)
+{
+  /*
+   * calibrate and store
+   */
+
+  /* check pointers */
+  if (!histoVertexTimestamp || !histoDeltatTimestamp)
+    return kFALSE;
+
+  /*** GET RUN-NUMBER AND START-TIMESTAMP ***/
+
+  TString str;
+  TObjArray *strarr = NULL;
+  TObjString *ostr = NULL;
+  str = histoVertexTimestamp->GetTitle();
+  strarr = str.Tokenize(",");
+  if (!strarr) {
+    AliError("problems whith tokenize histogram title");
+    return kFALSE;
+  }
+  
+  /* run number */
+  ostr = (TObjString *)strarr->At(0);
+  if (!ostr) {
+    AliError("problems while getting run number from histogram title");
+    return kFALSE;
+  }
+  str = ostr->GetString();
+  if (!str.BeginsWith("run:")) {
+    AliError("problems while getting run number from histogram title");
+    return kFALSE;
+  }
+  str.Remove(0, 5);
+  Int_t runNb = atoi(str.Data());
+  if (runNb <= 0) {
+    AliError(Form("bad run number: %d", runNb));
+    return kFALSE;
+  }
+  AliInfo(Form("got run number: %d", runNb));
+
+  /* start timestamp */
+  ostr = (TObjString *)strarr->At(1);
+  if (!ostr) {
+    AliError("problems while getting start timestamp from histogram title");
+    return kFALSE;
+  }
+  str = ostr->GetString();
+  str.Remove(0, 1); /* remove empty space at the beginning */
+  if (!str.BeginsWith("startTimestamp:")) {
+    AliError("problems while getting start timestamp from histogram title");
+    return kFALSE;
+  }
+  str.Remove(0, 16);
+  UInt_t startTimestamp = atoi(str.Data());
+  if (startTimestamp <= 0) {
+    AliError(Form("bad start timestamp: %d", startTimestamp));
+    return kFALSE;
+  }
+  TTimeStamp ts = startTimestamp;
+  AliInfo(Form("got start timestamp: %d (%s)", startTimestamp, ts.AsString()));
+
+  /*** CALIBRATION STAGE ***/
+
+  /* get fit function */
+  TF1 *fitFunc = (TF1 *)gROOT->GetFunction("gaus");
+
+  /* projection-x */
+  TH1D *histoVertexTimestamppx = histoVertexTimestamp->ProjectionX("histoVertexTimestamppx");
+  TH1D *histoDeltatTimestamppx = histoDeltatTimestamp->ProjectionX("histoDeltatTimestamppx");
+
+  /* define mix and max time bin */
+  Int_t minBin = histoVertexTimestamppx->FindFirstBinAbove(0);
+  Int_t maxBin = histoVertexTimestamppx->FindLastBinAbove(0);
+  Float_t minTime = histoVertexTimestamppx->GetBinLowEdge(minBin);
+  Float_t maxTime = histoVertexTimestamppx->GetBinLowEdge(maxBin + 1);
+  AliInfo(Form("min/max time defined: %d < t < %d s [%d, %d]", (Int_t)minTime, (Int_t)maxTime, minBin, maxBin));
+
+  /* loop over time bins */
+  Int_t nPoints = 0;
+  Float_t time[fgkMaxNumberOfPoints], timeerr[fgkMaxNumberOfPoints];
+  Float_t vertexMean[fgkMaxNumberOfPoints], vertexMeanerr[fgkMaxNumberOfPoints];
+  Float_t vertexSigma[fgkMaxNumberOfPoints], vertexSigmaerr[fgkMaxNumberOfPoints];
+  Float_t timeZeroMean[fgkMaxNumberOfPoints], timeZeroMeanerr[fgkMaxNumberOfPoints];
+  Float_t timeZeroSigma[fgkMaxNumberOfPoints], timeZeroSigmaerr[fgkMaxNumberOfPoints];
+  Float_t averageTimeZero = 0.;
+  Float_t averageTimeSigma = 0.;
+  Float_t averageVertexSpread = 0.;
+  for (Int_t ibin = minBin; ibin <= maxBin; ibin++) {
+
+    /* define time window */
+    Int_t startBin = ibin;
+    Int_t endBin = ibin;
+    while(histoVertexTimestamppx->Integral(startBin, endBin) < fgkMinVertexIntegral ||
+         histoDeltatTimestamppx->Integral(startBin, endBin) < fgkMinDeltatIntegral) {
+      if (endBin < maxBin) endBin++;
+      else if (startBin > minBin) startBin--;
+      else break;
+    }
+    if (histoVertexTimestamppx->Integral(startBin, endBin) <= 0 ||
+        histoDeltatTimestamppx->Integral(startBin, endBin) <= 0) continue;
+    Float_t startTime = histoVertexTimestamppx->GetBinLowEdge(startBin);
+    Float_t endTime = histoVertexTimestamppx->GetBinLowEdge(endBin + 1);
+    Float_t vertexIntegral = histoVertexTimestamppx->Integral(startBin, endBin);
+    Float_t deltatIntegral = histoDeltatTimestamppx->Integral(startBin, endBin);
+    AliInfo(Form("time window defined: %d < t < %d s [%d, %d]: %d vertices, %d tracks", (Int_t)startTime, (Int_t)endTime, startBin, endBin, (Int_t)vertexIntegral, (Int_t)deltatIntegral));
+
+    /* projection-y */
+    TH1D *histoVertexTimestamppy = histoVertexTimestamp->ProjectionY("histoVertexTimestamppy", startBin, endBin);
+    TH1D *histoDeltatTimestamppy = histoDeltatTimestamp->ProjectionY("histoDeltatTimestamppy", startBin, endBin);
+
+    /* average time */
+    histoVertexTimestamppx->GetXaxis()->SetRange(startBin, endBin);
+    time[nPoints] = histoVertexTimestamppx->GetMean();
+    timeerr[nPoints] = histoVertexTimestamppx->GetMeanError();
+
+    /* fit vertex */
+    if (FitPeak(fitFunc, histoVertexTimestamppy, 10., 3., 3.) != 0) {
+      AliError("troubles fitting vertex, skip");
+      delete histoVertexTimestamppy;
+      delete histoDeltatTimestamppy;
+      continue;
+    }
+    vertexMean[nPoints] = fitFunc->GetParameter(1);
+    vertexMeanerr[nPoints] = fitFunc->GetParError(1);
+    vertexSigma[nPoints] = fitFunc->GetParameter(2);
+    vertexSigmaerr[nPoints] = fitFunc->GetParError(2);
+    averageVertexSpread += fitFunc->GetParameter(2);
+
+    /* fit time-zero */
+    if (FitPeak(fitFunc, histoDeltatTimestamppy, 500., 2., 1.) != 0) {
+      AliError("troubles fitting time-zero TRACKS, skip");
+      delete histoVertexTimestamppy;
+      delete histoDeltatTimestamppy;
+      continue;
+    }
+    timeZeroMean[nPoints] = fitFunc->GetParameter(1);
+    timeZeroMeanerr[nPoints] = fitFunc->GetParError(1);
+    timeZeroSigma[nPoints] = fitFunc->GetParameter(2);
+    timeZeroSigmaerr[nPoints] = fitFunc->GetParError(2);
+    averageTimeZero += fitFunc->GetParameter(1);
+    averageTimeSigma += fitFunc->GetParameter(2);
+
+    /* delete projection-y */
+    delete histoVertexTimestamppy;
+    delete histoDeltatTimestamppy;
+
+    /* increment n points */
+    nPoints++;
+
+    /* set current bin */
+    ibin = endBin;
+
+  } /* end of loop over time bins */
+
+  /* delete projection-x */
+  delete histoVertexTimestamppx;
+  delete histoDeltatTimestamppx;
+
+  /* check points */
+  if (nPoints <= 0) {
+    AliError("no measurement available, quit");
+    return kFALSE;
+  }
+  AliInfo(Form("average time-zero  = %f", averageTimeZero / nPoints));
+  AliInfo(Form("average time-sigma = %f", averageTimeSigma / nPoints));
+  AliInfo(Form("average v-spread   = %f", averageVertexSpread / nPoints));
+
+  /*** CREATE RUN PARAMS OBJECT ***/
+
+#if 0
+  /* get start time from GRP */
+  TGrid::Connect("alien://", 0, 0, "t");
+  AliCDBManager *cdb = AliCDBManager::Instance();
+  cdb->SetDefaultStorage("raw://");
+  cdb->SetRun(runNb);
+  AliGRPManager grp;
+  if (!grp.ReadGRPEntry()) {
+    AliError("error while reading GRP entry");
+    return kFALSE;
+  }
+  UInt_t startTimestamp = grp.GetGRPData()->GetTimeStart();
+  TTimeStamp ts;
+  ts = startTimestamp;
+  AliInfo(Form("got start time from GRP: %s", ts.AsString()));
+#endif
+    
+  /* create arrays */
+  UInt_t timestamp[fgkMaxNumberOfPoints];
+  Float_t t0[fgkMaxNumberOfPoints];
+  Float_t tofReso[fgkMaxNumberOfPoints];
+  Float_t t0Spread[fgkMaxNumberOfPoints];
+  for (Int_t ipoint = 0; ipoint < nPoints; ipoint++) {
+    timestamp[ipoint] = time[ipoint] + startTimestamp;
+    t0[ipoint] = timeZeroMean[ipoint];
+    tofReso[ipoint] = timeZeroSigma[ipoint];
+    t0Spread[ipoint] = vertexSigma[ipoint] / 2.99792457999999984e-02;
+  }
+  UInt_t run[1] = {runNb};
+  UInt_t runFirstPoint[1] = {0};
+  UInt_t runLastPoint[1] = {nPoints - 1};
+  
+  /* create run params object */
+  AliTOFRunParams obj(nPoints, 1);
+  AliInfo(Form("create run params object for run %d with %d points", runNb, nPoints));
+  obj.SetTimestamp(timestamp);
+  obj.SetT0(t0);
+  obj.SetTOFResolution(tofReso);
+  obj.SetT0Spread(t0Spread);
+  obj.SetRunNb(run);
+  obj.SetRunFirstPoint(runFirstPoint);
+  obj.SetRunLastPoint(runLastPoint);
+  
+  /*** CREATE OCDB ENTRY ***/
+
+  if (!dbString) {
+    AliError("cannot store object because of NULL string");
+    return kFALSE;
+  }
+
+  /* install run params object in OCDB */
+  AliCDBManager *cdb = AliCDBManager::Instance();
+  AliCDBStorage *sto = cdb->GetStorage(dbString);
+  if (!sto) {
+    AliError(Form("cannot get storage %s", dbString));
+    return kFALSE;
+  }
+  AliCDBId id("TOF/Calib/RunParams", runNb, runNb);
+  AliCDBMetaData md;
+  md.SetResponsible("Roberto Preghenella");
+  md.SetComment("offline TOF run parameters");
+  md.SetAliRootVersion(gSystem->Getenv("ARVERSION"));
+  md.SetBeamPeriod(0);
+  if (!sto->Put(&obj, id, &md)) {
+    AliError(Form("error while putting object in storage %s", dbString));
+  }
+
+  /* success */
+  return kTRUE;
+}
+
+//_____________________________________________________________
+
+Int_t
+AliTOFAnalysisTaskCalibPass0::FitPeak(TF1 *fitFunc, TH1D *h, Float_t startSigma, Float_t nSigmaMin, Float_t nSigmaMax)
+{
+  /*
+   * fit peak
+   */
+
+  Double_t fitCent = h->GetBinCenter(h->GetMaximumBin());
+  Double_t fitMin = fitCent - nSigmaMin * startSigma;
+  Double_t fitMax = fitCent + nSigmaMax * startSigma;
+  if (fitMin < h->GetXaxis()->GetXmin()) fitMin = h->GetXaxis()->GetXmin();
+  if (fitMax > h->GetXaxis()->GetXmax()) fitMax = h->GetXaxis()->GetXmax();
+  fitFunc->SetParameter(1, fitCent);
+  fitFunc->SetParameter(2, startSigma);
+  Int_t fitres = h->Fit(fitFunc, "WWq0", "", fitMin, fitMax);
+  if (fitres != 0) return fitres;
+  /* refit with better range */
+  for (Int_t i = 0; i < 3; i++) {
+    fitCent = fitFunc->GetParameter(1);
+    fitMin = fitCent - nSigmaMin * fitFunc->GetParameter(2);
+    fitMax = fitCent + nSigmaMax * fitFunc->GetParameter(2);
+    if (fitMin < h->GetXaxis()->GetXmin()) fitMin = h->GetXaxis()->GetXmin();
+    if (fitMax > h->GetXaxis()->GetXmax()) fitMax = h->GetXaxis()->GetXmax();
+    fitres = h->Fit(fitFunc, "q0", "", fitMin, fitMax);
+    if (fitres != 0) return fitres;
+  }
+  return fitres;
+}
diff --git a/TOF/AliTOFAnalysisTaskCalibPass0.h b/TOF/AliTOFAnalysisTaskCalibPass0.h
new file mode 100644 (file)
index 0000000..1ce0841
--- /dev/null
@@ -0,0 +1,110 @@
+#ifndef ALITOFANALYSISTASKCALIBPASS0_H
+#define ALITOFANALYSISTASKCALIBPASS0_H
+
+/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ * See cxx source for full Copyright notice                               */
+
+///////////////////////////////////////////////////////////////
+//                                                           //
+//   This class provides TOF pass0/passX calibration tools   //
+//                                                           //
+///////////////////////////////////////////////////////////////
+
+#include "AliAnalysisTaskSE.h"
+
+class AliESDtrack;
+class AliESDEvent;
+class AliPhysicsSelection;
+class AliESDtrackCuts;
+class AliESDVertex;
+class AliGRPManager;
+class AliGRPObject;
+class AliTOFcalib;
+class TList;
+class TH2F;
+class TF1;
+class TH1D;
+
+class AliTOFAnalysisTaskCalibPass0 :
+public AliAnalysisTaskSE
+{
+
+ public:
+
+  AliTOFAnalysisTaskCalibPass0(); // default constructor
+  virtual ~AliTOFAnalysisTaskCalibPass0(); // default destructor
+
+  virtual void UserCreateOutputObjects(); // user create output objects
+  virtual void UserExec(Option_t *); // user exec
+
+  /* getters */
+  AliPhysicsSelection *GetEventCuts() const {return fEventCuts;}; // getter
+  AliESDtrackCuts *GetTrackCuts() const {return fTrackCuts;}; // getter
+  AliTOFcalib *GetTOFcalib() const {return fTOFcalib;}; // getter
+  
+  /* setters */
+  void SetEventSelectionFlag(Bool_t value = kTRUE) {fEventSelectionFlag = value;}; // setter
+  void SetVertexSelectionFlag(Bool_t value = kTRUE) {fVertexSelectionFlag = value;}; // setter
+  void SetVertexCut(Double_t value) {fVertexCut = value;}; // setter
+
+  /* post-processing methods */
+  Bool_t ProcessOutput(const Char_t *filename, const Char_t *dbString); // process output
+  
+ protected:
+
+  AliTOFAnalysisTaskCalibPass0(const AliTOFAnalysisTaskCalibPass0 &); // copy constructor
+  AliTOFAnalysisTaskCalibPass0 &operator=(const AliTOFAnalysisTaskCalibPass0 &); // operator=
+
+  /* methods */
+  Bool_t InitRun(); // init run
+  Bool_t InitEvent(); // init event
+  Bool_t HasTOFMeasurement(const AliESDtrack *track) const ; // has TOF measurement
+
+  /* post-processing methods */
+  Bool_t CheckMatchingPerformance(const TH2F *histoDeltazEta, const TH2F *histoAcceptedTracksEtaPt, const TH2F *histoMatchedTracksEtaPt) const; // check matching efficiency
+  Bool_t CalibrateAndStore(TH2F *histoVertexTimestamp, TH2F *histoDeltatTimestamp, const Char_t *dbString); // calibrate and store
+  Int_t FitPeak(TF1 *fitFunc, TH1D *h, Float_t startSigma, Float_t nSigmaMin, Float_t nSigmaMax); // fit peak
+
+  /* flags and cuts */
+  Bool_t fInitFlag; // init flag
+  Bool_t fEventSelectionFlag; // event selection flag
+  Bool_t fVertexSelectionFlag; // vertex selection flag
+  Double_t fVertexCut; // vertex cut
+
+  /* ESD analysis */
+  Int_t fRunNumber; // run number
+  AliESDEvent *fESDEvent; // ESD event
+  AliPhysicsSelection *fEventCuts; // event cuts
+  AliESDtrackCuts *fTrackCuts; // track cuts
+  UInt_t fStartTime; // start time
+  UInt_t fEndTime; // end time
+  UInt_t fEventTime; // event time
+  UInt_t fElapsedTime; // event time since start
+  const AliESDVertex *fkVertex; // vertex
+
+  /* GRP related stuff */
+  AliGRPManager *fGRPManager; // GRP manager
+  const AliGRPObject *fkGRPObject; // GRP object
+  
+  /* TOF related stuff */
+  AliTOFcalib *fTOFcalib; // TOF calib
+
+  /* lists and histos */
+  TList *fHistoList; // list of histograms
+  TH2F *fHistoVertexTimestamp; // vertex-timestamp histo
+  TH2F *fHistoDeltatTimestamp; // deltat-timestamp histo
+  TH2F *fHistoDeltazEta; // deltaz-eta histo
+  TH2F *fHistoDeltazCosTheta; // deltaz-costheta histo
+  TH2F *fHistoAcceptedTracksEtaPt; // accepted tracks eta-pt histo
+  TH2F *fHistoMatchedTracksEtaPt; // matched tracks eta-pt histo
+
+  /* post-processing variables */
+  static const Int_t fgkMaxNumberOfPoints; // max number of points
+  static const Double_t fgkMinVertexIntegral; // min vertex integral
+  static const Double_t fgkMinDeltatIntegral; // min vertex integral
+
+
+  ClassDef(AliTOFAnalysisTaskCalibPass0, 1);
+};
+
+#endif /* ALIANALYSISTASKEVENTTIME_H */
diff --git a/TOF/CMakelibTOFcalib.pkg b/TOF/CMakelibTOFcalib.pkg
new file mode 100644 (file)
index 0000000..9b8b60a
--- /dev/null
@@ -0,0 +1,43 @@
+#--------------------------------------------------------------------------------#
+# Package File for TOFbase                                                       #
+# Author : Johny Jose (johny.jose@cern.ch)                                       #
+# Variables Defined :                                                            #
+#                                                                                #
+# SRCS - C++ source files                                                        #
+# HDRS - C++ header files                                                        #
+# DHDR - ROOT Dictionary Linkdef header file                                     #
+# CSRCS - C source files                                                         #
+# CHDRS - C header files                                                         #
+# EINCLUDE - Include directories                                                 #
+# EDEFINE - Compiler definitions                                                 #
+# ELIBS - Extra libraries to link                                                #
+# ELIBSDIR - Extra library directories                                           #
+# PACKFFLAGS - Fortran compiler flags for package                                #
+# PACKCXXFLAGS - C++ compiler flags for package                                  #
+# PACKCFLAGS - C compiler flags for package                                      #
+# PACKSOFLAGS - Shared library linking flags                                     #
+# PACKLDFLAGS - Module linker flags                                              #
+# PACKBLIBS - Libraries to link (Executables only)                               #
+# EXPORT - Header files to be exported                                           #
+# CINTHDRS - Dictionary header files                                             #
+# CINTAUTOLINK - Set automatic dictionary generation                             #
+# ARLIBS - Archive Libraries and objects for linking (Executables only)          #
+# SHLIBS - Shared Libraries and objects for linking (Executables only)           #
+#--------------------------------------------------------------------------------#
+
+set ( SRCS  AliTOFAnalysisTaskCalibPass0.cxx)
+
+string ( REPLACE ".cxx" ".h" HDRS "${SRCS}" )
+
+set ( DHDR TOFcalibLinkDef.h)
+
+set ( EINCLUDE  RAW)
+
+if( ALICE_TARGET STREQUAL "win32gcc")
+       
+                               set ( PACKSOFLAGS  ${SOFLAGS}
+                               -L${ALICE_ROOT}/lib/tgt_${ALICE_TARGET}
+                               -lTOFbase -lSTEER -lCDB -lSTEERBase
+                               -lRAWDatarec -lRAWDatabase -L${ROOTLIBDIR} )
+
+endif( ALICE_TARGET STREQUAL "win32gcc")
diff --git a/TOF/TOFCalibPass0.C b/TOF/TOFCalibPass0.C
new file mode 100644 (file)
index 0000000..19ed86b
--- /dev/null
@@ -0,0 +1,82 @@
+//___________________________________________________________________________
+
+LoadLibraries()
+{
+  gSystem->Load("libANALYSIS");
+  gSystem->Load("libANALYSISalice");
+  gSystem->Load("libTOFcalib");
+}
+
+//___________________________________________________________________________
+
+MakeOCDB(const Char_t *filename = "TOFCalibPass0.root", const Char_t *dbString = "local://$HOME/OCDB")
+{
+  LoadLibraries();
+  AliTOFAnalysisTaskCalibPass0 calibTask;
+  calibTask.ProcessOutput(filename, dbString);
+}
+
+//___________________________________________________________________________
+
+SteerTask(const Char_t *inputfilename, Int_t maxFiles = kMaxInt, Int_t maxEv = kMaxInt)
+{
+
+  LoadLibraries();
+
+  /* setup input chain */
+  TString str = inputfilename;
+  const Char_t *filename;
+  TChain *chain = new TChain("esdTree");
+  if (str.EndsWith(".xml")) {
+    TGrid::Connect("alien://");
+    Info("SteerTaskEventTime", "reading data list from collection:");
+    TAlienCollection coll(inputfilename, maxFiles);
+    coll.Reset();
+    while (coll.Next()) {
+      filename = coll.GetTURL();
+      Info("SteerTaskEventTime", Form("%s", filename));
+      chain->Add(filename);
+    }
+  }
+  else if (str.EndsWith(".txt")) {
+    Info("SteerTaskEventTime", "reading data list from text file:");
+    ifstream is(inputfilename);
+    Char_t buf[4096];
+    while(!is.eof()) {
+      is.getline(buf, 4096);
+      if (is.eof()) break;
+      chain->Add(buf);
+      Info("SteerTaskEventTime", Form("%s", buf));
+    }
+    is.close();
+  }
+  else {
+    Info("SteerTaskEventTime", "single file:");
+    filename = inputfilename;
+    Info("SteerTaskEventTime", Form("%s", filename));
+    chain->Add(filename);
+  }
+  Info("SteerTaskEventTime", Form("chain is ready: %d events", chain->GetEntries()));
+
+  /* create analysis manager */
+  AliAnalysisManager *mgr = new AliAnalysisManager("EventTime");
+
+  /* define input event handler */
+  AliESDInputHandler *esdh = new AliESDInputHandler();
+  esdh->SetReadFriends(kFALSE);
+  mgr->SetInputEventHandler(esdh);
+
+  /* add tasks */
+  gROOT->LoadMacro("$ALICE_ROOT/TOF/AddTOFAnalysisTaskCalibPass0.C");
+  AliTOFAnalysisTaskCalibPass0 *thisTask = AddTOFAnalysisTaskCalibPass0();
+
+  /* start analysis */
+  mgr->SetDebugLevel(0);
+  if (!mgr->InitAnalysis()) return;
+  mgr->PrintStatus();
+  mgr->StartAnalysis("local", chain, maxEv);
+
+  /* create dummy file to tell we are done */
+  gSystem->Exec("touch done");
+
+}
diff --git a/TOF/TOFcalibLinkDef.h b/TOF/TOFcalibLinkDef.h
new file mode 100644 (file)
index 0000000..cd350ac
--- /dev/null
@@ -0,0 +1,13 @@
+#ifdef __CINT__
+/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ * See cxx source for full Copyright notice                               */
+
+/* $Id: TOFbaseLinkDef.h 44711 2010-10-28 12:45:01Z rpreghen $ */
+
+#pragma link off all globals;
+#pragma link off all classes;
+#pragma link off all functions;
+#pragma link C++ class AliTOFAnalysisTaskCalibPass0+;
+
+#endif