~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

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