+#include <TROOT.h>
+#include <TClonesArray.h>
+#include <TObjArray.h>
+
+#include "AliCDBManager.h"
+#include "AliCDBEntry.h"
+#include "AliLog.h"
+
+#include "AliTRDPIDReference.h"
+#include "AliTRDPIDResponseObject.h"
+#include "AliTRDcalibDB.h"
+#include "AliTRDtrapConfig.h"
+#include "AliTRDtrapConfigHandler.h"
+#include "AliTRDCommonParam.h"
+#include "AliTRDgeometry.h"
+
+#include "Cal/AliTRDCalROC.h"
+#include "Cal/AliTRDCalPad.h"
+#include "Cal/AliTRDCalDet.h"
+#include "Cal/AliTRDCalDCS.h"
+#include "Cal/AliTRDCalDCSFEE.h"
+#include "Cal/AliTRDCalDCSv2.h"
+#include "Cal/AliTRDCalDCSFEEv2.h"
+#include "Cal/AliTRDCalPID.h"
+#include "Cal/AliTRDCalMonitoring.h"
+#include "Cal/AliTRDCalChamberStatus.h"
+#include "Cal/AliTRDCalPadStatus.h"
+#include "Cal/AliTRDCalSingleChamberStatus.h"
+#include "Cal/AliTRDCalTrkAttach.h"
+#include "Cal/AliTRDCalOnlineGainTable.h"
+
+ClassImp(AliTRDcalibDB)
+
+AliTRDcalibDB *AliTRDcalibDB::fgInstance = 0;
+Bool_t AliTRDcalibDB::fgTerminated = kFALSE;
+
+//_ singleton implementation __________________________________________________
+AliTRDcalibDB* AliTRDcalibDB::Instance()
+{
+ //
+ // Singleton implementation
+ // Returns an instance of this class, it is created if neccessary
+ //
+
+ if (fgTerminated != kFALSE) {
+ return 0;
+ }
+
+ if (fgInstance == 0) {
+ fgInstance = new AliTRDcalibDB();
+ }
+
+ return fgInstance;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDcalibDB::Terminate()
+{
+ //
+ // Singleton implementation
+ // Deletes the instance of this class and sets the terminated flag,
+ // instances cannot be requested anymore
+ // This function can be called several times.
+ //
+
+ fgTerminated = kTRUE;
+
+ if (fgInstance != 0) {
+ delete fgInstance;
+ fgInstance = 0;
+ }
+
+}
+
+//_____________________________________________________________________________
+AliTRDcalibDB::AliTRDcalibDB()
+ :TObject()
+ ,fRun(-1)
+ ,fPRFsmp(0)
+ ,fPRFbin(0)
+ ,fPRFlo(0)
+ ,fPRFhi(0)
+ ,fPRFwid(0)
+ ,fPRFpad(0)
+ ,fPIDResponse(NULL)
+ ,fOnlineGainTableID(0)
+ ,fTrapConfig(0x0)
+ ,fTrapConfigName("")
+ ,fTrapConfigVersion("")
+{
+ //
+ // Default constructor
+ //
+ // TODO Default runnumber is set to 0, this should be changed later
+ // to an invalid value (e.g. -1) to prevent
+ // TODO invalid calibration data to be used.
+ //
+
+ for (Int_t i = 0; i < kCDBCacheSize; ++i) {
+ fCDBCache[i] = 0;
+ fCDBEntries[i] = 0;
+ }
+
+ // Create the sampled PRF
+ SamplePRF();
+
+}
+
+//_____________________________________________________________________________
+AliTRDcalibDB::AliTRDcalibDB(const AliTRDcalibDB &c)
+ :TObject(c)
+ ,fRun(-1)
+ ,fPRFsmp(0)
+ ,fPRFbin(0)
+ ,fPRFlo(0)
+ ,fPRFhi(0)
+ ,fPRFwid(0)
+ ,fPRFpad(0)
+ ,fPIDResponse(NULL)
+ ,fOnlineGainTableID(0)
+ ,fTrapConfig(0x0)
+ ,fTrapConfigName("")
+ ,fTrapConfigVersion("")
+{
+ //
+ // Copy constructor (not that it make any sense for a singleton...)
+ //
+
+ for (Int_t i = 0; i < kCDBCacheSize; ++i) {
+ fCDBCache[i] = 0;
+ fCDBEntries[i] = 0;
+ }
+
+ // Create the sampled PRF
+ SamplePRF();
+
+}
+
+//_____________________________________________________________________________
+AliTRDcalibDB &AliTRDcalibDB::operator=(const AliTRDcalibDB &c)
+{
+ //
+ // Assignment operator (same as above ...)
+ //
+
+ if (this != &c) {
+ AliFatal("No assignment operator defined");
+ }
+
+ return *this;
+
+}
+
+//_____________________________________________________________________________
+AliTRDcalibDB::~AliTRDcalibDB()
+{
+ //
+ // destructor
+ //
+
+ if (fPRFsmp) {
+ delete [] fPRFsmp;
+ fPRFsmp = 0;
+ }
+
+ if (fPIDResponse) {
+ delete fPIDResponse;
+ fPIDResponse = 0x0;
+ }
+
+ Invalidate();
+ fgInstance = 0;
+}
+
+//_caching functions____________________________________________________________
+const TObject *AliTRDcalibDB::GetCachedCDBObject(Int_t id)
+{
+ //
+ // Retrieves a cdb object with the given id. The objects are cached as
+ // long as the run number is not changed.
+ //
+ // Put together the available objects here by using the lines
+ // a) For usual calibration objects:
+ // case kID<Name> :
+ // return CacheCDBEntry(kID<Name>,"TRD/Calib/<Path>");
+ // break;
+ // See function CacheCDBEntry for details.
+ // and
+ // b) For calibration data which depends on two objects: One containing
+ // a value per detector and one the local fluctuations per pad:
+ // case kID<Name> :
+ // return CacheMergeCDBEntry(kID<Name>,"TRD/Calib/<padPath>","TRD/Calib/<chamberPath>");
+ // break;
+ // See function CacheMergeCDBEntry for details.
+ //
+
+ switch (id) {
+
+ // Parameters defined per pad and chamber
+ case kIDVdriftPad :
+ return CacheCDBEntry(kIDVdriftPad ,"TRD/Calib/LocalVdrift");
+ break;
+ case kIDVdriftChamber :
+ return CacheCDBEntry(kIDVdriftChamber ,"TRD/Calib/ChamberVdrift");
+ break;
+ case kIDExBChamber :
+ return CacheCDBEntry(kIDExBChamber ,"TRD/Calib/ChamberExB");
+ break;
+ case kIDT0Pad :
+ return CacheCDBEntry(kIDT0Pad ,"TRD/Calib/LocalT0");
+ break;
+ case kIDT0Chamber :
+ return CacheCDBEntry(kIDT0Chamber ,"TRD/Calib/ChamberT0");
+ break;
+ case kIDGainFactorPad :
+ return CacheCDBEntry(kIDGainFactorPad ,"TRD/Calib/LocalGainFactor");
+ break;
+ case kIDGainFactorChamber :
+ return CacheCDBEntry(kIDGainFactorChamber ,"TRD/Calib/ChamberGainFactor");
+ break;
+
+ case kIDOnlineGainFactor :
+ switch(GetOnlineGainTableID()) {
+ case 0:
+ // For testing purposes only !!!
+ AliInfo("No gain table name from OCDB. Use default table!");
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Krypton_2011-01");
+ break;
+ case 1:
+ // Online gain table ID 1
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Krypton_2011-01");
+ break;
+ case 2:
+ // Online gain table ID 2
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Gaintbl_Uniform_FGAN0_2011-01");
+ break;
+ case 3:
+ // Online gain table ID 3
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Gaintbl_Uniform_FGAN8_2011-01");
+ break;
+ case 4:
+ // Online gain table ID 4
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Krypton_2011-02");
+ break;
+ case 5:
+ // Online gain table ID 5
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Krypton_2011-03");
+ break;
+ case 6:
+ // Online gain table ID 6
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Gaintbl_Uniform_FGAN0_2012-01");
+ break;
+ case 7:
+ // Online gain table ID 7
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Gaintbl_Uniform_FGAN8_2012-01");
+ break;
+ case 8:
+ // Online gain table ID 8
+ return CacheCDBEntry(kIDOnlineGainFactor ,"TRD/Calib/Krypton_2012-01");
+ break;
+ default:
+ AliError(Form("unknown gaintable requested with ID"));
+ }
+ break;
+
+ case kIDNoiseChamber :
+ return CacheCDBEntry(kIDNoiseChamber ,"TRD/Calib/DetNoise");
+ break;
+ case kIDNoisePad :
+ return CacheCDBEntry(kIDNoisePad ,"TRD/Calib/PadNoise");
+ break;
+
+ // Parameters defined per pad
+ case kIDPRFWidth :
+ return CacheCDBEntry(kIDPRFWidth ,"TRD/Calib/PRFWidth");
+ break;
+
+ // Status values
+ case kIDChamberStatus :
+ return CacheCDBEntry(kIDChamberStatus ,"TRD/Calib/ChamberStatus");
+ break;
+ case kIDPadStatus :
+ return CacheCDBEntry(kIDPadStatus ,"TRD/Calib/PadStatus");
+ break;
+
+ // Global parameters
+ case kIDMonitoringData :
+ return CacheCDBEntry(kIDMonitoringData ,"TRD/Calib/MonitoringData");
+ break;
+ case kIDFEE :
+ return CacheCDBEntry(kIDFEE ,"TRD/Calib/FEE");
+ break;
+ case kIDTrapConfig :
+ return CacheCDBEntry(kIDTrapConfig ,"TRD/Calib/TrapConfig");
+ break;
+ case kIDDCS :
+ return CacheCDBEntry(kIDDCS ,"TRD/Calib/DCS");
+ break;
+ case kIDPIDNN :
+ return CacheCDBEntry(kIDPIDNN ,"TRD/Calib/PIDNN");
+ break;
+ case kIDPIDLQ :
+ return CacheCDBEntry(kIDPIDLQ ,"TRD/Calib/PIDLQ");
+ break;
+ case kIDPIDLQ1D:
+ return CacheCDBEntry(kIDPIDLQ1D ,"TRD/Calib/PIDLQ1D");
+ break;
+ case kIDRecoParam :
+ return CacheCDBEntry(kIDRecoParam ,"TRD/Calib/RecoParam");
+ break;
+ case kIDAttach :
+ return CacheCDBEntry(kIDAttach ,"TRD/Calib/TrkAttach");
+ break;
+ case kIDPHQ :
+ return CacheCDBEntry(kIDPHQ ,"TRD/Calib/PHQ");
+ break;
+ }
+
+ return 0;
+
+}
+
+//_____________________________________________________________________________
+AliCDBEntry *AliTRDcalibDB::GetCDBEntry(const char *cdbPath)
+{
+ //
+ // Retrieves an entry with path <cdbPath> from the CDB.
+ //
+
+ AliCDBEntry *entry = AliCDBManager::Instance()->Get(cdbPath,fRun);
+ if (!entry) {
+ AliError(Form("Failed to get entry: %s",cdbPath));
+ return 0;
+ }
+
+ return entry;
+
+}
+
+//_____________________________________________________________________________
+const TObject *AliTRDcalibDB::CacheCDBEntry(Int_t id, const char *cdbPath)
+{
+ //
+ // Caches the entry <id> with cdb path <cdbPath>
+ //
+
+ if (!fCDBCache[id]) {
+ fCDBEntries[id] = GetCDBEntry(cdbPath);
+ if (fCDBEntries[id]) {
+ fCDBCache[id] = fCDBEntries[id]->GetObject();
+ if (id == kIDOnlineGainFactor)
+ AliInfo(Form("loaded gain table: %s", fCDBEntries[id]->GetId().GetAliCDBPath().GetPath().Data()));
+ }
+ }
+
+ return fCDBCache[id];
+
+}
+
+//_____________________________________________________________________________
+void AliTRDcalibDB::SetRun(Long64_t run)
+{
+ //
+ // Sets current run number. Calibration data is read from the corresponding file.
+ // When the run number changes the caching is invalidated.
+ //
+
+ if (fRun == run) {
+ return;
+ }
+
+ fRun = run;
+
+ Invalidate();
+
+}
+
+//_____________________________________________________________________________
+void AliTRDcalibDB::Invalidate()
+{
+ //
+ // Invalidates cache (when run number is changed).
+ //
+
+ for (Int_t i = 0; i < kCDBCacheSize; ++i) {
+ if (fCDBEntries[i]) {
+ if (AliCDBManager::Instance()->GetCacheFlag() == kFALSE) {
+ if ((fCDBEntries[i]->IsOwner() == kFALSE) &&
+ (fCDBCache[i])) {
+ delete fCDBCache[i];
+ }
+ delete fCDBEntries[i];
+ }
+ fCDBEntries[i] = 0;
+ fCDBCache[i] = 0;
+ }
+ }
+
+ fOnlineGainTableID = 0;
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetNoise(Int_t det, Int_t col, Int_t row)
+{
+ //
+ // Returns the noise level in ADC counts for the given pad.
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDNoisePad));
+ if (!calPad) {
+ return -1;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return -1;
+ }
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDNoiseChamber));
+ if (!calChamber) {
+ return -1;
+ }
+
+ return calChamber->GetValue(det) * roc->GetValue(col,row);
+
+}
+
+//_____________________________________________________________________________
+AliTRDCalROC *AliTRDcalibDB::GetNoiseROC(Int_t det)
+{
+ //
+ // Returns the Vdrift calibration object for a given ROC
+ // containing one number per pad
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDNoisePad));
+ if (!calPad) {
+ return 0;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return 0;
+ }
+ else {
+ return roc;
+ }
+
+}
+
+//_____________________________________________________________________________
+const AliTRDCalDet *AliTRDcalibDB::GetNoiseDet()
+{
+ //
+ // Returns the Vdrift calibration object
+ // containing one number per detector
+ //
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDNoiseChamber));
+ if (!calChamber) {
+ return 0;
+ }
+ else {
+ return calChamber;
+ }
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetVdrift(Int_t det, Int_t col, Int_t row)
+{
+ //
+ // Returns the drift velocity for the given pad.
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDVdriftPad));
+ if (!calPad) {
+ return -1;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return -1;
+ }
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDVdriftChamber));
+ if (!calChamber) {
+ return -1;
+ }
+
+ return calChamber->GetValue(det) * roc->GetValue(col,row);
+
+}
+
+//_____________________________________________________________________________
+AliTRDCalROC *AliTRDcalibDB::GetVdriftROC(Int_t det)
+{
+ //
+ // Returns the Vdrift calibration object for a given ROC
+ // containing one number per pad
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDVdriftPad));
+ if (!calPad) {
+ return 0;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return 0;
+ }
+ else {
+ return roc;
+ }
+
+}
+
+//_____________________________________________________________________________
+const AliTRDCalDet *AliTRDcalibDB::GetVdriftDet()
+{
+ //
+ // Returns the Vdrift calibration object
+ // containing one number per detector
+ //
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDVdriftChamber));
+ if (!calChamber) {
+ return 0;
+ }
+ else {
+ return calChamber;
+ }
+
+}
+
+//_____________________________________________________________________________
+TObjArray * AliTRDcalibDB::GetPHQ()
+{
+ //
+ //return PHQ calibration object
+ //
+ TObjArray *arr = (TObjArray *) (GetCachedCDBObject(kIDPHQ));
+ return arr;
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetVdriftAverage(Int_t det)
+{
+ //
+ // Returns the average drift velocity for the given detector
+ //
+
+ const AliTRDCalDet *calDet = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDVdriftChamber));
+ if (!calDet) {
+ return -1;
+ }
+
+ return calDet->GetValue(det);
+
+}
+//_____________________________________________________________________________
+const AliTRDCalDet *AliTRDcalibDB::GetExBDet()
+{
+ //
+ // Returns the exB calibration object
+ // containing one number per detector
+ //
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> (GetCachedCDBObject(kIDExBChamber));
+
+ Double_t meanexb = 100.0;
+ if (calChamber) meanexb = calChamber->GetMean();
+ //printf("mean %f\n",meanexb);
+
+ if ((!calChamber) || (meanexb > 70.0)) {
+
+ const AliTRDCalDet *calChambervdrift = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDVdriftChamber));
+ if (!calChambervdrift) {
+ return 0;
+ }
+ else {
+ AliTRDCalDet *calDetExB = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
+ for(Int_t k = 0; k < 540; k++){
+ calDetExB->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(calChambervdrift->GetValue(k)));
+ }
+ return calDetExB;
+ }
+ }
+ else return calChamber;
+
+}
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetT0(Int_t det, Int_t col, Int_t row)
+{
+ //
+ // Returns t0 for the given pad.
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDT0Pad));
+ if (!calPad) {
+ return -1;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return -1;
+ }
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDT0Chamber));
+ if (!calChamber) {
+ return -1;
+ }
+
+ return calChamber->GetValue(det) + roc->GetValue(col,row);
+
+}
+
+//_____________________________________________________________________________
+AliTRDCalROC *AliTRDcalibDB::GetT0ROC(Int_t det)
+{
+ //
+ // Returns the t0 calibration object for a given ROC
+ // containing one number per pad
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDT0Pad));
+ if (!calPad) {
+ return 0;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return 0;
+ }
+ else {
+ return roc;
+ }
+
+}
+
+//_____________________________________________________________________________
+const AliTRDCalDet *AliTRDcalibDB::GetT0Det()
+{
+ //
+ // Returns the t0 calibration object
+ // containing one number per detector
+ //
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDT0Chamber));
+ if (!calChamber) {
+ return 0;
+ }
+ else {
+ return calChamber;
+ }
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetT0Average(Int_t det)
+{
+ //
+ // Returns the average t0 for the given detector
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDT0Pad));
+ if (!calPad) {
+ return -1;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return -1;
+ }
+
+ const AliTRDCalDet *calDet = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDT0Chamber));
+ if (!calDet) {
+ return -1;
+ }
+
+ Double_t sum = 0.0;
+ for (Int_t channel = 0; channel < roc->GetNchannels(); ++channel) {
+ sum += roc->GetValue(channel);
+ }
+ sum /= roc->GetNchannels();
+ sum += calDet->GetValue(det);
+ return sum;
+
+}
+
+//_____________________________________________________________________________
+AliTRDCalOnlineGainTableROC* AliTRDcalibDB::GetOnlineGainTableROC(Int_t det)
+{
+ //
+ // Returns the online gain factor table for a given ROC.
+ //
+
+ if (!HasOnlineFilterGain()) {
+ return 0x0;
+ }
+
+ const AliTRDCalOnlineGainTable *calOnline
+ = dynamic_cast<const AliTRDCalOnlineGainTable *>
+ (GetCachedCDBObject(kIDOnlineGainFactor));
+ if (!calOnline) {
+ return 0x0;
+ }
+
+ return calOnline->GetGainTableROC(det);
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetOnlineGainFactor(Int_t det, Int_t col, Int_t row)
+{
+ //
+ // Returns the online gain factor for the given pad.
+ //
+
+ if (!HasOnlineFilterGain()) {
+ return -1;
+ }
+
+ const AliTRDCalOnlineGainTable *calOnline
+ = dynamic_cast<const AliTRDCalOnlineGainTable *>
+ (GetCachedCDBObject(kIDOnlineGainFactor));
+ if (!calOnline) {
+ return -1;
+ }
+
+ return calOnline->GetGainCorrectionFactor(det,row,col);
+
+}
+
+//_____________________________________________________________________________
+AliTRDCalROC *AliTRDcalibDB::GetGainFactorROC(Int_t det)
+{
+ //
+ // Returns the gain factor calibration object for a given ROC
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDGainFactorPad));
+ if (!calPad) {
+ return 0;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return 0;
+ }
+ else {
+ return roc;
+ }
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetGainFactor(Int_t det, Int_t col, Int_t row)
+{
+ //
+ // Returns the gain factor for the given pad.
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDGainFactorPad));
+ if (!calPad) {
+ return -1;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return -1;
+ }
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDGainFactorChamber));
+ if (!calChamber) {
+ return -1;
+ }
+
+ return calChamber->GetValue(det) * roc->GetValue(col,row);
+
+}
+
+//_____________________________________________________________________________
+const AliTRDCalDet *AliTRDcalibDB::GetGainFactorDet()
+{
+ //
+ // Returns the gain factor calibration object
+ // containing one number per detector
+ //
+
+ const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDGainFactorChamber));
+ if (!calChamber) {
+ return 0;
+ }
+ else {
+ return calChamber;
+ }
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetGainFactorAverage(Int_t det)
+{
+ //
+ // Returns the average gain factor for the given detector
+ //
+
+ const AliTRDCalDet *calDet = dynamic_cast<const AliTRDCalDet *>
+ (GetCachedCDBObject(kIDGainFactorChamber));
+ if (!calDet) {
+ return -1;
+ }
+
+ return calDet->GetValue(det);
+
+}
+
+//_____________________________________________________________________________
+AliTRDCalROC *AliTRDcalibDB::GetPRFROC(Int_t det)
+{
+ //
+ // Returns the PRF calibration object for a given ROC
+ // containing one number per pad
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDPRFWidth));
+ if (!calPad) {
+ return 0;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return 0;
+ }
+ else {
+ return roc;
+ }
+
+}
+
+//_____________________________________________________________________________
+Float_t AliTRDcalibDB::GetPRFWidth(Int_t det, Int_t col, Int_t row)
+{
+ //
+ // Returns the PRF width for the given pad.
+ //
+
+ const AliTRDCalPad *calPad = dynamic_cast<const AliTRDCalPad *>
+ (GetCachedCDBObject(kIDPRFWidth));
+ if (!calPad) {
+ return -1;
+ }
+
+ AliTRDCalROC *roc = calPad->GetCalROC(det);
+ if (!roc) {
+ return -1;
+ }
+
+ return roc->GetValue(col,row);
+
+}
+
+//_____________________________________________________________________________
+Int_t AliTRDcalibDB::ExtractTimeBinsFromString(TString tbstr)
+{
+ // Check if there is any content in the string first
+ if (tbstr.Length() == 0) {
+ AliError("Parameter for number of timebins is empty!");
+ return -1;
+ }
+
+ // Check if we have the correct config parameter
+ TString tbident = "tb";
+ TString tbsubstr = tbstr(0,2);
+ if (!tbsubstr.EqualTo(tbident)) {
+ AliError(Form("Parameter for number of timebins is corrupted (%s)!", tbstr.Data()));
+ return -1;
+ }
+
+ tbstr.Remove(0,2);
+ // check if there is more than a number
+ if (!tbstr.IsDigit()) {
+ AliError(Form("Parameter for number of timebins is corrupted (%s)!", tbstr.Data()));
+ return -1;
+ }
+
+ return tbstr.Atoi();
+
+}
+
+//_____________________________________________________________________________
+Int_t AliTRDcalibDB::GetNumberOfTimeBinsDCSBoard(){
+ // This is an old way to extract the number of time bins,
+ // only to be used as a fallback to see whether there's
+ // a patched OCDB object!
+
+ Int_t nTB=-1;
+ // Get the array with SOR and EOR
+ const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
+ if(!dcsArr)
+ return -1; // Error
+ // Check CalDCS version
+ Int_t calver = 0;
+ if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS")) calver = 1;
+ else if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
+ // CalDCS version 1
+ if (calver == 1) {
+ // DCS object
+ AliTRDCalDCS *calSOR = dynamic_cast<AliTRDCalDCS *>(dcsArr->At(0));
+ // SOR mandantory
+ if(!calSOR)
+ return -1; // Error
+ else
+ nTB=calSOR->GetGlobalNumberOfTimeBins();
+ AliTRDCalDCS *calEOR = dynamic_cast<AliTRDCalDCS *>(dcsArr->At(1));
+ if(calEOR && calEOR->GetGlobalNumberOfTimeBins()!=nTB)
+ return -2; // Mixed
+ }
+ else if (calver == 2) {
+ // DCS object
+ AliTRDCalDCSv2 *calSOR = dynamic_cast<AliTRDCalDCSv2 *>(dcsArr->At(0));
+ // SOR mandantory
+ if(!calSOR)
+ return -1; // Error
+ else
+ nTB=calSOR->GetGlobalNumberOfTimeBins();
+ AliTRDCalDCSv2 *calEOR = dynamic_cast<AliTRDCalDCSv2 *>(dcsArr->At(1));
+ if(calEOR && calEOR->GetGlobalNumberOfTimeBins()!=nTB)
+ return -2; // Mixed
+ }
+ else{
+ // No version 1 nor version 2 object
+ return -1; // Error
+ }
+
+ // All well
+ return nTB;
+}
+//_____________________________________________________________________________
+Int_t AliTRDcalibDB::GetNumberOfTimeBinsDCS()
+{
+ //
+ // Returns number of time bins from the DCS
+ //
+
+ // Initialize with values indicating no information
+ TString cfg="";
+ Int_t nTB = -1;
+ // Extract the global configuration name
+ GetGlobalConfigurationByChamber(cfg,kTimebin);
+ // Extract the number of time bins from
+ // the global configuration
+ if(cfg.Length()>0){
+ nTB = ExtractTimeBinsFromString(cfg);
+ }
+ if(nTB>0){
+ // All well, we could extract the number
+ // of time bins from the configuration name
+ return nTB;
+ }
+ else{
+ // No number of time bins from config name.
+ // No board responded or similar.
+ // We patched some OCDB entries with
+ // only the global number of time bins set.
+ // We should get these here
+ nTB=GetNumberOfTimeBinsDCSBoard();
+ if(nTB>0){
+ AliWarning("Using old method for number of time bins."
+ " This is probably a patched OCDB entry");
+ return nTB;
+ }
+ else{
+ // Error
+ AliError("No number of time bins either"
+ " from config name or patched OCDB entry");
+ return -1;
+ }
+ }
+
+
+ // // Get the corresponding parameter
+ // TString cfgstr = "", cfgname = "";
+ // GetGlobalConfiguration(cfgname);
+ // if(cfgname.Length()==0)
+ // return -1;
+ // GetDCSConfigParOption(cfgname, kTimebin, 0, cfgstr);
+ // if(cfgstr.Length()==0)
+ // return -1;
+ // return ExtractTimeBinsFromString(cfgstr);
+
+}
+
+//_____________________________________________________________________________
+void AliTRDcalibDB::GetFilterType(TString &filterType)
+{
+ //
+ // Returns the filter type
+ //
+
+ TString cfgname = "";
+ GetGlobalConfiguration(cfgname);
+ GetDCSConfigParOption(cfgname, kFltrSet, 0, filterType);
+
+}
+
+//_____________________________________________________________________________
+Int_t AliTRDcalibDB::GetOnlineGainTableID()
+{
+ //
+ // Get the gain table ID from the DCS
+ //
+
+ if (fOnlineGainTableID > 0) {
+ return fOnlineGainTableID;
+ }
+
+ const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
+ if (!dcsArr){
+ return -1;
+ }
+
+ Int_t esor = 0; // Take SOR
+ Int_t calver = 0; // Check CalDCS version
+ if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS")) calver = 1;
+ if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
+
+ if (calver == 1) {
+
+ // No data for old DCS object available, anyway
+ return -1;
+
+ }
+ else if (calver == 2) {
+
+ // DCSv2 object
+ const AliTRDCalDCSv2 *calDCSv2 = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(esor));
+ if(!calDCSv2){
+ return -1;
+ }
+
+ TString tableName = "";
+ for (Int_t i = 0; i < 540; i++) {
+ const AliTRDCalDCSFEEv2 *calDCSFEEv2 = calDCSv2->GetCalDCSFEEObj(0);
+ tableName = calDCSFEEv2->GetGainTableName();
+ if (tableName.Length() > 0) {
+ break;
+ }
+ }
+ if (tableName.CompareTo("Krypton_2011-01") == 0)
+ fOnlineGainTableID = 1;
+ else if (tableName.CompareTo("Gaintbl_Uniform_FGAN0_2011-01") == 0)
+ fOnlineGainTableID = 2;
+ else if (tableName.CompareTo("Gaintbl_Uniform_FGAN8_2011-01") == 0)
+ fOnlineGainTableID = 3;
+ else if (tableName.CompareTo("Krypton_2011-02") == 0)
+ fOnlineGainTableID = 4;
+ else if (tableName.CompareTo("Krypton_2011-03") == 0)
+ fOnlineGainTableID = 5;
+ else if (tableName.CompareTo("Gaintbl_Uniform_FGAN0_2012-01") == 0)
+ fOnlineGainTableID = 6;
+ else if (tableName.CompareTo("Gaintbl_Uniform_FGAN8_2012-01") == 0)
+ fOnlineGainTableID = 7;
+ else if (tableName.CompareTo("Krypton_2012-01") == 0)
+ fOnlineGainTableID = 8;
+ else
+ AliFatal(Form("unknown gaintable <%s> requested", tableName.Data()));
+
+ AliInfo(Form("looking for gaintable: %s (id %i)",
+ tableName.Data(), fOnlineGainTableID));
+
+ if (fOnlineGainTableID > 0)
+ return fOnlineGainTableID;
+ }
+ else {
+
+ AliError("NO DCS/DCSv2 OCDB entry found!");
+ return -1;
+
+ }
+
+ return -1;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDcalibDB::GetGlobalConfiguration(TString &config)
+{
+ //
+ // Get Configuration from the DCS
+ //
+
+ const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
+ if(!dcsArr){
+ AliError("No DCS CDB Object available!");
+ config = "";
+ return;
+ }
+
+ Int_t idSOR = 0, idEOR=1; // The index of SOR and EOR
+ Bool_t hasSOR = (dcsArr->At(idSOR));
+ Bool_t hasEOR = (dcsArr->At(idEOR));
+ TString cfgSOR = "", cfgEOR = ""; // The configuration at SOR/EOR
+
+ // The SOR object is mandatory
+ if (!hasSOR) {
+ AliError("NO SOR object found in CDB file!");
+ config = "";
+ return;
+ }
+ if (!hasEOR) AliWarning("NO EOR object found in CDB file!");
+
+ // Check CalDCS version
+ Int_t calver = 0;
+ if (!strcmp(dcsArr->At(idSOR)->ClassName(),"AliTRDCalDCS")) calver = 1;
+ else if (!strcmp(dcsArr->At(idSOR)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
+
+ // Get the configuration strings
+ if (calver == 1) {
+ // DCS object
+ const AliTRDCalDCS *calSOR = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(idSOR));
+ cfgSOR = calSOR->GetGlobalConfigName();
+ if (hasEOR) {
+ const AliTRDCalDCS *calEOR = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(idEOR));
+ cfgEOR = calEOR->GetGlobalConfigName();
+ }
+ }
+ else if (calver == 2) {
+ // DCSv2 object
+ const AliTRDCalDCSv2 *calv2SOR = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(idSOR));
+ cfgSOR = calv2SOR->GetGlobalConfigName();
+ if (hasEOR) {
+ const AliTRDCalDCSv2 *calv2EOR = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(idEOR));
+ cfgEOR = calv2EOR->GetGlobalConfigName();
+ }
+ }
+ else {
+ AliError("NO DCS/DCSv2 OCDB entry found!");
+ config = "";
+ return;
+ }
+
+ // If there is no EOR entry, return the SOR value
+ if (!hasEOR || cfgEOR.Length()==0) {
+ config = cfgSOR;
+ return;
+ }
+
+ // Check if the configuration is the same for both
+ if (cfgSOR.EqualTo(cfgEOR)) {
+ config = cfgSOR;
+ return;
+ }
+
+ // When both SOR and EOR have an entry but are different, the config is not defined
+ AliError("Inconsistent configuration at start and end of run found!");
+ config = "";
+ return;
+
+}
+//_____________________________________________________________________________
+void AliTRDcalibDB::GetGlobalConfigurationByChamber(TString &config,Int_t par, Int_t opt)
+{
+ //
+ // Get Configuration from the DCS
+ //
+
+ // par is the enumeration from kFltrSet = 1 ... kAddOpti 6
+ // example:
+ // cf_p_nozs_tb30_csmtrk_ptrg
+ // par 1 2 3 4 5
+ // kFltrSet kTimebin kTrigSet
+ // kReadout kTrkMode
+ // opt is 0 for the value itself and 1,2,3 for the first, second option
+ // opt has standard value of 0 in header file
+
+
+ // The point is that we extract the parameter/option for
+ // each of the 540 chambers and compare only this
+ // parameter/option for a global value, not the full
+ // configuration name with all parameters
+
+ // Get the array with SOR and EOR
+ const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
+ if(!dcsArr){
+ AliError("No DCS CDB Object available!");
+ config = "";
+ return;
+ }
+
+ // Check CalDCS version
+ Int_t calver = 0;
+ if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS")) calver = 1;
+ else if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
+ //
+ // Get the configuration strings
+ //
+ // CalDCS version 1
+ if (calver == 1) {
+
+ // Loop over SOR/EOR
+ for(Int_t iSEOR=0;iSEOR<2;iSEOR++){
+
+ // DCS object
+ AliTRDCalDCS *cal = dynamic_cast<AliTRDCalDCS *>(dcsArr->At(iSEOR));
+ if(!cal){
+ // SOR mandantory
+ if(iSEOR==0){
+ AliError("NO SOR object found in CDB file!");
+ config = "";
+ return;
+ }
+ else{
+ AliWarning("NO EOR object found in CDB file!");
+ continue;
+ }
+ } // Check DCS object is there
+
+ // Loop over 540 chambers
+ {
+ Int_t iDet=0;
+ // Find the first chamber in SOR
+ if(iSEOR==0){
+ // Loop until we find the first chamber
+ for(;iDet<kNdet;iDet++){
+ const AliTRDCalDCSFEE *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
+ // Check it's an installed chamber that responded
+ if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
+
+ // We ask for a valid configuration name starting with cf_
+ if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
+ // Set the parameter of the first good ROC as global,
+ // we take only the requested part!
+ GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, config);
+ // Increase counter to not look at this chamber again
+ // and break loop
+ iDet++;
+ break;
+ } // End: valid config name
+ } // End: first good chamber
+ } // End: find the first chamber
+ } // End: is SOR
+
+ // Now compare the other chambers with the first one
+ for(;iDet<kNdet;iDet++){
+ const AliTRDCalDCSFEE *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
+ // Check it's an installed chamber that responded
+ if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
+
+ // We ask for a valid configuration name starting with cf_
+ if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
+
+ // Get the parameter/option of this chamber
+ TString tmpcfg;
+ GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, tmpcfg);
+ // Compare with the global value
+ if(config.CompareTo(tmpcfg)){
+ // Two cases mixed or changed during run
+ if(iSEOR==0){
+ AliError("Mixed DCS configuration for different chambers!");
+ config="mixed";
+ return;
+ } else {
+ AliError("Inconsistent configuration at start and end of run found!");
+ config = "";
+ return;
+ }
+ }
+ }// Valid config name
+ }// Good chamber
+ } // Second half loop
+ } // Loop over 540 chambers
+ } // Loop over SOR / EOR
+ } // End calver 1
+ //
+ // CalDCS version 2
+ //
+ else if (calver == 2) {
+
+ // Loop over SOR/EOR
+ for(Int_t iSEOR=0;iSEOR<2;iSEOR++){
+
+ // DCS object
+ AliTRDCalDCSv2 *cal = dynamic_cast<AliTRDCalDCSv2 *>(dcsArr->At(iSEOR));
+ if(!cal){
+ // SOR mandantory
+ if(iSEOR==0){
+ AliError("NO SOR object found in CDB file!");
+ config = "";
+ return;
+ }
+ else{
+ AliWarning("NO EOR object found in CDB file!");
+ continue;
+ }
+ } // Check DCS object is there
+
+ // Loop over 540 chambers
+ {
+ Int_t iDet=0;
+ // Find the first chamber in SOR
+ if(iSEOR==0){
+ // Loop until we find the first chamber
+ for(;iDet<kNdet;iDet++){
+ const AliTRDCalDCSFEEv2 *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
+ // Check it's an installed chamber that responded
+ if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
+ // Check for a valid config name
+ if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
+
+ // Set the parameter of the first good ROC as global,
+ // we take only the requested part!
+ GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, config);
+ // Increase counter to not look at this chamber again
+ // and break loop
+ iDet++;
+ break;
+ } // End: valid config name
+ } // End: first good chamber
+ } // End: find the first chamber
+ } // End: is SOR
+
+ // Now compare the other chambers with the first one
+ for(;iDet<kNdet;iDet++){
+ const AliTRDCalDCSFEEv2 *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
+ // Check it's an installed chamber that responded
+ if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
+
+ // Check for a valid config name
+ if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
+
+ // Get the parameter/option of this chamber
+ TString tmpcfg;
+ GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, tmpcfg);
+ // Compare with the global value
+ if(config.CompareTo(tmpcfg)){
+ // Two cases mixed or changed during run
+ if(iSEOR==0){
+ AliError("Mixed DCS configuration for different chambers!");
+ config="mixed";
+ return;
+ } else {
+ AliError("Inconsistent configuration at start and end of run found!");
+ config = "";
+ return;
+ }
+ }
+ } // End: valid config name
+ } // End: Good chamber
+ } // End: Second half loop
+ } // Loop over 540 chambers
+ } // Loop over SOR / EOR
+ } // End calver 2
+ else {
+ AliError("NO DCS/DCSv2 OCDB entry found!");
+ config = "";
+ return;
+ }
+
+}
+//_____________________________________________________________________________
+void AliTRDcalibDB::GetGlobalConfigurationVersion(TString &version)
+{
+ //
+ // Get Version of Configuration from the DCS
+ //
+
+ const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
+ if(!dcsArr){
+ version = "";
+ return;
+ }