ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/Divide2hists1D.C
Revision: 1.1
Committed: Sat Sep 6 18:51:44 2008 UTC (16 years, 7 months ago) by anderson
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Divide and plot two histograms

File Contents

# User Rev Content
1 anderson 1.1 /*************************************************
2     Divides 2 histograms either from the same or
3     different files and plots the output.
4    
5     Example commands typed
6     at ROOT prompt:
7     Divide2hists1D("JetEt", "Jet2Et", "aFile.root", 1.0, 100.0)
8     Divide2hists1D("JetEt", "Jet2Et", "firstFile.root", "secondFile.root", 1.0, 100.0)
9    
10     with this format:
11     Divide2hists1D("Hist1Name", "Hist2Name", "File1.root", "File2.root", Hist1Scale, Hist2Scale)
12    
13    
14     Michael B. Anderson
15     May 13, 2006
16     *************************************************/
17    
18    
19     void Divide2hists1D(TString hist1Name,
20     TString hist2Name,
21     TString file1Name,
22     TString file2Name = "",
23     Double_t scale1 = 1.0,
24     Double_t scale2 = 1.0) {
25    
26     int lineWidth = 2;
27     TString outputFolder("Images");
28     Bool_t displayStatsBox = 0; // 0 = false, 1 = true
29     TString imageType = "gif";
30    
31    
32     // Image output info
33     TString outputType("."+imageType); // OR: png, eps, pdf, etc...
34     TString outputName = ""; // set depending on whether file2Name is set - see later
35    
36     // Make the output folder (if it exists, nothing is done)
37     gSystem->MakeDirectory(outputFolder);
38    
39     // Open the 1st file
40     TFile *file1 = new TFile(file1Name);
41    
42     if ( !file1 ) {
43     cout << file1Name << " does not exist." << endl;
44     return;
45     }
46    
47     // Grab a copy of its histogram
48     TH1* hist1Copy = (TH1*)file1->Get(hist1Name);
49    
50     if ( !hist1Copy ) {
51     cout << file1Name << " does not contain " << hist1Name << endl;
52     return;
53     }
54    
55     // Open the 2nd file - if needed
56     TFile *file2;
57     TH1* hist2Copy;
58     if (file2Name == "") {
59    
60     hist2Copy = (TH1*)file1->Get(hist2Name);
61     if ( !hist2Copy ) {
62     cout << file1Name << " does not contain " << hist1Name << endl;
63     return;
64     }
65    
66     outputName = outputFolder+"/"+file1Name+"-"+hist1Name+"-div-"+hist2name+outputType;
67    
68     } else {
69    
70     file2 = new TFile(file2Name);
71    
72     if ( !file2 ) {
73     cout << file2Name << " does not exist." << endl;
74     return;
75     }
76    
77     hist2Copy = (TH1*)file2->Get(hist2Name);
78    
79     if ( !hist2Copy ) {
80     cout << file2Name << " does not contain " << hist2Name << endl;
81     return;
82     }
83    
84     outputName = outputFolder+"/"+hist1Name+"-"+file1Name+"-div-"+file2Name+outputType;
85    
86     } // end of if file2 was chosen
87    
88     TH1* finalHist = hist1Copy->Clone();
89     finalHist->Divide(hist1Copy,hist2Copy,scale1,scale2);
90     finalHist->SetLineWidth(lineWidth);
91     finalHist->SetStats(displayStatsBox);
92     finalHist->Draw();
93    
94     outputName.ReplaceAll(".root",""); // remove ".root" from the output image name
95     c1->Print(outputName); // Save the histogram to a file
96     }