~christopher-hunt08/maus/maus_radiation_det

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* This file is part of MAUS: http://  micewww.pp.rl.ac.uk:8080/projects/maus
 * 
 * MAUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MAUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MAUS.  If not, see <http://  www.gnu.org/licenses/>.
 */

#include "gtest/gtest.h"

#include "Geant4/G4Step.hh"
#include "Geant4/G4ParticleTable.hh"

#include "src/common_cpp/Utils/JsonWrapper.hh"
#include "src/common_cpp/Simulation/MAUSGeant4Manager.hh"
#include "src/common_cpp/Simulation/MAUSSteppingAction.hh"
#include "src/common_cpp/Simulation/VirtualPlanes.hh"

#include "src/legacy/Interface/MICERun.hh"

using namespace MAUS;

namespace {
class MAUSSteppingActionTest : public ::testing::Test {
 protected:
  MAUSSteppingActionTest() : stepping(MAUSGeant4Manager::GetInstance()->GetStepping()) {
    G4ParticleDefinition* pd =
                         G4ParticleTable::GetParticleTable()->FindParticle(-13);
    G4DynamicParticle* dyn =
                        new G4DynamicParticle(pd, G4ThreeVector(1., 2., 3.));
    track = new G4Track(dyn, 7., G4ThreeVector(4., 5., 6.));

    step = new G4Step(); // memory leak?
    point = new G4StepPoint();
    point->SetPosition(G4ThreeVector(1., 2., 3.));
    point->SetMass(pd->GetPDGMass());
    point->SetMomentumDirection(G4ThreeVector(0., ::sqrt(0.5), ::sqrt(0.5)));
    point->SetKineticEnergy(100.);
    post = new G4StepPoint();
    post->SetPosition(G4ThreeVector(7., 8., 9.));
    post->SetMass(pd->GetPDGMass());
    post->SetMomentumDirection(G4ThreeVector(::sqrt(1./3.), ::sqrt(1./3.), ::sqrt(1./3.)));
    post->SetKineticEnergy(110.);
    post->SetGlobalTime(-10.);
    track->SetStep(step);
    step->SetTrack(track);
    step->SetPreStepPoint(point);
    step->SetPostStepPoint(post);

    track->AddTrackLength(10.);
    step->SetTotalEnergyDeposit(8.);

    point->SetGlobalTime(1.);
    point->SetProperTime(2.);
    point->SetPosition(G4ThreeVector(2.,3.,4.));
    point->SetMomentumDirection(G4ThreeVector(1.,2.,3.));
    point->SetKineticEnergy(9.);
  }
  virtual ~MAUSSteppingActionTest() {}
  virtual void SetUp() {}
  virtual void TearDown() {}

  MAUS::MAUSSteppingAction* stepping;
  G4Track* track;
  G4Step* step;
  G4StepPoint* point;
  G4StepPoint* post;

};

// test get and set work correctly
TEST_F(MAUSSteppingActionTest, GetSetTest) {
  stepping->SetWillKeepSteps(true);
  EXPECT_EQ(stepping->GetWillKeepSteps(), true);
  stepping->SetWillKeepSteps(false);
  EXPECT_EQ(stepping->GetWillKeepSteps(), false);

//  EXPECT_THROW(stepping->SetSteps(Json::Value(Json::objectValue)), Squeal);
//  Json::Value array(Json::arrayValue);
//  array.append(Json::Value("Hello"));
//  EXPECT_NO_THROW(stepping->SetSteps(array));
//  EXPECT_EQ(array, stepping->GetSteps());
  StepArray* steps = new StepArray;
  stepping->SetSteps( steps );
  EXPECT_EQ(steps, stepping->GetSteps() );

  delete steps;
}

// test that we set stop and kill if stepping goes more than max n steps
TEST_F(MAUSSteppingActionTest, UserSteppingActionMaxNStepsTest) {
//  Json::Value& conf = *MICERun::getInstance()->jsonConfiguration;
//  int maxNSteps = JsonWrapper::GetProperty
//               (conf, "maximum_number_of_steps", JsonWrapper::intValue).asInt();
//  MAUSTrackingAction* tracking = MAUSGeant4Manager::GetInstance()->GetTracking();
//  tracking->SetWillKeepTracks(true);
//  tracking->SetTracks(Json::Value(Json::arrayValue));
//  tracking->PreUserTrackingAction(step->GetTrack());
//  stepping->SetSteps(Json::Value(Json::arrayValue));
//  stepping->UserSteppingAction(step);
//
//  ASSERT_TRUE(tracking->GetTracks()[Json::Value::UInt(0)].isObject());
//  EXPECT_EQ(tracking->GetTracks()[Json::Value::UInt(0)]["kill_reason"].type(),
//                                                             Json::stringValue);
//  for (int i = 0; i < maxNSteps+1; ++i)
//    track->IncrementCurrentStepNumber();
//  stepping->UserSteppingAction(step);
//  EXPECT_EQ(tracking->GetTracks()[Json::Value::UInt(0)]["kill_reason"].type(),
//                                                             Json::stringValue);
//  EXPECT_EQ(track->GetTrackStatus(), fStopAndKill);

  Json::Value& conf = *MICERun::getInstance()->jsonConfiguration;
  int maxNSteps = JsonWrapper::GetProperty
               (conf, "maximum_number_of_steps", JsonWrapper::intValue).asInt();
  MAUSTrackingAction* tracking = MAUSGeant4Manager::GetInstance()->GetTracking();
  tracking->SetWillKeepTracks(true);
  tracking->SetTracks( new TrackArray );
  tracking->PreUserTrackingAction(step->GetTrack());
  stepping->SetSteps( new StepArray );
  stepping->UserSteppingAction(step);
  EXPECT_NE( tracking->GetTracks(), (TrackArray*)NULL );

  for (int i = 0; i < maxNSteps+1; ++i)
    track->IncrementCurrentStepNumber();
  stepping->UserSteppingAction(step);
  EXPECT_NE(tracking->GetTracks()->front().GetKillReason(), std::string( "" ) );
  EXPECT_EQ(track->GetTrackStatus(), fStopAndKill);

  delete tracking->GetTracks();
  delete stepping->GetSteps();
}

// test that we write steps correctly on each step (or not if flag is false)
TEST_F(MAUSSteppingActionTest, UserSteppingActionWriteStepsTest) {
//  stepping->SetWillKeepSteps(false);
//  stepping->SetSteps(Json::Value(Json::arrayValue));
//  stepping->UserSteppingAction(step);
//  EXPECT_EQ(stepping->GetSteps()[Json::UInt(0)].type(), Json::nullValue);
//  stepping->SetWillKeepSteps(true);
//  stepping->UserSteppingAction(step);
//  stepping->UserSteppingAction(step);
//  ASSERT_EQ(stepping->GetSteps().type(), Json::arrayValue);
//  // we want 3 steps - 2 post steps plus the first one
//  EXPECT_EQ(stepping->GetSteps().size(), static_cast<unsigned int>(3));

  stepping->SetWillKeepSteps(false);
  stepping->SetSteps( new StepArray );
  stepping->UserSteppingAction(step);
  EXPECT_EQ(stepping->GetSteps()->size(), (StepArray::size_type) 0);

  stepping->SetWillKeepSteps(true);
  stepping->UserSteppingAction(step);
  stepping->UserSteppingAction(step);
  // we want 3 steps - 2 post steps plus the first one
  EXPECT_EQ(stepping->GetSteps()->size(), (StepArray::size_type) 3);

  delete stepping->GetSteps();
}

// test that we write to json correctly
//TEST_F(MAUSSteppingActionTest, StepToJsonTest) {
//  Json::Value out = stepping->StepToJson(step, true);
//  EXPECT_DOUBLE_EQ(out["position"]["x"].asDouble(), point->GetPosition().x());
//  EXPECT_DOUBLE_EQ(out["position"]["y"].asDouble(), point->GetPosition().y());
//  EXPECT_DOUBLE_EQ(out["position"]["z"].asDouble(), point->GetPosition().z());
//  EXPECT_DOUBLE_EQ(out["momentum"]["x"].asDouble(), point->GetMomentum().x());
//  EXPECT_DOUBLE_EQ(out["momentum"]["y"].asDouble(), point->GetMomentum().y());
//  EXPECT_DOUBLE_EQ(out["momentum"]["z"].asDouble(), point->GetMomentum().z());
//  EXPECT_DOUBLE_EQ(out["energy_deposited"].asDouble(), 8.);
//  EXPECT_DOUBLE_EQ(out["energy"].asDouble(), point->GetTotalEnergy());
//  EXPECT_DOUBLE_EQ(out["time"].asDouble(), point->GetGlobalTime());
//  EXPECT_DOUBLE_EQ(out["proper_time"].asDouble(), point->GetProperTime());
//  EXPECT_DOUBLE_EQ(out["path_length"].asDouble(), track->GetTrackLength());
//
//  out = stepping->StepToJson(step, false);
//  EXPECT_DOUBLE_EQ(out["time"].asDouble(), post->GetGlobalTime());
//}

G4Track* SetG4TrackAndStep(G4Step* step) {
  G4ParticleDefinition* pd =
                       G4ParticleTable::GetParticleTable()->FindParticle(-13);
  G4DynamicParticle* dyn =
                      new G4DynamicParticle(pd, G4ThreeVector(1., 2., 3.));
  G4Track* track = new G4Track(dyn, 7., G4ThreeVector(4., 5., 6.));

  track->SetStep(step);
  step->SetTrack(track);
  step->SetPreStepPoint(new G4StepPoint());
  step->SetPostStepPoint(new G4StepPoint());
  track->SetTrackID(10);

  step->GetPreStepPoint()->SetGlobalTime(-1.);
  step->GetPreStepPoint()->SetPosition(CLHEP::Hep3Vector(2., 3., 4.));
  step->GetPreStepPoint()->SetMass(dyn->GetMass());
  step->GetPreStepPoint()->SetKineticEnergy(100.);
  step->GetPreStepPoint()->SetMomentumDirection
                                    (CLHEP::Hep3Vector(0., 0.1, 1.)/::sqrt(1.01));

  step->GetPostStepPoint()->SetGlobalTime(1.);
  step->GetPostStepPoint()->SetPosition(CLHEP::Hep3Vector(3., 4., 8.));
  step->GetPostStepPoint()->SetMass(dyn->GetMass());
  step->GetPostStepPoint()->SetKineticEnergy(110.);
  step->GetPostStepPoint()->SetMomentumDirection
                                    (CLHEP::Hep3Vector(0., 0.1, 1.)/::sqrt(1.01));
  return track;
}

// test that we do virtual stepping correctly
TEST_F(MAUSSteppingActionTest, UserSteppingActionVirtualTest) {
  MiceModule mod;
  mod.setProperty<double>("PlaneTime", 0.);
  mod.setProperty<std::string>("SensitiveDetector", "Virtual");
  mod.setProperty<std::string>("IndependentVariable", "time");
  mod.setProperty<std::string>("MultiplePasses", "NewStation");

  VirtualPlaneManager* vpm = MAUSGeant4Manager::GetInstance()->GetVirtualPlanes();
  //vpm->SetVirtualHits(Json::Value(Json::arrayValue));
  vpm->SetVirtualHits( new VirtualHitArray );
  vpm->ConstructVirtualPlanes(NULL, &mod);
  vpm->ConstructVirtualPlanes(NULL, &mod);

  G4Step*  step  = new G4Step();
  G4Track* track = SetG4TrackAndStep(step);
  if(track) {}

  stepping->UserSteppingAction(step);
  stepping->UserSteppingAction(step);
  EXPECT_EQ(vpm->GetVirtualHits()->size(), (VirtualHitArray::size_type)4);
}

} //namespace end