CMS 3D CMS Logo

RPCGeometryValidate.cc
Go to the documentation of this file.
1 /*
2 //\class RPCGeometryValidate
3 
4  Description: RPC GeometryValidate from DD & DD4hep
5 
6 //
7 // Author: Sergio Lo Meo (sergio.lo.meo@cern.ch) following what Ianna Osburne made for DTs (DD4HEP migration)
8 // Created: Fri, 20 Sep 2019
9 */
10 
17 
23 
25 
28 
29 #include <TFile.h>
30 #include <TH1.h>
31 
32 #include <limits>
33 #include <string>
34 #include <type_traits>
35 #include <algorithm>
36 
37 using namespace std;
38 
39 template <class T>
40 typename enable_if<!numeric_limits<T>::is_integer, bool>::type almost_equal(T x, T y, int ulp) {
41  // the machine epsilon has to be scaled to the magnitude of the values used
42  // and multiplied by the desired precision in ULPs (units in the last place)
43  return abs(x - y) <= numeric_limits<T>::epsilon() * abs(x + y) * ulp
44  // unless the result is subnormal
45  || abs(x - y) < numeric_limits<T>::min();
46 }
47 
48 using namespace edm;
49 
50 class RPCGeometryValidate : public one::EDAnalyzer<> {
51 public:
52  explicit RPCGeometryValidate(const ParameterSet&);
53  ~RPCGeometryValidate() override {}
54 
55 private:
56  void beginJob() override;
57  void analyze(const edm::Event&, const edm::EventSetup&) override;
58  void endJob() override;
59 
60  void validateRPCChamberGeometry();
61  void validateRPCStripsGeometry();
62 
63  void compareTransform(const GlobalPoint&, const TGeoMatrix*);
64  void compareShape(const GeomDet*, const float*);
65 
66  float getDistance(const GlobalPoint&, const GlobalPoint&);
67  float getDiff(const float, const float);
68 
69  void makeHistograms(const char*);
70  void makeHistograms2(const char*);
71  void makeHistogram(const string&, vector<float>&);
72 
73  void clearData() {
74  globalDistances_.clear();
75  topWidths_.clear();
76  bottomWidths_.clear();
77  lengths_.clear();
78  thicknesses_.clear();
79  }
80 
81  void clearData2() {
82  nstrips_.clear();
83  pitch_.clear();
84  stripslen_.clear();
85  }
86 
89  TFile* outFile_;
90  vector<float> globalDistances_;
91  vector<float> topWidths_;
92  vector<float> bottomWidths_;
93  vector<float> lengths_;
94  vector<float> thicknesses_;
95  vector<float> nstrips_;
96  vector<float> pitch_;
97  vector<float> stripslen_;
98  string infileName_;
99  string outfileName_;
101 };
102 
104  : infileName_(iConfig.getUntrackedParameter<string>("infileName", "cmsGeom10.root")),
105  outfileName_(iConfig.getUntrackedParameter<string>("outfileName", "validateRPCGeometry.root")),
106  tolerance_(iConfig.getUntrackedParameter<int>("tolerance", 6)) {
108  outFile_ = new TFile(outfileName_.c_str(), "RECREATE");
109 }
110 
112  eventSetup.get<MuonGeometryRecord>().get(rpcGeometry_);
113  if (rpcGeometry_.isValid()) {
114  LogVerbatim("RPCGeometry") << "Validating RPC chamber geometry";
117  } else
118  LogVerbatim("RPCGeometry") << "Invalid RPC geometry";
119 }
120 
122  clearData();
123 
124  for (auto const& it : rpcGeometry_->rolls()) {
125  RPCDetId chId = it->id();
126  GlobalPoint gp = it->surface().toGlobal(LocalPoint(0.0, 0.0, 0.0));
127 
128  const TGeoMatrix* matrix = fwGeometry_.getMatrix(chId.rawId());
129 
130  if (!matrix) {
131  LogVerbatim("RPCGeometry") << "Failed to get matrix of RPC chamber with detid: " << chId.rawId();
132  continue;
133  }
134  compareTransform(gp, matrix);
135 
136  auto const& shape = fwGeometry_.getShapePars(chId.rawId());
137 
138  if (!shape) {
139  LogVerbatim("RPCGeometry") << "Failed to get shape of RPC chamber with detid: " << chId.rawId();
140  continue;
141  }
142  compareShape(it, shape);
143  }
144  makeHistograms("RPC Chamber");
145 }
146 
148  clearData2();
149 
150  for (auto const& it : rpcGeometry_->rolls()) {
151  RPCDetId chId = it->id();
152  const int n_strips = it->nstrips();
153  const float n_pitch = it->pitch();
154  const StripTopology& topo = it->specificTopology();
155  const float stripLen = topo.stripLength();
156  const float* parameters = fwGeometry_.getParameters(chId.rawId());
157 
158  if (n_strips) {
159  for (int istrips = 1; istrips <= n_strips; istrips++) {
160  nstrips_.push_back(fabs(n_strips - parameters[0]));
161  pitch_.push_back(fabs(n_pitch - parameters[2]));
162  stripslen_.push_back(fabs(stripLen - parameters[1]));
163  }
164  } else {
165  LogVerbatim("RPCGeometry") << "ATTENTION! nStrips == 0";
166  }
167  }
168  makeHistograms2("RPC Strips");
169 }
170 
172  double local[3] = {0.0, 0.0, 0.0};
173  double global[3];
174 
175  matrix->LocalToMaster(local, global);
176 
177  float distance = getDistance(GlobalPoint(global[0], global[1], global[2]), gp);
178  if ((distance >= 0.0) && (distance < 1.0e-7))
179  distance = 0.0; // set a tollerance for the distance inside Histos
180  globalDistances_.push_back(distance);
181 }
182 
183 void RPCGeometryValidate::compareShape(const GeomDet* det, const float* shape) {
184  float shapeTopWidth;
185  float shapeBottomWidth;
186  float shapeLength;
187  float shapeThickness;
188 
189  if (shape[0] == 1) {
190  shapeTopWidth = shape[2];
191  shapeBottomWidth = shape[1];
192  shapeLength = shape[4];
193  shapeThickness = shape[3];
194  } else if (shape[0] == 2) {
195  shapeTopWidth = shape[1];
196  shapeBottomWidth = shape[1];
197  shapeLength = shape[2];
198  shapeThickness = shape[3];
199  } else {
200  LogVerbatim("RPCGeometry") << "Failed to get box or trapezoid from shape";
201 
202  return;
203  }
204 
205  float topWidth, bottomWidth;
206  float length, thickness;
207 
208  const Bounds* bounds = &(det->surface().bounds());
209  if (const TrapezoidalPlaneBounds* tpbs = dynamic_cast<const TrapezoidalPlaneBounds*>(bounds)) {
210  array<const float, 4> const& ps = tpbs->parameters();
211 
212  assert(ps.size() == 4);
213 
214  bottomWidth = ps[0];
215  topWidth = ps[1];
216  thickness = ps[2];
217  length = ps[3];
218  } else if ((dynamic_cast<const RectangularPlaneBounds*>(bounds))) {
219  length = det->surface().bounds().length() * 0.5;
220  topWidth = det->surface().bounds().width() * 0.5;
221  bottomWidth = topWidth;
222  thickness = det->surface().bounds().thickness() * 0.5;
223  } else {
224  LogVerbatim("RPCGeometry") << "Failed to get bounds";
225 
226  return;
227  }
228  topWidths_.push_back(fabs(shapeTopWidth - topWidth));
229  bottomWidths_.push_back(fabs(shapeBottomWidth - bottomWidth));
230  lengths_.push_back(fabs(shapeLength - length));
231  thicknesses_.push_back(fabs(shapeThickness - thickness));
232 }
233 
235  return sqrt((p1.x() - p2.x()) * (p1.x() - p2.x()) + (p1.y() - p2.y()) * (p1.y() - p2.y()) +
236  (p1.z() - p2.z()) * (p1.z() - p2.z()));
237 }
238 
239 float RPCGeometryValidate::getDiff(const float val1, const float val2) {
240  if (almost_equal(val1, val2, tolerance_))
241  return 0.0f;
242  else
243  return (val1 - val2);
244 }
245 
247  outFile_->cd();
248 
249  string d(detector);
250 
251  string gdn = d + ": distance between points in global coordinates";
253 
254  string twn = d + ": absolute difference between top widths (along X)";
256 
257  string bwn = d + ": absolute difference between bottom widths (along X)";
259 
260  string ln = d + ": absolute difference between lengths (along Y)";
261  makeHistogram(ln, lengths_);
262 
263  string tn = d + ": absolute difference between thicknesses (along Z)";
265 }
266 
268  outFile_->cd();
269 
270  string d(detector);
271 
272  string ns = d + ": absolute difference between nStrips";
273  makeHistogram(ns, nstrips_);
274 
275  string pi = d + ": absolute difference between Strips Pitch";
276  makeHistogram(pi, pitch_);
277 
278  string pl = d + ": absolute difference between Strips Length";
280 }
281 
282 void RPCGeometryValidate::makeHistogram(const string& name, vector<float>& data) {
283  if (data.empty())
284  return;
285 
286  const auto [minE, maxE] = minmax_element(begin(data), end(data));
287 
288  TH1D hist(name.c_str(), name.c_str(), 100, *minE * (1 + 0.10), *maxE * (1 + 0.10));
289 
290  for (auto const& it : data)
291  hist.Fill(it);
292 
293  hist.GetXaxis()->SetTitle("[cm]");
294  hist.Write();
295 }
296 
298 
300  LogVerbatim("RPCGeometry") << "Done.";
301  LogVerbatim("RPCGeometry") << "Results written to " << outfileName_;
302  outFile_->Close();
303 }
304 
type
Definition: HCALResponse.h:21
vector< float > bottomWidths_
virtual float stripLength() const =0
virtual float length() const =0
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
const float * getParameters(unsigned int id) const
Definition: FWGeometry.cc:474
const std::vector< const RPCRoll * > & rolls() const
Return a vector of all RPC rolls.
Definition: RPCGeometry.cc:44
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
T y() const
Definition: PV3DBase.h:60
const Bounds & bounds() const
Definition: Surface.h:89
void makeHistograms(const char *)
enable_if<!numeric_limits< T >::is_integer, bool >::type almost_equal(T x, T y, int ulp)
const TGeoMatrix * getMatrix(unsigned int id) const
Definition: FWGeometry.cc:221
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
vector< float > nstrips_
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
vector< float > stripslen_
void beginJob()
Definition: Breakpoints.cc:14
const Double_t pi
vector< float > topWidths_
virtual float width() const =0
const float * getShapePars(unsigned int id) const
Definition: FWGeometry.cc:485
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void loadMap(const char *fileName)
Definition: FWGeometry.cc:99
T sqrt(T t)
Definition: SSEVec.h:19
float getDistance(const GlobalPoint &, const GlobalPoint &)
vector< float > thicknesses_
T z() const
Definition: PV3DBase.h:61
void makeHistogram(const string &, vector< float > &)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define end
Definition: vmac.h:39
T min(T a, T b)
Definition: MathUtil.h:58
double p2[4]
Definition: TauolaWrapper.h:90
d
Definition: ztail.py:151
void compareTransform(const GlobalPoint &, const TGeoMatrix *)
RPCGeometryValidate(const ParameterSet &)
vector< float > globalDistances_
vector< float > lengths_
virtual float thickness() const =0
float getDiff(const float, const float)
#define begin
Definition: vmac.h:32
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
double p1[4]
Definition: TauolaWrapper.h:89
T get() const
Definition: EventSetup.h:73
edm::ESHandle< RPCGeometry > rpcGeometry_
Definition: Bounds.h:20
bool isValid() const
Definition: ESHandle.h:44
void makeHistograms2(const char *)
long double T
T x() const
Definition: PV3DBase.h:59
void analyze(const edm::Event &, const edm::EventSetup &) override
void compareShape(const GeomDet *, const float *)
Definition: event.py:1