Belle II Software  release-05-02-19
Styling.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: dschneider, Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/display/ColorMapping.h>
13 #include <tracking/trackFindingCDC/display/Mapping.h>
14 #include <tracking/trackFindingCDC/display/AttributeMap.h>
15 
16 
17 #include <map>
18 #include <sstream>
19 #include <memory>
20 
21 namespace Belle2 {
26  class RecoTrack;
27  class CDCHit;
28 
29  namespace TrackFindingCDC {
30  class CDCSegment2D;
31 
33  template <class AObject>
34  class Styling {
35 
36  public:
38  virtual ~Styling() = default;
39 
41  virtual AttributeMap map(int index __attribute__((unused)),
42  AObject& object __attribute__((unused)))
43  {
44  return {};
45  }
46 
48  virtual std::string info()
49  {
50  return "(no info available)\n";
51  }
52  };
53 
55  template <class AObject>
56  class FixedStyling : public Styling<AObject> {
57 
58  public:
60  AttributeMap map(int index __attribute__((unused)),
61  AObject& object __attribute__((unused))) override
62  {
63  return m_attributeMap;
64  }
65 
67  std::string info() override
68  {
69  std::ostringstream oss;
70  for (const auto& keyValue : m_attributeMap) {
71  oss << keyValue.first << ": " << keyValue.second << '\n';
72  }
73  return oss.str();
74  }
75 
77  void set(const std::string& key, const std::string& value)
78  {
79  m_attributeMap.emplace(key, value);
80  }
81 
83  void setStroke(const std::string& value)
84  {
85  set("stroke", value);
86  }
87 
89  void setStrokeWidth(const std::string& value)
90  {
91  set("stroke-width", value);
92  }
93 
94  private:
96  AttributeMap m_attributeMap;
97  };
98 
100  template <class AObject>
101  class ChooseableStyling : public Styling<AObject> {
102 
103  public:
106 
107  public:
109  AttributeMap map(int index, AObject& object) override
110  {
111  AttributeMap attributeMap;
112  for (const auto& keyMapping : m_mappingsByKey) {
113  const std::string& key = keyMapping.first;
114  const std::unique_ptr<ObjectMapping>& mapping = keyMapping.second;
115  attributeMap[key] = mapping->map(index, object);
116  }
117  return attributeMap;
118  }
119 
121  std::string info() override
122  {
123  std::ostringstream oss;
124  for (const auto& keyMapping : m_mappingsByKey) {
125  const std::string& key = keyMapping.first;
126  const std::unique_ptr<ObjectMapping>& mapping = keyMapping.second;
127  oss << key << ": " << mapping->info();
128  }
129  return oss.str();
130  }
131 
138  virtual std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName)
139  {
140  return std::make_unique<ConstantMapping<AObject>>(mappingName);
141  }
142 
144  void set(const std::string& key, std::unique_ptr<ObjectMapping> mapping)
145  {
146  if (mapping) {
147  m_mappingsByKey[key] = std::move(mapping);
148  }
149  }
150 
152  void set(const std::string& key, const std::string& mappingName)
153  {
154  std::unique_ptr<ObjectMapping> objectMapping = this->createMapping(mappingName);
155  if (objectMapping) {
156  set(key, std::move(objectMapping));
157  }
158  }
159 
165  void setStroke(const std::string& mappingName)
166  {
167  set("stroke", mappingName);
168  }
169 
175  void setStrokeWidth(const std::string& mappingName)
176  {
177  set("stroke-width", mappingName);
178  }
179 
180  protected:
182  std::map<std::string, std::unique_ptr<ObjectMapping> > m_mappingsByKey;
183  };
184 
186  template <class AObject>
187  class DefaultColorCycleStyling : public ChooseableStyling<AObject> {
188  public:
191  {
192  this->set("stroke", std::make_unique<DefaultColorCycleMapping<AObject>>());
193  }
194  };
195 
200  class ChooseableRecoTrackStyling : public ChooseableStyling<const RecoTrack> {
201 
202  private:
205 
207  using Super::ObjectMapping;
208 
209  public:
211  std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName) override;
212  };
213 
218  class ChooseableSegmentStyling : public ChooseableStyling<const CDCSegment2D> {
219 
220  private:
223 
225  using Super::ObjectMapping;
226 
227  public:
229  std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName) override;
230  };
231 
236  class ChooseableHitStyling : public ChooseableStyling<const CDCHit> {
237 
238  private:
241 
243  using Super::ObjectMapping;
244 
245  public:
247  std::unique_ptr<ObjectMapping> createMapping(const std::string& mappingName) override;
248  };
249  }
251 }
Belle2::TrackFindingCDC::FixedStyling::setStroke
void setStroke(const std::string &value)
Legacy - Sets the stroke color to the fixed value.
Definition: Styling.h:91
Belle2::TrackFindingCDC::DefaultColorCycleMapping
Class template for coloring objects in different Colors.
Definition: ColorMapping.h:36
Belle2::TrackFindingCDC::ChooseableStyling::ObjectMapping
Mapping< AObject > ObjectMapping
Mapping for the object type.
Definition: Styling.h:113
Belle2::TrackFindingCDC::ChooseableRecoTrackStyling::createMapping
std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName) override
Method defining the available mapping names.
Definition: Styling.cc:19
Belle2::TrackFindingCDC::ChooseableStyling::set
void set(const std::string &key, std::unique_ptr< ObjectMapping > mapping)
Sets the given attribute to the attribute mapping.
Definition: Styling.h:152
Belle2::TrackFindingCDC::Mapping::info
virtual std::string info()
Informal string summarizing the translation from the object to the attribute value.
Definition: Mapping.h:47
Belle2::TrackFindingCDC::FixedStyling::m_attributeMap
AttributeMap m_attributeMap
Memory for the fixed attribute values.
Definition: Styling.h:104
Belle2::TrackFindingCDC::ChooseableSegmentStyling
This Class handles the mapping from the colormapping-method name given as a string to the actual ACol...
Definition: Styling.h:226
Belle2::TrackFindingCDC::ChooseableStyling::setStrokeWidth
void setStrokeWidth(const std::string &mappingName)
Legacy method to set the mapping on how to match a object to the stroke width.
Definition: Styling.h:183
Belle2::TrackFindingCDC::ChooseableHitStyling::createMapping
std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName) override
Method defining the available mapping names.
Definition: Styling.cc:55
Belle2::TrackFindingCDC::DefaultColorCycleStyling::DefaultColorCycleStyling
DefaultColorCycleStyling()
Constructor. Sets the stroke mapping the the default color cycle.
Definition: Styling.h:198
Belle2::TrackFindingCDC::Styling::info
virtual std::string info()
Informal string summarizing the translation from the object to the styling attributes.
Definition: Styling.h:56
Belle2::TrackFindingCDC::Mapping< AObject >
Belle2::TrackFindingCDC::FixedStyling::map
AttributeMap map(int index __attribute__((unused)), AObject &object __attribute__((unused))) override
Return the fixed attributes on each invocation.
Definition: Styling.h:68
Belle2::TrackFindingCDC::ChooseableStyling
Implementation of a styling composed from several predefined mappings chooseable by their name.
Definition: Styling.h:109
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::ChooseableRecoTrackStyling::Super
ChooseableStyling< const RecoTrack > Super
Type of the base class.
Definition: Styling.h:212
Belle2::TrackFindingCDC::ChooseableSegmentStyling::createMapping
std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName) override
Method defining the available mapping names.
Definition: Styling.cc:33
Belle2::TrackFindingCDC::ChooseableStyling::map
AttributeMap map(int index, AObject &object) override
Create a map of attributes from the stored attribute maps.
Definition: Styling.h:117
Belle2::TrackFindingCDC::ChooseableStyling::m_mappingsByKey
std::map< std::string, std::unique_ptr< ObjectMapping > > m_mappingsByKey
Map of attribute keys to mappings to be used.
Definition: Styling.h:190
Belle2::TrackFindingCDC::FixedStyling::setStrokeWidth
void setStrokeWidth(const std::string &value)
Legacy - Sets the stroke width to the fixed value.
Definition: Styling.h:97
Belle2::TrackFindingCDC::ChooseableRecoTrackStyling
This Class handles the mapping from the colormapping-method name given as a string to the actual colo...
Definition: Styling.h:208
Belle2::TrackFindingCDC::Styling::~Styling
virtual ~Styling()=default
Make destructor of interface virtual.
Belle2::TrackFindingCDC::ChooseableStyling::createMapping
virtual std::unique_ptr< ObjectMapping > createMapping(const std::string &mappingName)
Create a mapping for the object from a name.
Definition: Styling.h:146
Belle2::TrackFindingCDC::ChooseableHitStyling
This Class handles the mapping from the colormapping-method name given as a string to the actual colo...
Definition: Styling.h:244
Belle2::TrackFindingCDC::FixedStyling::set
void set(const std::string &key, const std::string &value)
Sets the given attribute to the fixed value.
Definition: Styling.h:85
Belle2::TrackFindingCDC::DefaultColorCycleStyling
Class template for coloring objects with stroke colors prepared to be the default color cycle.
Definition: Styling.h:195
Belle2::TrackFindingCDC::Styling::map
virtual AttributeMap map(int index __attribute__((unused)), AObject &object __attribute__((unused)))
Maps the object at the given index to attribute values.
Definition: Styling.h:49
Belle2::TrackFindingCDC::FixedStyling::info
std::string info() override
Informal string summarizing the translation from the object to the styling attributes.
Definition: Styling.h:75
Belle2::TrackFindingCDC::Styling
Interface for a mapping of object and an index to styling attributes.
Definition: CDCSVGPlotter.h:30
Belle2::TrackFindingCDC::ChooseableStyling::info
std::string info() override
Returns informal string about the currently set mappings.
Definition: Styling.h:129
Belle2::TrackFindingCDC::ChooseableStyling::setStroke
void setStroke(const std::string &mappingName)
Legacy method to set the mapping on how to match a object to the stroke color.
Definition: Styling.h:173
Belle2::TrackFindingCDC::ChooseableHitStyling::Super
ChooseableStyling< const CDCHit > Super
Type of the base class.
Definition: Styling.h:248
Belle2::TrackFindingCDC::ChooseableSegmentStyling::Super
ChooseableStyling< const CDCSegment2D > Super
Type of the base class.
Definition: Styling.h:230
Belle2::TrackFindingCDC::Mapping::map
virtual std::string map(int index __attribute__((unused)), T &t __attribute__((unused)))
Main function returning an attribute value for an object at the given index.
Definition: Mapping.h:40
Belle2::TrackFindingCDC::FixedStyling
Implementation of a styling from fixed attribute map.
Definition: Styling.h:64