Belle II Software  release-05-01-25
TrackFindingCDCTestWithSimpleSimulation.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost <oliver.frost@desy.de> *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/display/EventDataPlotter.h>
13 #include <tracking/trackFindingCDC/sim/CDCSimpleSimulation.h>
14 
15 #include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
16 #include <tracking/trackFindingCDC/eventdata/segments/CDCSegment3D.h>
17 #include <tracking/trackFindingCDC/eventdata/segments/CDCSegment2D.h>
18 #include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectory3D.h>
19 
20 #include <tracking/trackFindingCDC/topology/CDCWireTopology.h>
21 
22 #include <tracking/trackFindingCDC/geometry/Helix.h>
23 
24 #include <tracking/trackFindingCDC/testFixtures/TrackFindingCDCTestWithTopology.h>
25 #include <tracking/trackFindingCDC/utilities/TimeIt.h>
26 
27 #include <array>
28 
29 namespace Belle2 {
34  namespace TrackFindingCDC {
35 
37  class TrackFindingCDCTestWithSimpleSimulation :
38  public TrackFindingCDCTestWithTopology {
39 
40  public:
41  TrackFindingCDCTestWithSimpleSimulation() : m_simpleSimulation()
42  {
43  }
44 
46 
47  void SetUp() override
48  {
49 
52  bool run_disabled = ::testing::GTEST_FLAG(also_run_disabled_tests);
53  if (run_disabled) {
54  Belle2::LogSystem::Instance().getLogConfig()->setLogLevel(LogConfig::ELogLevel::c_Debug);
56  }
57 
58  m_plotter.clear();
59  m_mcAxialSegment2Ds.clear();
60  m_mcSegment2Ds.clear();
61  m_mcTracks.clear();
62  m_mcTrajectories.clear();
63  m_axialWireHits.clear();
64  m_wireHits.clear();
65  }
66 
68  void simulate(const std::initializer_list<Helix>& helices)
69  {
70  simulate(std::vector<Helix>(helices));
71  }
72 
73 
75  void simulate(const std::vector<Helix>& helices)
76  {
77  std::vector<CDCTrajectory3D> trajectories;
78  trajectories.reserve(helices.size());
79  for (const Helix& helix : helices) {
80  // had to make the implicit convesion to an explicit conversion. Maybe there is a more elegant way to do it
81  trajectories.emplace_back(UncertainHelix(helix));
82  }
83  simulate(trajectories);
84  }
85 
87  void simulate(const std::initializer_list<CDCTrajectory3D>& trajectories)
88  {
89  simulate(std::vector<CDCTrajectory3D>(trajectories));
90  }
91 
93  void simulate(const std::vector<CDCTrajectory3D>& trajectories)
94  {
95  m_mcTrajectories = trajectories;
96 
97  // Construct tracks. Wire hits are stored in the simple simulation
98  m_mcTracks = m_simpleSimulation.simulate(trajectories);
99 
100  fillCaches();
101  }
102 
104  void loadPreparedEvent()
105  {
106  // No trajectory information present
107  m_mcTrajectories.clear();
108 
109  // Load prepared tracks wire hits are stored in the simple simulation
111 
113  }
114 
116  void fillCaches()
117  {
118  for (size_t iTrack = 0; iTrack < m_mcTracks.size(); ++iTrack) {
119  B2INFO("Size mc track " << iTrack << " : " << m_mcTracks[iTrack].size());
120  }
121 
122  // Prepare the monte carlo segments
123  for (const CDCTrack& mcTrack : m_mcTracks) {
124  std::vector<CDCSegment3D> segment3DsInTrack = mcTrack.splitIntoSegments();
125  for (const CDCSegment3D& segment3D : segment3DsInTrack) {
126  m_mcSegment2Ds.push_back(segment3D.stereoProjectToRef());
127  }
128  }
129 
130  // Filter the axial segments
131  for (const CDCSegment2D& segment2D : m_mcSegment2Ds) {
132  if (segment2D.getStereoKind() == EStereoKind::c_Axial) {
133  m_mcAxialSegment2Ds.push_back(&segment2D);
134  }
135  }
136 
137  // Filter for axial hits
138  for (const CDCWireHit& wireHit : m_simpleSimulation.getWireHits()) {
139  if (wireHit.isAxial()) {
140  m_axialWireHits.push_back(&wireHit);
141  }
142  }
143 
144  // Pick up points for all hits
145  for (const CDCWireHit& wireHit : m_simpleSimulation.getWireHits()) {
146  m_wireHits.push_back(&wireHit);
147  }
148 
150  for (const CDCWireHit& wireHit : m_simpleSimulation.getWireHits()) {
151  m_plotter.draw(wireHit);
152  }
153  }
154 
156  void plotMCTracks()
157  { for (const CDCTrack& mcTrack : m_mcTracks) m_plotter.draw(mcTrack); }
158 
160  void plotMCTrajectories()
161  {
162  for (const CDCTrajectory3D& mcTrajectory : m_mcTrajectories) {
163  m_plotter.draw(mcTrajectory.getTrajectory2D());
164  }
165  }
166 
168  void saveDisplay(const std::string& svgFileName)
169  {
170  bool run_disabled = ::testing::GTEST_FLAG(also_run_disabled_tests);
171  if (run_disabled) {
172  m_plotter.save(svgFileName);
173  } else {
174  B2INFO("Not writing display file. To activate svg display output run with --gtest_also_run_disabled_tests");
175  }
176  }
177 
183  template<class AFunction >
185  timeIt(size_t nExecutions,
186  bool activateCallgrind,
187  const AFunction& function,
188  const std::function<void()>& setUp = doNothing,
189  const std::function<void()>& tearDown = doNothing)
190  {
191  bool run_disabled = ::testing::GTEST_FLAG(also_run_disabled_tests);
192  if (not run_disabled) {
193  nExecutions = 1;
194  }
195  return Belle2::TrackFindingCDC::timeIt(nExecutions,
196  activateCallgrind,
197  function,
198  setUp,
199  tearDown);
200 
201  }
202 
204  template<class... Ts>
205  void draw(Ts&& ... args)
206  {
207  m_plotter.draw(std::forward<Ts>(args) ...);
208  }
209 
211  void TearDown() override
212  {
215 
216  m_plotter.clear();
217  m_mcAxialSegment2Ds.clear();
218  m_mcSegment2Ds.clear();
219  m_mcTracks.clear();
220  m_mcTrajectories.clear();
221  m_axialWireHits.clear();
222  m_wireHits.clear();
223  }
224 
225  protected:
228 
230  unsigned int m_savedDebugLogInfo = 100;
231 
233  const std::array<std::string, 6> m_colors{{ "red", "blue", "green", "yellow", "violet", "cyan" }};
234 
236  CDCSimpleSimulation m_simpleSimulation;
237 
239  std::vector<CDCTrajectory3D> m_mcTrajectories;
240 
242  std::vector<CDCTrack> m_mcTracks;
243 
245  std::vector<CDCSegment2D> m_mcSegment2Ds;
246 
248  std::vector<const CDCSegment2D*> m_mcAxialSegment2Ds;
249 
251  std::vector<const CDCWireHit*> m_axialWireHits;
252 
254  std::vector<const CDCWireHit*> m_wireHits;
255 
256  private:
259 
260  };
261 
262  }
264 }
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::plotMCTrajectories
void plotMCTrajectories()
Add the Monte Carlo trajectories to the event plot.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:168
Belle2::TrackFindingCDC::EventDataPlotter
A class that can plot event related data types.
Definition: EventDataPlotter.h:64
Belle2::LogConfig::getLogInfo
unsigned int getLogInfo(ELogLevel logLevel) const
Returns the configured log information for the given level.
Definition: LogConfig.h:130
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_mcAxialSegment2Ds
std::vector< const CDCSegment2D * > m_mcAxialSegment2Ds
Memory for the axial Monte Carlo segments of the current event.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:256
Belle2::TrackFindingCDC::CDCSimpleSimulation::simulate
std::vector< CDCTrack > simulate(const std::vector< CDCTrajectory3D > &trajectories3D)
Propagates the trajectories through the CDC as without energy loss until they first leave the CDC.
Definition: CDCSimpleSimulation.cc:50
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_mcTracks
std::vector< CDCTrack > m_mcTracks
Memory for the Monte Carlo tracks of the current event.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:250
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_savedLogLevel
LogConfig::ELogLevel m_savedLogLevel
Memory for the log level that was set before the test.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:235
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_plotter
EventDataPlotter m_plotter
Plotter facility to generate visual representations.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:266
Belle2::TrackFindingCDC::CDCWireTopology::getInstance
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
Definition: CDCWireTopology.cc:22
Belle2::TrackFindingCDC::Helix
Extension of the generalized circle also caching the perigee coordinates.
Definition: Helix.h:38
Belle2::LogConfig::setLogLevel
void setLogLevel(ELogLevel logLevel)
Configure the log level.
Definition: LogConfig.cc:27
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_axialWireHits
std::vector< const CDCWireHit * > m_axialWireHits
Memory for the axial hits of the current event.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:259
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::saveDisplay
void saveDisplay(const std::string &svgFileName)
Save content of the plotter to svg file only if running in unrestricted mode –gtest_also_run_disabled...
Definition: TrackFindingCDCTestWithSimpleSimulation.h:176
Belle2::TrackFindingCDC::CDCSegment3D
A segment consisting of three dimensional reconstructed hits.
Definition: CDCSegment3D.h:36
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::TearDown
void TearDown() override
Clean up after test.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:219
Belle2::LogConfig::c_Info
@ c_Info
Info: for informational messages, e.g.
Definition: LogConfig.h:37
Belle2::TrackFindingCDC::EventDataPlotter::save
const std::string save(const std::string &fileName)
Saves the current plot stead to a file.
Definition: EventDataPlotter.cc:74
Belle2::LogConfig::ELogLevel
ELogLevel
Definition of the supported log levels.
Definition: LogConfig.h:36
Belle2::TrackFindingCDC::EventDataPlotter::draw
void draw(const Belle2::TrackFindingCDC::Circle2D &circle, AttributeMap attributeMap=AttributeMap())
Draws a filled circle.
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_colors
const std::array< std::string, 6 > m_colors
Some colors to cycle for plotting.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:241
Belle2::TrackFindingCDC::EventDataPlotter::clear
void clear()
Clears all drawed elements from the plotter.
Definition: EventDataPlotter.cc:84
Belle2::LogSystem::getLogConfig
LogConfig * getLogConfig()
Returns global log system configuration.
Definition: LogSystem.h:88
Belle2::TrackFindingCDC::CDCSimpleSimulation::getWireHits
ConstVectorRange< CDCWireHit > getWireHits() const
Getter for the wire hits created in the simulation.
Definition: CDCSimpleSimulation.cc:35
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_mcSegment2Ds
std::vector< CDCSegment2D > m_mcSegment2Ds
Memory for the Monte Carlo segments of the current event.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:253
Belle2::LogConfig::c_Level
@ c_Level
Log level of the message.
Definition: LogConfig.h:46
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_mcTrajectories
std::vector< CDCTrajectory3D > m_mcTrajectories
Memory for the Monte Carlo trajectories of the current event.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:247
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::plotMCTracks
void plotMCTracks()
Add the Monte Carlo tracks to the event plot.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:164
Belle2::LogSystem::Instance
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:33
Belle2::LogConfig::setLogInfo
void setLogInfo(ELogLevel logLevel, unsigned int logInfo)
Configure the printed log information for the given level.
Definition: LogConfig.h:123
Belle2::LogConfig::c_Message
@ c_Message
Log message text.
Definition: LogConfig.h:47
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_wireHits
std::vector< const CDCWireHit * > m_wireHits
Memory for the hits of the current event.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:262
Belle2::LogConfig::c_Debug
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:36
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::simulate
void simulate(const std::vector< Helix > &helices)
Populate the event with hits generated from the given helices.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:83
Belle2::LogConfig::getLogLevel
ELogLevel getLogLevel() const
Returns the configured log level.
Definition: LogConfig.h:87
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::loadPreparedEvent
void loadPreparedEvent()
Populate the event with hits hard codes presimulated hits.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:112
Belle2::TrackFindingCDC::UncertainHelix
A general helix class including a covariance matrix.
Definition: UncertainHelix.h:44
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_savedDebugLogInfo
unsigned int m_savedDebugLogInfo
Memory for the log info of debug that was set before the test.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:238
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::SetUp
void SetUp() override
Preparations before the test.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:55
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::simulate
void simulate(const std::initializer_list< Helix > &helices)
Populate the event with hits generated from the given helices.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:76
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::fillCaches
void fillCaches()
Prepare a set of hits, axial hits, segments, axial segments and track hits.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:124
Belle2::TrackFindingCDC::CDCSimpleSimulation::loadPreparedEvent
std::vector< CDCTrack > loadPreparedEvent()
Fills the wire hits with a hard coded event from the real simulation.
Definition: CDCSimpleSimulation.cc:384
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::m_simpleSimulation
CDCSimpleSimulation m_simpleSimulation
Simple simulation generating the hits.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:244
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::draw
void draw(Ts &&... args)
Forwarding draw class to the plotter instance.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:213
Belle2::TrackFindingCDC::TrackFindingCDCTestWithSimpleSimulation::timeIt
TimeItResult timeIt(size_t nExecutions, bool activateCallgrind, const AFunction &function, const std::function< void()> &setUp=doNothing, const std::function< void()> &tearDown=doNothing)
Repeat a time critical simulation section a couple of times.
Definition: TrackFindingCDCTestWithSimpleSimulation.h:193
Belle2::TrackFindingCDC::TimeItResult
Class to capture the time a repeated execution took.
Definition: TimeIt.h:39