~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to tests/cpp_unit/Recon/Global/ComPIDVarDTest.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/ComPIDVarD.hh"
 
33
 
 
34
 
 
35
 
 
36
TEST(ComPIDVarDTestSetUp, TestSetUpAndTearDown) {
 
37
        MAUS::recon::global::ComPIDVarD *testComPIDVarD = NULL;
 
38
        ASSERT_NO_THROW(testComPIDVarD = new MAUS::recon::global::ComPIDVarD("test",
 
39
                                                                       "test"));
 
40
        std::string testfile = testComPIDVarD->Get_filename();
 
41
        std::string testdir = testComPIDVarD->Get_directory();
 
42
        ASSERT_NO_THROW(delete testComPIDVarD);
 
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 ComPIDVarDTest : public ::testing::Test {
 
54
 
 
55
        virtual void SetUp() {
 
56
                MAUS::recon::global::ComPIDVarD testComPIDVarD("test", "test");
 
57
                testfile = testComPIDVarD.Get_filename();
 
58
                testdir = testComPIDVarD.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* tpEMR =
 
64
                    new MAUS::DataStructure::Global::TrackPoint();
 
65
 
 
66
                  double emr_range = r.Gaus(50, 10);
 
67
                  testTrack->set_emr_range_primary(emr_range);
 
68
 
 
69
                  tpEMR->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
70
                  tpEMR->set_detector(MAUS::DataStructure::Global::kEMR);
 
71
                  testTrack->AddTrackPoint(tpEMR);
 
72
                  testTrack->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
73
                  testTracks.push_back(testTrack);
 
74
                }
 
75
        }
 
76
 
 
77
        virtual void TearDown() {
 
78
                gSystem->Unlink(testfile.c_str());
 
79
                gSystem->Unlink(testdir.c_str());
 
80
        }
 
81
 
 
82
 public:
 
83
        bool fileExists(std::string filename) {
 
84
                TFile f(filename.c_str(), "READ");
 
85
                return !f.IsZombie();
 
86
        }
 
87
 
 
88
        bool IsFiniteNumber(double x) {
 
89
        return (x <= DBL_MAX && x >= -DBL_MAX);
 
90
    }
 
91
 
 
92
        std::string testfile;
 
93
        std::string testdir;
 
94
        TFile *file;
 
95
        TH1F *hist;
 
96
        std::string histname;
 
97
        std::vector<MAUS::DataStructure::Global::Track*> testTracks;
 
98
};
 
99
 
 
100
TEST_F(ComPIDVarDTest, CreateFileConstructor) {
 
101
 
 
102
        ASSERT_TRUE(fileExists(testfile));
 
103
}
 
104
 
 
105
TEST_F(ComPIDVarDTest, ReadFileConstructor) {
 
106
 
 
107
        file = new TFile(testfile.c_str(), "READ");
 
108
 
 
109
        ASSERT_TRUE(file);
 
110
 
 
111
        ASSERT_FALSE(file->IsZombie());
 
112
 
 
113
        ASSERT_NO_THROW(MAUS::recon::global::ComPIDVarD testComPIDVarD(file, "test", 0, 8000));
 
114
 
 
115
        ASSERT_ANY_THROW(MAUS::recon::global::ComPIDVarD testComPIDVarD(NULL, "test", 0, 8000));
 
116
 
 
117
        ASSERT_ANY_THROW(MAUS::recon::global::ComPIDVarD testComPIDVarD(file, "sasquatch", 0, 8000));
 
118
}
 
119
 
 
120
/* N.B. if either FillHist or LogL are failing, check the values of min and max
 
121
*        bins in ComPIDVarD.hh, as if the testTracks fall outside of this range FillHist 
 
122
*  and LogL will fail.
 
123
*/
 
124
 
 
125
TEST_F(ComPIDVarDTest, FillHist) {
 
126
 
 
127
        MAUS::recon::global::ComPIDVarD testComPIDVarD("test", "test");
 
128
        for (int i = 0; i < 1000; i++) {
 
129
                testComPIDVarD.Fill_Hist(testTracks[i]);
 
130
        }
 
131
        testfile = testComPIDVarD.Get_filename();
 
132
        testdir = testComPIDVarD.Get_directory();
 
133
        // check histogram exists
 
134
        hist = testComPIDVarD.Get_hist();
 
135
        ASSERT_TRUE(hist);
 
136
        // check number of entries
 
137
        ASSERT_EQ(1000, hist->GetEntries());
 
138
}
 
139
 
 
140
TEST_F(ComPIDVarDTest, LogL) {
 
141
        // Constructor called within parentheses so that destructor will be
 
142
        // called when it goes out of scope
 
143
         {
 
144
                MAUS::recon::global::ComPIDVarD writetestComPIDVarD("test", "test");
 
145
                for (int i = 0; i < 1000; i++) {
 
146
                        writetestComPIDVarD.Fill_Hist(testTracks[i]);
 
147
                }
 
148
                testfile = writetestComPIDVarD.Get_filename();
 
149
                testdir = writetestComPIDVarD.Get_directory();
 
150
         }
 
151
 
 
152
        MAUS::DataStructure::Global::Track* checkTrack =
 
153
                        new MAUS::DataStructure::Global::Track();
 
154
        MAUS::DataStructure::Global::TrackPoint* ctpEMR =
 
155
                        new MAUS::DataStructure::Global::TrackPoint();
 
156
        ctpEMR->set_detector(MAUS::DataStructure::Global::kCalorimeter);
 
157
 
 
158
        double emr_range = 50;
 
159
        checkTrack->set_emr_range_primary(emr_range);
 
160
        ctpEMR->set_detector(MAUS::DataStructure::Global::kEMR);
 
161
        ctpEMR->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
162
        checkTrack->AddTrackPoint(ctpEMR);
 
163
        checkTrack->set_mapper_name("MapCppGlobalTrackMatching-Through");
 
164
 
 
165
        file = new TFile(testfile.c_str(), "READ");
 
166
 
 
167
        ASSERT_TRUE(file);
 
168
 
 
169
        ASSERT_FALSE(file->IsZombie());
 
170
 
 
171
        ASSERT_NO_THROW(MAUS::recon::global::ComPIDVarD readtestComPIDVarD(file, "test", 0, 1000));
 
172
 
 
173
        MAUS::recon::global::ComPIDVarD readtestComPIDVarD(file, "test", 0, 1000);
 
174
 
 
175
        hist = readtestComPIDVarD.Get_hist();
 
176
        ASSERT_TRUE(hist);
 
177
        // check number of entries in histogram
 
178
        // NB as destructor was called above, the number of entries in the histogram
 
179
        // will be increased by the number of bins (inc over and underflow) as in
 
180
        // ComPIDVarD the behaviour to add one event across all bins is turned on.
 
181
        ASSERT_EQ((1000 + hist->GetSize()), hist->GetEntries());
 
182
        double prob = readtestComPIDVarD.logL(checkTrack);
 
183
 
 
184
        ASSERT_LE(prob, 0);
 
185
        ASSERT_TRUE(IsFiniteNumber(prob));
 
186
}