BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
comparator.cc
Go to the documentation of this file.
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
22#include "Compare.hh"
23
24#include <fstream>
25#include <iostream>
26#include <string>
27#include <vector>
28
29#include "TFile.h"
30#include "TH1.h"
31#include "TTree.h"
32
33class Result;
34
35void usage();
36bool FileExists(const std::string& fileName);
37
38enum EXIT_CODE {
39 _EXIT_SUCCESS = 0,
40 _EXIT_FAILED = 1,
41 _EXIT_INCORRECT_ARGS = 2,
42 _EXIT_FILE_NOT_FOUND = 3,
43 _EXIT_BAD_FILE = 4,
44 _EXIT_SUCCESS_NONE = 5 // all 0 tests passed i.e. there were no tests
45};
46
47int main(int argc, char* argv[])
48{
49 if (argc != 3)
50 {
51 usage();
52 return EXIT_CODE::_EXIT_INCORRECT_ARGS;
53 }
54
55 std::string fname1 = std::string(argv[1]);
56 std::string fname2 = std::string(argv[2]);
57 // try to open files - check validity
58 if (!FileExists(fname1))
59 {
60 std::cout << "No such file \"" << fname1 << "\"" << std::endl;
61 return EXIT_CODE::_EXIT_BAD_FILE;
62 }
63 TFile* f1 = new TFile(fname1.c_str());
64 if(f1->IsZombie())
65 {
66 std::cout << "error : could not open " << fname1 << std::endl;
67 return EXIT_CODE::_EXIT_FILE_NOT_FOUND;
68 }
69
70 if (!FileExists(fname2))
71 {
72 std::cout << "No such file \"" << fname2 << "\"" << std::endl;
73 return EXIT_CODE::_EXIT_BAD_FILE;
74 }
75 TFile* f2 = new TFile(fname2.c_str());
76 if (f2->IsZombie())
77 {
78 std::cout << "error : could not open " << fname2 << std::endl;
79 return EXIT_CODE::_EXIT_FILE_NOT_FOUND;
80 }
81
82 std::vector<Result*> results = Compare::Files(f1,f2);
83
84 f1->Close();
85 delete f1;
86 f2->Close();
87 delete f2;
88
89 bool allPassed = Compare::Summarise(results);
90 for (auto r : results)
91 {delete r;}
92 if (!allPassed)
93 {
94 std::cout << "TESTS_FAILED" << std::endl; // key to look for
95 return EXIT_CODE::_EXIT_FAILED;
96 }
97 else
98 {
99 if (results.empty())
100 {
101 std::cout << "No tests" << std::endl;
102 return EXIT_CODE::_EXIT_SUCCESS_NONE;
103 }
104 else
105 {return EXIT_CODE::_EXIT_SUCCESS;}
106 }
107}
108
109void usage()
110{
111 std::cout << "Usage: comparator <rootfile1> <rootfile2>" << std::endl;
112 std::cout << "Compares <rootfile2> to <rootfile1> - ie <rootfile1> is the reference." << std::endl;
113}
114
115bool FileExists(const std::string& fileName)
116{
117 std::ifstream infile(fileName.c_str());
118 return infile.good();
119 // note the destructor of ifstream will close the stream
120}
Base class of a result.
Definition Result.hh:36
G4bool FileExists(const G4String &filename)
Checks if filename exists.
std::vector< Result * > Files(TFile *f1, TFile *f2)
Compare two files.
Definition Compare.cc:52
bool Summarise(const std::vector< Result * > &results)
Loop over results and print any failed ones. Returns true if all passed.