Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:44

0001 #include <iostream>
0002 
0003 #include "TFile.h"
0004 #include "TH1.h"
0005 #include "TKey.h"
0006 #include "TObject.h"
0007 #include "TClass.h"
0008 #include "TCanvas.h"
0009 #include "TString.h"
0010 
0011 void CompareAllHistograms(const TString & fileName1, const TString & fileName2)
0012 {
0013   TFile file1(fileName1);
0014   TFile file2(fileName2);
0015 
0016   TH1 *histo1, *histo2;
0017   TKey *key;
0018   TIter nextkey(file1.GetListOfKeys());
0019 
0020   TFile outputFile("ComparedHistograms.root", "RECREATE");
0021   outputFile.cd();
0022 
0023   while( key = (TKey*)nextkey() ) {
0024     TKey * obj = (TKey*)(key->ReadObj());
0025     if( (obj->IsA()->InheritsFrom("TH1F")) || (obj->IsA()->InheritsFrom("TH1D")) ) {
0026       histo1 = (TH1*)obj; 
0027       histo2 = (TH1*)file2.FindObjectAny(histo1->GetName());
0028       std::cout << histo1->GetName() << std::endl;
0029       TCanvas canvas(TString(histo1->GetName())+"_canvas", histo1->GetName(), 1000, 800);
0030       canvas.cd();
0031       histo1->Draw("E1");
0032       histo2->SetLineColor(kRed);
0033       histo2->Sumw2();
0034       histo2->Scale(histo1->Integral()/histo2->Integral());
0035       // histo2->Scale(histo1->GetEntries()/histo2->GetEntries());
0036       histo2->Draw("E1same");
0037       canvas.Write();
0038       // c0->Modified();
0039       // c0->Update(); 
0040     }
0041   }
0042   outputFile.Write();
0043   outputFile.Close();
0044 }