~chris-rogers/maus/emr_mc_digitization

« back to all changes in this revision

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

  • Committer: Chris Rogers
  • Date: 2014-04-16 11:48:45 UTC
  • mfrom: (707 merge)
  • mto: This revision was merged to the branch mainline in revision 711.
  • Revision ID: chris.rogers@stfc.ac.uk-20140416114845-h3u3q7pdcxkxvovs
Update to trunk

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/PIDVarA.hh"
 
33
 
 
34
 
 
35
 
 
36
TEST(PIDVarATestSetUp, TestSetUpAndTearDown) {
 
37
        MAUS::recon::global::PIDVarA *testPIDVarA = NULL;
 
38
        ASSERT_NO_THROW(testPIDVarA = new MAUS::recon::global::PIDVarA("test",
 
39
                                                                                                                                                                                                                                                                 "test"));
 
40
        std::string testfile = testPIDVarA->Get_filename();
 
41
        std::string testdir = testPIDVarA->Get_directory();
 
42
        ASSERT_NO_THROW(delete testPIDVarA);
 
43
 
 
44
        EXPECT_EQ(gSystem->Unlink(testfile.c_str()), 0);
 
45
        EXPECT_EQ(gSystem->Unlink(testdir.c_str()), 0);
 
46
}
 
47
 
 
48
class PIDVarATest : public ::testing::Test {
 
49
 
 
50
        virtual void SetUp() {
 
51
                MAUS::recon::global::PIDVarA testPIDVarA("test", "test");
 
52
                testfile = testPIDVarA.Get_filename();
 
53
                testdir = testPIDVarA.Get_directory();
 
54
                TRandom3 r;
 
55
                for (int i = 0; i < 1000; i++) {
 
56
                        MAUS::DataStructure::Global::Track* testTrack =
 
57
                                        new MAUS::DataStructure::Global::Track();
 
58
                        MAUS::DataStructure::Global::TrackPoint* tp0 =
 
59
                                        new MAUS::DataStructure::Global::TrackPoint();
 
60
                        MAUS::DataStructure::Global::TrackPoint* tp1 =
 
61
                                        new MAUS::DataStructure::Global::TrackPoint();
 
62
                        tp0->set_detector(MAUS::DataStructure::Global::kTOF0_1);
 
63
                        tp1->set_detector(MAUS::DataStructure::Global::kTOF1_1);
 
64
 
 
65
                        double x0 = 0.0;
 
66
                        double y0 = 0.0;
 
67
                        double z0 = 0.0;
 
68
                        double t0 = r.Gaus(15, 2);
 
69
                        TLorentzVector pos0(x0, y0, z0, t0);
 
70
 
 
71
                        double x1 = 0.0;
 
72
                        double y1 = 0.0;
 
73
                        double z1 = 0.0;
 
74
                        double t1 = r.Gaus(45, 2);
 
75
                        TLorentzVector pos1(x1, y1, z1, t1);
 
76
 
 
77
                        tp0->set_position(pos0);
 
78
                        tp1->set_position(pos1);
 
79
                        testTrack->AddTrackPoint(tp0);
 
80
                        testTrack->AddTrackPoint(tp1);
 
81
                        testTracks.push_back(testTrack);
 
82
                }
 
83
        }
 
84
 
 
85
        virtual void TearDown() {
 
86
                gSystem->Unlink(testfile.c_str());
 
87
                gSystem->Unlink(testdir.c_str());
 
88
        }
 
89
 
 
90
 public:
 
91
        bool fileExists(std::string filename) {
 
92
                TFile f(filename.c_str(), "READ");
 
93
                return !f.IsZombie();
 
94
        }
 
95
 
 
96
        bool IsFiniteNumber(double x) {
 
97
        return (x <= DBL_MAX && x >= -DBL_MAX);
 
98
    }
 
99
 
 
100
        std::string testfile;
 
101
        std::string testdir;
 
102
        TFile *file;
 
103
        TH1F *hist;
 
104
        std::string histname;
 
105
        std::vector<MAUS::DataStructure::Global::Track*> testTracks;
 
106
};
 
107
 
 
108
TEST_F(PIDVarATest, CreateFileConstructor) {
 
109
 
 
110
        ASSERT_TRUE(fileExists(testfile));
 
111
}
 
112
 
 
113
TEST_F(PIDVarATest, ReadFileConstructor) {
 
114
 
 
115
        file = new TFile(testfile.c_str(), "READ");
 
116
 
 
117
        ASSERT_TRUE(file);
 
118
 
 
119
        ASSERT_FALSE(file->IsZombie());
 
120
 
 
121
        ASSERT_NO_THROW(MAUS::recon::global::PIDVarA testPIDVarA(file, "test"));
 
122
 
 
123
        ASSERT_ANY_THROW(MAUS::recon::global::PIDVarA testPIDVarA(NULL, "test"));
 
124
 
 
125
        ASSERT_ANY_THROW(MAUS::recon::global::PIDVarA testPIDVarA(file, "sasquatch"));
 
126
}
 
127
 
 
128
/* N.B. if either FillHist or LogL are failing, check the values of min and max
 
129
*        bin in PIDVarA.hh, as if the testTracks fall outside of this range FillHist 
 
130
*  and LogL will fail.
 
131
*/
 
132
 
 
133
TEST_F(PIDVarATest, FillHist) {
 
134
 
 
135
        MAUS::recon::global::PIDVarA testPIDVarA("test", "test");
 
136
        for (int i = 0; i < 1000; i++) {
 
137
                testPIDVarA.Fill_TH1(testTracks[i]);
 
138
        }
 
139
        testfile = testPIDVarA.Get_filename();
 
140
        testdir = testPIDVarA.Get_directory();
 
141
        // check histogram exists
 
142
        hist = testPIDVarA.Get_hist();
 
143
        ASSERT_TRUE(hist);
 
144
        // check number of entries
 
145
        ASSERT_EQ(1000, hist->GetEntries());
 
146
}
 
147
 
 
148
TEST_F(PIDVarATest, LogL) {
 
149
        // Constructor called within parentheses so that destructor will be
 
150
        // called when it goes out of scope
 
151
         {
 
152
                MAUS::recon::global::PIDVarA writetestPIDVarA("test", "test");
 
153
                for (int i = 0; i < 1000; i++) {
 
154
                        writetestPIDVarA.Fill_TH1(testTracks[i]);
 
155
                }
 
156
                testfile = writetestPIDVarA.Get_filename();
 
157
                testdir = writetestPIDVarA.Get_directory();
 
158
         }
 
159
 
 
160
        MAUS::DataStructure::Global::Track* checkTrack =
 
161
                        new MAUS::DataStructure::Global::Track();
 
162
        MAUS::DataStructure::Global::TrackPoint* ctp0 =
 
163
                        new MAUS::DataStructure::Global::TrackPoint();
 
164
        MAUS::DataStructure::Global::TrackPoint* ctp1 =
 
165
                        new MAUS::DataStructure::Global::TrackPoint();
 
166
        ctp0->set_detector(MAUS::DataStructure::Global::kTOF0_1);
 
167
        ctp1->set_detector(MAUS::DataStructure::Global::kTOF1_1);
 
168
 
 
169
        double x0 = 0.0;
 
170
        double y0 = 0.0;
 
171
        double z0 = 0.0;
 
172
        double t0 = 15.0;
 
173
        TLorentzVector cpos0(x0, y0, z0, t0);
 
174
 
 
175
        double x1 = 0.0;
 
176
        double y1 = 0.0;
 
177
        double z1 = 0.0;
 
178
        double t1 = 45.0;
 
179
        TLorentzVector cpos1(x1, y1, z1, t1);
 
180
 
 
181
        ctp0->set_position(cpos0);
 
182
        ctp1->set_position(cpos1);
 
183
        checkTrack->AddTrackPoint(ctp0);
 
184
        checkTrack->AddTrackPoint(ctp1);
 
185
 
 
186
        file = new TFile(testfile.c_str(), "READ");
 
187
 
 
188
        ASSERT_TRUE(file);
 
189
 
 
190
        ASSERT_FALSE(file->IsZombie());
 
191
 
 
192
        ASSERT_NO_THROW(MAUS::recon::global::PIDVarA readtestPIDVarA(file, "test"));
 
193
 
 
194
        MAUS::recon::global::PIDVarA readtestPIDVarA(file, "test");
 
195
 
 
196
        hist = readtestPIDVarA.Get_hist();
 
197
        ASSERT_TRUE(hist);
 
198
        // check number of entries in histogram
 
199
        // NB as destructor was called above, the number of entries in the histogram
 
200
        // will be increased by the number of bins (inc over and underflow) as in
 
201
        // PIDVarA the behaviour to add one event across all bins is turned on.
 
202
        ASSERT_EQ((1000 + hist->GetSize()), hist->GetEntries());
 
203
        double prob = readtestPIDVarA.logL(checkTrack);
 
204
 
 
205
        ASSERT_LE(prob, 0);
 
206
        ASSERT_TRUE(IsFiniteNumber(prob));
 
207
}