Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
compareHistograms.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file compareHistograms.C
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include "TFile.h"
10 #include "TH1F.h"
11 #include "TROOT.h"
12 #include "TTree.h"
13 
14 // This root script draws the histograms with the name "hist1Name" and the name
15 // "hist2Name" (and possibly also a third histogram with name "hist3Name") from
16 // the input file into the same canvas with the colors given
17 
18 void
20  std::string hist1Name,
21  int col1,
22  std::string inFile2,
23  std::string hist2Name,
24  int col2,
25  std::string inFile3 = "",
26  std::string hist3Name = "",
27  int col3 = 0)
28 {
29  std::cout << "Opening file: " << inFile1 << std::endl;
30  TFile inputFile1(inFile1.c_str());
31  std::cout << "Opening file: " << inFile2 << std::endl;
32  TFile inputFile2(inFile2.c_str());
33  std::cout << "Comparing Histograms: " << hist1Name << " & " << hist2Name
34  << std::endl;
35 
36  TH1F* h1 = (TH1F*)inputFile1.Get(hist1Name.c_str());
37  TH1F* h2 = (TH1F*)inputFile2.Get(hist2Name.c_str());
38  std::cout << "col1: " << col1 << ", col2: " << col2 << std::endl;
39 
40  h1->SetMarkerColor(col1);
41  h1->SetLineColor(col1);
42  h1->SetMarkerStyle(3);
43  h1->Draw("");
44  h2->SetMarkerColor(col2);
45  h2->SetLineColor(col2);
46  h2->Draw("same");
47  TLegend* leg = new TLegend(0.72, 0.696, 0.99, 0.936);
48  leg->AddEntry(h1, hist1Name.c_str());
49  leg->AddEntry(h2, hist2Name.c_str());
50 
51  if (!inFile3.empty()) {
52  TFile inputFile3(inFile3.c_str());
53  TH1F* h3 = (TH1F*)inputFile3.Get(hist3Name.c_str());
54  std::cout << " & " << hist3Name << std::endl;
55  std::cout << "from file: " << inFile3 << std::endl;
56  h3->SetMarkerColor(col3);
57  h3->SetLineColor(col3);
58  h3->Draw("same");
59  h3->SetDirectory(0);
60  leg->AddEntry(h3, hist3Name.c_str());
61  inputFile3.Close();
62  }
63 
64  leg->Draw();
65 
66  h1->SetDirectory(0);
67  h2->SetDirectory(0);
68 
69  inputFile1.Close();
70  inputFile2.Close();
71 }