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 
17 
22 
24 
27 
28 #include <TFile.h>
29 #include <TH1.h>
30 
31 #include <limits>
32 #include <string>
33 #include <type_traits>
34 #include <algorithm>
35 #include <cmath>
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 
51 public:
52  explicit GEMGeometryValidate(const ParameterSet&);
53  ~GEMGeometryValidate() 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 validateGEMChamberGeometry();
61  void validateGEMEtaPartitionGeometry();
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", "cmsRecoGeom-2021.root")),
105  outfileName_(iConfig.getUntrackedParameter<string>("outfileName", "validateGEMGeometry.root")),
106  tolerance_(iConfig.getUntrackedParameter<int>("tolerance", 6)) {
108  outFile_ = TFile::Open(outfileName_.c_str(), "RECREATE");
109 }
110 
112  eventSetup.get<MuonGeometryRecord>().get(gemGeometry_);
113 
114  if (gemGeometry_.isValid()) {
115  LogVerbatim("GEMGeometry") << "Validating GEM chamber geometry";
116 
118 
120 
121  } else
122  LogVerbatim("GEMGeometry") << "Invalid GEM geometry";
123 }
124 
126  clearData();
127 
128  for (auto const& it : gemGeometry_->chambers()) {
129  GEMDetId chId = it->id();
130  GlobalPoint gp = it->surface().toGlobal(LocalPoint(0.0, 0.0, 0.0));
131 
132  const TGeoMatrix* matrix = fwGeometry_.getMatrix(chId.rawId());
133 
134  if (!matrix) {
135  LogVerbatim("GEMGeometry") << "Failed to get matrix of GEM chamber with detid: " << chId.rawId();
136  continue;
137  }
139 
140  auto const& shape = fwGeometry_.getShapePars(chId.rawId());
141 
142  if (!shape) {
143  LogVerbatim("GEMGeometry") << "Failed to get shape of GEM chamber with detid: " << chId.rawId();
144  continue;
145  }
146  compareShape(it, shape);
147  }
148  makeHistograms("GEM Chamber");
149 }
150 
152  clearData2();
153 
154  for (auto const& it : gemGeometry_->etaPartitions()) {
155  GEMDetId chId = it->id();
156  const int n_strips = it->nstrips();
157  const float n_pitch = it->pitch();
158  const StripTopology& topo = it->specificTopology();
159  const float stripLen = topo.stripLength();
160  const float* parameters = fwGeometry_.getParameters(chId.rawId());
161  nstrips_.push_back(abs(n_strips - parameters[0]));
162 
163  if (n_strips) {
164  for (int istrips = 1; istrips <= n_strips; istrips++) {
165  pitch_.push_back(fabs(n_pitch - parameters[2]));
166 
167  stripslen_.push_back(fabs(stripLen - parameters[1]));
168  }
169  } else {
170  LogVerbatim("GEMGeometry") << "ATTENTION! nStrips == 0";
171  }
172  }
173  makeHistograms2("GEM Eta Partition");
174 }
175 
177  double local[3] = {0.0, 0.0, 0.0};
178  double global[3];
179 
180  matrix->LocalToMaster(local, global);
181 
182  float distance = getDistance(GlobalPoint(global[0], global[1], global[2]), gp);
183  if (abs(distance) < 1.0e-7)
184  distance = 0.0; // set a tollerance for the distance inside Histos
185  globalDistances_.push_back(distance);
186 }
187 
188 void GEMGeometryValidate::compareShape(const GeomDet* det, const float* shape) {
189  float shapeTopWidth;
190  float shapeBottomWidth;
191  float shapeLength;
192  float shapeThickness;
193 
194  if (shape[0] == 1) {
195  shapeTopWidth = shape[2];
196  shapeBottomWidth = shape[1];
197  shapeLength = shape[4];
198  shapeThickness = shape[3];
199  } else if (shape[0] == 2) {
200  shapeTopWidth = shape[1];
201  shapeBottomWidth = shape[1];
202  shapeLength = shape[2];
203  shapeThickness = shape[3];
204  } else {
205  LogVerbatim("GEMGeometry") << "Failed to get box or trapezoid from shape";
206 
207  return;
208  }
209 
210  float topWidth, bottomWidth;
211  float length, thickness;
212 
213  const Bounds* bounds = &(det->surface().bounds());
214  if (const TrapezoidalPlaneBounds* tpbs = dynamic_cast<const TrapezoidalPlaneBounds*>(bounds)) {
215  array<const float, 4> const& ps = tpbs->parameters();
216 
217  assert(ps.size() == 4);
218 
219  bottomWidth = ps[0];
220  topWidth = ps[1];
221  thickness = ps[2];
222  length = ps[3];
223  } else if ((dynamic_cast<const RectangularPlaneBounds*>(bounds))) {
224  length = det->surface().bounds().length() * 0.5;
225  topWidth = det->surface().bounds().width() * 0.5;
226  bottomWidth = topWidth;
227  thickness = det->surface().bounds().thickness() * 0.5;
228  } else {
229  LogVerbatim("GEMGeometry") << "Failed to get bounds";
230 
231  return;
232  }
233  topWidths_.push_back(fabs(shapeTopWidth - topWidth));
234  bottomWidths_.push_back(fabs(shapeBottomWidth - bottomWidth));
235  lengths_.push_back(fabs(shapeLength - length));
236  thicknesses_.push_back(fabs(shapeThickness - thickness));
237 }
238 
240  return sqrt((p1.x() - p2.x()) * (p1.x() - p2.x()) + (p1.y() - p2.y()) * (p1.y() - p2.y()) +
241  (p1.z() - p2.z()) * (p1.z() - p2.z()));
242 }
243 
244 float GEMGeometryValidate::getDiff(const float val1, const float val2) {
245  if (almost_equal(val1, val2, tolerance_))
246  return 0.0f;
247  else
248  return (val1 - val2);
249 }
250 
252  outFile_->cd();
253 
254  string d(detector);
255 
256  string gdn = d + ": distance between points in global coordinates";
258 
259  string twn = d + ": absolute difference between top widths (along X)";
261 
262  string bwn = d + ": absolute difference between bottom widths (along X)";
264 
265  string ln = d + ": absolute difference between lengths (along Y)";
266  makeHistogram(ln, lengths_);
267 
268  string tn = d + ": absolute difference between thicknesses (along Z)";
270 }
271 
273  outFile_->cd();
274 
275  string d(detector);
276 
277  string ns = d + ": absolute difference between nStrips";
278  makeHistogram(ns, nstrips_);
279 
280  string pi = d + ": absolute difference between Strips Pitch";
282 
283  string pl = d + ": absolute difference between Strips Length";
285 }
286 
287 void GEMGeometryValidate::makeHistogram(const string& name, vector<float>& data) {
288  if (data.empty())
289  return;
290 
291  const auto [minE, maxE] = minmax_element(begin(data), end(data));
292 
293  TH1D hist(name.c_str(), name.c_str(), 100, *minE * (1 + 0.10), *maxE * (1 + 0.10));
294 
295  for (auto const& it : data)
296  hist.Fill(it);
297 
298  hist.GetXaxis()->SetTitle("[cm]");
299  hist.Write();
300 }
301 
303 
305  LogVerbatim("GEMGeometry") << "Done.";
306  LogVerbatim("GEMGeometry") << "Results written to " << outfileName_;
307  outFile_->Close();
308 }
309 
GEMGeometryValidate::globalDistances_
vector< float > globalDistances_
Definition: GEMGeometryValidate.cc:90
GEMGeometryValidate::clearData
void clearData()
Definition: GEMGeometryValidate.cc:73
GEMGeometryValidate::compareTransform
void compareTransform(const GlobalPoint &, const TGeoMatrix *)
Definition: GEMGeometryValidate.cc:176
GEMGeometryValidate::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: GEMGeometryValidate.cc:111
bk::beginJob
void beginJob()
Definition: Breakpoints.cc:14
EDAnalyzer.h
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
ESHandle.h
FWGeometry::loadMap
void loadMap(const char *fileName)
Definition: FWGeometry.cc:100
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:486
Bounds
Definition: Bounds.h:18
cms::cuda::assert
assert(be >=bs)
HLT_2018_cff.distance
distance
Definition: HLT_2018_cff.py:6417
GEMGeometryValidate::validateGEMEtaPartitionGeometry
void validateGEMEtaPartitionGeometry()
Definition: GEMGeometryValidate.cc:151
GEMGeometryValidate::makeHistograms2
void makeHistograms2(const char *)
Definition: GEMGeometryValidate.cc:272
GEMGeometryValidate::clearData2
void clearData2()
Definition: GEMGeometryValidate.cc:81
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
StripTopology.h
align::LocalPoint
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
end
#define end
Definition: vmac.h:39
geometryDiff.epsilon
int epsilon
Definition: geometryDiff.py:26
Bounds::length
virtual float length() const =0
GEMGeometryValidate::gemGeometry_
edm::ESHandle< GEMGeometry > gemGeometry_
Definition: GEMGeometryValidate.cc:87
parameters
parameters
Definition: BeamSpot_PayloadInspector.cc:14
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:475
MakerMacros.h
FWGeometry::getMatrix
const TGeoMatrix * getMatrix(unsigned int id) const
Definition: FWGeometry.cc:222
edm::EventSetup::get
T get() const
Definition: EventSetup.h:73
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
compare.hist
hist
Definition: compare.py:376
TrapezoidalPlaneBounds.h
Calorimetry_cff.thickness
thickness
Definition: Calorimetry_cff.py:114
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:188
edm::ESHandle< GEMGeometry >
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:239
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
GEMGeometryValidate::getDiff
float getDiff(const float, const float)
Definition: GEMGeometryValidate.cc:244
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:36
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
GEMDetId
Definition: GEMDetId.h:17
GEMGeometryValidate::lengths_
vector< float > lengths_
Definition: GEMGeometryValidate.cc:93
createfilelist.int
int
Definition: createfilelist.py:10
GEMGeometryValidate::makeHistograms
void makeHistograms(const char *)
Definition: GEMGeometryValidate.cc:251
GEMGeometryValidate::beginJob
void beginJob() override
Definition: GEMGeometryValidate.cc:302
almost_equal
enable_if<!numeric_limits< T >::is_integer, bool >::type almost_equal(T x, T y, int ulp)
Definition: GEMGeometryValidate.cc:40
GEMGeometryValidate::infileName_
string infileName_
Definition: GEMGeometryValidate.cc:98
edm::LogVerbatim
Definition: MessageLogger.h:297
GEMGeometryValidate
Definition: GEMGeometryValidate.cc:50
GEMGeometryValidate::~GEMGeometryValidate
~GEMGeometryValidate() override
Definition: GEMGeometryValidate.cc:53
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
edm::EventSetup
Definition: EventSetup.h:57
GEMChamber.h
HLT_2018_cff.maxE
maxE
Definition: HLT_2018_cff.py:12256
TrapezoidalPlaneBounds
Definition: TrapezoidalPlaneBounds.h:15
get
#define get
edm::ESHandleBase::isValid
bool isValid() const
Definition: ESHandle.h:44
GEMGeometryValidate::validateGEMChamberGeometry
void validateGEMChamberGeometry()
Definition: GEMGeometryValidate.cc:125
GEMGeometry.h
type
type
Definition: HCALResponse.h:21
std
Definition: JetResolutionObject.h:76
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
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:304
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
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
GEMGeometryValidate::makeHistogram
void makeHistogram(const string &, vector< float > &)
Definition: GEMGeometryValidate.cc:287
StripTopology
Definition: StripTopology.h:11
MuonGeometryRecord
Definition: MuonGeometryRecord.h:34
begin
#define begin
Definition: vmac.h:32
GEMGeometryValidate::tolerance_
int tolerance_
Definition: GEMGeometryValidate.cc:100
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37