Hi Des,
the problem is that you are writing the entire directory multiple times.
Here you find the corrected version of your script (indented as well). I added a line and changed one, you can identify them by the comments I added.
Cheers,
D
//////////takes file with correct folder hierarcy and does rebinning IF WANTED,
///and customizes bin length
//////////User Needs to specify in the beginning 1. varname, 2.number of bins
///in the final histo, 3. REBIN 4. if REBIN, array with binning, FileName
#include "TH1.h"
#include "TH1.h"
#include "TH1F.h"
#include "THStack.h"
#include "TLatex.h"
#include "TLatex.h"
#include "TLine.h"
#include "TMarker.h"
#include "TPave.h"
#include "TROOT.h"
#include "TRint.h"
#include "TStyle.h"
#include <TCanvas.h>
#include <TColor.h>
#include <TFile.h>
#include <TGraph.h>
#include <TH2F.h>
#include <TLegend.h>
#include <TMultiGraph.h>
#include <TROOT.h>
#include <TSystem.h>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "TGraphAsymmErrors.h"
#include "TGraphErrors.h"
#define REBIN 1
using namespace std;
void reform_step2_fix_crs() {
//////CHANGE!!!!!!!!!!!
string varName = "mJJ";
// string varName ="centrality";
const int newhist_size = 7;
string filename_out = "final_F_" + varName + "_18bins_180_v1_noNEG.root";
// Double_t xbins[9]={0,150,300,500,650,800,900,1000,2500};//7bins
// Double_t xbins[8]={0,180,300,500,650,800,900,2500};//7bins
// Double_t xbins[6]={0, 0.3, 0.6, 0.9, 1.2, 1.5};//centrality 10bins
// Double_t xbins[11]={0,40,80,120,160,200,240,280,320,360,400};//centrality
// 10bins
// Double_t xbins[8]={0,150,300,500,650,800,900,2500};//7bins_v1
// Double_t xbins[7]={0,150,300,500,650,800,2500};//6bins
// Double_t xbins[6]={0,150,300,500,800,2500};//5bins
// Double_t xbins[5]={0,150,500,800, 2500};//4bins
// Double_t xbins[2]={0,2500};//1bin
// Double_t xbins[4]={0,150,500,2500};//3bins
//////////////////////new mjj binning
// Double_t xbins[6]={0,180,300,500,800,2500};//mjjBbins (5bins_180_v1)
// Double_t xbins[7]={0,180,300,500,650,800,2500};//mjjBbins (6bins_180_v1)
// Double_t xbins[8]={0,180,300,500,650,800,900,2500};//mjjBbins
// (7bins_180_v1)
// Double_t xbins[9]={0,180,300,500,650,800,900,1000,2500};//mjjBbins
// (8bins_180_v1)
// Double_t xbins[10]={0,180,300,500,650,800,900,1000,1100,2500};//mjjBbins
// (9bins_180_v1)
// Double_t
// xbins[11]={0,180,300,500,650,800,900,1000,1100,1200,2500};//mjjBbins
// (10bins_180_v1)
// Double_t
// xbins[12]={0,180,300,500,650,800,900,1000,1100,1200,1300,2500};//mjjBbins
// (11bins_180_v1)
// Double_t
// xbins[13]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,2500};//mjjBbins
// (12bins_180_v1)
// Double_t
// xbins[14]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,1500,2500};//mjjBbins
// (13bins_180_v1)
// Double_t
// xbins[15]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,1500,1600,2500};//mjjBbins
// (14bins_180_v1)
// Double_t
// xbins[16]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,1500,1600,1700,2500};//mjjBbins
// (15bins_180_v1)
// Double_t
// xbins[17]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,2500};//mjjBbins
// (16bins_180_v1)
// Double_t
// xbins[18]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2500};//mjjBbins
// (17bins_180_v1)
// Double_t
// xbins[19]={0,180,300,500,650,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2500};//mjjBbins
// (18bins_180_v1)
Double_t xbins[8] = {0, 150, 300, 500,
650, 800, 900, 2500}; // mjjBbins (7bins_180_v2)
TFile *top = new TFile((filename_out).data(), "recreate");
// TFile *f_undercheck = TFile::Open("output2/intermediate_F_mJJ_v1.root");
TFile *f_undercheck = TFile::Open("intermediate_F_mJJ_v1.root");
TDirectory *folders1[5];
TDirectory *folders2[10];
TDirectory *folders3[5];
string sys_cases[3];
string srlist[5];
string samplelist[10];
string channellist[5];
/// CHANGE_CR
srlist[0] = "WZjj";
srlist[1] = "WZQCD";
srlist[2] = "ttbar";
srlist[3] = "zz";
srlist[4] = "WZjj150";
TH1F *hnew;
TH1F *pt1;
TH1F * final;
// final= new TH1F("final2","",newhist_size,0,newhist_size);
TH1F *final2;
TH1F *nozero_sr;
TH1F *nozero_qcd;
// TH1F* final;
const int xbins_sr_size = 4;
const int xbins_qcd_size = 6;
// Double_t xbins[8]={0,150,300,500,650,800,900,2500};
Double_t xbins_sr[5] = {500, 650, 800, 900, 2500};
Double_t xbins_qcd[7] = {150, 300, 500, 650, 800, 900, 2500};
sys_cases[0] = "nom";
sys_cases[1] = "sys_up";
sys_cases[2] = "sys_down";
/// CHANGE_SAMPLELIST
samplelist[0] = "WZjj";
samplelist[1] = "WZEW";
samplelist[2] = "ttbar";
samplelist[3] = "ttbarV";
samplelist[4] = "Zgamma";
samplelist[5] = "tZ";
samplelist[6] = "Zll";
samplelist[7] = "VVV";
samplelist[8] = "ZZ";
samplelist[9] = "data";
channellist[0] = "EEE";
channellist[1] = "EMM";
channellist[2] = "MEE";
channellist[3] = "MMM";
channellist[4] = "eee";
cout << "CREATED folders" << endl;
/// CHANGE_CR
for (int i = 0; i < 5; i++) {
folders1[i] = top->mkdir((srlist[i]).data());
folders1[i]->cd();
/// CHANGE_SAMPLELIST
for (int j = 0; j < 10; j++) {
folders2[j] = folders1[i]->mkdir((samplelist[j]).data());
folders2[j]->cd();
for (int k = 0; k < 5; k++) {
folders3[k] = folders2[j]->mkdir((channellist[k]).data());
folders3[k]->cd();
}
}
}
cout << "CREATED folders 2!!" << endl;
// TFile *f_undercheck =
// TFile::Open("output/input_F_mJJB_1lengthbins_right_v7_allchannels_eee_test2.root");
for (int sample_iter = 0; sample_iter < 1; sample_iter++) {
for (unsigned int cregion_iter = 0; cregion_iter < 1; cregion_iter++) {
for (int channel_iter = 0; channel_iter < 5; channel_iter++) {
// int channel_iter=0;int cregion_iter=4;int sample_iter =0;
string cr_test = srlist[cregion_iter] + "_" + channellist[channel_iter];
string pathname = srlist[cregion_iter] + "/" + samplelist[sample_iter] +
"/" + channellist[channel_iter];
string filename = varName + "_" + srlist[cregion_iter] + "_" +
samplelist[sample_iter] + "_" +
channellist[channel_iter] + "_" + sys_cases[0];
string histo_name2 = pathname + "/" + filename;
cout << "histoname " << histo_name2 << endl;
pt1 = (TH1F *)f_undercheck->Get(histo_name2.c_str());
if (REBIN) {
// cout<<"rebin1 "<<histo_name2<<endl;
final2 = (TH1F *)pt1->Rebin(newhist_size, "final2", xbins);
// cout<<"REBIN!"<<endl;
} else {
final2 = (TH1F *)pt1->Clone("final2");
}
// hnew=(TH1*)final2->Clone("hnew");
/// fix CRs
if (cregion_iter == 0) {
// cout<<"cr1 "<<histo_name2<<endl;
nozero_sr =
(TH1F *)final2->Rebin(xbins_sr_size, "nozero_sr", xbins_sr);
hnew = (TH1F *)nozero_sr->Clone("hnew");
} else if (cregion_iter == 1 || cregion_iter == 4) {
nozero_qcd =
(TH1F *)final2->Rebin(xbins_qcd_size, "nozero_qcd", xbins_qcd);
hnew = (TH1F *)nozero_qcd->Clone("hnew");
} else {
hnew = (TH1F *)final2->Clone("hnew");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////se length=1
///bins/////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
if (cregion_iter == 0) {
final = new TH1F("final", "", xbins_sr_size, 0, xbins_sr_size);
} else if (cregion_iter == 1 || cregion_iter == 4) {
final = new TH1F("final", "", xbins_qcd_size, 0, xbins_qcd_size);
} else {
final = new TH1F("final", "", newhist_size, 0, newhist_size);
}
for (int k = 1; k <= final->GetNbinsX(); k++) {
if (hnew->GetBinContent(k) < 0) {
cout << "NEGATIVE BIN!!!" << endl;
cout << histo_name2 << endl;
final->SetBinContent(k, 0);
final->SetBinError(k, hnew->GetBinError(k));
} else {
final->SetBinContent(k, hnew->GetBinContent(k));
final->SetBinError(k, hnew->GetBinError(k));
}
}
cout << "paei na grapsei to " << filename << "sto " << pathname << endl;
auto dir = top->GetDirectory((pathname.data()));
dir->cd(); // New Line!
final->SetName(filename.data());
final->SetDirectory(dir);
final->Write(); // Changed Line! Was: top->Write()
// cout<<"prin to delete "<<histo_name2<<endl;
// delete final2;
// delete final;
// delete pt1;
}
}
}
/* TCanvas *c1= new TCanvas();
pt1->Draw();
c1->Update();
TCanvas *c2= new TCanvas();
final2->Draw();
c2->Update();
TCanvas *c3= new TCanvas();
hnew->Draw();
c2->Update();
*/
cout << "File was written and saved as: " << filename_out << endl;
} // end of new2()