using namespace std;
#include "hpidhistogramscont.h"
#include "piddef.h"
#include "hparamlist.h"
#include <iostream>
#include "TH1.h"
#include "TClass.h"
#include "TVirtualPad.h"
#include "TROOT.h"
#include "RVersion.h"
ClassImp(HPidHistogramsCont)
#define DEF_PID_ID -1
HPidHistogramsCont::HPidHistogramsCont(void)
: HPidParCont("HPidHistogramsCont",
"HPidHistogramsCont", "HPidHistogramsCont")
{
setDefault();
}
HPidHistogramsCont::HPidHistogramsCont(const Char_t *name, const Char_t *title,
const Char_t *context,
Int_t iHistSet)
: HPidParCont(name, title, context)
{
setDefault();
if(iHistSet < 1)
{
bOk = kFALSE;
Error("HPidHistogramsCont", "Wrong size: %d\n", iHistSet);
return;
}
iHistsPerPart = iHistSet;
resize(0);
}
void HPidHistogramsCont::setDefault(void)
{
strcpy(detName, "Pid");
iNParticles = 0;
iHistsPerPart = 0;
aHistograms.SetOwner();
bWarnings = kTRUE;
bOk = kTRUE;
resize(0);
}
HPidHistogramsCont::~HPidHistogramsCont(void)
{
clear();
resize(0);
}
Bool_t HPidHistogramsCont::resize(Int_t iNPSet)
{
if( ! bOk)
{
Error("resize", "The class is not initialized !!!");
return kFALSE;
}
aParticles.Set(iNPSet);
aHistograms.Expand(iNPSet * iHistsPerPart * 6 );
iNParticles = iNPSet;
return kTRUE;
}
void HPidHistogramsCont::clear(void)
{
aParticles.Reset(DEF_PID_ID);
aHistograms.Clear();
}
void HPidHistogramsCont::detachHistograms(void)
{
Int_t i, c;
TH1 *pH;
c = aHistograms.GetLast() + 1;
for(i = 0; i < c; i++)
{
if((pH = (TH1 *) aHistograms.At(i)) != NULL)
pH->SetDirectory(NULL);
}
}
void HPidHistogramsCont::putParams(HParamList* l)
{
if( ! bOk)
{
Error("putParams", "The class is not initialized !!!");
return;
}
HPidParCont::putParams(l);
l->addObject("hists", this);
}
Bool_t HPidHistogramsCont::getParams(HParamList* l)
{
clear();
resize(0);
bOk = kTRUE;
if(HPidParCont::getParams(l) != kTRUE)
return kFALSE;
if( ! l->fillObject("hists", this))
{
Error("getParams", "Cannot get the class");
return kFALSE;
}
return kTRUE;
}
Int_t HPidHistogramsCont::findParticleIndex(Int_t iPart) const
{
for(Int_t i = 0; i < iNParticles; i++)
{
if(aParticles.At(i) == iPart)
return i;
}
return -1;
}
Bool_t HPidHistogramsCont::addHistogram(Int_t iPartId, Int_t iOffset, Int_t iSector,
const TH1 *pHist)
{
if( ! bOk)
{
Error("addHistogram", "The class is not initialized !!!");
return kFALSE;
}
if(pHist == NULL)
{
removeHistogram(iPartId, iOffset, iSector );
return kTRUE;
}
if(pHist->IsA()->InheritsFrom("TH1") == kFALSE)
{
Error("addHistogram", "%p doesn't inherit from TH1", pHist);
return kFALSE;
}
if((iOffset < 0) || (iOffset >= iHistsPerPart))
{
Error("addHistogram", "iOffset = %d not in the range [0, %d)",
iOffset, iHistsPerPart);
return kFALSE;
}
if((iSector < 0) || (iSector > 5))
{
Error("addHistogram", "iSector = %d not in the range [0, 5]",
iSector, iHistsPerPart);
return kFALSE;
}
Int_t i, c;
if((i = findParticleIndex(iPartId)) < 0)
{
if(resize(iNParticles + 1) == kFALSE)
return kFALSE;
i = iNParticles - 1;
aParticles[i] = iPartId;
c = getIndex(i, iOffset, iSector );
}
else
{
TH1 *pOld;
c = getIndex(i, iOffset, iSector );
if((pOld = (TH1 *)aHistograms.RemoveAt(c)) != NULL)
{
if(bWarnings)
{
Warning("addHistogram", "Removing old histogram for %d %d %d",
iPartId, iOffset, iSector );
}
delete pOld;
}
}
TH1 *pClone;
if((pClone = (TH1 *) pHist->Clone()) == NULL)
{
Error("addHistogram", "Cannot clone the histogram");
return kFALSE;
}
pClone->SetDirectory(NULL);
aHistograms.AddAt(pClone, c);
return kTRUE;
}
void HPidHistogramsCont::removeHistogram(Int_t iPartId, Int_t iOffset, Int_t iSector )
{
if( ! bOk)
{
Error("removeHistogram", "The class is not initialized !!!");
return;
}
if((iOffset < 0) || (iOffset >= iHistsPerPart))
{
Error("removeHistogram", "iOffset = %d not in the range [0, %d)",
iPartId, iHistsPerPart);
return;
}
if((iSector < 0) || (iSector > 5))
{
Error("addHistogram", "iSector = %d not in the range [0, 5]",
iSector, iHistsPerPart);
return;
}
Int_t i, c, k;
TH1 *pOld;
if((i = findParticleIndex(iPartId)) < 0)
return;
if( ( c = getIndex(i, iOffset, iSector) ) > aHistograms.GetLast() )
return;
if((pOld = (TH1 *)aHistograms.RemoveAt(c)) != NULL)
delete pOld;
for(c = 0; c < iHistsPerPart; c++)
{
if(aHistograms[getIndex(i, c, iSector )] != NULL)
return;
}
for(c = i; c < iNParticles - 1; c++)
{
aParticles[c] = aParticles[c + 1];
for(k = 0; k < iHistsPerPart; k++)
aHistograms[getIndex(c, k, iSector)] = aHistograms[getIndex(c + 1, k, iSector)];
}
aParticles[c] = DEF_PID_ID;
for(k = 0; k < iHistsPerPart; k++)
aHistograms.RemoveAt(getIndex(c, k, iSector ));
resize(iNParticles - 1);
}
TH1* HPidHistogramsCont::getHistogram(Int_t iPartId, Int_t iOffset, Int_t iSector) const
{
Int_t i;
if( ! bOk)
{
Error("getHistogram", "The class is not initialized !!!");
return NULL;
}
if((iOffset < 0) || (iOffset >= iHistsPerPart))
{
Error("getHistogram", "iOffset = %d not in the range [0, %d)",
iOffset, iHistsPerPart);
return NULL;
}
if((iSector < 0) || (iSector > 5))
{
Error("addHistogram", "iSector = %d not in the range [0, 5]",
iSector, iHistsPerPart);
return NULL;
}
if((i = findParticleIndex(iPartId)) < 0)
{
if(bWarnings)
Warning("getHistogram", "No particle id: %d (sector %i)\n", iPartId, iSector);
return NULL;
}
if((i = getIndex(i, iOffset, iSector)) > aHistograms.GetLast())
return NULL;
return (TH1 *) aHistograms.At(i);
}
Stat_t HPidHistogramsCont::getValue(Int_t iPartId, Int_t iOffset, Int_t iSector,
Float_t fX, Float_t fY, Float_t fZ) const
{
TH1 *pH;
if((pH = (TH1 *) getHistogram(iPartId, iOffset, iSector )) == NULL)
{
return 0.0f;
}
TAxis *pA;
Int_t iBinX, iBinY, iBinZ;
pA = pH->GetXaxis();
if((iBinX = pA->FindBin(fX)) <= 0)
iBinX = 1;
else
{
if(iBinX > pA->GetNbins())
iBinX = pA->GetNbins();
}
pA = pH->GetYaxis();
if((iBinY = pA->FindBin(fY)) <= 0)
iBinY = 1;
else
{
if(iBinY > pA->GetNbins())
iBinY = pA->GetNbins();
}
pA = pH->GetZaxis();
if((iBinZ = pA->FindBin(fZ)) <= 0)
iBinZ = 1;
else
{
if(iBinZ > pA->GetNbins())
iBinZ = pA->GetNbins();
}
return pH->GetBinContent(pH->GetBin(iBinX, iBinY, iBinZ));
}
void HPidHistogramsCont::print(void)
{
HParSet::print();
if( ! bOk)
{
Error("print", "The class is not initialized !!!");
return;
}
printf("Particles : %3d\nHistPerPart : %3d\n",
iNParticles, iHistsPerPart);
Int_t i, c, iMax, k, sec;
TH1 *pH;
printf("PartId ");
for(c = 0; c < iHistsPerPart; c++)
printf("%15d ", c);
printf("\n\n");
iMax = aHistograms.GetLast();
for(i = 0; i < iNParticles; i++)
{
printf("particle %6d : \n~~~~~~~~~~~~~~~~~\n", aParticles[i]);
for( sec = 0; sec < 6; sec ++ )
{
for(c = 0; c < iHistsPerPart; c++)
{
k = getIndex(i, c, sec);
printf("%15s ",
((k > iMax) || (pH = (TH1 *)aHistograms.At(k)) == NULL)
? "-----" : pH->GetName());
}
printf("\n");
}
printf("\n");
}
}
void HPidHistogramsCont::Draw(const Option_t *pO)
{
if( ! gPad)
{
#if ROOT_VERSION_CODE > ROOT_VERSION(4,4,2)
gROOT->MakeDefCanvas();
#else
(gROOT->GetMakeDefCanvas())();
#endif
}
TVirtualPad *pPad = gPad;
gPad->Clear();
gPad->Divide(iHistsPerPart, iNParticles, 0, 0, 10);
Int_t iR, iC, iI, iS;
TH1 *h;
Char_t s[100];
for(iS = 0; iS < 6; iS++)
{
for(iR = 0; iR < iNParticles; iR++)
{
for(iC = 0; iC < iHistsPerPart; iC++)
{
pPad->cd(1 + iR * iHistsPerPart + iC);
if((iI = getIndex(iR, iC, iS)) > aHistograms.GetLast())
continue;
if((h = (TH1 *) aHistograms.At(iI)) != NULL)
{
sprintf(s, "%s ", (h->GetDimension() == 2) ? "colz" : "");
if(pO != NULL)
sprintf(s + strlen(s), "%s", pO);
h->Draw(s);
}
}
}
}
}
Bool_t HPidHistogramsCont::checkContext(Int_t nMomAlgInd){
TString sContext=this->getParamContext();
cout << "Checking context: " << sContext.Data() << endl;
cout << "Checking context number: " << nMomAlgInd << endl;
sContext.ToUpper();
if(sContext.Contains("KICK123") && nMomAlgInd==ALG_KICK123){
return kTRUE;
}
else if(sContext.Contains("KICK") && nMomAlgInd==ALG_KICK){
return kTRUE;
}
else if(sContext.Contains("SPLINE") && nMomAlgInd==ALG_SPLINE){
return kTRUE;
}
else if(sContext.Contains("REFT") && nMomAlgInd==ALG_REFT){
return kTRUE;
}
else if(sContext.Contains("RUNGEKUTTA") && nMomAlgInd==ALG_RUNGEKUTTA){
return kTRUE;
}
else {
Error("checkContext","Can not find suitable parameter context for used momentum alg id=%d.\n Current context is:%s",nMomAlgInd,sContext.Data());
return kFALSE;
}
}
void HPidHistogramsCont::setContext(Int_t nMomAlgInd){
cout << "Setting context: " << nMomAlgInd << endl;
TString sContext=this->getParamContext();
if(nMomAlgInd == ALG_KICK123)
{
if(!sContext.Contains("KICK123"))
{
sContext.Append("KICK123");
cout << "Appending : KICK123; Context is now: " <<sContext.Data() << endl;
}
}
else if(nMomAlgInd == ALG_KICK)
{
if(!sContext.Contains("KICK"))
{
sContext.Append("KICK");
cout << "Appending : KICK; Context is now: " <<sContext.Data() << endl;
}
}
else if(nMomAlgInd == ALG_SPLINE)
{
if(!sContext.Contains("SPLINE"))
{
sContext.Append("SPLINE");
cout << "Appending : SPLINE; Context is now: " <<sContext.Data() << endl;
}
}
else if(nMomAlgInd == ALG_REFT)
{
if(!sContext.Contains("REFT"))
{
sContext.Append("REFT");
cout << "Appending : REFT; Context is now: " <<sContext.Data() << endl;
}
}
else if(nMomAlgInd == ALG_RUNGEKUTTA)
{
if(!sContext.Contains("RUNGEKUTTA"))
{
sContext.Append("RUNGEKUTTA");
cout << "Appending : RUNGEKUTTA; Context is now: " <<sContext.Data() << endl;
}
}
else
{
Error("setContext","Can not find suitable parameter context for used momentum alg id=%d.\n Current context is:%s",nMomAlgInd,sContext.Data());
}
this->setParamContext(sContext.Data());
}
void HPidHistogramsCont::Streamer(TBuffer &R__b)
{
TString tmpContext(this->getParamContext());
UInt_t R__s, R__c;
if(R__b.IsReading())
{
Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
if (R__v) { }
HPidParCont::Streamer(R__b);
R__b >> iNParticles;
R__b >> iHistsPerPart;
aParticles.Streamer(R__b);
aHistograms.Streamer(R__b);
R__b >> bWarnings;
R__b.CheckByteCount(R__s, R__c, HPidHistogramsCont::IsA());
detachHistograms();
bOk = kTRUE;
}
else
{
R__c = R__b.WriteVersion(HPidHistogramsCont::IsA(), kTRUE);
HPidParCont::Streamer(R__b);
R__b << iNParticles;
R__b << iHistsPerPart;
aParticles.Streamer(R__b);
aHistograms.Streamer(R__b);
R__b << bWarnings;
R__b.SetByteCount(R__c, kTRUE);
}
this->setParamContext(tmpContext.Data());
}
Last change: Sat May 22 13:07:08 2010
Last generated: 2010-05-22 13:07
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.