#pragma implementation
#include "hpidcandidate.h"
#include "hpidtrackcand.h"
#include "hpidfl.h"
#include "hades.h"
#include "hevent.h"
#include "hlinearcategory.h"
#include <iostream>
#include "TError.h"
#include "TROOT.h"
using namespace std;
ClassImp(HPidCandidate)
HPidCandidate::HPidCandidate(void)
{
}
HPidCandidate::HPidCandidate(Short_t numpart, Int_t numalgs, Int_t candIndex, Int_t momAlgIndex)
{
NUM_PARTICLES=numpart;
NUM_ALGORITHMS=numalgs;
NUM_VALUES=numpart*numalgs;
aAlgorithms.Set(NUM_ALGORITHMS);
aParticles.Set(NUM_PARTICLES);
aValues.Set(NUM_PARTICLES*NUM_ALGORITHMS);
iTrackCandIndex = candIndex;
nMomAlgIndex = momAlgIndex;
flags = 0;
Reset();
}
HPidCandidate::~HPidCandidate(void)
{
}
void HPidCandidate::setFlagBit (Int_t bit)
{
if(bit >= 0 && bit < 32 ) flags |= ( 0x01 << bit );
else {
Error("HPidCandidate::setFlagBit(Int_t)","Bit number out of range : %i!",bit);
}
}
void HPidCandidate::unsetFlagBit (Int_t bit)
{
if(bit >= 0 && bit < 32 ) flags &= ~( 0x01 << bit );
else {
Error("HPidCandidate::unsetFlagBit(Int_t)","Bit number out of range : %i!",bit);
}
}
Bool_t HPidCandidate::isFlagBit (Int_t bit)
{
if (bit >= 0 && bit < 32 ) return (flags >> bit ) & 0x01;
else {
Error("HPidCandidate::isFlagBit(Int_t)","Bit number out of range : %i!",bit);
return kFALSE;
}
}
Bool_t HPidCandidate::isFlagOR(Int_t num,...)
{
va_list ap;
va_start(ap,num);
for(Int_t i=0;i<num;i++){
Int_t bit=va_arg(ap,Int_t);
if(bit < 0 || bit > 31)
{
Error("HPidCandidate::isFlagOR()","Bit number out of range : i=%i ,%i!",i,bit);
va_end(ap);
return kFALSE;
}
if(isFlagBit(bit)) {
va_end(ap);
return kTRUE;
}
}
va_end(ap);
return kFALSE;
}
Bool_t HPidCandidate::isFlagAND(Int_t num, ...)
{
va_list ap;
va_start(ap,num);
for(Int_t i=0;i<num;i++){
Int_t bit=va_arg(ap,Int_t);
if(bit < 0 || bit > 31)
{
Error("HPidCandidate::isFlagAND()","Bit number out of range : i=%i ,%i!",i,bit);
va_end(ap);
return kFALSE;
}
if(!isFlagBit(bit)) {
va_end(ap);
return kFALSE;
}
}
va_end(ap);
return kTRUE;
}
void HPidCandidate::printFlags(TString comment)
{
TString out="";
for(Int_t i=32;i>0;i--){
if(i%4==0) out+=" ";
out+= ( (flags>>(i-1)) & 0x1 );
}
cout<<"bin "<<out.Data()<<" "<<comment.Data()<<endl;
}
void HPidCandidate::Clear(Option_t* opt)
{
aAlgorithms.Set(0);
aParticles.Set(0);
aValues.Set(0);
}
void HPidCandidate::Reset(void)
{
Int_t i, iMax;
if((iMax = getNAlgorithms()) > 0 && aAlgorithms.GetSize()== iMax)
{
for(i = 0; i < iMax; i++)
{
aAlgorithms[i] = algNotSet;
}
}
else Error("HPidCandidate::Reset","There was a mismatch in the number of algorithms and the size of the array");
if(((iMax = getNParticles()) > 0) && (aParticles.GetSize() ==iMax))
{
for(i = 0; i < iMax; i++)
aParticles[i] = -99;
}
else Error("HPidCandidate::Reset","There was a mismatch in the number of particles and the size of the array");
iMax = getNParticles() * getNAlgorithms();
if(iMax == aValues.GetSize())
{ for(i = 0; i < iMax; i++)
{
aValues[i] = nUNKNOWN;
}
}
else Error("HPidCandidate::Reset","There was a mismatch in the number of values and the size of the array");
}
Int_t HPidCandidate::print(void) const
{
UInt_t iAlg;
UInt_t iPart;
Bool_t arraysOK=kTRUE;
printf(" P \\ A |");
if(aAlgorithms.GetSize() != (Int_t)getNAlgorithms())
{
arraysOK=kFALSE;
printf("Mismatch in size of algorithms array");
}
if(aParticles.GetSize() != (Int_t)getNParticles())
{
arraysOK=kFALSE;
printf("Mismatch in size of particles array");
}
if((aValues.GetSize() != (Int_t)getNValues()))
{
arraysOK=kFALSE;
printf("Mismatch in size of values array");
}
if(arraysOK)
{
for(iAlg = 0; iAlg < getNAlgorithms(); iAlg++)
printf(" %6d |", aAlgorithms[iAlg]);
printf("\n-------+");
for(iAlg = 0; iAlg < getNAlgorithms(); iAlg++)
printf("---------+");
printf("\n");
for(iPart = 0; iPart < getNParticles(); iPart++)
{
printf("%6d |", aParticles[iPart]);
for(iAlg = 0; iAlg < getNAlgorithms(); iAlg++)
printf("%8.4f |", aValues[getValuePositionByIndex(iAlg, iPart)]);
printf("\n");
}
}
printf("\n");
return 0;
}
HPidTrackCand* HPidCandidate::getTrackCandidate(HCategory *pCat) const
{
if(iTrackCandIndex < 0)
return NULL;
return HPidFL::getTrackCand(iTrackCandIndex, pCat);
}
void HPidCandidate::setAlgorithmIdByIndex(UInt_t uiPos, Short_t eAlg)
{
if((aAlgorithms.GetSize()<(Int_t)NUM_ALGORITHMS) || (uiPos >=NUM_ALGORITHMS))
{
Error("setAlgorithmIdByIndex", "out of bound: [0, %d)", NUM_ALGORITHMS);
return;
}
aAlgorithms[uiPos] = eAlg;
}
void HPidCandidate::setAlgorithmIds(Short_t aeAlgs[])
{
if(sizeof(aeAlgs)/sizeof(Short_t)!=NUM_ALGORITHMS)
{
::Error("HPidCandidate::setAlgorithms","Number of algorithms not matching selection!");
return;
}
if(aAlgorithms.GetSize() != (Int_t)NUM_ALGORITHMS)
{
Error("setAlgorithms", "Mismatch in size of algorithms array");
return;
}
aAlgorithms.Set(NUM_ALGORITHMS,aeAlgs);
}
Int_t HPidCandidate::getAlgorithmIdByIndex(UInt_t uiPos) const
{
if((aAlgorithms.GetSize()<=(Int_t)uiPos) || (uiPos >= NUM_ALGORITHMS))
{
Error("getAlgorithmIdByIndex", "out of bound: [0, %d)", NUM_ALGORITHMS);
return algNotSet;
}
return aAlgorithms[uiPos];
}
Int_t HPidCandidate::getAlgorithmIndexById(Int_t eAlg) const
{
Int_t i, iMax;
iMax = getNAlgorithms();
for(i = 0; i < iMax; i++)
{
if(aAlgorithms[i] == eAlg)
return i;
}
return -1;
}
void HPidCandidate::setParticleIdByIndex(UInt_t uiPos, Short_t nPart)
{
if((aParticles.GetSize() <= (Int_t)uiPos) || (uiPos >= getNParticles()))
{
Error("setParticleIdByIndex", "out of bound: [0, %d)", getNParticles());
return;
}
aParticles[uiPos] = nPart;
}
void HPidCandidate::setParticleIds(Short_t anPart[], Int_t nrOfParticles)
{
if( nrOfParticles != (Int_t)NUM_PARTICLES )
{
::Error("HPidCandidate::setParticleIds","Number of particles does not match");
return;
}
if(aParticles.GetSize() != nrOfParticles)
{
Error("setParticleIds", "Mismatch in size of particles array");
return;
}
aParticles.Set(NUM_PARTICLES,anPart);
}
Short_t HPidCandidate::getParticleIdByIndex(UInt_t uiPos) const
{
if((aParticles.GetSize()<=(Int_t)uiPos) || (uiPos >= getNParticles()))
{
Error("getParticleIdByIndex", "out of bound: [0, %d)", getNParticles());
return -10;
}
return aParticles[uiPos];
}
Int_t HPidCandidate::getParticleIndexById(Short_t nPartId) const
{
Int_t i, iMax;
iMax = getNParticles();
for(i = 0; i < iMax; i++)
{
if(aParticles[i] == nPartId)
return i;
}
return -1;
}
void HPidCandidate::setValueById(Int_t eAlg, Short_t nPartId,
Float_t fVal)
{
Int_t iPart, iAlg;
if(aValues.GetSize()!=(Int_t)(getNParticles())*(Int_t)(getNAlgorithms()))
{
Error("setValueById", "Mismatch in size of values array");
return;
}
if(((iAlg = getAlgorithmIndexById(eAlg)) < 0)
|| ((iPart = getParticleIndexById(nPartId)) < 0))
{
return;
}
aValues[getValuePositionByIndex(iAlg, iPart)] = fVal;
}
void HPidCandidate::setValueByIndex(UInt_t uiAlgIdx, UInt_t uiPartIdx,
Float_t fVal)
{
Int_t i;
if((i = getValuePositionByIndex(uiAlgIdx, uiPartIdx)) < 0)
Error("setValueByIndex", "Out of bounds");
else
aValues[i] = fVal;
}
Float_t HPidCandidate::getValueById(Int_t eAlg, Short_t nPartId) const
{
#warning This implementation has to be extended to strict checking
Int_t iPart, iAlg;
if(((iAlg = getAlgorithmIndexById(eAlg)) < 0)
|| ((iPart = getParticleIndexById(nPartId)) < 0))
{
return 0.0f;
}
return aValues[getValuePositionByIndex(iAlg, iPart)];
}
Float_t HPidCandidate::getValueByIndex(UInt_t uiAlgIdx, UInt_t uiPartIdx) const
{
#warning This implementation has to be extended to strict checking
Int_t i = getValuePositionByIndex(uiAlgIdx, uiPartIdx);
return (i < 0 ? -1.0f : aValues[i]);
}
HCategory* HPidCandidate::buildPidCandidateCategory()
{
HCategory *pCat;
static const Char_t* sCatName="HPidCandidate";
HEvent *pEvent;
if((gHades == NULL) || ((pEvent = gHades->getCurrentEvent()) == NULL))
{
::Error("buildPidCandidateCategory", "Cannot access current event");
return NULL;
}
if((pCat = pEvent->getCategory(catPidCandidate)) != NULL)
{
((HLinearCategory*)pCat)->setDynamicObjects(kTRUE);
return pCat;
}
if((pCat = new HLinearCategory(sCatName, 1000)) == NULL)
{
::Error("HPidCandidate::buildPidCandidateCategory",
"Cannot create new category");
return NULL;
}
((HLinearCategory*)pCat)->setDynamicObjects(kTRUE);
pEvent->addCategory(catPidCandidate, pCat, "Pid");
return pCat;
}
Int_t HPidCandidate::calcBayesVector(Float_t fOut[],const Int_t aAlgs[], Int_t iAlgs)
{
if(fOut == NULL)
{
Error("calcBayesVector", "fOut == NULL");
return 0;
}
Int_t iRelInt;
if((iRelInt = getAlgorithmIndexById(algRelInt)) < 0)
{
Error("calcBayesVector", "no algRelInt in the container");
return 0;
}
Int_t i;
Int_t iMax = getNParticles();
if((i = calcMergedPDFVector(fOut, aAlgs, iAlgs)) <= 0)
return i;
Float_t fSum = 0.0f;
for(i = 0; i < iMax; i++)
{
if(fOut[i] < 0.0)
continue;
if(getValueByIndex(iRelInt,i) < 0.0)
{
setValueByIndex(iRelInt,i,0.0);
Error("HPidCandidate::calcBayesVector()","Negatiev relint value detected and resetted!");
}
fOut[i] *= getValueByIndex(iRelInt,i) ;
fSum += fOut[i];
}
if((fSum > 0.0f) && (fSum != 1.0f))
{
fSum = 1.0f / fSum;
for(i = 0; i < iMax; i++)
{
if(fOut[i] <= 0.0f)
continue;
fOut[i] *= fSum;
}
}
return iMax;
}
#define FILL_ARRAY() \
if(iAlg0 > 0) aAlg[i++] = iAlg0; \
if(iAlg1 > 0) aAlg[i++] = iAlg1; \
if(iAlg2 > 0) aAlg[i++] = iAlg2; \
if(iAlg3 > 0) aAlg[i++] = iAlg3; \
if(iAlg4 > 0) aAlg[i++] = iAlg4; \
if(iAlg5 > 0) aAlg[i++] = iAlg5; \
if(iAlg6 > 0) aAlg[i++] = iAlg6; \
if(iAlg7 > 0) aAlg[i++] = iAlg7; \
if(iAlg8 > 0) aAlg[i++] = iAlg8; \
if(iAlg9 > 0) aAlg[i++] = iAlg9
Int_t HPidCandidate::calcBayesVectorFromAlgSelection(Float_t fOut[], Int_t iAlg0,
Int_t iAlg1, Int_t iAlg2, Int_t iAlg3,
Int_t iAlg4, Int_t iAlg5, Int_t iAlg6,
Int_t iAlg7, Int_t iAlg8, Int_t iAlg9)
{
Int_t aAlg[10];
Int_t i = 0;
FILL_ARRAY();
return calcBayesVector(fOut, aAlg, i);
}
Float_t HPidCandidate::getBayesValue(Short_t nPID,
const Int_t aAlgs[], Int_t iAlgs)
{
Int_t iIdx;
if((iIdx = getParticleIndexById(nPID)) < 0)
{
Error("getBayesVect", "Particle %d not in the container", nPID);
return -1.0f;
}
Float_t *fArr;
if((fArr = new Float_t[getNParticles()]) == NULL)
{
Error("getBayesVect", "Cannot create temp array");
return -1.0f;
}
Float_t fVal;
if(calcBayesVector(fArr, aAlgs, iAlgs) <= 0)
fVal = -1.0f;
else
fVal = fArr[iIdx];
delete [] fArr;
return fVal;
}
Float_t HPidCandidate::getBayesValueFromAlgSelection(Short_t nPID, Int_t iAlg0,
Int_t iAlg1, Int_t iAlg2, Int_t iAlg3,
Int_t iAlg4, Int_t iAlg5, Int_t iAlg6,
Int_t iAlg7, Int_t iAlg8, Int_t iAlg9)
{
Int_t aAlg[10];
Int_t i = 0;
FILL_ARRAY();
return getBayesValue(nPID, aAlg, i);
}
Int_t HPidCandidate::calcMergedPDFVector(Float_t fOut[],
const Int_t aAlgs[], Int_t iAlgs) const
{
if(fOut == NULL)
{
Error("calcMergedPDFVector", "fOut == NULL");
return 0;
}
UInt_t iMax = getNParticles();
UInt_t i;
Int_t a, c, k;
Float_t pdf_factor=0.0;
for(i = 0; i < iMax; i++)
fOut[i] = -1.0f;
if(iAlgs <= 0)
{
c = 0;
for(k = 0; k < (Int_t)getNAlgorithms(); k++)
{
if(((a = getAlgorithmIdByIndex(k)) <= LAST_COMMON_ALG)
|| (a >= CL_ALOGRITHM_OFFSET))
{
continue;
}
#warning This takes Long_t and can be done much more efficiently
for(i = 0; i < iMax; i++)
{
pdf_factor=getValueByIndex(k,i);
if(pdf_factor >= 0.0f)
{
if(fOut[i] < 0.0f)
fOut[i] = pdf_factor;
else
fOut[i] *= pdf_factor;
}
}
c++;
}
if(c <= 0)
{
Error("calcMergedPDFVector", "No PDF vector found");
goto lab_Error;
}
}
else
{
for(c = 0; c < iAlgs; c++)
{
if((k = getAlgorithmIndexById(aAlgs[c])) < 0)
{
Error("calcMergedPDFVector", "No values for algorithm %d", aAlgs[c]);
goto lab_Error;
}
for(i = 0; i < iMax; i++)
{
pdf_factor=getValueByIndex(k,i);
if(pdf_factor >= 0.0f)
{
if(fOut[i] < 0.0f)
fOut[i] = pdf_factor;
else
fOut[i] *= pdf_factor;
}
}
}
}
return iMax;
lab_Error:
for(i = 0; i < iMax; i++)
fOut[i] = -1.0f;
return 0;
}
Int_t HPidCandidate::calcMergedPDFVectorFromAlgSelection(Float_t fOut[], Int_t iAlg0,
Int_t iAlg1, Int_t iAlg2, Int_t iAlg3,
Int_t iAlg4, Int_t iAlg5, Int_t iAlg6,
Int_t iAlg7, Int_t iAlg8, Int_t iAlg9) const
{
Int_t aAlg[10];
Int_t i = 0;
FILL_ARRAY();
return calcMergedPDFVector(fOut, aAlg, i);
}
Float_t HPidCandidate::getMergedPDFValue(Short_t nPID,
const Int_t aAlgs[], Int_t iAlgs) const
{
Int_t iIdx;
if((iIdx = getParticleIndexById(nPID)) < 0)
{
Error("getMergedPDFValue", "Particle %d not in the container", nPID);
return -1.0f;
}
Float_t *fArr;
if((fArr = new Float_t[getNParticles()]) == NULL)
{
Error("getMergedPDFValue", "Cannot create temp array");
return -1.0f;
}
Float_t fVal;
if(calcMergedPDFVector(fArr, aAlgs, iAlgs) <= 0)
fVal = -1.0f;
else
fVal = fArr[iIdx];
delete [] fArr;
return fVal;
}
Float_t HPidCandidate::getMergedPDFValueFromAlgSelection(Short_t nPID, Int_t iAlg0,
Int_t iAlg1, Int_t iAlg2, Int_t iAlg3,
Int_t iAlg4, Int_t iAlg5, Int_t iAlg6,
Int_t iAlg7, Int_t iAlg8, Int_t iAlg9) const
{
Int_t aAlg[10];
Int_t i = 0;
FILL_ARRAY();
cout << "Calling fobidden function!" <<endl ;
return getMergedPDFValue(nPID, aAlg, i);
}
void HPidCandidate::Streamer(TBuffer &R__b)
{
UInt_t R__s, R__c;
if (R__b.IsReading()) {
Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
TObject::Streamer(R__b);
R__b >> iTrackCandIndex;
R__b >> NUM_ALGORITHMS;
R__b >> NUM_PARTICLES;
R__b >> NUM_VALUES;
aAlgorithms.Streamer(R__b);
aParticles.Streamer(R__b);
aValues.Streamer(R__b);
R__b >> nMomAlgIndex;
if (R__v > 1) {
R__b >> flags;
} else { flags = 0; }
R__b.CheckByteCount(R__s, R__c, HPidCandidate::IsA());
} else {
R__c = R__b.WriteVersion(HPidCandidate::IsA(), kTRUE);
TObject::Streamer(R__b);
R__b << iTrackCandIndex;
R__b << NUM_ALGORITHMS;
R__b << NUM_PARTICLES;
R__b << NUM_VALUES;
aAlgorithms.Streamer(R__b);
aParticles.Streamer(R__b);
aValues.Streamer(R__b);
R__b << nMomAlgIndex;
R__b << flags;
R__b.SetByteCount(R__c, kTRUE);
}
}
Last change: Sat May 22 13:06:59 2010
Last generated: 2010-05-22 13:06
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.