+
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateDeltaBCOffset()
+{
+ /*
+ * create deltaBC offset
+ */
+
+ if (fDeltaBCOffset) {
+ AliWarning("DeltaBCOffset object already defined, cannot create a new one");
+ return;
+ }
+ fDeltaBCOffset = new AliTOFDeltaBCOffset();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateCTPLatency()
+{
+ /*
+ * create CTP latency
+ */
+
+ if (fCTPLatency) {
+ AliWarning("CTPLatency object already defined, cannot create a new one");
+ return;
+ }
+ fCTPLatency = new AliTOFCTPLatency();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateT0Fill()
+{
+ /*
+ * create event-time
+ */
+
+ if (fT0Fill) {
+ AliWarning("T0Fill object already defined, cannot create a new one");
+ return;
+ }
+ fT0Fill = new AliTOFT0Fill();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateRunParams()
+{
+ /*
+ * create run params
+ */
+
+ if (fRunParams) {
+ AliWarning("RunParams object already defined, cannot create a new one");
+ return;
+ }
+ fRunParams = new AliTOFRunParams();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteDeltaBCOffsetOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * deltaBC offset on CDB
+ */
+
+ if (!fDeltaBCOffset) return;
+ AliCDBId id(Form("%s/DeltaBCOffset", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fDeltaBCOffset, id, md);
+ AliDebug(2,Form("DeltaBCOffset written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteCTPLatencyOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * write CTP latency on CDB
+ */
+
+ if (!fCTPLatency) return;
+ AliCDBId id(Form("%s/CTPLatency", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fCTPLatency, id, md);
+ AliDebug(2,Form("CTPLatency written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteT0FillOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * write event-time on CDB
+ */
+
+ if (!fT0Fill) return;
+ AliCDBId id(Form("%s/T0Fill", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fT0Fill, id, md);
+ AliDebug(2,Form("T0Fill written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteRunParamsOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * write run params on CDB
+ */
+
+ if (!fRunParams) return;
+ AliCDBId id(Form("%s/RunParams", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fRunParams, id, md);
+ AliDebug(2,Form("RunParams written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadDeltaBCOffsetFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read deltaBC offset from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/DeltaBCOffset", sel),nrun);
+ if (!entry) {
+ AliFatal("No DeltaBCOffset entry found in CDB");
+ exit(0);
+ }
+ fDeltaBCOffset =(AliTOFDeltaBCOffset *)entry->GetObject();
+ if(!fDeltaBCOffset){
+ AliFatal("No DeltaBCOffset object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadCTPLatencyFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read CTP latency from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/CTPLatency", sel),nrun);
+ if (!entry) {
+ AliFatal("No CTPLatency entry found in CDB");
+ exit(0);
+ }
+ fCTPLatency =(AliTOFCTPLatency *)entry->GetObject();
+ if(!fCTPLatency){
+ AliFatal("No CTPLatency object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadT0FillFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read event-time from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/T0Fill", sel),nrun);
+ if (!entry) {
+ AliFatal("No T0Fill entry found in CDB");
+ exit(0);
+ }
+ fT0Fill =(AliTOFT0Fill *)entry->GetObject();
+ if(!fT0Fill){
+ AliFatal("No T0Fill object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadRunParamsFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read run params from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/RunParams", sel),nrun);
+ if (!entry) {
+ AliFatal("No RunParams entry found in CDB");
+ exit(0);
+ }
+ fRunParams =(AliTOFRunParams *)entry->GetObject();
+ if(!fRunParams){
+ AliFatal("No RunParams object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::Init(Int_t run)
+{
+ /*
+ * init
+ */
+
+ if (fInitFlag) {
+ AliWarning("the class was already initialized, re-initialize it");
+ fInitFlag = kFALSE;
+ }
+
+ /* read channel status array */
+ if (!ReadParOnlineStatusFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"Status\" object from OCDB");
+ return kFALSE;
+ }
+ /* get par offline array */
+ if (!ReadParOfflineFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"ParOffline\" object from OCDB");
+ return kFALSE;
+ }
+ /* get deltaBC offset obj */
+ if (!ReadDeltaBCOffsetFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"DeltaBCOffset\" object from OCDB");
+ return kFALSE;
+ }
+ /* get CTP latency obj */
+ if (!ReadCTPLatencyFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"CTPLatency\" object from OCDB");
+ return kFALSE;
+ }
+ /* get run params obj */
+ if (!ReadRunParamsFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"RunParams\" object from OCDB");
+ return kFALSE;
+ }
+ /* get response params */
+ if (fCorrectTExp) {
+ TFile *responseFile = TFile::Open("$ALICE_ROOT/TOF/data/AliTOFresponsePar.root");
+ if (!responseFile || !responseFile->IsOpen()) {
+ AliError("cannot open \"ResponseParams\" local file");
+ return kFALSE;
+ }
+ fResponseParams = (AliTOFResponseParams *)responseFile->Get("ResponseParams");
+ if (!fResponseParams) {
+ AliError("cannot get \"ResponseParams\" object from local file");
+ return kFALSE;
+ }
+ responseFile->Close();
+ }
+
+ /* all done */
+ fInitFlag = kTRUE;
+ return kTRUE;
+
+}
+
+//----------------------------------------------------------------------------
+
+Double_t
+AliTOFcalib::GetTimeCorrection(Int_t index, Double_t tot, Int_t deltaBC, Int_t l0l1, UInt_t timestamp)
+{
+ /*
+ * get time correction
+ */
+
+ if (!fInitFlag) {
+ AliError("class not yet initialized. Initialize it before.");
+ return 0.;
+ }
+
+ /* get calibration params */
+ AliTOFChannelOffline *parOffline = (AliTOFChannelOffline *)fTOFCalOffline->At(index);
+ Int_t deltaBCOffset = fDeltaBCOffset->GetDeltaBCOffset();
+ Float_t ctpLatency = fCTPLatency->GetCTPLatency();
+ Float_t tdcLatencyWindow = fStatus->GetLatencyWindow(index) * 1.e3;
+ Float_t timezero = fRunParams->EvalT0(timestamp);
+ /* check whether to remove mean T0.
+ * useful when one wants to compute mean T0 */
+ if (!fRemoveMeanT0) timezero = 0.;
+
+ /* compute correction */
+ Double_t corr = 0.;
+ /* deltaBC correction */
+ deltaBC = deltaBCOffset; /* inhibit deltaBC correction for the time being */
+ corr += (deltaBC - deltaBCOffset) * AliTOFGeometry::BunchCrossingBinWidth();
+ /* L0-L1 latency correction */
+ corr -= l0l1 * AliTOFGeometry::BunchCrossingBinWidth();
+ /* CTP latency correction */
+ corr -= ctpLatency;
+ /* TDC latency window correction */
+ corr += tdcLatencyWindow;
+ /* time-zero correction */
+ corr += timezero;
+ /* time calibration correction */
+ if (tot < AliTOFGeometry::SlewTOTMin())
+ tot = AliTOFGeometry::SlewTOTMin();
+ if (tot > AliTOFGeometry::SlewTOTMax())
+ tot = AliTOFGeometry::SlewTOTMax();
+ for (Int_t islew = 0; islew < 6; islew++)
+ corr += parOffline->GetSlewPar(islew) * TMath::Power(tot, islew) * 1.e3;
+
+ /* return correction */
+ return corr;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CalibrateESD(AliESDEvent *event)
+{
+ /*
+ * calibrate ESD
+ */
+
+ if (!fInitFlag) {
+ AliError("class not yet initialized. Initialize it before.");
+ return;
+ }
+
+ /* loop over tracks */
+ AliESDtrack *track = NULL;
+ Int_t index, l0l1, deltaBC;
+ Double_t time, tot, corr, texp[AliPID::kSPECIES];
+ UInt_t timestamp = event->GetTimeStamp();
+ for (Int_t itrk = 0; itrk < event->GetNumberOfTracks(); itrk++) {
+
+ /* get track */
+ track = event->GetTrack(itrk);
+ if (!track || !(track->GetStatus() & AliESDtrack::kTOFout)) continue;
+
+ /* calibrate TOF signal */
+ if (fCalibrateTOFsignal) {
+ /* get info */
+ index = track->GetTOFCalChannel();
+ time = track->GetTOFsignalRaw();
+ tot = track->GetTOFsignalToT();
+ l0l1 = track->GetTOFL0L1();
+ deltaBC = track->GetTOFDeltaBC();
+ /* get correction */
+ corr = GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp);
+ /* apply correction */
+ time -= corr;
+ /* set new TOF signal */
+ track->SetTOFsignal(time);
+ }
+
+ /* correct expected time */
+ if (fCorrectTExp) {
+ /* get integrated times */
+ track->GetIntegratedTimes(texp);
+ /* loop over particle types and correct expected time */
+ for (Int_t ipart = 0; ipart < AliPID::kSPECIES; ipart++)
+ texp[ipart] += fResponseParams->EvalTExpCorr(ipart, track->P());
+ /* set integrated times */
+ track->SetIntegratedTimes(texp);
+ }
+
+ }
+
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::IsChannelEnabled(Int_t index)
+{
+ /*
+ * is channel enabled
+ */
+
+ if (!fInitFlag) {
+ AliError("class not yet initialized. Initialize it before.");
+ return kTRUE;
+ }
+
+ /* check bad status */
+ if (fStatus->GetPulserStatus(index) == AliTOFChannelOnlineStatusArray::kTOFPulserBad) return kFALSE;
+ if (fStatus->GetNoiseStatus(index) == AliTOFChannelOnlineStatusArray::kTOFNoiseBad) return kFALSE;
+ if (fStatus->GetHWStatus(index) == AliTOFChannelOnlineStatusArray::kTOFHWBad) return kFALSE;
+
+ /* good status */
+ return kTRUE;
+
+}