All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions | Variables
diff_spectra.cc File Reference
#include <cassert>
#include <set>
#include <iostream>
#include "TClass.h"
#include "TFile.h"
#include "TH1.h"
#include "TObject.h"
#include "TObjString.h"
#include "TPad.h"
#include "TTree.h"
#include "TVectorD.h"
#include "TVector3.h"

Go to the source code of this file.

Functions

std::string ConcatPath (const std::string &a, const std::string &b)
 
bool operator== (const TObjString &a, const TObjString &b)
 
bool operator== (const TVectorD &a, const TVectorD &b)
 
bool operator== (const TH1 &a, const TH1 &b)
 
std::ostream & operator<< (std::ostream &os, const TObjString &a)
 
std::ostream & operator<< (std::ostream &os, const TH1 &a)
 
std::ostream & operator<< (std::ostream &os, const TObject &a)
 
void Plots (const TH1 &a, const TH1 &b, const std::string &key)
 
template<class T >
bool Compare (const T &a, const T &b, const std::string &key)
 
bool CompareObjects (const TObject &a, const TObject &b, const std::string &key)
 
void ReportUnique (const std::vector< std::string > &only, TDirectory *dir)
 
bool CheckDirectory (TDirectory *a, TDirectory *b, std::string path="")
 
void usage ()
 
int main (int argc, char **argv)
 

Variables

bool gEnablePdf = true
 
bool gPdfStarted = false
 
int gNChecked = 0
 

Function Documentation

bool CheckDirectory ( TDirectory *  a,
TDirectory *  b,
std::string  path = "" 
)

Definition at line 150 of file diff_spectra.cc.

151 {
152  std::set<std::string> keysA, keysB;
153  TIter nextA(a->GetListOfKeys()), nextB(b->GetListOfKeys());
154  while(TObject* x = nextA()) keysA.insert(x->GetName());
155  while(TObject* x = nextB()) keysB.insert(x->GetName());
156 
157  std::vector<std::string> onlyA, onlyB, bothAB;
158  std::set_difference (keysA.begin(), keysA.end(), keysB.begin(), keysB.end(), std::back_inserter(onlyA));
159  std::set_difference (keysB.begin(), keysB.end(), keysA.begin(), keysA.end(), std::back_inserter(onlyB));
160  std::set_intersection(keysA.begin(), keysA.end(), keysB.begin(), keysB.end(), std::back_inserter(bothAB));
161 
162  ReportUnique(onlyA, a);
163  ReportUnique(onlyB, b);
164 
165  bool ok = onlyA.empty() && onlyB.empty();
166 
167  for(const std::string& key: bothAB){
168  TObject* objA = a->Get(key.c_str());
169  TObject* objB = b->Get(key.c_str());
170 
171  TDirectory* dirA = dynamic_cast<TDirectoryFile*>(objA);
172  TDirectory* dirB = dynamic_cast<TDirectoryFile*>(objB);
173  if(dirA && dirB){
174  if(!CheckDirectory(dirA, dirB, ConcatPath(path, key))) ok = false;
175  }
176  else{
177  if(!CompareObjects(*objA, *objB, ConcatPath(path, key))) ok = false;
178  }
179 
180  delete objA;
181  delete objB;
182  } // end for key
183 
184  return ok;
185 }
process_name opflash particleana ie x
bool CheckDirectory(TDirectory *a, TDirectory *b, std::string path="")
bool CompareObjects(const TObject &a, const TObject &b, const std::string &key)
process_name gaushit a
BEGIN_PROLOG triggeremu_data_config_icarus settings PMTADCthresholds sequence::icarus_stage0_multiTPC_TPC physics sequence::icarus_stage0_EastHits_TPC physics sequence::icarus_stage0_WestHits_TPC physics producers purityana0 caloskimCalorimetryCryoE physics caloskimCalorimetryCryoW physics path
std::string ConcatPath(const std::string &a, const std::string &b)
Definition: diff_spectra.cc:22
void ReportUnique(const std::vector< std::string > &only, TDirectory *dir)
template<class T >
bool Compare ( const T &  a,
const T &  b,
const std::string &  key 
)

Definition at line 93 of file diff_spectra.cc.

94 {
95  if(a == b){
96  std::cout << "\rChecked " << ++gNChecked << " keys" << std::flush;
97  return true;
98  }
99 
100  std::cout << "\n'" << key << "' differs: " << a << " vs " << b << std::endl;
101 
102  if constexpr(std::is_same_v<T, TH1>)
103  if(gEnablePdf)
104  Plots(a, b, key);
105 
106  return false;
107 }
then if[["$THISISATEST"==1]]
Definition: neoSmazza.sh:95
int gNChecked
Definition: diff_spectra.cc:92
process_name gaushit a
bool gEnablePdf
Definition: diff_spectra.cc:16
void Plots(const TH1 &a, const TH1 &b, const std::string &key)
Definition: diff_spectra.cc:68
BEGIN_PROLOG could also be cout
bool CompareObjects ( const TObject &  a,
const TObject &  b,
const std::string &  key 
)

Definition at line 110 of file diff_spectra.cc.

111 {
112  if(a.ClassName() != std::string(b.ClassName())){
113  std::cout << key << " is of unlike type: "
114  << a.ClassName() << " vs "
115  << b.ClassName() << std::endl;
116  return false;
117  }
118 
119  if(a.ClassName() == std::string("TObjString")){
120  return Compare((TObjString&)a, (TObjString&)b, key);
121  }
122 
123  if(a.ClassName() == std::string("TVectorT<double>")){
124  return Compare((TVectorD&)a, (TVectorD&)b, key);
125  }
126 
127  if(a.ClassName() == std::string("TVector3")){
128  return Compare((TVector3&)a, (TVector3&)b, key);
129  }
130 
131  if(a.InheritsFrom(TH1::Class()) && b.InheritsFrom(TH1::Class())){
132  return Compare((TH1&)a, (TH1&)b, key);
133  }
134 
135  std::cout << "Don't know how to compare '" << key << "' of type "
136  << a.ClassName() << std::endl;
137  return true; // give them the benefit of the doubt
138 }
bool Compare(const T &a, const T &b, const std::string &key)
Definition: diff_spectra.cc:93
process_name gaushit a
BEGIN_PROLOG could also be cout
std::string ConcatPath ( const std::string &  a,
const std::string &  b 
)

Definition at line 22 of file diff_spectra.cc.

23 {
24  if(a.empty()) return b;
25  return a+"/"+b;
26 }
process_name gaushit a
int main ( int  argc,
char **  argv 
)

Definition at line 198 of file diff_spectra.cc.

199 {
200  if(argc != 3 && argc != 4) usage();
201  if(argc == 4 && argv[1] != std::string("-np")) usage();
202 
203  if(argc == 4){
204  // Must have been -np
205  gEnablePdf = false;
206  // Shift to maintain filenames as [1] and [2]
207  ++argv;
208  }
209 
210  const std::string fnameA = argv[1];
211  const std::string fnameB = argv[2];
212 
213  std::cout << "Comparing " << fnameA << " and " << fnameB << std::endl;
214 
215  if(CheckDirectory(TFile::Open(fnameA.c_str()),
216  TFile::Open(fnameB.c_str()))){
217  std::cout << "\nFiles match!" << std::endl;
218  return 0;
219  }
220  else{
221  if(gPdfStarted) gPad->Print("diff.pdf]");
222 
223  std::cout << "\nFiles do not match" << std::endl;
224  return 1;
225  }
226 }
bool CheckDirectory(TDirectory *a, TDirectory *b, std::string path="")
usage
Definition: doGit.sh:21
bool gEnablePdf
Definition: diff_spectra.cc:16
bool gPdfStarted
Definition: diff_spectra.cc:19
BEGIN_PROLOG could also be cout
std::ostream& operator<< ( std::ostream &  os,
const TObjString &  a 
)

Definition at line 51 of file diff_spectra.cc.

52 {
53  return os << "\"" << a.GetString() << "\"";
54 }
process_name gaushit a
std::ostream& operator<< ( std::ostream &  os,
const TH1 &  a 
)

Definition at line 56 of file diff_spectra.cc.

57 {
58  return os << "integral " << a.Integral(0, -1);
59 }
process_name gaushit a
std::ostream& operator<< ( std::ostream &  os,
const TObject &  a 
)

Definition at line 61 of file diff_spectra.cc.

62 {
63  a.Print();
64  return os;
65 }
process_name gaushit a
bool operator== ( const TObjString &  a,
const TObjString &  b 
)

Definition at line 29 of file diff_spectra.cc.

30 {
31  return a.GetString() == b.GetString();
32 }
process_name gaushit a
bool operator== ( const TVectorD &  a,
const TVectorD &  b 
)

Definition at line 34 of file diff_spectra.cc.

35 {
36  return (a-b).Norm1() == 0;
37 }
process_name gaushit a
bool operator== ( const TH1 &  a,
const TH1 &  b 
)

Definition at line 39 of file diff_spectra.cc.

40 {
41  if(a.GetNcells() != b.GetNcells()) return false;
42  // TODO - test bin edges
43  for(int i = 0; i <= a.GetNcells(); ++i)
44  if(a.GetBinContent(i) != b.GetBinContent(i))
45  return false;
46 
47  return true;
48 }
process_name gaushit a
void Plots ( const TH1 &  a,
const TH1 &  b,
const std::string &  key 
)

Definition at line 68 of file diff_spectra.cc.

69 {
70  TH1* ac = (TH1*)a.Clone();
71  TH1* bc = (TH1*)b.Clone();
72 
73  ac->SetLineColor(kRed);
74  bc->SetLineColor(kBlue);
75 
76  if(ac->Integral() < bc->Integral()) std::swap(ac, bc);
77 
78  ac->SetTitle(key.c_str());
79 
80  ac->Draw("hist");
81  bc->Draw("hist same");
82 
83  if(!gPdfStarted){
84  gPdfStarted = true;
85  gPad->Print("diff.pdf[");
86  }
87 
88  gPad->Print("diff.pdf");
89 }
process_name gaushit a
bool gPdfStarted
Definition: diff_spectra.cc:19
void ReportUnique ( const std::vector< std::string > &  only,
TDirectory *  dir 
)

Definition at line 141 of file diff_spectra.cc.

142 {
143  if(only.empty()) return;
144 
145  std::cout << "The following keys exist only in " << dir->GetFile()->GetName() << std::endl;
146  for(const std::string& k: only) std::cout << " " << k << std::endl;
147 }
tuple dir
Definition: dropbox.py:28
pdgs k
Definition: selectors.fcl:22
BEGIN_PROLOG could also be cout
void usage ( )

Definition at line 189 of file diff_spectra.cc.

190 {
191  std::cout << "Usage: diff_spectra [-np] input1.root input2.root" << std::endl;
192  std::cout << " -np Disable plotting functionality" << std::endl;
193 
194  exit(2);
195 }
BEGIN_PROLOG could also be cout

Variable Documentation

bool gEnablePdf = true

Definition at line 16 of file diff_spectra.cc.

int gNChecked = 0

Definition at line 92 of file diff_spectra.cc.

bool gPdfStarted = false

Definition at line 19 of file diff_spectra.cc.