Dear root experts,
I would like to convert some 2D histograms to 1D histograms by keeping their names unchanged. The histograms are inside TDirectories which contain sometimes only one histogram named sig or two histograms named sig_up and sig_down.
I attached my little code but the last line of the code gives me an error such as:
Error in TDirectoryFile::WriteTObject: Directory BRscaled_gaussiansignal_2e2muchannel_mZd100_mH400GeV.root is not writable
#include "TROOT.h"
#include "TObject.h"
#include "TFile.h"
#include "TTree.h"
#include "TH2.h"
#include "TMath.h"
#include "TGraph.h"
#include "TColor.h"
#include "TCanvas.h"
#include "TLegend.h"
#include <TStyle.h>
#include <TString.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void convert2Dto1D()
{
ifstream myfileSig ("inputSig.txt");
string line;
vector<string> lineCointainerSig;
const char *Syst[] =
{
"Nominal",
"STAT",
"LUMI",
"QCDSCALE",
"PDF",
"EL_EFF_ID_TOTAL",
"EL_EFF_ISO_TOTAL",
"EL_EFF_RECO_TOTAL",
"MUON_EFF_ISO_STAT",
"MUON_EFF_ISO_SYS",
"MUON_EFF_RECO_STAT",
"MUON_EFF_RECO_STAT_LOWPT",
"MUON_EFF_RECO_SYS",
"MUON_EFF_RECO_SYS_LOWPT",
"MUON_EFF_TTVA_STAT",
"MUON_EFF_TTVA_SYS",
"PRW_DATASF",
"EG_RESOLUTION_ALL",
"EG_SCALE_ALL",
"MUONS_ID",
"MUONS_MS1",
"MUONS_SAGITTA_RESBIAS",
"MUONS_SAGITTA_RHO",
"MUONS_SCALE"
};
if (myfileSig.is_open())
{
while (getline (myfileSig,line))
{
lineCointainerSig.push_back(line);
}
myfileSig.close();
}
/*for (int i =0; i < lineCointainerSig.size(); i++)
{
cout << " lines : " << lineCointainerSig[i] << '\n';
}*/
//cout << " size " << lineCointainerSig.size() << '\n';
TFile* SigFile[lineCointainerSig.size()];
TH2D *sig_temp;
TH2D *sig_up_temp;
TH2D *sig_down_temp;
/*TH1D *sig;
TH1D *sig_up;
TH1D *sig_down;*/
int Nbinx;
int Nbiny;
int Ser;
TH1D* tempHist = new TH1D("tempHist","" , 46000, 0, 46000);
for (int i = 0; i < 1; i++)//only one loop for now to simplify.
{
SigFile[i] = new TFile(lineCointainerSig[i].c_str());
for (int j = 0; j < 1; j++) //only one loop for now but it should go through all the TDirectories.
{
SigFile[i]->cd(TString::Format("%s" ,Syst[j]));
//getting the 2D hist inside the directory
sig_temp = (TH2D*)gDirectory->Get("sig");
// getting the number of bins
Nbinx = sig_temp->GetNbinsX();
Nbiny = sig_temp->GetNbinsY();
//conveerting the 2D histo to a 1D one
for(int ix =0; ix<= Nbinx; ix++)
{
for(int iy =0; iy<= Nbiny; iy++)
{
//Ser = ix+(iy*ix);
Ser = ((ix - 1) * Nbiny + iy - 1);
tempHist->Fill(Ser, sig_temp->GetBinContent(ix,iy));
}
}
//tempHist->Draw("hist");
tempHist->SetName("sig");
//gDirectory->pwd();
tempHist->Write("");
}
}
}
I also attached one root file and the .dat file used in the code.BRscaled_gaussiansignal_2e2muchannel_mZd100_mH400GeV.root (457.9 KB)
inputSig.txt (1.6 KB)
Could someone assists please?
Thanks in advance.
ROOT Version: 6.18/04
Built for macosx64 on Sep 11 2019, 15:38:23
From tags/v6-18-04@v6-18-04