Belle II Software  release-08-01-10
SVDAPVHistograms.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #pragma once
10 #include <vxd/dataobjects/VxdID.h>
11 #include <vxd/geometry/GeoCache.h>
12 #include <vxd/geometry/SensorInfoBase.h>
13 #include <string>
14 #include <regex>
15 
16 namespace Belle2 {
23  template < class H > // H is an histogram
24  class SVDAPVHistograms: public TObject {
25 
26  public:
29  SVDAPVHistograms(H()) {};
30 
32  explicit SVDAPVHistograms(const H& templateAPV);
33 
36 
39  enum E_side { VIndex = 0, UIndex = 1 };
40 
44  H* getHistogram(const VxdID& vxdID, int view, int apv)
45  {
46  H* returnValue = m_defaultHistogram;
47  try {
48  auto layer = m_histograms.at(vxdID.getLayerNumber());
49  auto ladder = layer.at(vxdID.getLadderNumber());
50  auto sensor = ladder.at(vxdID.getSensorNumber());
51  auto theView = sensor.at(view);
52  returnValue = theView.at(apv);
53  } catch (...) {
54  B2WARNING("Unexpected VxdID/view/apv. VxdID: " << (std::string)(vxdID)
55  << " view : " << view
56  << " apv : " << apv);
57 
58  returnValue = m_defaultHistogram;
59  }
60 
61  return returnValue;
62  }
63 
64  // variable number of arguments for TH1 TH2...
70  template< class ... Types>
71  void fill(const VxdID& vxdID, int view, int apv, Types ... args)
72  {
73  getHistogram(vxdID, view, apv)->Fill(args...);
74  }
75 
76  // variable number of arguments for TH1 TH2...
82  template< class ... Types>
83  void fill(const VxdID& vxdID, bool isU, int apv, Types ... args)
84  {
85  int view = isU ? UIndex : VIndex;
86  getHistogram(vxdID, view, apv)->Fill(args...);
87  }
88 
90  void customizeString(std::string& base, const VxdID& vxdID, bool isU, int user_apv)
91  {
92  std::string layer = std::to_string(vxdID.getLayerNumber());
93  std::string ladder = std::to_string(vxdID.getLadderNumber());
94  std::string sensor = std::to_string(vxdID.getSensorNumber());
95  std::string apv = std::to_string(user_apv);
96  std::string view = isU ? "U" : "V" ;
97  base = std::regex_replace(base, std::regex("[@]layer"), layer);
98  base = std::regex_replace(base, std::regex("[@]ladder"), ladder);
99  base = std::regex_replace(base, std::regex("[@]sensor"), sensor);
100  base = std::regex_replace(base, std::regex("[@]view"), view);
101  std::string side = isU ? "P" : "N" ;
102  base = std::regex_replace(base, std::regex("[@]side"), side);
103  base = std::regex_replace(base, std::regex("[@]apv"), apv);
104  }
105 
107  void clean()
108  {
109 
110  for (auto layer : m_histograms)
111  for (auto ladder : layer)
112  for (auto sensor : ladder)
113  for (auto view : sensor)
114  for (auto apv : view)
115  delete apv;
116  }
117 
118 
119 
120 
121  private:
128  typedef std::vector< H* > t_Views;
130  typedef std::vector< t_Views > t_SVDSensor;
133  typedef std::vector< t_SVDSensor > t_SVDLadder;
136  typedef std::vector< t_SVDLadder > t_SVDLayer;
139  typedef std::vector< t_SVDLayer > t_SVD;
144  void customize(H& histogram, VxdID vxdID, int view, int apv);
147  };
148 
150  template <class H>
152  {
153  m_defaultHistogram = new H(templateAPV);
154 
156  for (auto layer : geoCache.getLayers(VXD::SensorInfoBase::SVD)) {
157  unsigned int layerNumber = layer.getLayerNumber();
158  if (m_histograms.size() <= layerNumber)
159  m_histograms.resize(layerNumber + 1);
160 
161  for (auto ladder : geoCache.getLadders(layer)) {
162  unsigned int ladderNumber = ladder.getLadderNumber();
163  if (m_histograms[layerNumber].size() <= ladderNumber)
164  m_histograms[layerNumber].resize(ladderNumber + 1);
165 
166  for (Belle2::VxdID sensor : geoCache.getSensors(ladder)) {
167  unsigned int sensorNumber = sensor.getSensorNumber();
168  if (m_histograms[layerNumber][ladderNumber].size() <= sensorNumber)
169  m_histograms[layerNumber][ladderNumber].resize(sensorNumber + 1);
170  m_histograms[layerNumber][ladderNumber][sensorNumber].resize(2);
171 
172  for (int view = VIndex ; view < UIndex + 1; view++) {
173  int nAPV = 6;
174  unsigned int viewNumber = 3;
175 
176  if (m_histograms[layerNumber][ladderNumber][view].size() < viewNumber)
177  m_histograms[layerNumber][ladderNumber][sensorNumber].resize(viewNumber);
178  m_histograms[layerNumber][ladderNumber][sensorNumber][view].resize(nAPV);
179  for (int apv = 0; apv < nAPV; apv ++) {
180 
181  H h = templateAPV;
182  customize(h, sensor, view, apv);
183  m_histograms[layerNumber][ladderNumber][sensorNumber][view][apv] = new H(h);
184  }
185  }
186  }
187  }
188  }
189  }
190 
192  template < class H >
193  void SVDAPVHistograms<H>::customize(H& histogram, VxdID vxdID, int view, int apv)
194  {
195  bool isU = view == UIndex;
196  std::string name = histogram.GetName();
197  customizeString(name, vxdID, isU, apv);
198  histogram.SetName(name.c_str());
199 
200  std::string title = histogram.GetTitle();
201  customizeString(title, vxdID, isU, apv);
202  histogram.SetTitle(title.c_str());
203 
204  std::string xAxis = histogram.GetXaxis()->GetTitle();
205  customizeString(xAxis, vxdID, isU, apv);
206  histogram.SetXTitle(xAxis.c_str());
207 
208  std::string yAxis = histogram.GetYaxis()->GetTitle();
209  customizeString(yAxis, vxdID, isU, apv);
210  histogram.SetYTitle(yAxis.c_str());
211 
212  std::string zAxis = histogram.GetZaxis()->GetTitle();
213  customizeString(zAxis, vxdID, isU, apv);
214  histogram.SetZTitle(zAxis.c_str());
215 
216  }
218 }
template class for the APV Histograms
t_SVD m_histograms
the vector of vector ...
~SVDAPVHistograms()
clean everything in the destructor
void clean()
delete pointers
std::vector< H * > t_Views
A t_SVDView is a vector of H that will have length = # APV chips.
std::vector< t_Views > t_SVDSensor
a vector of vector of H, length = 2
H * getHistogram(const VxdID &vxdID, int view, int apv)
get a reference to the histogram for
void customizeString(std::string &base, const VxdID &vxdID, bool isU, int user_apv)
replaces layer ladder sensor view and apv with the current numbers
std::vector< t_SVDLadder > t_SVDLayer
A t_SVDLayer is a vector of t_SVDLadders.
ClassDef(SVDAPVHistograms, 1)
needed by root
void fill(const VxdID &vxdID, int view, int apv, Types ... args)
fill the histogram for
std::vector< t_SVDLayer > t_SVD
The t_SVD is a vector of t_SVDLayers.
H * m_defaultHistogram
the default histogram
SVDAPVHistograms()
Default constructor.
std::vector< t_SVDSensor > t_SVDLadder
A t_SVDLadder is a vector of t_SVDSensors.
E_side
This enumeration assure the same semantic of the isU methods defined by Peter Kv.
void fill(const VxdID &vxdID, bool isU, int apv, Types ... args)
fill the histogram for
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
Definition: GeoCache.h:39
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:214
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:33
baseType getSensorNumber() const
Get the sensor id.
Definition: VxdID.h:100
baseType getLadderNumber() const
Get the ladder id.
Definition: VxdID.h:98
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:96
SVDAPVHistograms(const H &templateAPV)
Use.
void customize(H &histogram, VxdID vxdID, int view, int apv)
customize the histogram with the sensor, view and APV numbers
Abstract base class for different kinds of events.