Hi Stephane,
Your problem comes from the following facts:
In the graph class you store the name of the histogram as:
char *Histoname;
....
Graph(char name[20]){Histoname = name;};
Thus after you call:
sprintf(scully,"P%i",layer);
h2[0]= new Graph(scully);
sprintf(scully,"Z%i",layer);
h2[1] = new Graph(scully);
the following is true:
h1[0]->Histoname == scully;
h2[0]->Histoname == scully;
scully points to the string "Z0".
So whether you run h1[0]->Draw() OR h2[0]->Draw(), the histogram
retrieves from the file is "Z0".
In your second example, you use 2 different strings so the problem
does not appear.
What you need to do is to "copy" the string when construcing the
Graph objects.
To do so just replace the char* by a TString:
class Graph
{
public:
TString Histoname; // Only this line has been modified.
Graph(){};
~Graph(){};
Graph(char name[20]){Histoname = name;};
void Draw();
};
void Graph::Draw()
{
TFile *f = new TFile("SiliMon85983aa.root");
TH1F *plot = new TH1F();
plot = (TH1F*)f->Get(Histoname);
plot->Draw();
}
Philippe.
-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Stephane Tourneur
Sent: Thursday, March 22, 2001 9:34 PM
To: roottalk@pcroot.cern.ch
Subject: [ROOT] XFiles in ROOT?
Dear Rooters,
I have a problem with my root program that I don't manage to solve.
To present it to you, I simplified my program in that way :
-I have a file minigraph.C which builds a class named Graph :
#include <iostream.h>
#include <TFile.h>
#include <TH1.h>
#include <TCanvas.h>
#include <TGClient.h>
#include <TGWindow.h>
#include <TObject.h>
class Graph
{
public:
char* Histoname;
Graph(){};
~Graph(){};
Graph(char name[20]){Histoname = name;};
void Draw();
};
void Graph::Draw()
{
TFile *f = new TFile("SiliMon85983aa.root");
TH1F *plot = new TH1F();
plot = (TH1F*)f->Get(Histoname);
plot->Draw();
}
-I have then a macro easybookgraphSOS.C :
{
gInterpreter->ProcessLine(".L minigraph.C+");
Graph *h2[2];
char scully[30];
int layer = 0;
sprintf(scully,"P%i",layer);
h2[0]= new Graph(scully);
sprintf(scully,"Z%i",layer);
h2[1] = new Graph(scully);
}
Now, on the ROOT prompt, when I execute :
root [0] .x easybookgraphSOS.C
root [1] h2[0]->Draw();
Instead of P0, the histo Z0 is drawn!
BUT, strangely, if I use two different names of variables (instead of
scully alone), that is to say if I replace easybookgraphSOS.C by :
{
gInterpreter->ProcessLine(".L minigraph.C+");
Graph *h2[2];
char scully[30];
char mulder[30];
int layer = 0;
sprintf(scully,"P%i",layer);
h2[0]= new Graph(scully);
sprintf(mulder,"Z%i",layer);
h2[1] = new Graph(mulder);
}
this time it works : h2[0]->Draw(); draws L0, and
h2[1]->Draw(); draws Z0, as expected... What definitely proves that
letting scully without mulder is a fundamental mistake!! (sorry for those
who don't know anything about those famous TV heroes...)
I must use one variable only because I have to book thousands of
histograms in a similar way.
Could anybody help me?
Thanks a lot in advance.
Stephane
student at CDF/Fermilab for the UC Davis
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:40 MET