~mbogomilov/maus/devel3

« back to all changes in this revision

Viewing changes to tests/cpp_unit/Optics/LinearApproximationOpticsModelTest.cc

  • Committer: Durga Rajaram
  • Date: 2014-01-14 04:39:44 UTC
  • mfrom: (663.38.24 merge)
  • mto: (698.1.1 release)
  • mto: This revision was merged to the branch mainline in revision 693.
  • Revision ID: durga@fnal.gov-20140114043944-qf0i8nksnwu74cll
candidate 0.7.6 - trunk r1026

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <string>
26
26
 
27
27
#include <cstdlib>
28
 
/*
29
 
#include <unistd.h>
30
 
#include <sys/param.h> // MAXPATHLEN definition
31
 
*/
32
28
 
33
29
#include "gtest/gtest.h"
34
30
#include "CLHEP/Vector/Rotation.h"
35
31
#include "CLHEP/Vector/ThreeVector.h"
36
32
 
37
33
#include "BeamTools/BTTracker.hh"
38
 
#include "Interface/Squeal.hh"
 
34
#include "Utils/Exception.hh"
39
35
#include "src/common_cpp/Globals/GlobalsManager.hh"
40
36
#include "src/common_cpp/Optics/CovarianceMatrix.hh"
41
37
#include "src/common_cpp/Optics/LinearApproximationOpticsModel.hh"
56
52
class LinearApproximationOpticsModelTest : public testing::Test {
57
53
 public:
58
54
  LinearApproximationOpticsModelTest()
59
 
      : default_virtual_planes_(new MAUS::VirtualPlaneManager(*
60
 
            MAUS::MAUSGeant4Manager::GetInstance()->GetVirtualPlanes())) {
 
55
      : default_virtual_planes_(new MAUS::VirtualPlaneManager(
 
56
          *MAUS::MAUSGeant4Manager::GetInstance()->GetVirtualPlanes())),
 
57
        virtual_planes_(new MAUS::VirtualPlaneManager()) {
61
58
    MAUS::MAUSGeant4Manager * simulation
62
59
        = MAUS::MAUSGeant4Manager::GetInstance();
63
 
    /*
64
 
    char path1[MAXPATHLEN]; // This is a buffer for the text
65
 
    getcwd(path1, MAXPATHLEN);
66
 
    std::cout << "CWD: " << path1 << std::endl;
67
 
    */
68
 
    /*
69
 
    const std::string maus_root_dir(getenv("MAUS_ROOT_DIR"));
70
 
    const std::string geometry_filename = maus_root_dir + "/"
71
 
                                        + kGeometryFilename;
72
 
    */
73
60
 
74
61
    Json::Value * config = MAUS::Globals::GetConfigurationCards();
 
62
 
75
63
    (*config)["verbose_level"] = Json::Value(2);
76
 
 
77
64
    (*config)["reference_physics_processes"] = Json::Value("none");
78
65
    (*config)["physics_processes"] = Json::Value("none");
79
66
    (*config)["particle_decay"] = Json::Value(false);
80
67
    simulation->GetPhysicsList()->Setup();
81
68
 
82
 
    (*config)["simulation_reference_particle"] = JsonWrapper::StringToJson(
83
 
      std::string("{\"position\":{\"x\":0.0,\"y\":0.0,\"z\":-1000.0},")+
84
 
      std::string("\"momentum\":{\"x\":0.0,\"y\":0.0,\"z\":200.0},")+
85
 
      std::string("\"particle_id\":-13,\"energy\":226.1939223,\"time\":0.0,")+
86
 
      std::string("\"random_seed\":2}")
87
 
    );
 
69
    std::stringstream reference_particle_string;
 
70
    reference_particle_string
 
71
      << std::setprecision(1)
 
72
      << "{\"position\":{\"x\":0.0,\"y\":0.0,\"z\":" << kPrimaryPlane << "},"
 
73
      << "\"momentum\":{\"x\":0.0,\"y\":0.0,\"z\":200.0},"
 
74
      << "\"particle_id\":-13,\"energy\":226.1939223,\"time\":0.0,"
 
75
      << "\"random_seed\":2}";
 
76
    (*config)["simulation_reference_particle"]
 
77
      = JsonWrapper::StringToJson(reference_particle_string.str());
88
78
 
89
79
    Json::Value ellipse(Json::objectValue);
90
80
    ellipse["Emittance_T"] = Json::Value(10.0);
109
99
    MAUS::GlobalsManager::DeleteGlobals();
110
100
    MAUS::GlobalsManager::InitialiseGlobals(config_string);
111
101
 
112
 
    std::cout << "Globals:" << std::endl
113
 
              << (*MAUS::Globals::GetConfigurationCards()) << std::endl;
114
 
    virtual_planes_ = new MAUS::VirtualPlaneManager();
115
102
    simulation->SetVirtualPlanes(virtual_planes_);
116
103
    MAUS::VirtualPlane start_plane = MAUS::VirtualPlane::BuildVirtualPlane(
117
 
        CLHEP::HepRotation(), CLHEP::Hep3Vector(0., 0., -1000.), -1, true,
118
 
        -1000., BTTracker::z, MAUS::VirtualPlane::ignore, false);
 
104
        CLHEP::HepRotation(), CLHEP::Hep3Vector(0., 0., kPrimaryPlane), -1,
 
105
        true, kPrimaryPlane, BTTracker::z, MAUS::VirtualPlane::ignore, false);
119
106
    virtual_planes_->AddPlane(new MAUS::VirtualPlane(start_plane), NULL);
120
107
 
121
108
    MAUS::VirtualPlane mid_plane = MAUS::VirtualPlane::BuildVirtualPlane(
122
 
        CLHEP::HepRotation(), CLHEP::Hep3Vector(0., 0., 0.), -1, true,
123
 
        0., BTTracker::z, MAUS::VirtualPlane::ignore, false);
 
109
        CLHEP::HepRotation(), CLHEP::Hep3Vector(0., 0., kPrimaryPlane+1000.),
 
110
        -1, true, kPrimaryPlane+1000., BTTracker::z, MAUS::VirtualPlane::ignore,
 
111
        false);
124
112
    virtual_planes_->AddPlane(new MAUS::VirtualPlane(mid_plane), NULL);
125
113
 
126
114
    MAUS::VirtualPlane end_plane = MAUS::VirtualPlane::BuildVirtualPlane(
127
 
        CLHEP::HepRotation(), CLHEP::Hep3Vector(0., 0., 1000.), -1, true,
128
 
        1000., BTTracker::z, MAUS::VirtualPlane::ignore, false);
 
115
        CLHEP::HepRotation(), CLHEP::Hep3Vector(0., 0., kPrimaryPlane+2000.),
 
116
        -1, true, kPrimaryPlane+2000., BTTracker::z, MAUS::VirtualPlane::ignore,
 
117
        false);
129
118
    virtual_planes_->AddPlane(new MAUS::VirtualPlane(end_plane), NULL);
130
119
  }
131
120
 
139
128
    std::cout << "*** Reset Globals ***" << std::endl;
140
129
  }
141
130
 
 
131
  static const double kPrimaryPlane;
142
132
 protected:
143
133
  void ResolveRootDirectory(std::string & str,
144
134
                            const std::string & maus_root_dir) {
153
143
  static const double kCovariances[36];
154
144
  static const MAUS::CovarianceMatrix kCovarianceMatrix;
155
145
 private:
156
 
  MAUS::VirtualPlaneManager * virtual_planes_;
157
 
  MAUS::VirtualPlaneManager * default_virtual_planes_;
 
146
  MAUS::VirtualPlaneManager * const default_virtual_planes_;
 
147
  MAUS::VirtualPlaneManager * const virtual_planes_;
158
148
};
159
149
 
160
150
// *************************************************
161
151
// LinearApproximationOpticsModelTest static const initializations
162
152
// *************************************************
 
153
const double LinearApproximationOpticsModelTest::kPrimaryPlane = -1000;
163
154
const double LinearApproximationOpticsModelTest::kCovariances[36] = {
164
155
  0., 1., 2., 3., 4., 5.,
165
156
  1., 2., 3., 4., 5., 6.,
178
169
 
179
170
TEST_F(LinearApproximationOpticsModelTest, Constructor) {
180
171
  const LinearApproximationOpticsModel optics_model(
181
 
      *MAUS::Globals::GetConfigurationCards());
 
172
      MAUS::Globals::GetConfigurationCards());
182
173
}
183
174
 
184
175
TEST_F(LinearApproximationOpticsModelTest, Accessors) {
185
176
  LinearApproximationOpticsModel optics_model(
186
 
      *MAUS::Globals::GetConfigurationCards());
187
 
  double first_plane = optics_model.first_plane();
188
 
  ASSERT_DOUBLE_EQ(-1000., first_plane);
 
177
      MAUS::Globals::GetConfigurationCards());
 
178
  double primary_plane = optics_model.primary_plane();
 
179
  ASSERT_DOUBLE_EQ(kPrimaryPlane, primary_plane);
189
180
 
190
 
  optics_model.set_first_plane(0.);
191
 
  first_plane = optics_model.first_plane();
192
 
  ASSERT_DOUBLE_EQ(0., first_plane);
 
181
  optics_model.set_primary_plane(0.);
 
182
  primary_plane = optics_model.primary_plane();
 
183
  ASSERT_DOUBLE_EQ(0., primary_plane);
193
184
}
194
185
 
195
186
TEST_F(LinearApproximationOpticsModelTest, Transport) {
196
187
  LinearApproximationOpticsModel optics_model(
197
 
      *MAUS::Globals::GetConfigurationCards());
198
 
 
 
188
      MAUS::Globals::GetConfigurationCards());
199
189
  // The configuration specifies a 2m drift between -1m and +1 m.
200
190
  optics_model.Build();
201
191
 
202
192
  // Check transport to start plane
203
193
  MAUS::PhaseSpaceVector input_vector(0., 226., 1., 0., 3., 0.);
204
194
  MAUS::PhaseSpaceVector output_vector = optics_model.Transport(input_vector,
205
 
                                                                -1000.);
 
195
                                                                kPrimaryPlane);
206
196
  EXPECT_EQ(input_vector, output_vector);
207
197
 
208
198
  MAUS::CovarianceMatrix output_errors
209
 
      = optics_model.Transport(kCovarianceMatrix, -1000.);
 
199
      = optics_model.Transport(kCovarianceMatrix, kPrimaryPlane);
210
200
  EXPECT_EQ(kCovarianceMatrix, output_errors);
211
201
 
212
202
  // Check transport to end plane
213
203
  MAUS::PhaseSpaceVector expected_vector(7.5466, 226., 1., 0., 3., 0.);
214
 
  output_vector = optics_model.Transport(input_vector, 1000.);
 
204
  output_vector = optics_model.Transport(input_vector, kPrimaryPlane+2000.);
215
205
  for (int index = 0; index < 6; ++index) {
216
206
    EXPECT_NEAR(expected_vector[index], output_vector[index], 5.0e-4);
217
207
  }
218
208
 
219
 
  output_errors = optics_model.Transport(kCovarianceMatrix, 1000.);
 
209
  output_errors = optics_model.Transport(kCovarianceMatrix,
 
210
                                         kPrimaryPlane+2000.);
220
211
  EXPECT_EQ(kCovarianceMatrix, output_errors);
221
212
 
222
213
  // Check transport to mid plane
223
214
  expected_vector = MAUS::PhaseSpaceVector(3.7733, 226., 1., 0., 3., 0.);
224
 
  output_vector = optics_model.Transport(input_vector, 0.);
 
215
  output_vector = optics_model.Transport(input_vector, kPrimaryPlane+1000.);
225
216
  for (int index = 0; index < 6; ++index) {
226
217
    EXPECT_NEAR(expected_vector[index], output_vector[index], 5.0e-4);
227
218
  }
228
219
 
229
 
  output_errors = optics_model.Transport(kCovarianceMatrix, 0.);
 
220
  output_errors = optics_model.Transport(kCovarianceMatrix,
 
221
                                         kPrimaryPlane+1000.);
230
222
  EXPECT_EQ(kCovarianceMatrix, output_errors);
231
223
 
232
224
  // Check transport between mid plane and end plane
233
 
  output_vector = optics_model.Transport(input_vector, 0., 1000.);
 
225
  output_vector = optics_model.Transport(input_vector,
 
226
                                         kPrimaryPlane+1000.,
 
227
                                         kPrimaryPlane+2000.);
234
228
  for (int index = 0; index < 6; ++index) {
235
229
    EXPECT_NEAR(expected_vector[index], output_vector[index], 5.0e-4);
236
230
  }
237
231
 
238
 
  output_errors = optics_model.Transport(kCovarianceMatrix, 0., 1000.);
 
232
  output_errors = optics_model.Transport(kCovarianceMatrix,
 
233
                                         kPrimaryPlane+1000.,
 
234
                                         kPrimaryPlane+2000.);
239
235
  EXPECT_EQ(kCovarianceMatrix, output_errors);
240
236
 
241
237
  // Check failure for off mass shell particle transport
242
238
  MAUS::PhaseSpaceVector off_shell_vector(0., 1., 0., 0., 0., 0.);
243
239
  bool transport_failed = false;
244
240
  try {
245
 
    optics_model.Transport(off_shell_vector, -1000.);
246
 
  } catch (Squeal squeal) {
 
241
    optics_model.Transport(off_shell_vector, kPrimaryPlane);
 
242
  } catch (MAUS::Exception exc) {
247
243
    transport_failed = true;
248
244
  }
249
245
  EXPECT_TRUE(transport_failed);