Re: Saving simple things on a file

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Apr 07 1998 - 12:31:37 MEST


Carlos Lacasta wrote:
> 
> Hi,
> 
> How can I write a basic object, like a number, on a file as a key ?
> 


Only objects derived from TObject can be written to a file.
I will take a few examples below:
  1- Suppose you simply want to use an interactive Root session,
you do not want to define a new class and you only have 2 numbers 
(say -89 and 1.2) to write. I would suggest to use a simple histogram
and define xmin and xmax as being your 2 numbers. Later on you can
access these 2 numbers with
   Float_t value1 = simple2.GetXmin();
   Float_t value2 = simple2.GetXmax();

   TH1F simple2("simple2","just 2 numbers xmin and xmax",2,-89,1.2);
   TFile f("simple2.root","recreate");
   simple2.Write();

   In the next session you can access these 2 numbers with
   Float_t value1 = simple2.GetXmin();
   Float_t value2 = simple2.GetXmax();

  2- Suppose you simply want to use an interactive Root session,
you do not want to define a new class and you only have an array
of nvalues to write. I would suggest:

   Int_t nvalues = 5;
   TH1D simple("simple","just a few numbers",nvalues,0,nvalues);
   Double_t values[nvalues] = {1.2,-89,67,122};
   simple.SetContent(values);
   TFile f("simple.root","recreate");
   simple.Write();

   In a next session, you can access these numbers like:
   Double_t val3 = simple.GetBinContent(3);


  3- You have your own compiled class XX derived from TObject.

   XX x;
   x.SetSomeNumbers(..); // here you initialize your object x
   TFile f("simplex.root","recreate");
   x.Write("simplex");

  4- You have your own compiled class XX derived from TNamed.

   XX x("simplex");
   x.SetSomeNumbers(..); // here you initialize your object x
   TFile f("simplex.root","recreate");
   x.Write();

In examples 1,2 and 4, the object written is derived from 
the class TNamed. In this case, the Write function creates
automatically a key using the object name.
In case, your object is not derived from TNamed, but simply
from TObject, you must specify the name of the key as a parameter
in the Write function (case 3).


Rene Brun



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:31 MET