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