CMS 3D CMS Logo

GEMGeometryValidate.cc
Go to the documentation of this file.
1 /*
2 //\class GEMGeometryValidate
3 
4  Description: GEM 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: 27 Jan 2020
9 */
10 
16 
21 
23 
26 
27 #include <TFile.h>
28 #include <TH1.h>
29 
30 #include <limits>
31 #include <string>
32 #include <type_traits>
33 #include <algorithm>
34 #include <cmath>
35 
36 using namespace std;
37 
38 template <class T>
39 typename enable_if<!numeric_limits<T>::is_integer, bool>::type almost_equal(T x, T y, int ulp) {
40  // the machine epsilon has to be scaled to the magnitude of the values used
41  // and multiplied by the desired precision in ULPs (units in the last place)
42  return abs(x - y) <= numeric_limits<T>::epsilon() * abs(x + y) * ulp
43  // unless the result is subnormal
44  || abs(x - y) < numeric_limits<T>::min();
45 }
46 
47 using namespace edm;
48 
50 public:
51  explicit GEMGeometryValidate(const ParameterSet&);
52  ~GEMGeometryValidate() override {}
53 
54 private:
55  void beginJob() override;
56  void analyze(const edm::Event&, const edm::EventSetup&) override;
57  void endJob() override;
58 
59  void validateGEMChamberGeometry();
60  void validateGEMEtaPartitionGeometry();
61 
62  void compareTransform(const GlobalPoint&, const TGeoMatrix*);
63  void compareShape(const GeomDet*, const float*);
64 
65  float getDistance(const GlobalPoint&, const GlobalPoint&);
66  float getDiff(const float, const float);
67 
68  void makeHistograms(const char*);
69  void makeHistograms2(const char*);
70  void makeHistogram(const string&, vector<float>&);
71 
72  void clearData() {
73  globalDistances_.clear();
74  topWidths_.clear();
75  bottomWidths_.clear();
76  lengths_.clear();
77  thicknesses_.clear();
78  }
79 
80  void clearData2() {
81  nstrips_.clear();
82  pitch_.clear();
83  stripslen_.clear();
84  }
85 
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  : tokGeom_{esConsumes<GEMGeometry, MuonGeometryRecord>(edm::ESInputTag{})},
105  infileName_(iConfig.getUntrackedParameter<string>("infileName", "cmsRecoGeom-2021.root")),
106  outfileName_(iConfig.getUntrackedParameter<string>("outfileName", "validateGEMGeometry.root")),
107  tolerance_(iConfig.getUntrackedParameter<int>("tolerance", 6)) {
108  fwGeometry_.loadMap(infileName_.c_str());
109  outFile_ = TFile::Open(outfileName_.c_str(), "RECREATE");
110 }
111 
113  gemGeometry_ = &eventSetup.getData(tokGeom_);
114 
115  LogVerbatim("GEMGeometry") << "Validating GEM chamber geometry";
116 
118 
120 }
121 
123  clearData();
124 
125  for (auto const& it : gemGeometry_->chambers()) {
126  GEMDetId chId = it->id();
127  GlobalPoint gp = it->surface().toGlobal(LocalPoint(0.0, 0.0, 0.0));
128 
129  const TGeoMatrix* matrix = fwGeometry_.getMatrix(chId.rawId());
130 
131  if (!matrix) {
132  LogVerbatim("GEMGeometry") << "Failed to get matrix of GEM chamber with detid: " << chId.rawId();
133  continue;
134  }
136 
137  auto const& shape = fwGeometry_.getShapePars(chId.rawId());
138 
139  if (!shape) {
140  LogVerbatim("GEMGeometry") << "Failed to get shape of GEM chamber with detid: " << chId.rawId();
141  continue;
142  }
143  compareShape(it, shape);
144  }
145  makeHistograms("GEM Chamber");
146 }
147 
149  clearData2();
150 
151  for (auto const& it : gemGeometry_->etaPartitions()) {
152  GEMDetId chId = it->id();
153  const int n_strips = it->nstrips();
154  const float n_pitch = it->pitch();
155  const StripTopology& topo = it->specificTopology();
156  const float stripLen = topo.stripLength();
157  const float* parameters = fwGeometry_.getParameters(chId.rawId());
158  nstrips_.push_back(abs(n_strips - parameters[0]));
159 
160  if (n_strips) {
161  for (int istrips = 1; istrips <= n_strips; istrips++) {
162  pitch_.push_back(fabs(n_pitch - parameters[2]));
163 
164  stripslen_.push_back(fabs(stripLen - parameters[1]));
165  }
166  } else {
167  LogVerbatim("GEMGeometry") << "ATTENTION! nStrips == 0";
168  }
169  }
170  makeHistograms2("GEM Eta Partition");
171 }
172 
174  double local[3] = {0.0, 0.0, 0.0};
175  double global[3];
176 
177  matrix->LocalToMaster(local, global);
178 
179  float distance = getDistance(GlobalPoint(global[0], global[1], global[2]), gp);
180  if (abs(distance) < 1.0e-7)
181  distance = 0.0; // set a tollerance for the distance inside Histos
182  globalDistances_.push_back(distance);
183 }
184 
185 void GEMGeometryValidate::compareShape(const GeomDet* det, const float* shape) {
186  float shapeTopWidth;
187  float shapeBottomWidth;
188  float shapeLength;
189  float shapeThickness;
190 
191  if (shape[0] == 1) {
192  shapeTopWidth = shape[2];
193  shapeBottomWidth = shape[1];
194  shapeLength = shape[4];
195  shapeThickness = shape[3];
196  } else if (shape[0] == 2) {
197  shapeTopWidth = shape[1];
198  shapeBottomWidth = shape[1];
199  shapeLength = shape[2];
200  shapeThickness = shape[3];
201  } else {
202  LogVerbatim("GEMGeometry") << "Failed to get box or trapezoid from shape";
203 
204  return;
205  }
206 
207  float topWidth, bottomWidth;
208  float length, thickness;
209 
210  const Bounds* bounds = &(det->surface().bounds());
211  if (const TrapezoidalPlaneBounds* tpbs = dynamic_cast<const TrapezoidalPlaneBounds*>(bounds)) {
212  array<const float, 4> const& ps = tpbs->parameters();
213 
214  assert(ps.size() == 4);
215 
216  bottomWidth = ps[0];
217  topWidth = ps[1];
218  thickness = ps[2];
219  length = ps[3];
220  } else if ((dynamic_cast<const RectangularPlaneBounds*>(bounds))) {
221  length = det->surface().bounds().length() * 0.5;
222  topWidth = det->surface().bounds().width() * 0.5;
223  bottomWidth = topWidth;
224  thickness = det->surface().bounds().thickness() * 0.5;
225  } else {
226  LogVerbatim("GEMGeometry") << "Failed to get bounds";
227 
228  return;
229  }
230  topWidths_.push_back(fabs(shapeTopWidth - topWidth));
231  bottomWidths_.push_back(fabs(shapeBottomWidth - bottomWidth));
232  lengths_.push_back(fabs(shapeLength - length));
233  thicknesses_.push_back(fabs(shapeThickness - thickness));
234 }
235 
237  return sqrt((p1.x() - p2.x()) * (p1.x() - p2.x()) + (p1.y() - p2.y()) * (p1.y() - p2.y()) +
238  (p1.z() - p2.z()) * (p1.z() - p2.z()));
239 }
240 
241 float GEMGeometryValidate::getDiff(const float val1, const float val2) {
242  if (almost_equal(val1, val2, tolerance_))
243  return 0.0f;
244  else
245  return (val1 - val2);
246 }
247 
249  outFile_->cd();
250 
251  string d(detector);
252 
253  string gdn = d + ": distance between points in global coordinates";
255 
256  string twn = d + ": absolute difference between top widths (along X)";
258 
259  string bwn = d + ": absolute difference between bottom widths (along X)";
261 
262  string ln = d + ": absolute difference between lengths (along Y)";
263  makeHistogram(ln, lengths_);
264 
265  string tn = d + ": absolute difference between thicknesses (along Z)";
267 }
268 
270  outFile_->cd();
271 
272  string d(detector);
273 
274  string ns = d + ": absolute difference between nStrips";
275  makeHistogram(ns, nstrips_);
276 
277  string pi = d + ": absolute difference between Strips Pitch";
279 
280  string pl = d + ": absolute difference between Strips Length";
282 }
283 
284 void GEMGeometryValidate::makeHistogram(const string& name, vector<float>& data) {
285  if (data.empty())
286  return;
287 
288  const auto [minE, maxE] = minmax_element(begin(data), end(data));
289 
290  TH1D hist(name.c_str(), name.c_str(), 100, *minE * (1 + 0.10), *maxE * (1 + 0.10));
291 
292  for (auto const& it : data)
293  hist.Fill(it);
294 
295  hist.GetXaxis()->SetTitle("[cm]");
296  hist.Write();
297 }
298 
300 
302  LogVerbatim("GEMGeometry") << "Done.";
303  LogVerbatim("GEMGeometry") << "Results written to " << outfileName_;
304  outFile_->Close();
305 }
306 
GEMGeometryValidate::globalDistances_
vector< float > globalDistances_
Definition: GEMGeometryValidate.cc:90
GEMGeometryValidate::clearData
void clearData()
Definition: GEMGeometryValidate.cc:72
GEMGeometryValidate::compareTransform
void compareTransform(const GlobalPoint &, const TGeoMatrix *)
Definition: GEMGeometryValidate.cc:173
GEMGeometryValidate::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: GEMGeometryValidate.cc:112
bk::beginJob
void beginJob()
Definition: Breakpoints.cc:14
HLT_FULL_cff.maxE
maxE
Definition: HLT_FULL_cff.py:13661
BeamSpotPI::parameters
parameters
Definition: BeamSpotPayloadInspectorHelper.h:30
EDAnalyzer.h
edm::ESInputTag
Definition: ESInputTag.h:87
FWGeometry
Definition: FWGeometry.h:27
Bounds::width
virtual float width() const =0
MessageLogger.h
GeomDet
Definition: GeomDet.h:27
makeMuonMisalignmentScenario.matrix
list matrix
Definition: makeMuonMisalignmentScenario.py:141
min
T min(T a, T b)
Definition: MathUtil.h:58
edm
HLT enums.
Definition: AlignableModifier.h:19
GEMGeometryValidate::outfileName_
string outfileName_
Definition: GEMGeometryValidate.cc:99
GEMGeometryValidate::GEMGeometryValidate
GEMGeometryValidate(const ParameterSet &)
Definition: GEMGeometryValidate.cc:103
FWGeometry::getShapePars
const float * getShapePars(unsigned int id) const
Definition: FWGeometry.cc:489
Bounds
Definition: Bounds.h:18
cms::cuda::assert
assert(be >=bs)
GEMGeometryValidate::validateGEMEtaPartitionGeometry
void validateGEMEtaPartitionGeometry()
Definition: GEMGeometryValidate.cc:148
GEMGeometryValidate::makeHistograms2
void makeHistograms2(const char *)
Definition: GEMGeometryValidate.cc:269
GEMGeometryValidate::clearData2
void clearData2()
Definition: GEMGeometryValidate.cc:80
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
StripTopology.h
align::LocalPoint
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
geometryDiff.epsilon
int epsilon
Definition: geometryDiff.py:26
Bounds::length
virtual float length() const =0
GeomDet::surface
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
FWGeometry::getParameters
const float * getParameters(unsigned int id) const
Definition: FWGeometry.cc:478
MakerMacros.h
FWGeometry::getMatrix
const TGeoMatrix * getMatrix(unsigned int id) const
Definition: FWGeometry.cc:225
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TrapezoidalPlaneBounds.h
Calorimetry_cff.thickness
thickness
Definition: Calorimetry_cff.py:115
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
GEMGeometryValidate::bottomWidths_
vector< float > bottomWidths_
Definition: GEMGeometryValidate.cc:92
Surface::bounds
const Bounds & bounds() const
Definition: Surface.h:87
GEMGeometryValidate::compareShape
void compareShape(const GeomDet *, const float *)
Definition: GEMGeometryValidate.cc:185
mps_fire.end
end
Definition: mps_fire.py:242
p2
double p2[4]
Definition: TauolaWrapper.h:90
RectangularPlaneBounds.h
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
Point3DBase< float, GlobalTag >
CSCSkim_cfi.makeHistograms
makeHistograms
Definition: CSCSkim_cfi.py:15
GEMGeometryValidate::getDistance
float getDistance(const GlobalPoint &, const GlobalPoint &)
Definition: GEMGeometryValidate.cc:236
GEMGeometryValidate::getDiff
float getDiff(const float, const float)
Definition: GEMGeometryValidate.cc:241
GEMGeometryValidate::outFile_
TFile * outFile_
Definition: GEMGeometryValidate.cc:89
runTauDisplay.gp
gp
Definition: runTauDisplay.py:431
FWGeometry.h
Bounds::thickness
virtual float thickness() const =0
StripTopology::stripLength
virtual float stripLength() const =0
edm::ParameterSet
Definition: ParameterSet.h:47
GEMGeometry::chambers
const std::vector< const GEMChamber * > & chambers() const
Return a vector of all GEM chambers.
Definition: GEMGeometry.cc:38
GEMGeometryValidate::thicknesses_
vector< float > thicknesses_
Definition: GEMGeometryValidate.cc:94
GEMGeometryValidate::pitch_
vector< float > pitch_
Definition: GEMGeometryValidate.cc:96
type
type
Definition: SiPixelVCal_PayloadInspector.cc:39
gpuVertexFinder::hist
__shared__ Hist hist
Definition: gpuClusterTracksDBSCAN.h:48
GEMDetId
Definition: GEMDetId.h:18
GEMGeometryValidate::lengths_
vector< float > lengths_
Definition: GEMGeometryValidate.cc:93
GEMGeometryValidate::makeHistograms
void makeHistograms(const char *)
Definition: GEMGeometryValidate.cc:248
GEMGeometryValidate::beginJob
void beginJob() override
Definition: GEMGeometryValidate.cc:299
almost_equal
enable_if<!numeric_limits< T >::is_integer, bool >::type almost_equal(T x, T y, int ulp)
Definition: GEMGeometryValidate.cc:39
GEMGeometryValidate::infileName_
string infileName_
Definition: GEMGeometryValidate.cc:98
GEMGeometryValidate
Definition: GEMGeometryValidate.cc:49
GEMGeometryValidate::~GEMGeometryValidate
~GEMGeometryValidate() override
Definition: GEMGeometryValidate.cc:52
p1
double p1[4]
Definition: TauolaWrapper.h:89
GEMGeometryValidate::fwGeometry_
FWGeometry fwGeometry_
Definition: GEMGeometryValidate.cc:88
analyze
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
GEMGeometryValidate::tokGeom_
const edm::ESGetToken< GEMGeometry, MuonGeometryRecord > tokGeom_
Definition: GEMGeometryValidate.cc:86
edm::EventSetup
Definition: EventSetup.h:58
GEMChamber.h
TrapezoidalPlaneBounds
Definition: TrapezoidalPlaneBounds.h:15
edm::ESGetToken< GEMGeometry, MuonGeometryRecord >
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
GEMGeometryValidate::validateGEMChamberGeometry
void validateGEMChamberGeometry()
Definition: GEMGeometryValidate.cc:122
GEMGeometry.h
std
Definition: JetResolutionObject.h:76
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
GEMGeometryValidate::gemGeometry_
const GEMGeometry * gemGeometry_
Definition: GEMGeometryValidate.cc:87
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
T
long double T
Definition: Basic3DVectorLD.h:48
GEMGeometryValidate::nstrips_
vector< float > nstrips_
Definition: GEMGeometryValidate.cc:95
GEMGeometryValidate::endJob
void endJob() override
Definition: GEMGeometryValidate.cc:301
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
GEMGeometryValidate::stripslen_
vector< float > stripslen_
Definition: GEMGeometryValidate.cc:97
GEMGeometry::etaPartitions
const std::vector< const GEMEtaPartition * > & etaPartitions() const
Return a vector of all GEM eta partitions.
Definition: GEMGeometry.cc:40
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
hgcalTestNeighbor_cfi.detector
detector
Definition: hgcalTestNeighbor_cfi.py:6
GEMGeometry
Definition: GEMGeometry.h:24
ztail.d
d
Definition: ztail.py:151
pi
const Double_t pi
Definition: trackSplitPlot.h:36
DTRecHitClients_cfi.local
local
Definition: DTRecHitClients_cfi.py:10
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
GEMGeometryValidate::topWidths_
vector< float > topWidths_
Definition: GEMGeometryValidate.cc:91
MuonGeometryRecord.h
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
HLT_FULL_cff.distance
distance
Definition: HLT_FULL_cff.py:7746
GEMGeometryValidate::makeHistogram
void makeHistogram(const string &, vector< float > &)
Definition: GEMGeometryValidate.cc:284
StripTopology
Definition: StripTopology.h:11
GEMGeometryValidate::tolerance_
int tolerance_
Definition: GEMGeometryValidate.cc:100
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37