#include <TList.h>
#include <TMath.h>
#include <TSortedList.h>
#include "AliLog.h"
#include "AliTRDPIDParams.h"
ClassImp(AliTRDPIDParams)
const Double_t AliTRDPIDParams::kVerySmall = 1e-5;
AliTRDPIDParams::AliTRDPIDParams():
TNamed(),
fEntries(NULL)
{
}
AliTRDPIDParams::AliTRDPIDParams(const char *name) :
TNamed(name, ""),
fEntries(NULL)
{
fEntries = new TList;
}
AliTRDPIDParams::AliTRDPIDParams(const AliTRDPIDParams &ref):
TNamed(ref),
fEntries(NULL)
{
fEntries=(TList*)ref.fEntries->Clone();
}
AliTRDPIDParams::~AliTRDPIDParams(){
delete fEntries;
}
void AliTRDPIDParams::AddCentralityClass(Double_t minCentrality, Double_t maxCentrality){
AliTRDPIDCentrality *checklow = FindCentrality(minCentrality + 0.01),
*checkhigh = FindCentrality(maxCentrality - 0.01);
if(!checklow && ! checkhigh)
fEntries->Add(new AliTRDPIDCentrality(minCentrality, maxCentrality));
}
AliTRDPIDParams::AliTRDPIDCentrality *AliTRDPIDParams::FindCentrality(Double_t val) const {
TIter centralities(fEntries);
AliTRDPIDCentrality *obj(NULL), *tmp(NULL);
while((obj = dynamic_cast<AliTRDPIDCentrality *>(centralities()))){
if(val >= obj->GetMinCentrality() && val <= obj->GetMaxCentrality()){
tmp = obj;
break;
}
}
return tmp;
}
Bool_t AliTRDPIDParams::GetThresholdParameters(Int_t ntracklets, Double_t efficiency, Double_t *params, Double_t centrality) const{
AliTRDPIDCentrality *cent = FindCentrality(centrality);
if(!cent)cent = FindCentrality(-1);
if(!cent){
AliDebug(1, "Centrality class not available");
return kFALSE;
}
cent->GetThresholdParameters(ntracklets, efficiency, params);
return kTRUE;
}
void AliTRDPIDParams::SetThresholdParameters(Int_t ntracklets, Double_t effMin, Double_t effMax, Double_t *params, Double_t centrality){
AliTRDPIDCentrality *cent = FindCentrality(centrality);
if(cent) cent->SetThresholdParameters(ntracklets, effMin, effMax, params);
else AliDebug(1, "Centrality class not available");
}
void AliTRDPIDParams::Print(Option_t *) const {
TIter centIter(fEntries);
AliTRDPIDCentrality *cent;
while((cent = dynamic_cast<AliTRDPIDCentrality *>(centIter()))) cent->Print(NULL);
}
AliTRDPIDParams::AliTRDPIDThresholds::AliTRDPIDThresholds():
TObject(),
fNTracklets(0)
{
memset(fParams, 0, sizeof(Double_t) * 4);
memset(fEfficiency, 0, sizeof(Double_t) * 2);
}
AliTRDPIDParams::AliTRDPIDThresholds::AliTRDPIDThresholds(Int_t nTracklets, Double_t effMin, Double_t effMax, Double_t *params) :
TObject(),
fNTracklets(nTracklets)
{
fEfficiency[0] = effMin;
fEfficiency[1] = effMax;
if(params) memcpy(fParams, params, sizeof(Double_t) * 4);
else memset(fParams, 0, sizeof(Double_t) * 4);
}
AliTRDPIDParams::AliTRDPIDThresholds::AliTRDPIDThresholds(Int_t nTracklets, Double_t eff, Double_t *params) :
TObject(),
fNTracklets(nTracklets)
{
fEfficiency[0] = fEfficiency[1] = eff;
if(params) memcpy(fParams, params, sizeof(Double_t) * 4);
else memset(fParams, 0, sizeof(Double_t) * 4);
}
AliTRDPIDParams::AliTRDPIDThresholds::AliTRDPIDThresholds(const AliTRDPIDThresholds &ref) :
TObject(ref),
fNTracklets(ref.fNTracklets)
{
memcpy(fParams, ref.fParams, sizeof(Double_t) * 4);
memcpy(fEfficiency, ref.fEfficiency, sizeof(Double_t) * 2);
}
AliTRDPIDParams::AliTRDPIDThresholds &AliTRDPIDParams::AliTRDPIDThresholds::operator=(const AliTRDPIDThresholds &ref){
if(&ref == this) return *this;
TObject::operator=(ref);
fNTracklets = ref.fNTracklets;
memcpy(fEfficiency, ref.fEfficiency, sizeof(Double_t) * 2);
memcpy(fParams, ref.fParams, sizeof(Double_t) * 4);
return *this;
}
Int_t AliTRDPIDParams::AliTRDPIDThresholds::Compare(const TObject *ref) const{
const AliTRDPIDThresholds *refObj = static_cast<const AliTRDPIDThresholds *>(ref);
if(fNTracklets < refObj->GetNTracklets()) return -1;
else if(fNTracklets > refObj->GetNTracklets()) return 1;
else{
if(fEfficiency[1] < refObj->GetElectronEfficiency(0)) return -1;
else if(fEfficiency[0] > refObj->GetElectronEfficiency(1)) return 1;
else return 0;
}
}
Bool_t AliTRDPIDParams::AliTRDPIDThresholds::IsEqual(const TObject *ref) const {
const AliTRDPIDThresholds *refObj = dynamic_cast<const AliTRDPIDThresholds *>(ref);
if(!refObj) return kFALSE;
Bool_t eqNTracklets = fNTracklets == refObj->GetNTracklets();
Bool_t eqEff = kFALSE;
Bool_t hasRange = TMath::Abs(fEfficiency[1] - fEfficiency[0]) > kVerySmall;
Bool_t hasRangeRef = TMath::Abs(refObj->GetElectronEfficiency(1) - refObj->GetElectronEfficiency(0)) > kVerySmall;
if(hasRange && hasRangeRef){
eqEff = TMath::Abs(fEfficiency[0] - refObj->GetElectronEfficiency(0)) < kVerySmall && TMath::Abs(fEfficiency[1] - refObj->GetElectronEfficiency(1)) < kVerySmall;
} else if(hasRange){
eqEff = refObj->GetElectronEfficiency(0) >= fEfficiency[0] && refObj->GetElectronEfficiency(0) < fEfficiency[1];
} else {
eqEff = fEfficiency[0] >= refObj->GetElectronEfficiency(0) && fEfficiency[0] < refObj->GetElectronEfficiency(1);
}
return eqNTracklets && eqEff;
}
AliTRDPIDParams::AliTRDPIDCentrality::AliTRDPIDCentrality():
fEntries(NULL),
fMinCentrality(-1.),
fMaxCentrality(-1.)
{
}
AliTRDPIDParams::AliTRDPIDCentrality::AliTRDPIDCentrality(Double_t minCentrality, Double_t maxCentrality):
fEntries(NULL),
fMinCentrality(minCentrality),
fMaxCentrality(maxCentrality)
{
fEntries = new TSortedList;
fEntries->SetOwner();
}
AliTRDPIDParams::AliTRDPIDCentrality::AliTRDPIDCentrality(const AliTRDPIDParams::AliTRDPIDCentrality &ref):
TObject(),
fEntries(NULL),
fMinCentrality(ref.fMinCentrality),
fMaxCentrality(ref.fMaxCentrality)
{
fEntries = new TSortedList;
TIter entries(ref.fEntries);
TObject *o;
while((o = entries())) fEntries->Add(o);
}
AliTRDPIDParams::AliTRDPIDCentrality &AliTRDPIDParams::AliTRDPIDCentrality::operator=(const AliTRDPIDCentrality &ref){
if(&ref != this){
if(fEntries) delete fEntries;
fEntries = new TSortedList;
TIter entries(ref.fEntries);
TObject *o;
while((o = entries())) fEntries->Add(o);
fMinCentrality = ref.fMinCentrality;
fMaxCentrality = ref.fMaxCentrality;
}
return *this;
}
AliTRDPIDParams::AliTRDPIDCentrality::~AliTRDPIDCentrality(){
if(fEntries) delete fEntries;
}
Bool_t AliTRDPIDParams::AliTRDPIDCentrality::GetThresholdParameters(Int_t ntracklets, Double_t efficiency, Double_t *params) const{
AliTRDPIDThresholds test(ntracklets, efficiency);
TObject *result = fEntries->FindObject(&test);
if(!result){
AliDebug(1, Form("No threshold params found for %d tracklets and an electron efficiency of %f", ntracklets, efficiency));
return kFALSE;
}
AliTRDPIDThresholds *parResult = static_cast<AliTRDPIDThresholds *>(result);
AliDebug(1, Form("Threshold params found: NTracklets %d, Electron Efficiency %f", parResult->GetNTracklets(), parResult->GetElectronEfficiency()));
memcpy(params, parResult->GetThresholdParams(), sizeof(Double_t) * 4);
return kTRUE;
}
void AliTRDPIDParams::AliTRDPIDCentrality::SetThresholdParameters(Int_t ntracklets, Double_t effMin, Double_t effMax, Double_t *params){
if(effMin > effMax){
AliError("Min. efficiency has to be >= max. efficiency");
return;
}
AliDebug(1, Form("Save Parameters for %d tracklets at and electron efficiency of [%f|%f]", ntracklets, effMin, effMax));
fEntries->Add(new AliTRDPIDThresholds(ntracklets, effMin, effMax, params));
}
void AliTRDPIDParams::AliTRDPIDCentrality::Print(Option_t *) const {
printf("Min. Centrality: %f, Max. Centrality: %f\n", fMinCentrality, fMaxCentrality);
printf("Available thresholds:\n");
printf("_________________________________________\n");
TIter objects(fEntries);
AliTRDPIDThresholds *par;
while((par = dynamic_cast<AliTRDPIDThresholds *>(objects()))){
printf("Number of tracklets %d, Electron efficiency %f\n", par->GetNTracklets(), 0.5*(par->GetElectronEfficiency(0)+par->GetElectronEfficiency(1)));
}
}