Converting 2D histo to 1D histo inside TDirectories

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


I think you need to open you data file in write mode,
do:

SigFile[i] = new TFile(lineCointainerSig[i].c_str(),"UPDATE");

Lorenzo

I did it actually, by opening the file in write mode as you said and also adding this piece of code:

 TObject *obj;
  TKey *key;
  TIter next(gDirectory->GetListOfKeys());
  while ((key = (TKey *) next()))
    {
      obj = gDirectory->Get(key->GetName());
      key->Delete();
    }

then rename (with the same name they had before), write and reset the histograms. But thanks very much for replying.

Hi Lorenzo,

Sorry to come back to this thread since it’s already resolved but I have an extra question that I think doesn’t need to open another thread.
slicing2D.pdf (22.7 KB)
The 2D figure I attached has many slices along the X-axis and I would like to store the content of each slice in a 1D histogram. Do you have any idea of how to proceed?
The 2D histogram I am referring to is in the same root file I attached before.

Thanks.

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