Hi guys,
I run into this problem while trying to make a generic macro to help me do this:
loop over variables
in this loop do:
get wbb histo
get top histo
get wh histo
draw histograms
save plot
end loop
I got this error message:
[quote]File not opened for /home/minh/Mine/ROOT/higgs (histo was mjj
File not opened for /home/minh/Mine/ROOT/higgs (histo was mjj
File not opened for /home/minh/Mine/ROOT/higgs (histo was mjj
Error in TFile::cd: Unknown directory home
File Name: WH120.root
While getting histo, could not change to /home/minh/Mine/ROOT/higgs[/quote]
while trying to compile and run this myplot.C macro by "root -l myplot.C+"
I stored the macro file and 3 root files in a same folder, which is “higgs”.
I guess I’m wrong somewhere in the GetHisto or GetHistogram function. I tried to debug but not very successful. Actually, I’m not sure about those parts (GetHisto and GetHistogram) because I adopt them from what my supervisor often uses.
[code]#define myplot_cxx
#include
#include <TH1.h>
#include <TH2.h>
#include <TString.h>
#include <TStyle.h>
#include <TCanvas.h>
#include
#include <TFile.h>
#include <TLegend.h>
using namespace std;
TString dir = “/home/minh/Mine/ROOT/higgs”;
// get files from samples passed by reference
vector<TFile*> GetFiles(vector& samples) {
vector<TFile*> theFiles;
// push back null pointer (0) for each sample
for(unsigned samp=0;samp<samples.size();samp++) {
theFiles.push_back(0);
}
// now open the files
for (unsigned i=0;i<samples.size();i++) {
theFiles.push_back(TFile::Open(samples[i]+".root"));
}
return theFiles;
}
// get histogram
TH1F* GetHisto(TFile* f,TString dir,TString h_name) {
// generic get histo function
if (!f) {
cerr << "File not opened for " << dir << " (histo was " << h_name << endl;
return 0;
}
TString path = f->GetPath();
path +=dir;
bool success = f->cd(path);
if(!success) {
cerr << "File Name: " << f->GetName() << endl;
cerr << "While getting histo, could not change to " << dir << endl;
exit(-1);
}
TH1F* h;
f->GetObject(h_name,h);
if(!h) {
cerr << “Histogram " << h_name << " from file " << dir << " not loaded” << endl;
exit(-1);
}
return h;
}
vector<TH1F*> GetHistograms(vector<TFile*>& files, TString h_name) {
vector<TH1F*> hists;
// loop over files
for (unsigned k=0;k<files.size();k++) {
TH1F* h = GetHisto(files[k],dir, h_name);
// put histogram in hists vector
hists.push_back(h);
}
return hists;
}
void myplot() {
vector rootfiles;
rootfiles.push_back(“WH120”);
rootfiles.push_back(“Wbb”);
rootfiles.push_back(“top”);
vector<TFile*> files = GetFiles(rootfiles);
vector histname;
histname.push_back(“mjj”);
histname.push_back(“mjjj”);
histname.push_back(“mWb1lep”);
histname.push_back(“mWb2lep”);
histname.push_back(“pt1”);
histname.push_back(“pt2”);
histname.push_back(“pt3”);
histname.push_back(“pTbb”);
histname.push_back(“pTW”);
histname.push_back(“mb1”);
histname.push_back(“mb2”);
histname.push_back(“m3”);
histname.push_back(“eta1”);
histname.push_back(“eta2”);
histname.push_back(“eta3”);
histname.push_back(“phi1”);
histname.push_back(“phi2”);
histname.push_back(“phi3”);
histname.push_back(“weight1”);
histname.push_back(“weight2”);
histname.push_back(“weight3”);
histname.push_back(“jvf1”);
histname.push_back(“jvf2”);
histname.push_back(“jvf3”);
histname.push_back(“flav1”);
histname.push_back(“flav2”);
histname.push_back(“flav3”);
histname.push_back(“etaLep”);
histname.push_back(“phiLep”);
histname.push_back(“ptLep”);
histname.push_back(“MET”);
histname.push_back(“METz1”);
histname.push_back(“METz2”);
histname.push_back(“METphi”);
histname.push_back(“PhiWH”);
histname.push_back(“deltaRbb”);
histname.push_back(“deltaEtabb”);
histname.push_back(“weight”);
for(unsigned histno=0;histno<histname.size();histno++) {
vector<TH1F*> histos = GetHistograms(files,histname[histno]);
// line styles
vector styles;
styles.push_back(1);
styles.push_back(10);
styles.push_back(5);
// line colors
vector colors;
colors.push_back(2);
colors.push_back(1);
colors.push_back(4);
// canvas+legend
TCanvas *can = new TCanvas();
TLegend *legend = new TLegend(0.7,0.7,0.85,0.85);
for(unsigned n=0;n<histos.size();n++) {
histos[n]->SetLineColor(colors[n]);
histos[n]->SetLineStyle(styles[n]);
legend->AddEntry(histos[n],rootfiles[n],"l");
if(n==0) {
(histos[n])->Draw("e");
}
else {
(histos[n])->Draw("esame");
}
}
can->Print(histname[histno]+TString(".png"));
can->Print(histname[histno]+TString(".eps"));
}
}[/code]