Belle II Software  release-05-02-19
SVDHistograms.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Eugenio Paoloni, Giulia Casarosa, Ludovico Massaccesi *
7  * *
8  * This software is provided "as is" without any warranty. *
9  * WARNING! Have the potential to cause addiction, but not always... *
10  **************************************************************************/
11 
12 #pragma once
13 #include <vxd/dataobjects/VxdID.h>
14 #include <vxd/geometry/GeoCache.h>
15 #include <vxd/geometry/SensorInfoBase.h>
16 #include <string>
17 #include <regex>
18 
19 namespace Belle2 {
26  template < class H > // H is an histogram
27  class SVDHistograms: public TObject {
28 
29  public:
31  SVDHistograms():
32  SVDHistograms(H(), H(), H() , H()) {};
35  explicit SVDHistograms(const H& templateHisto):
36  SVDHistograms(templateHisto, templateHisto,
37  templateHisto, templateHisto)
38  {};
39 
46  SVDHistograms(const H& templateU3, const H& templateV3,
47  const H& templateU456, const H& templateV456);
48 
50  ~SVDHistograms() { clean(); };
51 
54  enum E_side { VIndex = 0 , UIndex = 1 };
55 
59  H* getHistogram(const VxdID& vxdID, int view)
60  {
61  H* returnValue = m_defaultHistogram;
62  try {
63  auto& layer = m_histograms.at(vxdID.getLayerNumber());
64  auto& ladder = layer.at(vxdID.getLadderNumber());
65  auto& sensor = ladder.at(vxdID.getSensorNumber());
66  returnValue = sensor.at(view);
67  } catch (...) {
68  B2WARNING("Unexpected VxdID /view. VxdID: " << (std::string)(vxdID)
69  << " view : " << view);
70 
71  returnValue = m_defaultHistogram;
72  }
73 
74  return returnValue;
75  }
76 
77  // variable number of arguments for TH1 TH2...
79  template< class ... Types>
80  void fill(const VxdID& vxdID, int view, Types ... args)
81  {
82  getHistogram(vxdID, view)->Fill(args...);
83  }
84 
85  // variable number of arguments for TH1 TH2...
87  template< class ... Types>
88  void fill(const VxdID& vxdID, bool isU, Types ... args)
89  {
90  int view = isU ? UIndex : VIndex;
91  getHistogram(vxdID, view)->Fill(args...);
92  }
93 
95  void customizeString(std::string& base, const VxdID& vxdID, bool isU)
96  {
97  std::string layer = std::to_string(vxdID.getLayerNumber());
98  std::string ladder = std::to_string(vxdID.getLadderNumber());
99  std::string sensor = std::to_string(vxdID.getSensorNumber());
100  std::string view = isU ? "U" : "V" ;
101  base = std::regex_replace(base, std::regex("[@]layer") , layer);
102  base = std::regex_replace(base, std::regex("[@]ladder"), ladder);
103  base = std::regex_replace(base, std::regex("[@]sensor"), sensor);
104  base = std::regex_replace(base, std::regex("[@]view") , view);
105  std::string side = isU ? "P" : "N" ;
106  base = std::regex_replace(base, std::regex("[@]side") , side);
107  }
108 
110  void clean()
111  {
112 
113  for (auto& layer : m_histograms)
114  for (auto& ladder : layer)
115  for (auto& sensor : ladder)
116  for (auto& view : sensor)
117  delete view;
118  }
119 
121  void reset()
122  {
123  for (auto& layer : m_histograms)
124  for (auto& ladder : layer)
125  for (auto& sensor : ladder)
126  for (auto& view : sensor)
127  view->Reset();
128  }
129 
130 
131  private:
132  // A t_SVDSensor is a vector of H that will have length 2.
133  // Index 0 for the V side, index 1 for the U side
134  // Please, please, pleaseeeee use SVDHistograms<...>::UIndex
135  // and SVDHistograms<...>::VIndex instead of 1 and 0 for better
136  // code readibility
137  typedef std::vector< H* > t_SVDSensor;
139  // A t_SVDLadder is a vector of t_SVDSensors
140  typedef std::vector< t_SVDSensor > t_SVDLadder;
142  // A t_SVDLayer is a vector of t_SVDLAdders
143  typedef std::vector< t_SVDLadder > t_SVDLayer;
145  // The t_SVD is a vector of t_SVDLayers
146  typedef std::vector< t_SVDLayer > t_SVD;
152  void customize(H& histogram, VxdID vxdID, int view);
154  ClassDef(SVDHistograms , 1);
155  };
156 
158  template <class H>
159  SVDHistograms<H>::SVDHistograms(const H& templateU3, const H& templateV3,
160  const H& templateU456, const H& templateV456)
161  {
162  m_defaultHistogram = new H(templateU3);
163 
165  for (auto layer : geoCache.getLayers(VXD::SensorInfoBase::SVD)) {
166  unsigned int layerNumber = layer.getLayerNumber();
167  if (m_histograms.size() <= layerNumber)
168  m_histograms.resize(layerNumber + 1);
169 
170  for (auto ladder : geoCache.getLadders(layer)) {
171  unsigned int ladderNumber = ladder.getLadderNumber();
172  if (m_histograms[layerNumber].size() <= ladderNumber)
173  m_histograms[layerNumber].resize(ladderNumber + 1);
174 
175  for (Belle2::VxdID sensor : geoCache.getSensors(ladder)) {
176  unsigned int sensorNumber = sensor.getSensorNumber();
177  if (m_histograms[layerNumber][ladderNumber].size() <= sensorNumber)
178  m_histograms[layerNumber][ladderNumber].resize(sensorNumber + 1);
179  m_histograms[layerNumber][ladderNumber][sensorNumber].resize(2);
180 
181  for (int view = VIndex ; view < UIndex + 1; view++) {
182  H h = layerNumber == 3 && view == UIndex ? templateU3 :
183  layerNumber == 3 && view == VIndex ? templateV3 :
184  view == UIndex ? templateU456 : templateV456 ;
185  customize(h , sensor, view);
186  m_histograms[layerNumber][ladderNumber][sensorNumber][view] = new H(h);
187  }
188  }
189  }
190  }
191  }
192 
194  template < class H >
195  void SVDHistograms<H>::customize(H& histogram, VxdID vxdID, int view)
196  {
197  bool isU = view == UIndex;
198  std::string name = histogram.GetName();
199  customizeString(name, vxdID, isU);
200  histogram.SetName(name.c_str());
201 
202  std::string title = histogram.GetTitle();
203  customizeString(title, vxdID, isU);
204  histogram.SetTitle(title.c_str());
205 
206  std::string xAxis = histogram.GetXaxis()->GetTitle();
207  customizeString(xAxis, vxdID, isU);
208  histogram.SetXTitle(xAxis.c_str());
209 
210  std::string yAxis = histogram.GetYaxis()->GetTitle();
211  customizeString(yAxis, vxdID, isU);
212  histogram.SetYTitle(yAxis.c_str());
213 
214  std::string zAxis = histogram.GetZaxis()->GetTitle();
215  customizeString(zAxis, vxdID, isU);
216  histogram.SetZTitle(zAxis.c_str());
217 
218  }
220 }
Belle2::SVDHistograms::~SVDHistograms
~SVDHistograms()
clean everything in the destructor
Definition: SVDHistograms.h:59
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43
Belle2::SVDHistograms
template class for SVd histograms
Definition: SVDHistograms.h:36
Belle2::SVDHistograms::t_SVDLadder
std::vector< t_SVDSensor > t_SVDLadder
a vector of vector of H, length = # svd sensors
Definition: SVDHistograms.h:149
Belle2::SVDHistograms::m_defaultHistogram
H * m_defaultHistogram
the default histogram
Definition: SVDHistograms.h:158
Belle2::VxdID::getLadderNumber
baseType getLadderNumber() const
Get the ladder id.
Definition: VxdID.h:108
Belle2::SVDHistograms::SVDHistograms
SVDHistograms()
Default constructor.
Definition: SVDHistograms.h:40
Belle2::SVDHistograms::reset
void reset()
Call Reset() on all histograms.
Definition: SVDHistograms.h:130
Belle2::SVDHistograms::clean
void clean()
delete pointers
Definition: SVDHistograms.h:119
Belle2::SVDHistograms::t_SVDLayer
std::vector< t_SVDLadder > t_SVDLayer
a vector of vector of vector of H, length = # ladders
Definition: SVDHistograms.h:152
Belle2::SVDHistograms::ClassDef
ClassDef(SVDHistograms, 1)
needed by root
Belle2::SVDHistograms::t_SVDSensor
std::vector< H * > t_SVDSensor
a vector of H, length = 2
Definition: SVDHistograms.h:146
Belle2::VXD::GeoCache::getInstance
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:215
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::VXD::SensorInfoBase::SVD
@ SVD
SVD Sensor.
Definition: SensorInfoBase.h:45
Belle2::VxdID::getSensorNumber
baseType getSensorNumber() const
Get the sensor id.
Definition: VxdID.h:110
Belle2::SVDHistograms::fill
void fill(const VxdID &vxdID, int view, Types ... args)
fill the histogram for
Definition: SVDHistograms.h:89
Belle2::VXD::GeoCache
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
Definition: GeoCache.h:41
Belle2::SVDHistograms::E_side
E_side
This enumeration assure the same semantic of the isU methods defined by Peter Kv.
Definition: SVDHistograms.h:63
Belle2::SVDHistograms::getHistogram
H * getHistogram(const VxdID &vxdID, int view)
get a reference to the histogram for
Definition: SVDHistograms.h:68
Belle2::SVDHistograms::customize
void customize(H &histogram, VxdID vxdID, int view)
customize the histogram with the sensor, view
Definition: SVDHistograms.h:204
Belle2::SVDHistograms::customizeString
void customizeString(std::string &base, const VxdID &vxdID, bool isU)
replaces layer ladder sensor view and apv with the current numbers
Definition: SVDHistograms.h:104
Belle2::VxdID::getLayerNumber
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:106
Belle2::SVDHistograms::t_SVD
std::vector< t_SVDLayer > t_SVD
a vector of vector of vector of vector of H, length = # layers
Definition: SVDHistograms.h:155
Belle2::SVDHistograms::m_histograms
t_SVD m_histograms
the vector of vector ...
Definition: SVDHistograms.h:157