Re: Putting objects in a root file [newbie]

From: Sue Kasahara <schubert_at_hep.umn.edu>
Date: Tue, 27 Sep 2005 12:43:36 -0500


Hi Roger,
Open the output file before constructing the TNtuple like this:

TFile f("MySpectrum.root","RECREATE");
TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","wn:shift:signal");

The TNtuple automatically attaches itself on construction to the current gDirectory, which is set to the TFile root directory on TFile construction.
Regards,
-Sue

Roger Mason wrote:

>Hello,
>
>I created the following macro:
>
>{
>// Macro to read column data from an ascii file (wavenumber, shift,
>// signal), do some fitting and save to a root file.
>// Modified from tutorials/basic.C in the root distro.
>
> gROOT->Reset();
>#include "Riostream.h"
>
> ifstream in;
>// we assume a file file1.dat in the current directory
>// this file has 3 columns of float data
> in.open("file1.dat");
>
> const Int_t nlines = 1850;
> Float_t wn[nlines],shift[nlines],signal[nlines];
> TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","wn:shift:signal");
>
> for (Int_t i = 0; i < nlines; i++) {
> in >> wn[i] >> shift[i] >> signal[i];
> if (!in.good()) break;
> if (i < 5) printf("x=%8f, y=%8f, z=%8f\n",wn[i],shift[i],signal[i]);
> ntuple->Fill(wn[i],shift[i],signal[i]);
> // Debugging
> // if (i >= 100) break;
> }
> printf(" found %d points\n",i);
> in.close();
>
>// Plot the graph
> cSp = new TCanvas("cSp","Spectrum",200,10,700,500);
> cSp->SetFillColor(42);
> cSp->SetGrid();
>
> TGraph *gr = new TGraph(i,wn,signal);
> gr->SetLineColor(2);
> gr->SetLineWidth(1);
> gr->SetMarkerColor(4);
> gr->SetMarkerSize(0.2);
> gr->SetMarkerStyle(21);
> gr->SetTitle("File1.dat (050617)");
> gr->GetXaxis()->SetTitle("Wavenumber");
> gr->GetYaxis()->SetTitle("Signal");
> gr->Draw("AP");
>
> // TCanvas::Update() draws the frame, after which one can change it
> cSp->Update();
> cSp->GetFrame()->SetFillColor(21);
> cSp->GetFrame()->SetBorderSize(12);
> cSp->Modified();
>
>g1 = new TF1("m1","gaus",13400,18200);
>g2 = new TF1("m2","gaus",18200,18600);
>total = new TF1("mstotal","gaus(0)+gaus(3)",13400,18600);
>Double_t par[9];
>gr->Fit(g1,"R");
>gr->Fit(g2,"R+");
>g1->GetParameters(&par[0]);
>g2->GetParameters(&par[3]);
>total->SetParameters(par);
>gr->Fit(total,"R+");
>
> TFile f("MySpectrum.root","RECREATE");
>
> f->Write();
> f.Close();
>}
>
>When I run it the plots are made and displayed and fit information
>appears on the canvas.
>
>If I then exit root, start a new session:
>
>root [0] TFile f("MySpectrum.root")
>TFile f("MySpectrum.root")
>root [1] .ls
>.ls
>TFile** MySpectrum.root
> TFile* MySpectrum.root
>
>Should not .ls list the objects in the file?
>
>If it matters, I'm using Version 5.02/00.
>
>What am I doing wrong?
>
>Roger Mason
>
>
>
Received on Tue Sep 27 2005 - 19:44:33 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:12 MET