CMS 3D CMS Logo

SurveyInputCSCfromPins.cc
Go to the documentation of this file.
8 
11 
12 #include <iostream>
13 #include <fstream>
14 #include "TFile.h"
15 #include "TTree.h"
16 #include "TRandom3.h"
17 
18 #include "SurveyInputCSCfromPins.h"
19 
20 #define SQR(x) ((x) * (x))
21 
23  : DTGeoToken_(esConsumes()),
24  CSCGeoToken_(esConsumes()),
25  GEMGeoToken_(esConsumes()),
26  m_pinPositions(cfg.getParameter<std::string>("pinPositions")),
27  m_rootFile(cfg.getParameter<std::string>("rootFile")),
28  m_verbose(cfg.getParameter<bool>("verbose")),
29  m_errorX(cfg.getParameter<double>("errorX")),
30  m_errorY(cfg.getParameter<double>("errorY")),
31  m_errorZ(cfg.getParameter<double>("errorZ")),
32  m_missingErrorTranslation(cfg.getParameter<double>("missingErrorTranslation")),
33  m_missingErrorAngle(cfg.getParameter<double>("missingErrorAngle")),
34  m_stationErrorX(cfg.getParameter<double>("stationErrorX")),
35  m_stationErrorY(cfg.getParameter<double>("stationErrorY")),
36  m_stationErrorZ(cfg.getParameter<double>("stationErrorZ")),
37  m_stationErrorPhiX(cfg.getParameter<double>("stationErrorPhiX")),
38  m_stationErrorPhiY(cfg.getParameter<double>("stationErrorPhiY")),
39  m_stationErrorPhiZ(cfg.getParameter<double>("stationErrorPhiZ")) {}
40 
43  double a,
44  double b,
45  double &T,
46  double &dx,
47  double &dy,
48  double &dz,
49  double &PhX,
50  double &PhZ) {
51  double cosPhX, sinPhX, cosPhZ, sinPhZ;
52 
53  LocalPoint LP1(LC1.x(), LC1.y() + a, LC1.z() + b);
54  LocalPoint LP2(LC2.x(), LC2.y() - a, LC2.z() + b);
55 
56  LocalPoint P((LP1.x() - LP2.x()) / (2. * a), (LP1.y() - LP2.y()) / (2. * a), (LP1.z() - LP2.z()) / (2. * a));
57  LocalPoint Pp((LP1.x() + LP2.x()) / (2.), (LP1.y() + LP2.y()) / (2.), (LP1.z() + LP2.z()) / (2.));
58 
59  T = P.mag();
60 
61  sinPhX = P.z() / T;
62  cosPhX = sqrt(1 - SQR(sinPhX));
63  cosPhZ = P.y() / (T * cosPhX);
64  sinPhZ = -P.x() / (T * cosPhZ);
65 
66  PhX = atan2(sinPhX, cosPhX);
67 
68  PhZ = atan2(sinPhZ, cosPhZ);
69 
70  dx = Pp.x() - sinPhZ * sinPhX * b;
71  dy = Pp.y() + cosPhZ * sinPhX * b;
72  dz = Pp.z() - cosPhX * b;
73 }
74 
76  double b,
77  bool missing1,
78  bool missing2,
79  double &dx_dx,
80  double &dy_dy,
81  double &dz_dz,
82  double &phix_phix,
83  double &phiz_phiz,
84  double &dy_phix) {
85  dx_dx = 0.;
86  dy_dy = 0.;
87  dz_dz = 0.;
88  phix_phix = 0.;
89  phiz_phiz = 0.;
90  dy_phix = 0.;
91 
92  const double trials = 10000.; // two significant digits
93 
94  for (int i = 0; i < trials; i++) {
95  LocalVector LC1, LC2;
96  if (missing1) {
97  LC1 = LocalVector(gRandom->Gaus(0., m_missingErrorTranslation),
98  gRandom->Gaus(0., m_missingErrorTranslation),
99  gRandom->Gaus(0., m_missingErrorTranslation));
100  } else {
101  LC1 = LocalVector(gRandom->Gaus(0., m_errorX), gRandom->Gaus(0., m_errorY), gRandom->Gaus(0., m_errorZ));
102  }
103 
104  if (missing2) {
105  LC2 = LocalVector(gRandom->Gaus(0., m_missingErrorTranslation),
106  gRandom->Gaus(0., m_missingErrorTranslation),
107  gRandom->Gaus(0., m_missingErrorTranslation));
108  } else {
109  LC2 = LocalVector(gRandom->Gaus(0., m_errorX), gRandom->Gaus(0., m_errorY), gRandom->Gaus(0., m_errorZ));
110  }
111 
112  double dx, dy, dz, PhX, PhZ, T;
113  orient(LC1, LC2, a, b, T, dx, dy, dz, PhX, PhZ);
114 
115  dx_dx += dx * dx;
116  dy_dy += dy * dy;
117  dz_dz += dz * dz;
118  phix_phix += PhX * PhX;
119  phiz_phiz += PhZ * PhZ;
120  dy_phix += dy * PhX; // the only non-zero off-diagonal element
121  }
122 
123  dx_dx /= trials;
124  dy_dy /= trials;
125  dz_dz /= trials;
126  phix_phix /= trials;
127  phiz_phiz /= trials;
128  dy_phix /= trials;
129 }
130 
132  if (theFirstEvent) {
133  edm::LogInfo("SurveyInputCSCfromPins") << "***************ENTERING INITIALIZATION******************"
134  << " \n";
135 
136  std::ifstream in;
137  in.open(m_pinPositions.c_str());
138 
139  Double_t x1, y1, z1, x2, y2, z2, a, b, tot = 0.0, maxErr = 0.0, h, s1, dx, dy, dz, PhX, PhZ, T;
140 
141  int ID1, ID2, ID3, ID4, ID5, i = 1, ii = 0;
142 
143  TFile *file1 = new TFile(m_rootFile.c_str(), "recreate");
144  TTree *tree1 = new TTree("tree1", "alignment pins");
145 
146  if (m_verbose) {
147  tree1->Branch("displacement_x_pin1_cm", &x1, "x1/D");
148  tree1->Branch("displacement_y_pin1_cm", &y1, "y1/D");
149  tree1->Branch("displacement_z_pin1_cm", &z1, "z1/D");
150  tree1->Branch("displacement_x_pin2_cm", &x2, "x2/D");
151  tree1->Branch("displacement_y_pin2_cm", &y2, "y2/D");
152  tree1->Branch("displacement_z_pin2_cm", &z2, "z2/D");
153  tree1->Branch("error_vector_length_cm", &h, "h/D");
154  tree1->Branch("stretch_diff_cm", &s1, "s1/D");
155  tree1->Branch("stretch_factor", &T, "T/D");
156  tree1->Branch("chamber_displacement_x_cm", &dx, "dx/D");
157  tree1->Branch("chamber_displacement_y_cm", &dy, "dy/D");
158  tree1->Branch("chamber_displacement_z_cm", &dz, "dz/D");
159  tree1->Branch("chamber_rotation_x_rad", &PhX, "PhX/D");
160  tree1->Branch("chamber_rotation_z_rad", &PhZ, "PhZ/D");
161  }
162 
163  const DTGeometry *dtGeometry = &iSetup.getData(DTGeoToken_);
164  const CSCGeometry *cscGeometry = &iSetup.getData(CSCGeoToken_);
165  const GEMGeometry *gemGeometry = &iSetup.getData(GEMGeoToken_);
166 
167  AlignableMuon *theAlignableMuon = new AlignableMuon(dtGeometry, cscGeometry, gemGeometry);
168  AlignableNavigator *theAlignableNavigator = new AlignableNavigator(theAlignableMuon);
169 
170  const auto &theEndcaps = theAlignableMuon->CSCEndcaps();
171 
172  for (const auto &aliiter : theEndcaps) {
173  addComponent(aliiter);
174  }
175 
176  while (in.good()) {
177  bool missing1 = false;
178  bool missing2 = false;
179 
180  in >> ID1 >> ID2 >> ID3 >> ID4 >> ID5 >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> a >> b;
181 
182  if (fabs(x1 - 1000.) < 1e5 && fabs(y1 - 1000.) < 1e5 && fabs(z1 - 1000.) < 1e5) {
183  missing1 = true;
184  x1 = x2;
185  y1 = y2;
186  z1 = z2;
187  }
188 
189  if (fabs(x2 - 1000.) < 1e5 && fabs(y2 - 1000.) < 1e5 && fabs(z2 - 1000.) < 1e5) {
190  missing2 = true;
191  x2 = x1;
192  y2 = y1;
193  z2 = z1;
194  }
195 
196  x1 = x1 / 10.0;
197  y1 = y1 / 10.0;
198  z1 = z1 / 10.0;
199  x2 = x2 / 10.0;
200  y2 = y2 / 10.0;
201  z2 = z2 / 10.0;
202 
203  CSCDetId layerID(ID1, ID2, ID3, ID4, 1);
204 
205  // We cannot use chamber ID (when ID5=0), because AlignableNavigator gives the error (aliDet and aliDetUnit are undefined for chambers)
206 
207  Alignable *theAlignable1 = theAlignableNavigator->alignableFromDetId(layerID);
208  Alignable *chamberAli = theAlignable1->mother();
209 
210  LocalVector LC1 = chamberAli->surface().toLocal(GlobalVector(x1, y1, z1));
211  LocalVector LC2 = chamberAli->surface().toLocal(GlobalVector(x2, y2, z2));
212 
213  orient(LC1, LC2, a, b, T, dx, dy, dz, PhX, PhZ);
214 
215  GlobalPoint PG1 = chamberAli->surface().toGlobal(LocalPoint(LC1.x(), LC1.y() + a, LC1.z() + b));
216  chamberAli->surface().toGlobal(LocalPoint(LC2.x(), LC2.y() - a, LC2.z() + b));
217 
218  LocalVector lvector(dx, dy, dz);
219  GlobalVector gvector = (chamberAli->surface()).toGlobal(lvector);
220  chamberAli->move(gvector);
221 
222  chamberAli->rotateAroundLocalX(PhX);
223  chamberAli->rotateAroundLocalZ(PhZ);
224 
225  double dx_dx, dy_dy, dz_dz, phix_phix, phiz_phiz, dy_phix;
226  errors(a, b, missing1, missing2, dx_dx, dy_dy, dz_dz, phix_phix, phiz_phiz, dy_phix);
227  align::ErrorMatrix error = ROOT::Math::SMatrixIdentity();
228  error(0, 0) = dx_dx;
229  error(1, 1) = dy_dy;
230  error(2, 2) = dz_dz;
231  error(3, 3) = phix_phix;
232  error(4, 4) = m_missingErrorAngle;
233  error(5, 5) = phiz_phiz;
234  error(1, 3) = dy_phix;
235  error(3, 1) = dy_phix; // just in case
236 
237  chamberAli->setSurvey(new SurveyDet(chamberAli->surface(), error));
238 
239  if (m_verbose) {
240  edm::LogInfo("SurveyInputCSCfromPins") << " survey information = " << chamberAli->survey() << " \n";
241 
242  LocalPoint LP1n = chamberAli->surface().toLocal(PG1);
243 
244  LocalPoint hiP(LP1n.x(), LP1n.y() - a * T, LP1n.z() - b);
245 
246  h = hiP.mag();
247  s1 = LP1n.y() - a;
248 
249  if (h > maxErr) {
250  maxErr = h;
251 
252  ii = i;
253  }
254 
255  edm::LogInfo("SurveyInputCSCfromPins")
256  << " \n"
257  << "i " << i++ << " " << ID1 << " " << ID2 << " " << ID3 << " " << ID4 << " " << ID5 << " error " << h
258  << " \n"
259  << " x1 " << x1 << " y1 " << y1 << " z1 " << z1 << " x2 " << x2 << " y2 " << y2 << " z2 " << z2 << " \n"
260  << " error " << h << " S1 " << s1 << " \n"
261  << " dx " << dx << " dy " << dy << " dz " << dz << " PhX " << PhX << " PhZ " << PhZ << " \n";
262 
263  tot += h;
264 
265  tree1->Fill();
266  }
267  }
268 
269  in.close();
270 
271  if (m_verbose) {
272  file1->Write();
273  edm::LogInfo("SurveyInputCSCfromPins")
274  << " Total error " << tot << " Max Error " << maxErr << " N " << ii << " \n";
275  }
276 
277  file1->Close();
278 
279  for (const auto &aliiter : theEndcaps) {
280  fillAllRecords(aliiter);
281  }
282 
283  delete theAlignableMuon;
284  delete theAlignableNavigator;
285 
286  edm::LogInfo("SurveyInputCSCfromPins") << "*************END INITIALIZATION***************"
287  << " \n";
288 
289  theFirstEvent = false;
290  }
291 }
292 
294  if (ali->survey() == nullptr) {
295  AlignableCSCChamber *ali_AlignableCSCChamber = dynamic_cast<AlignableCSCChamber *>(ali);
296  AlignableCSCStation *ali_AlignableCSCStation = dynamic_cast<AlignableCSCStation *>(ali);
297 
298  if (ali_AlignableCSCChamber != nullptr) {
299  CSCDetId detid(ali->geomDetId());
300  if (abs(detid.station()) == 1 && (detid.ring() == 1 || detid.ring() == 4)) {
301  align::ErrorMatrix error = ROOT::Math::SMatrixIdentity();
305  error(3, 3) = m_missingErrorAngle;
306  error(4, 4) = m_missingErrorAngle;
307  error(5, 5) = m_missingErrorAngle;
308 
309  ali->setSurvey(new SurveyDet(ali->surface(), error));
310  } else {
311  double a = 100.;
312  double b = -9.4034;
313  if (abs(detid.station()) == 1 && detid.ring() == 2)
314  a = -90.260;
315  else if (abs(detid.station()) == 1 && detid.ring() == 3)
316  a = -85.205;
317  else if (abs(detid.station()) == 2 && detid.ring() == 1)
318  a = -97.855;
319  else if (abs(detid.station()) == 2 && detid.ring() == 2)
320  a = -164.555;
321  else if (abs(detid.station()) == 3 && detid.ring() == 1)
322  a = -87.870;
323  else if (abs(detid.station()) == 3 && detid.ring() == 2)
324  a = -164.555;
325  else if (abs(detid.station()) == 4 && detid.ring() == 1)
326  a = -77.890;
327 
328  double dx_dx, dy_dy, dz_dz, phix_phix, phiz_phiz, dy_phix;
329  errors(a, b, true, true, dx_dx, dy_dy, dz_dz, phix_phix, phiz_phiz, dy_phix);
330  align::ErrorMatrix error = ROOT::Math::SMatrixIdentity();
331  error(0, 0) = dx_dx;
332  error(1, 1) = dy_dy;
333  error(2, 2) = dz_dz;
334  error(3, 3) = phix_phix;
335  error(4, 4) = m_missingErrorAngle;
336  error(5, 5) = phiz_phiz;
337  error(1, 3) = dy_phix;
338  error(3, 1) = dy_phix; // just in case
339 
340  ali->setSurvey(new SurveyDet(ali->surface(), error));
341  }
342  }
343 
344  else if (ali_AlignableCSCStation != nullptr) {
345  align::ErrorMatrix error = ROOT::Math::SMatrixIdentity();
346  error(0, 0) = m_stationErrorX;
347  error(1, 1) = m_stationErrorY;
348  error(2, 2) = m_stationErrorZ;
349  error(3, 3) = m_stationErrorPhiX;
350  error(4, 4) = m_stationErrorPhiY;
351  error(5, 5) = m_stationErrorPhiZ;
352 
353  ali->setSurvey(new SurveyDet(ali->surface(), error));
354  }
355 
356  else {
357  align::ErrorMatrix error = ROOT::Math::SMatrixIdentity();
358  ali->setSurvey(new SurveyDet(ali->surface(), error * (1e-10)));
359  }
360  }
361 
362  for (const auto &iter : ali->components()) {
363  fillAllRecords(iter);
364  }
365 }
366 
367 // Plug in to framework
align::GlobalPoints toGlobal(const align::LocalPoints &) const
Return in global coord given a set of local points.
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const edm::ESGetToken< DTGeometry, MuonGeometryRecord > DTGeoToken_
void errors(double a, double b, bool missing1, bool missing2, double &dx_dx, double &dy_dy, double &dz_dz, double &phix_phix, double &phiz_phiz, double &dy_phix)
Local3DVector LocalVector
Definition: LocalVector.h:12
Alignable * mother() const
Return pointer to container alignable (if any)
Definition: Alignable.h:91
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
Definition: Alignable.h:132
virtual void rotateAroundLocalZ(Scalar radians)
Rotation around local z-axis.
Definition: Alignable.cc:183
T z() const
Definition: PV3DBase.h:61
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
virtual void move(const GlobalVector &displacement)=0
Movement with respect to the global reference frame.
static void addComponent(Alignable *)
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
align::Alignables CSCEndcaps()
void analyze(const edm::Event &, const edm::EventSetup &) override
Read ideal tracker geometry from DB.
void fillAllRecords(Alignable *ali)
const SurveyDet * survey() const
Return survey info.
Definition: Alignable.h:216
T sqrt(T t)
Definition: SSEVec.h:19
T mag() const
Definition: PV3DBase.h:64
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool getData(T &iHolder) const
Definition: EventSetup.h:122
SurveyInputCSCfromPins(const edm::ParameterSet &)
const edm::ESGetToken< CSCGeometry, MuonGeometryRecord > CSCGeoToken_
virtual const Alignables & components() const =0
Return vector of all direct components.
ii
Definition: cuy.py:589
Log< level::Info, false > LogInfo
const DetId & geomDetId() const
Definition: Alignable.h:177
void setSurvey(const SurveyDet *)
Set survey info.
Definition: Alignable.cc:266
double b
Definition: hdecay.h:118
std::pair< OmniClusterRef, TrackingParticleRef > P
const edm::ESGetToken< GEMGeometry, MuonGeometryRecord > GEMGeoToken_
align::RotationType toLocal(const align::RotationType &) const
Return in local frame a rotation given in global frame.
double a
Definition: hdecay.h:119
virtual void rotateAroundLocalX(Scalar radians)
Rotation around local x-axis.
Definition: Alignable.cc:155
math::Error< 6 >::type ErrorMatrix
Definition: Definitions.h:37
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
long double T
Constructor of the full muon geometry.
Definition: AlignableMuon.h:38
void orient(align::LocalVector LC1, align::LocalVector LC2, double a, double b, double &T, double &dx, double &dy, double &dz, double &PhX, double &PhZ)
AlignableDetOrUnitPtr alignableFromDetId(const DetId &detid)
Returns AlignableDetOrUnitPtr corresponding to given DetId.
#define SQR(x)
Global3DVector GlobalVector
Definition: GlobalVector.h:10
A muon CSC Chamber( an AlignableDet )