Re: [ROOT] Storing superposed histos in a root file

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Apr 26 2002 - 16:55:51 MEST


Sebastien,

Why don't you save the pad or canvas in the file?

Assuming a TCanvas *c1, you can do:
TFile *f = new TFile("c1.root","recreate");
c1->Write();
delete f;

In a new session
TFile f("c1.root");
TCanvas *c1 = (TCanvas*)f.Get("c1");
c1->Draw();

Rene Brun


Sebastien Greder wrote:
> 
> Hello Rene,
> 
> In fact it's what I've done through the following method I wrote and which
> is called in my analysis code while a file is opened (i.e is the current
> dir).
> 
> Plot2in1(hJetStudy109,hJetStudy116,1,0,0,"",111111,5,2) ; // in the ana.
> code , and :
> 
> void Plot2in1(TH1* hist1, TH1* hist2,
>               Bool_t logy = 0, Bool_t logx = 0, Bool_t grid = 0,
>               TString fit = "",
>               Int_t stati = 111111,Int_t histo1color =0 ,Int_t histo2color
> = 0)
> {
>   TCanvas *c1 = (TCanvas*)gROOT->FindObject("c1");
>   if (c1) {c1->Clear();}
>   else {c1 = new TCanvas("c1");}
> 
>   sebstyle(); // this method sets my default canvas, pad, texts ,etc .. style
>   TPad* pad1 = new TPad("pad1","pad",0.03,0.03,0.98,0.98);
>   pad1->Draw();
>   if (logy) pad1->SetLogy();
>   if (logx) pad1->SetLogx();
> 
>   pad1->cd();
> //  pad1->SetGrid();
> 
>   if(stati != 111111) gStyle->SetOptStat(stati);
>   if(histo1color !=0 ) hist1->SetFillColor(histo1color);
>   if(histo2color !=0 ) hist2->SetFillColor(histo2color);
>   if (fit != "") {
>   hist1->Fit(fit);
>   hist2->Fit(fit);}
> 
>    TString name1 = hist1->GetName();
>    TString name2 = hist2->GetName();
>    TString newname = name1+"and"+name2;
> 
>   hist1->Draw();
> //  hist1->Write(); // is in fact here not neede here since I also wrote a
>                        quite similar method for one histo
>   hist2->Draw("SAME");
>   hist2->Write(newname);
> 
>   c1->Update();
> }
> 
> 
> Moreover, when I set the log scale (by putting the 3rd argument to 1 when
> calling the method, this is not retained in the root file, I guess it's
> due to the TPad ownership of SetLogy as you already told me and which is
> not retained when the command histo->Write() is called, but this is not
> the important point here)
> 
> I don't do f->Write() since I only want some specified histos to be
> written in the root file.
> 
> Cheers,
> 
> seb.
> 
> (I'm using 3.02.07 on  Linux RedHat)
> 
> On Fri, 26 Apr 2002, Rene Brun wrote:
> 
> > It could be that I misunderstand your question, but why not simply do:
> >
> > TFile *f = new TFile("myfile.root","recreate");
> > ...
> > h1->Draw();
> > h1->Write();
> > h2->Draw("SAME");
> > h2->Write();
> > h3->Draw("SAME");
> > h3->Write();
> > ...
> >
> > doing f->Write();
> > will write all histograms created with f being the current dir to the file.
> > See Users Guide for more details.
> >
> > Rene Brun
> >
> > Sebastien Greder wrote:
> > >
> > > Hello Rooters,
> > >
> > > I just have a short question ; I'm running some code to fill histos and
> > > want them to be then stored in a root file.
> > > To store one histo is obvious but I'd like to superpose several histos and
> > > store the final superposed histo in the root file (it would be more
> > > practical than superposing after having stored all the histos in
> > > the root file).
> > > To sum up, I would like to do the same operation like :
> > >
> > > h1->Draw();
> > > h2->Draw("SAME");
> > > h3->Draw("SAME");
> > > ...
> > >
> > > but while writing the histo in a root file.
> > > I can't find out how.
> > > Could someone give me some advices about this ??
> > >
> > > Thanks in advance,
> > >
> > > seb.
> >



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:51 MET