Storing TGraph to a root file

I am trying to write some graphs to a root file after creating them, I use the TGraph->Draw() command to draw them and then store them in a root file with the TFile->Write() command, but this doesn’t work, what I get are the graphs created on canvas and printed to the screen and the root file is always empty, the code I use is below:

void Milagro_MC::GetBinSizeEfficiency(float BinSize) {

nSkip1=0;
nSkip2=0;
nSkip3=0;
nSkip4=0;

Float_t nproton=0.;
Float_t ndata=0.;
Float_t ngamma=0.;
Float_t nprotonCut=0.;
Float_t ndataCut=0.;
Float_t ngammaCut=0.;
Float_t fproton;
Float_t fdata;
Float_t fgamma;
Float_t nboth=0.,nbothCut=0.,fboth;
Float_t ngamma_nFit_20_Bin_1p2 = 6042.69;//number of gamma MC events retained after the cuts nFit>=20 & dAngle<=1.2
Float_t nproton_nFit_20 = 16146.3;//number of proton MC events retained after the cuts nFit>=20
Float_t QFactor=0.;
Int_t nentries = (Int_t) t1->GetEntries();
Float_t Q_FACTOR;

Float_t x[3]; // The array to store the binsize
Float_t FGAMMA[3]; // The array to store the fraction of gammas
Float_t FPROTON[3]; // The array to store the fraction of protons
Float_t QFACTOR[3]; // The array to store the Q-Facotr

int nfit[6] = {0,20,50,80,100,150};

for(Int_t k=0;k<1;k++) {

cout<<"\t# For nFit Cut >= "<<nfit[k]<<endl;
cout<<"\t# BinSize  ngCut   fgamma   npCut   fproton  Q-Factor"<<endl;
cout<<"\t# -------  -----  --------  ------  ------- ----------"<<endl;


for(Int_t j=1;j<=3;j++) {

  binSize = (float)j/10;
  x[j] = binSize;

  Float_t nproton=0.;
  Float_t ngamma=0.;
  Float_t nprotonCut=0.;
  Float_t ngammaCut=0.;
  Float_t fproton;
  Float_t fgamma;
  
  for(Int_t i=0; i<nentries; i++) {
t1->GetEntry(i);
setEWGT();
if(Trigger()) {
  if(primary==1) {
    ngamma += (rwgt*ewgt);
    if(dAngle<=binSize&&nFit>=nfit[k]) {
      ngammaCut += (rwgt*ewgt);
    }
  }
  if(primary==14) {
    nproton += (rwgt*ewgt);
    if(nFit>=nfit[k]) {
      //	  No dAngle cut is applied due to the geometrical effect of the dAngle cut on protons 
      //      which will be taken care of in the Q-factor calculation 
      nprotonCut += (rwgt*ewgt);
    }
  }
}
  }
  if(ngamma>0.)fgamma=ngammaCut/ngamma;
  if(nproton>0.)fproton=nprotonCut/nproton*binSize*binSize/1.2/1.2;
  if(nprotonCut>0.)QFactor=ngammaCut/ngamma_nFit_20_Bin_1p2*sqrt(nproton_nFit_20/nprotonCut)*1.2/binSize;
  if(fproton>0.)Q_FACTOR=fgamma/sqrt(fproton);//just to check the calculation of the Q-Factor

  // Fill the arrays
  FGAMMA[j]  = fgamma;
  FPROTON[j] = fproton;
  QFACTOR[j] = QFactor;
  
  if(k==0){
   	FGAMMA_nFit_0 = new TGraph(4,x,FGAMMA);
FPROTON_nFit_0 = new TGraph(3,x,FPROTON);
QFACTOR_nFit_0 = new TGraph(3,x,QFACTOR);
  }
  cout<<"\t   "<<binSize<<"   "<<ngammaCut<<"   "<<fgamma<<"   "<<nprotonCut<<"   "<<fproton<<"   "<<QFactor<<endl;
}

}
FGAMMA_nFit_0->Draw(“AC”);
FPROTON_nFit_0->Draw(“AC”);
QFACTOR_nFit_0->Draw(“AC”);

Graphs->Write();
Graphs->Close();
}

Hello Abdo,

only histograms and trees a written when calling TFile::Write(). All other objects like TGraphs have to be written explicetely.

TFile *f = new TFile("myfile.root", "recreate");
TGraph *g = new TGraph;
...
g->Write();
delete f;

Cheers,
Oliver

Thanks Oliver, it worked.
The problem now is that the graphs written to the root file don’t have any axes or titles which can be implemented as an option when using the Draw command but not with the Write command, any ideas.

Try saving the full canvas instead of just the TGraph.

Cheers,
Philippe

As suggested above, you can write your graphs one by one to the file
or alternatively add the graphs to the list of objects associated
to the file with
f->GetList()->Add(gr1);
f->GetList()->Add(gr2); //etc
f->Write(); //will write all the objects in the list
In this case, you better give a name to your graphs

When you want to draw the graph in a file
-via TBrowser, double click on the graph. If you use a version >= 4.02
the graph will be drawn by default with option “alp”.
you can change this default option by setting
TGraph::BrowseOption: alp
in the file .rootrc or $ROOTSYS/etc/system.rootrc

-via the command
TGraph gr1 = (TGraph)f->Get(“name of gr1”);
gr1->Draw(“alp”); //or any other option

Rene