#include #include #include #include #include "TCanvas.h" #include "TSystem.h" #include "TH1F.h" #include "TTree.h" #include "TString.h" using namespace std; void makeHists(int flag = 0x3, const char *outname = "chi2.root") { TH1::AddDirectory(false); vector< TH1F* > hvec; vector< string > varVec, histNameVec, histRangeVec, cutVec; map < string, void* > histMap; TH1F *hptr; // your tree name const char *treename = "candFCNC"; const int kNumFiles = 2; // the prefixes the histograms will have string prefix[kNumFiles] = {"fcnc", "znjet"}; // The three filenames I can find the trees string filenames[kNumFiles] = { "fcnc.list", "zjets.list" }; bool saveHistFile = false; bool writeEpsFiles = true; bool unitArea = true; string epsPrefix = "images/"; /////////////////////////////////////////////////////////////////// // // // // Note: In order for this to work, anytime you push something on // to one of the vectores (varVec, histNameVec, histRangeVec, and // cutVec), you need to push something on to all of the vectors. // old chi2 10 varVec.push_back ("sqrt(m_permTcaPtr.m_chi2)"); histNameVec.push_back ("old_10_chi2"); histRangeVec.push_back ("(100, 0, 10)"); cutVec.push_back ("10==m_permTcaPtr.m_type"); // old chi2 21 varVec.push_back ("sqrt(m_permTcaPtr.m_chi2)"); histNameVec.push_back ("old_21_chi2"); histRangeVec.push_back ("(100, 0, 10)"); cutVec.push_back ("21==m_permTcaPtr.m_type"); // // // /////////////////////////////////////////////////////////////////// int size = (int) varVec.size(); for (int outer = 0; outer < kNumFiles; ++outer) { cout << endl << "Making " << prefix[outer] << " histograms now." << endl; TChain *candFCNC = CLPFunctions::chainFromList (filenames[outer], treename); for (int inner = 0; inner < size; ++inner) { string histname = prefix[outer] + "_" + histNameVec[inner]; string drawstring = varVec[inner] + ">>" + histname + histRangeVec[inner]; // now do cuts string cuts = cutVec[inner]; cout << " draw: " << drawstring << endl << " cuts: " << cuts << endl; candFCNC->Draw(drawstring.c_str(), cuts.c_str()); TH1F *origPtr = (TH1F*) gROOT->FindObject(histname.c_str()); string clonename = histname + "C"; TH1F *clonePtr = (TH1F*) origPtr->Clone(clonename.c_str()); hvec.push_back( clonePtr ); histMap [clonename] = (void*) clonePtr; } // for inner } // for outer if (flag & 0x1) { cout << "Writing to file " << outname << endl; TFile output (outname, "recreate"); size = (int) hvec.size(); for (int loop = 0; loop < size; ++loop) { hptr = hvec[loop]; hptr->Write(); } // for loop output.Close(); }; if (flag & 0x2) { gROOT->SetStyle("Plain"); const int kNumColors = 3; const int kColors[kNumColors] = {1, 4, 2}; const int kNumStyles = 4; const int kStyles[kNumStyles] = {1, 2, 3, 4}; for (int hIndex = 0; hIndex < size; ++hIndex) { // get max size first float max = 0; for (int fIndex = 0; fIndex < kNumFiles; ++fIndex) { string histname = prefix[fIndex] + "_"+ histNameVec[hIndex] + "C"; //TH1F *hPtr = (TH1F*) gROOT->FindObject (histname.c_str()); //cout << "Trying to get " << histname.c_str() << endl; TH1F *hPtr = (TH1F*) histMap[histname]; if (unitArea) { // get the integral WITH overflow int numBins = hPtr->GetNbinsX(); double integral = hPtr->Integral(0, numBins + 1); if (integral > 0) { hPtr->Scale (1. / integral); } } float localmax = hPtr->GetMaximum(); if (localmax > max) { max = localmax; } // localmax } // for fIndex // Draw everything TCanvas *c1 = new TCanvas(); TLegend legend (0.7, 0.6, 0.9, 0.8); // x1, y1, x2, y2 legend.SetFillStyle(4000); // transparent for (int fIndex = 0; fIndex < kNumFiles; ++fIndex) { string histname = prefix[fIndex] +"_" + histNameVec[hIndex] + "C"; TH1F *hPtr = (TH1F*) histMap[histname]; legend.AddEntry (hPtr, prefix[fIndex].c_str(), "l"); hPtr->SetLineColor (kColors [fIndex % kNumColors] ); hPtr->SetLineStyle (kStyles [fIndex % kNumStyles] ); if (0 == fIndex) { hPtr->SetMaximum (1.1 * max); hPtr->Draw(); } else { hPtr->Draw("same"); } } // for fIndex legend.Draw("same"); // save eps file string epsname = epsPrefix + histNameVec[hIndex] + ".eps"; c1->Print (epsname.c_str()); delete c1; } // for hIndex } // writeEpsFile } // makeHists()