Storing the produced distribution in .root format

Here is the part of my macro. This macro produces some distribution providing an input “root” file. When I run the macro it produces me some distributions. I want to store the produced distribution in .root format. Please suggest me.

#include <TString.h>
using namespace std;
TH1F * Subtract(TH1F *data, TH1F *diboson, TH1F *wjets, TH1F *zjets, TH1F *singleTop);

void makeTrigSF_1 ()
   vector<string> trigger_name;
   vector<string> variables;
   //vector<string> variable_xaxis;

TH1F *lep3jet_MC_trig_eff = (TH1F *) ttbar[0][1]->Clone("MC_trig_eff");//MC_trig_eff
      lep3jet_MC_trig_eff->SetLineColor(kGreen);// it was green
      TLegend *mylegend2 = new TLegend(.7,.75,.89,.89);
      mylegend2->AddEntry(lep3jet_MC_trig_eff,"MC_Trigger_Efficiency","l");// for electron
      //mylegend2->AddEntry(lep3jet_MC_trig_eff,"Trigger_Scale_Factor","l");// for muon

      TH1F *SF = (TH1F *) lep3jet_trig_eff->Clone("SF_trig");// it was SF_trig

You need to open a new ROOT file using TFile with the option RECREATE and Write() the distributions you need to save in that ROOT file.

You mean to open a new file TFile in the same macro just before the Writing the distributions?

Yes or open the the original TFile in UPDATE mode to allow writing in it.

This is the usuall message I am getting i.e
Error in TFile::WriteTObject: Directory hist_tot_lep.root is not writable
the hist_tot_lep.root is the input file to produce the distributions.

This is my modification:
TH1F *SF1 = new TH1F(“SF”,“my histogram”,100,-3,3);

So try to save SF1 in a new file as I suggested

As per your kind suggestion I changed SF to SF1->Write(); and got the same message as before.

Hi Couet
The “UPDATE” works for me I want to save the distributions independently. I have to save several distributions and then merge them to make new root file. Please help me in saving independently. Here is my modification:
TH1F *SF1 = new TH1F(“SF”,“my histogram”,100,-3,3);

For that you need to make a new TFile as I told you first.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.