~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to tests/cpp_unit/Recon/Global/PIDVarFTest.cc

merging in changes in merge branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
2
 *
 
3
 * MAUS is free software: you can redistribute it and/or modify
 
4
 * it under the terms of the GNU General Public License as published by
 
5
 * the Free Software Foundation, either version 3 of the License, or
 
6
 * (at your option) any later version.
 
7
 *
 
8
 * MAUS is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include <cstdlib>
 
19
#include <ctime>
 
20
#include <cmath>
 
21
#include <fstream>
 
22
#include <iostream>
 
23
#include <limits>
 
24
#include <sstream>
 
25
#include <vector>
 
26
#include "float.h"
 
27
#include "TLorentzVector.h"
 
28
#include "TRandom3.h"
 
29
 
 
30
#include "gtest/gtest.h"
 
31
 
 
32
#include "Recon/Global/PIDVarF.hh"
 
33
 
 
34
 
 
35
 
 
36
TEST(PIDVarFTestSetUp, TestSetUpAndTearDown) {
 
37
        MAUS::recon::global::PIDVarF *testPIDVarF = NULL;
 
38
        ASSERT_NO_THROW(testPIDVarF = new MAUS::recon::global::PIDVarF("test",
 
39
                                                                       "test"));
 
40
        std::string testfile = testPIDVarF->Get_filename();
 
41
        std::string testdir = testPIDVarF->Get_directory();
 
42
        ASSERT_NO_THROW(delete testPIDVarF);
 
43
 
 
44
        EXPECT_EQ(gSystem->Unlink(testfile.c_str()), 0);
 
45
        // Unlink dir does not seem to work on some systems
 
46
        // suspect NFS mounts or stale handles
 
47
        // force system removal
 
48
        // std::string rmdirCmd = "rm -fr ";
 
49
        // rmdirCmd += testdir.c_str();
 
50
        // EXPECT_EQ(system(rmdirCmd.c_str()), 0);
 
51
}
 
52
 
 
53
class PIDVarFTest : public ::testing::Test {
 
54
 
 
55
        virtual void SetUp() {
 
56
                MAUS::recon::global::PIDVarF testPIDVarF("test", "test");
 
57
                testfile = testPIDVarF.Get_filename();
 
58
                testdir = testPIDVarF.Get_directory();
 
59
                TRandom3 r;
 
60
                for (int i = 0; i < 1000; i++) {
 
61
                  MAUS::DataStructure::Global::Track* testTrack =
 
62
                    new MAUS::DataStructure::Global::Track();
 
63
                  MAUS::DataStructure::Global::TrackPoint* tpTracker =
 
64
                    new MAUS::DataStructure::Global::TrackPoint();
 
65
                  tpTracker->set_detector(MAUS::DataStructure::Global::kTracker1_1);
 
66
                  MAUS::DataStructure::Global::TrackPoint* tpEMR =
 
67
                    new MAUS::DataStructure::Global::TrackPoint();
 
68
 
 
69
                  double emr_range = r.Gaus(50, 10);
 
70
                  testTrack->set_emr_range_primary(emr_range);
 
71
 
 
72
                  double px = 0.0;
 
73
                  double py = 0.0;
 
74
                  double pz = 200.0;
 
75
                  double E = 226.0;
 
76
                  TLorentzVector tpTrackerMom(px, py, pz, E);
 
77
 
 
78
                  tpTracker->set_momentum(tpTrackerMom);
 
79
                  tpTracker->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
80
                  testTrack->AddTrackPoint(tpTracker);
 
81
                  tpEMR->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
82
                  tpEMR->set_detector(MAUS::DataStructure::Global::kEMR);
 
83
                  testTrack->AddTrackPoint(tpEMR);
 
84
                  testTrack->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
85
                  testTracks.push_back(testTrack);
 
86
                }
 
87
        }
 
88
 
 
89
        virtual void TearDown() {
 
90
                gSystem->Unlink(testfile.c_str());
 
91
                gSystem->Unlink(testdir.c_str());
 
92
        }
 
93
 
 
94
 public:
 
95
        bool fileExists(std::string filename) {
 
96
                TFile f(filename.c_str(), "READ");
 
97
                return !f.IsZombie();
 
98
        }
 
99
 
 
100
        bool IsFiniteNumber(double x) {
 
101
        return (x <= DBL_MAX && x >= -DBL_MAX);
 
102
    }
 
103
 
 
104
        std::string testfile;
 
105
        std::string testdir;
 
106
        TFile *file;
 
107
        TH2F *hist;
 
108
        std::string histname;
 
109
        std::vector<MAUS::DataStructure::Global::Track*> testTracks;
 
110
};
 
111
 
 
112
TEST_F(PIDVarFTest, CreateFileConstructor) {
 
113
 
 
114
        ASSERT_TRUE(fileExists(testfile));
 
115
}
 
116
 
 
117
TEST_F(PIDVarFTest, ReadFileConstructor) {
 
118
 
 
119
        file = new TFile(testfile.c_str(), "READ");
 
120
 
 
121
        ASSERT_TRUE(file);
 
122
 
 
123
        ASSERT_FALSE(file->IsZombie());
 
124
 
 
125
        ASSERT_NO_THROW(MAUS::recon::global::PIDVarF testPIDVarF(file, "test", 50, 350, 0, 1000));
 
126
 
 
127
        ASSERT_ANY_THROW(MAUS::recon::global::PIDVarF testPIDVarF(NULL, "test", 50, 350, 0, 1000));
 
128
 
 
129
        ASSERT_ANY_THROW(MAUS::recon::global::PIDVarF testPIDVarF(file, "sasquatch", 50, 350, 0, 1000));
 
130
}
 
131
 
 
132
/* N.B. if either FillHist or LogL are failing, check the values of min and max
 
133
*        bins in PIDVarF.hh, as if the testTracks fall outside of this range FillHist 
 
134
*  and LogL will fail.
 
135
*/
 
136
 
 
137
TEST_F(PIDVarFTest, FillHist) {
 
138
 
 
139
        MAUS::recon::global::PIDVarF testPIDVarF("test", "test");
 
140
        for (int i = 0; i < 1000; i++) {
 
141
                testPIDVarF.Fill_Hist(testTracks[i]);
 
142
        }
 
143
        testfile = testPIDVarF.Get_filename();
 
144
        testdir = testPIDVarF.Get_directory();
 
145
        // check histogram exists
 
146
        hist = testPIDVarF.Get_hist();
 
147
        ASSERT_TRUE(hist);
 
148
        // check number of entries
 
149
        ASSERT_EQ(1000, hist->GetEntries());
 
150
}
 
151
 
 
152
TEST_F(PIDVarFTest, LogL) {
 
153
        // Constructor called within parentheses so that destructor will be
 
154
        // called when it goes out of scope
 
155
         {
 
156
                MAUS::recon::global::PIDVarF writetestPIDVarF("test", "test");
 
157
                for (int i = 0; i < 1000; i++) {
 
158
                        writetestPIDVarF.Fill_Hist(testTracks[i]);
 
159
                }
 
160
                testfile = writetestPIDVarF.Get_filename();
 
161
                testdir = writetestPIDVarF.Get_directory();
 
162
         }
 
163
 
 
164
        MAUS::DataStructure::Global::Track* checkTrack =
 
165
                        new MAUS::DataStructure::Global::Track();
 
166
        MAUS::DataStructure::Global::TrackPoint* ctpTracker =
 
167
                        new MAUS::DataStructure::Global::TrackPoint();
 
168
        ctpTracker->set_detector(MAUS::DataStructure::Global::kTracker1_1);
 
169
 
 
170
        double px = 0.0;
 
171
        double py = 0.0;
 
172
        double pz = 200.0;
 
173
        double E = 226.0;
 
174
        TLorentzVector ctpTrackerMom(px, py, pz, E);
 
175
 
 
176
        ctpTracker->set_momentum(ctpTrackerMom);
 
177
        MAUS::DataStructure::Global::TrackPoint* ctpEMR =
 
178
                        new MAUS::DataStructure::Global::TrackPoint();
 
179
        ctpEMR->set_detector(MAUS::DataStructure::Global::kCalorimeter);
 
180
 
 
181
        double emr_range = 50;
 
182
        checkTrack->set_emr_range_primary(emr_range);
 
183
        ctpEMR->set_detector(MAUS::DataStructure::Global::kEMR);
 
184
        ctpEMR->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
185
        checkTrack->AddTrackPoint(ctpEMR);
 
186
        ctpTracker->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
187
        checkTrack->AddTrackPoint(ctpTracker);
 
188
        checkTrack->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
189
 
 
190
        file = new TFile(testfile.c_str(), "READ");
 
191
 
 
192
        ASSERT_TRUE(file);
 
193
 
 
194
        ASSERT_FALSE(file->IsZombie());
 
195
 
 
196
        ASSERT_NO_THROW(MAUS::recon::global::PIDVarF readtestPIDVarF(file, "test", 50, 350, 0, 1000));
 
197
 
 
198
        MAUS::recon::global::PIDVarF readtestPIDVarF(file, "test", 50, 350, 0, 1000);
 
199
 
 
200
        hist = readtestPIDVarF.Get_hist();
 
201
        ASSERT_TRUE(hist);
 
202
        // check number of entries in histogram
 
203
        // NB as destructor was called above, the number of entries in the histogram
 
204
        // will be increased by the number of bins (inc over and underflow) as in
 
205
        // PIDVarF the behaviour to add one event across all bins is turned on.
 
206
        ASSERT_EQ((1000 + hist->GetSize()), hist->GetEntries());
 
207
        double prob = readtestPIDVarF.logL(checkTrack);
 
208
 
 
209
        ASSERT_LE(prob, 0);
 
210
        ASSERT_TRUE(IsFiniteNumber(prob));
 
211
}