TCanvas, TChains and TClonesArrays

Hi rooters,

I have a problem trying to represent in a TCanva a group of (10, for example) TH created inside a loop that read and select datas from a TChain. I had created a more siple model that I think that does roughly the same, that is:

#include “TApplication.h”
#include “TCanvas.h”
#include “TH1F.h”
#include “THStack.h”
#include “TRandom.h”
#include “TClonesArray.h”

using namespace std;

int main(int argc,char** argv)
{
Int_t n;
cout << " Number of histograms " << endl;
cin >> n;
Int_t dim = (int)sqrt(n);

TApplication *myApp = new TApplication(“myApp”,&argc, argv);

TCanvas *c1 = new TCanvas(“c1”,“the group of hists”,10,10,700,900);
c1->Divide(dim,dim);
TClonesArray histo(“TH1F”,n);

for (int i=0 ; i<n; i++){
TH1F *aver = new TH1F(“aver”,“test hstack”,100,-4,4);
histo[i] = new TH1F(“aver”,“test hstack”,100,-4,4);
aver->FillRandom(“gaus”,20000);
aver->SetFillColor(kRed);
c1->cd(i+1);
aver->DrawClone();
aver->Clear();
}

histo.Delete();
myApp->Run();

return 0;

}

And here is a part of the code where I have a problem:

// main loop !!-----------------------
TCanvas *c2 = new TCanvas(“c2”,“Canva of Nears”,200,10,700,900);
c2->Divide(dim,dim);
TClonesArray histo(“TH1F”,entries);

for (int j=0 ; j < entries ; j++){
TH1F AmNear= new TH1F(“Amplitud near”,“Peaks near”,30,0,30);
TH1F AmFar= new TH1F(“Amplitud far”,“Peaks far”,30,0,30);
histo[j] = new TH1F(“AmNear”,“test”,100,-4,4);
primero=j
muestras;
control=(j+1)
muestras;
for (int ev= primero ; ev< control ; ev++) {
if( ev>0 ){
rawEvent->Clear();
makeFit->Clear();
entryOk = chain.GetEvent(ev);}
if(entryOk < 0) {
cout << “Error in the event = " << ev << endl;
continue;
}
makeFit->fitSpectrum(rawEvent);
//Event ID and TH1F filling
Double_t pico = makeFit->getPeakExtreme();
if (pico < 100 && pico > 0){
AmNear->Fill(makeFit->getAmplitudeExtreme());
cout << " Event " << ev << " Near " << endl;
}
else {
AmFar->Fill(makeFit->getAmplitudeExtreme());
cout << " Event " << ev << " Far” << endl;
}
}
c2->cd(j+1);
// here you have the TH filled, right?
//AmNear->DrawCopy();
AmNear->DrawClone();

 maxnear[j] = analisis(AmNear, mode);
 cout << "Peak of the adjusted function (near):" << maxnear[j] << endl;
 near->SetBinContent(j,maxnear[j]);

 maxfar[j] = analisis(AmFar, mode); 
 cout << "Peak of the adjusted function (far):" << maxfar[j] << endl;
 far->SetBinContent(j,maxfar[j]);
 quot->SetBinContent(j,(maxnear[j]/maxfar[j]));
 
 AmNear->Clear();
 AmFar->Clear();

}// end of main loop-------------------------------

Any idea of why one method of filling (FillRandom) works and the other other one no? (getPeakExtreme and getAmplitudeExtreme are user defined function for a makefit object)

Thanks a lot:

Physlock

TClonesArray histo("TH1F",n);This is unlikely to be the container you want (it is used to optimize away new/delete when continually reusing the same cntainer … i.e. it is designed to be used for object going into a TTree).
Worse: histo[i] = new TH1F("aver","test hstack",100,-4,4);
is not doing what you expect … this is basically a no-op! (i.e histo[i] is still NOT point to the new histo).
In addition you actually do:

TH1F *aver = new TH1F("aver","test hstack",100,-4,4); histo[i] = new TH1F("aver","test hstack",100,-4,4); After this you have 3 different histogram (one pointed to by histo[i], one pointed by aver, and one lost histo). This 3 lines should be replaced by

new (histo[i]) TH1F("aver","test hstack",100,-4,4); TH1F *aver = histo[i];

Cheers,
Philippe