~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to tests/cpp_unit/Recon/Global/ComPIDVarGTest.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/ComPIDVarG.hh"
 
33
 
 
34
 
 
35
 
 
36
TEST(ComPIDVarGTestSetUp, TestSetUpAndTearDown) {
 
37
        MAUS::recon::global::ComPIDVarG *testComPIDVarG = NULL;
 
38
        ASSERT_NO_THROW(testComPIDVarG = new MAUS::recon::global::ComPIDVarG("test",
 
39
                                                                       "test"));
 
40
        std::string testfile = testComPIDVarG->Get_filename();
 
41
        std::string testdir = testComPIDVarG->Get_directory();
 
42
        ASSERT_NO_THROW(delete testComPIDVarG);
 
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 ComPIDVarGTest : public ::testing::Test {
 
54
 
 
55
        virtual void SetUp() {
 
56
                MAUS::recon::global::ComPIDVarG testComPIDVarG("test", "test");
 
57
                testfile = testComPIDVarG.Get_filename();
 
58
                testdir = testComPIDVarG.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* tp1 =
 
64
                    new MAUS::DataStructure::Global::TrackPoint();
 
65
                  MAUS::DataStructure::Global::TrackPoint* tp2 =
 
66
                    new MAUS::DataStructure::Global::TrackPoint();
 
67
                  tp1->set_detector(MAUS::DataStructure::Global::kTOF1);
 
68
                  tp2->set_detector(MAUS::DataStructure::Global::kTOF2);
 
69
 
 
70
                  MAUS::DataStructure::Global::TrackPoint* tpEMR =
 
71
                    new MAUS::DataStructure::Global::TrackPoint();
 
72
 
 
73
                  double emr_density = r.Gaus(0.5, 0.001);
 
74
                  testTrack->set_emr_plane_density(emr_density);
 
75
 
 
76
                  double x1 = 0.0;
 
77
                  double y1 = 0.0;
 
78
                  double z1 = 0.0;
 
79
                  double t1 = r.Gaus(45, 2);
 
80
                  TLorentzVector pos1(x1, y1, z1, t1);
 
81
 
 
82
                  double x2 = 0.0;
 
83
                  double y2 = 0.0;
 
84
                  double z2 = 0.0;
 
85
                  double t2 = r.Gaus(85, 2);
 
86
                  TLorentzVector pos2(x2, y2, z2, t2);
 
87
 
 
88
                  tp1->set_position(pos1);
 
89
                  tp2->set_position(pos2);
 
90
                  tp1->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
91
                  tp2->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
92
                  testTrack->AddTrackPoint(tp1);
 
93
                  testTrack->AddTrackPoint(tp2);
 
94
                  tpEMR->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
95
                  tpEMR->set_detector(MAUS::DataStructure::Global::kEMR);
 
96
                  testTrack->AddTrackPoint(tpEMR);
 
97
                  testTrack->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
98
                  testTracks.push_back(testTrack);
 
99
                }
 
100
        }
 
101
 
 
102
        virtual void TearDown() {
 
103
                gSystem->Unlink(testfile.c_str());
 
104
                gSystem->Unlink(testdir.c_str());
 
105
        }
 
106
 
 
107
 public:
 
108
        bool fileExists(std::string filename) {
 
109
                TFile f(filename.c_str(), "READ");
 
110
                return !f.IsZombie();
 
111
        }
 
112
 
 
113
        bool IsFiniteNumber(double x) {
 
114
        return (x <= DBL_MAX && x >= -DBL_MAX);
 
115
    }
 
116
 
 
117
        std::string testfile;
 
118
        std::string testdir;
 
119
        TFile *file;
 
120
        TH2F *hist;
 
121
        std::string histname;
 
122
        std::vector<MAUS::DataStructure::Global::Track*> testTracks;
 
123
};
 
124
 
 
125
TEST_F(ComPIDVarGTest, CreateFileConstructor) {
 
126
 
 
127
        ASSERT_TRUE(fileExists(testfile));
 
128
}
 
129
 
 
130
TEST_F(ComPIDVarGTest, ReadFileConstructor) {
 
131
 
 
132
        file = new TFile(testfile.c_str(), "READ");
 
133
 
 
134
        ASSERT_TRUE(file);
 
135
 
 
136
        ASSERT_FALSE(file->IsZombie());
 
137
 
 
138
        ASSERT_NO_THROW(MAUS::recon::global::ComPIDVarG testComPIDVarG(file, "test", 20, 40, 0, 1));
 
139
 
 
140
        ASSERT_ANY_THROW(MAUS::recon::global::ComPIDVarG testComPIDVarG(NULL, "test", 20, 40, 0, 1));
 
141
 
 
142
        ASSERT_ANY_THROW(MAUS::recon::global::ComPIDVarG testComPIDVarG(file, "sasquatch", 20, 40, 0, 1));
 
143
}
 
144
 
 
145
/* N.B. if either FillHist or LogL are failing, check the values of min and max
 
146
*        bin in ComPIDVarG.hh, as if the testTracks fall outside of this range FillHist 
 
147
*  and LogL will fail.
 
148
*/
 
149
 
 
150
TEST_F(ComPIDVarGTest, FillHist) {
 
151
 
 
152
        MAUS::recon::global::ComPIDVarG testComPIDVarG("test", "test");
 
153
        for (int i = 0; i < 1000; i++) {
 
154
                testComPIDVarG.Fill_Hist(testTracks[i]);
 
155
        }
 
156
        testfile = testComPIDVarG.Get_filename();
 
157
        testdir = testComPIDVarG.Get_directory();
 
158
        // check histogram exists
 
159
        hist = testComPIDVarG.Get_hist();
 
160
        ASSERT_TRUE(hist);
 
161
        // check number of entries
 
162
        ASSERT_EQ(1000, hist->GetEntries());
 
163
}
 
164
 
 
165
TEST_F(ComPIDVarGTest, LogL) {
 
166
        // Constructor called within parentheses so that destructor will be
 
167
        // called when it goes out of scope
 
168
         {
 
169
                MAUS::recon::global::ComPIDVarG writetestComPIDVarG("test", "test");
 
170
                for (int i = 0; i < 1000; i++) {
 
171
                        writetestComPIDVarG.Fill_Hist(testTracks[i]);
 
172
                }
 
173
                testfile = writetestComPIDVarG.Get_filename();
 
174
                testdir = writetestComPIDVarG.Get_directory();
 
175
         }
 
176
 
 
177
        MAUS::DataStructure::Global::Track* checkTrack =
 
178
                        new MAUS::DataStructure::Global::Track();
 
179
        MAUS::DataStructure::Global::TrackPoint* ctp1 =
 
180
                        new MAUS::DataStructure::Global::TrackPoint();
 
181
        MAUS::DataStructure::Global::TrackPoint* ctp2 =
 
182
                        new MAUS::DataStructure::Global::TrackPoint();
 
183
        ctp1->set_detector(MAUS::DataStructure::Global::kTOF1);
 
184
        ctp2->set_detector(MAUS::DataStructure::Global::kTOF2);
 
185
 
 
186
        double x1 = 0.0;
 
187
        double y1 = 0.0;
 
188
        double z1 = 0.0;
 
189
        double t1 = 15.0;
 
190
        TLorentzVector cpos1(x1, y1, z1, t1);
 
191
 
 
192
        double x2 = 0.0;
 
193
        double y2 = 0.0;
 
194
        double z2 = 0.0;
 
195
        double t2 = 55.0;
 
196
        TLorentzVector cpos2(x2, y2, z2, t2);
 
197
 
 
198
        MAUS::DataStructure::Global::TrackPoint* ctpEMR =
 
199
                        new MAUS::DataStructure::Global::TrackPoint();
 
200
        ctpEMR->set_detector(MAUS::DataStructure::Global::kCalorimeter);
 
201
 
 
202
        double emr_density = 0.5;
 
203
        checkTrack->set_emr_plane_density(emr_density);
 
204
        ctpEMR->set_detector(MAUS::DataStructure::Global::kEMR);
 
205
        ctpEMR->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
206
        checkTrack->AddTrackPoint(ctpEMR);
 
207
        ctp1->set_position(cpos1);
 
208
        ctp2->set_position(cpos2);
 
209
        ctp1->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
210
        ctp2->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
211
        checkTrack->AddTrackPoint(ctp1);
 
212
        checkTrack->AddTrackPoint(ctp2);
 
213
        checkTrack->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
214
 
 
215
        file = new TFile(testfile.c_str(), "READ");
 
216
 
 
217
        ASSERT_TRUE(file);
 
218
 
 
219
        ASSERT_FALSE(file->IsZombie());
 
220
 
 
221
        ASSERT_NO_THROW(MAUS::recon::global::ComPIDVarG readtestComPIDVarG(file, "test", 30, 50, 0, 1));
 
222
 
 
223
        MAUS::recon::global::ComPIDVarG readtestComPIDVarG(file, "test", 30, 50, 0, 1);
 
224
 
 
225
        hist = readtestComPIDVarG.Get_hist();
 
226
        ASSERT_TRUE(hist);
 
227
        // check number of entries in histogram
 
228
        // NB as destructor was called above, the number of entries in the histogram
 
229
        // will be increased by the number of bins (inc over and underflow) as in
 
230
        // ComPIDVarG the behaviour to add one event across all bins is turned on.
 
231
        ASSERT_EQ((1000 + hist->GetSize()), hist->GetEntries());
 
232
        double prob = readtestComPIDVarG.logL(checkTrack);
 
233
 
 
234
        ASSERT_LE(prob, 0);
 
235
        ASSERT_TRUE(IsFiniteNumber(prob));
 
236
}