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();
}