CMS 3D CMS Logo

MuonGeometryArrange.cc
Go to the documentation of this file.
3 #include "CLHEP/Vector/RotationInterfaces.h"
4 
9 
11 
15 // The following looks generic enough to use
23 
26 
27 #include "MuonGeometryArrange.h"
28 #include "TFile.h"
29 #include "TLatex.h"
30 #include "TArrow.h"
31 #include "TGraph.h"
32 #include "TH1F.h"
33 #include "TH2F.h"
34 #include "CLHEP/Vector/ThreeVector.h"
35 
36 // Database
38 
39 #include <iostream>
40 #include <fstream>
41 
43  theSurveyIndex(0),
44  _levelStrings(cfg.getUntrackedParameter<std::vector<std::string> >("levels")),
45  _writeToDB(false), _commonMuonLevel(align::invalid), firstEvent_(true)
46 {
47  referenceMuon=nullptr;
48  currentMuon=nullptr;
49  // Input is XML
50  _inputXMLCurrent = cfg.getUntrackedParameter<std::string> ("inputXMLCurrent");
51  _inputXMLReference = cfg.getUntrackedParameter<std::string> ("inputXMLReference");
52 
53  //input is ROOT
55  ("inputROOTFile1");
57  ("inputROOTFile2");
58  _inputTreename = cfg.getUntrackedParameter< std::string > ("treeName");
59 
60  //output file
61  _filename = cfg.getUntrackedParameter< std::string > ("outputFile");
62 
63 
64  _weightBy = cfg.getUntrackedParameter< std::string > ("weightBy");
65  _detIdFlag = cfg.getUntrackedParameter< bool > ("detIdFlag");
67  ("detIdFlagFile");
68  _weightById = cfg.getUntrackedParameter< bool > ("weightById");
70  ("weightByIdFile");
71  _endcap = cfg.getUntrackedParameter<int> ("endcapNumber");
72  _station = cfg.getUntrackedParameter<int> ("stationNumber");
73  _ring = cfg.getUntrackedParameter<int> ("ringNumber");
74 
75 
76  // if want to use, make id cut list
77  if (_detIdFlag){
78  std::ifstream fin;
79  fin.open( _detIdFlagFile.c_str() );
80 
81  while (!fin.eof() && fin.good() ){
82 
83  uint32_t id;
84  fin >> id;
85  _detIdFlagVector.push_back(id);
86  }
87  fin.close();
88  }
89 
90  // turn weightByIdFile into weightByIdVector
91  unsigned int lastID=999999999;
92  if (_weightById){
93  std::ifstream inFile;
94  inFile.open( _weightByIdFile.c_str() );
95  int ctr = 0;
96  while ( !inFile.eof() ){
97  ctr++;
98  unsigned int listId;
99  inFile >> listId;
100  inFile.ignore(256, '\n');
101  if(listId!=lastID){
102  _weightByIdVector.push_back( listId );
103  }
104  lastID=listId;
105  }
106  inFile.close();
107  }
108 
109 
110 
111  //root configuration
112  _theFile = new TFile(_filename.c_str(),"RECREATE");
113  _alignTree = new TTree("alignTree","alignTree");
114  _alignTree->Branch("id", &_id, "id/I");
115  _alignTree->Branch("level", &_level, "level/I");
116  _alignTree->Branch("mid", &_mid, "mid/I");
117  _alignTree->Branch("mlevel", &_mlevel, "mlevel/I");
118  _alignTree->Branch("sublevel", &_sublevel, "sublevel/I");
119  _alignTree->Branch("x", &_xVal, "x/F");
120  _alignTree->Branch("y", &_yVal, "y/F");
121  _alignTree->Branch("z", &_zVal, "z/F");
122  _alignTree->Branch("r", &_rVal, "r/F");
123  _alignTree->Branch("phi", &_phiVal, "phi/F");
124  _alignTree->Branch("eta", &_etaVal, "eta/F");
125  _alignTree->Branch("alpha", &_alphaVal, "alpha/F");
126  _alignTree->Branch("beta", &_betaVal, "beta/F");
127  _alignTree->Branch("gamma", &_gammaVal, "gamma/F");
128  _alignTree->Branch("dx", &_dxVal, "dx/F");
129  _alignTree->Branch("dy", &_dyVal, "dy/F");
130  _alignTree->Branch("dz", &_dzVal, "dz/F");
131  _alignTree->Branch("dr", &_drVal, "dr/F");
132  _alignTree->Branch("dphi", &_dphiVal, "dphi/F");
133  _alignTree->Branch("dalpha", &_dalphaVal, "dalpha/F");
134  _alignTree->Branch("dbeta", &_dbetaVal, "dbeta/F");
135  _alignTree->Branch("dgamma", &_dgammaVal, "dgamma/F");
136  _alignTree->Branch("ldx", &_ldxVal, "ldx/F");
137  _alignTree->Branch("ldy", &_ldyVal, "ldy/F");
138  _alignTree->Branch("ldz", &_ldzVal, "ldz/F");
139  _alignTree->Branch("ldr", &_ldrVal, "ldr/F");
140  _alignTree->Branch("ldphi", &_ldphiVal, "ldphi/F");
141  _alignTree->Branch("useDetId", &_useDetId, "useDetId/I");
142  _alignTree->Branch("detDim", &_detDim, "detDim/I");
143  _alignTree->Branch("rotx",&_rotxVal, "rotx/F");
144  _alignTree->Branch("roty",&_rotyVal, "roty/F");
145  _alignTree->Branch("rotz",&_rotzVal, "rotz/F");
146  _alignTree->Branch("drotx",&_drotxVal, "drotx/F");
147  _alignTree->Branch("droty",&_drotyVal, "droty/F");
148  _alignTree->Branch("drotz",&_drotzVal, "drotz/F");
149  _alignTree->Branch("surW", &_surWidth, "surW/F");
150  _alignTree->Branch("surL", &_surLength, "surL/F");
151  _alignTree->Branch("surRot", &_surRot, "surRot[9]/D");
152 
153  _mgacollection.clear();
154 }
157  // Unpack the list and create ntuples here.
158 
159  int size=_mgacollection.size();
160  if(size<=0) return; // nothing to do here.
161  std::vector<float> xp(size+1);
162  std::vector<float> yp(size+1);
163  int i;
164  float minV, maxV;
165  int minI, maxI;
166 
167  minV=99999999.; maxV=-minV; minI=9999999; maxI=-minI;
168  TGraph* grx=nullptr;
169  TH2F* dxh=nullptr;
170 
171 // for position plots:
172  for(i=0; i<size; i++){
173  if(_mgacollection[i].phipos<minI) minI=_mgacollection[i].phipos;
174  if(_mgacollection[i].phipos>maxI) maxI=_mgacollection[i].phipos;
175  xp[i]=_mgacollection[i].phipos;
176  }
177  if(minI>=maxI) return; // can't do anything?
178  xp[size]=xp[size-1]+1; // wraparound point
179 
180  if(1<minI) minI=1;
181  if(size>maxI) maxI=size;
182  maxI++; // allow for wraparound to show neighbors
183  int sizeI=maxI+1-minI;
184  float smi=minI-1;
185  float sma=maxI+1;
186 
187 
188 // Dx plot
189 
190  for(i=0; i<size; i++){
191  if(_mgacollection[i].ldx<minV) minV=_mgacollection[i].ldx;
192  if(_mgacollection[i].ldx>maxV) maxV=_mgacollection[i].ldx;
193  yp[i]=_mgacollection[i].ldx;
194  }
195  yp[size]=yp[0]; // wraparound point
196 
197  makeGraph(sizeI, smi, sma, minV, maxV,
198  dxh, grx, "delX_vs_position", "Local #delta X vs position",
199  "GdelX_vs_position","#delta x in cm", xp.data(), yp.data(), size);
200 // Dy plot
201  minV=99999999.; maxV=-minV;
202  for(i=0; i<size; i++){
203  if(_mgacollection[i].ldy<minV) minV=_mgacollection[i].ldy;
204  if(_mgacollection[i].ldy>maxV) maxV=_mgacollection[i].ldy;
205  yp[i]=_mgacollection[i].ldy;
206  }
207  yp[size]=yp[0]; // wraparound point
208 
209  makeGraph(sizeI, smi, sma, minV, maxV,
210  dxh, grx, "delY_vs_position", "Local #delta Y vs position",
211  "GdelY_vs_position","#delta y in cm", xp.data(), yp.data(), size);
212 
213 // Dz plot
214  minV=99999999.; maxV=-minV;
215  for(i=0; i<size; i++){
216  if(_mgacollection[i].dz<minV) minV=_mgacollection[i].dz;
217  if(_mgacollection[i].dz>maxV) maxV=_mgacollection[i].dz;
218  yp[i]=_mgacollection[i].dz;
219  }
220  yp[size]=yp[0]; // wraparound point
221 
222  makeGraph(sizeI, smi, sma, minV, maxV,
223  dxh, grx, "delZ_vs_position", "Local #delta Z vs position",
224  "GdelZ_vs_position","#delta z in cm", xp.data(), yp.data(), size);
225 
226 // Dphi plot
227  minV=99999999.; maxV=-minV;
228  for(i=0; i<size; i++){
229  if(_mgacollection[i].dphi<minV) minV=_mgacollection[i].dphi;
230  if(_mgacollection[i].dphi>maxV) maxV=_mgacollection[i].dphi;
231  yp[i]=_mgacollection[i].dphi;
232  }
233  yp[size]=yp[0]; // wraparound point
234 
235  makeGraph(sizeI, smi, sma, minV, maxV,
236  dxh, grx, "delphi_vs_position", "#delta #phi vs position",
237  "Gdelphi_vs_position","#delta #phi in radians", xp.data(), yp.data(), size);
238 
239 // Dr plot
240  minV=99999999.; maxV=-minV;
241  for(i=0; i<size; i++){
242  if(_mgacollection[i].dr<minV) minV=_mgacollection[i].dr;
243  if(_mgacollection[i].dr>maxV) maxV=_mgacollection[i].dr;
244  yp[i]=_mgacollection[i].dr;
245  }
246  yp[size]=yp[0]; // wraparound point
247 
248  makeGraph(sizeI, smi, sma, minV, maxV,
249  dxh, grx, "delR_vs_position", "#delta R vs position",
250  "GdelR_vs_position","#delta R in cm", xp.data(), yp.data(), size);
251 
252 // Drphi plot
253  minV=99999999.; maxV=-minV;
254  for(i=0; i<size; i++){
255  float ttemp=_mgacollection[i].r*_mgacollection[i].dphi;
256  if(ttemp<minV) minV=ttemp;
257  if(ttemp>maxV) maxV=ttemp;
258  yp[i]=ttemp;
259  }
260  yp[size]=yp[0]; // wraparound point
261 
262  makeGraph(sizeI, smi, sma, minV, maxV,
263  dxh, grx, "delRphi_vs_position", "R #delta #phi vs position",
264  "GdelRphi_vs_position","R #delta #phi in cm", xp.data(), yp.data(), size);
265 
266 // Dalpha plot
267  minV=99999999.; maxV=-minV;
268  for(i=0; i<size; i++){
269  if(_mgacollection[i].dalpha<minV) minV=_mgacollection[i].dalpha;
270  if(_mgacollection[i].dalpha>maxV) maxV=_mgacollection[i].dalpha;
271  yp[i]=_mgacollection[i].dalpha;
272  }
273  yp[size]=yp[0]; // wraparound point
274 
275  makeGraph(sizeI, smi, sma, minV, maxV,
276  dxh, grx, "delalpha_vs_position", "#delta #alpha vs position",
277  "Gdelalpha_vs_position","#delta #alpha in rad", xp.data(), yp.data(), size);
278 
279 // Dbeta plot
280  minV=99999999.; maxV=-minV;
281  for(i=0; i<size; i++){
282  if(_mgacollection[i].dbeta<minV) minV=_mgacollection[i].dbeta;
283  if(_mgacollection[i].dbeta>maxV) maxV=_mgacollection[i].dbeta;
284  yp[i]=_mgacollection[i].dbeta;
285  }
286  yp[size]=yp[0]; // wraparound point
287 
288  makeGraph(sizeI, smi, sma, minV, maxV,
289  dxh, grx, "delbeta_vs_position", "#delta #beta vs position",
290  "Gdelbeta_vs_position","#delta #beta in rad", xp.data(), yp.data(), size);
291 
292 // Dgamma plot
293  minV=99999999.; maxV=-minV;
294  for(i=0; i<size; i++){
295  if(_mgacollection[i].dgamma<minV) minV=_mgacollection[i].dgamma;
296  if(_mgacollection[i].dgamma>maxV) maxV=_mgacollection[i].dgamma;
297  yp[i]=_mgacollection[i].dgamma;
298  }
299  yp[size]=yp[0]; // wraparound point
300 
301  makeGraph(sizeI, smi, sma, minV, maxV,
302  dxh, grx, "delgamma_vs_position", "#delta #gamma vs position",
303  "Gdelgamma_vs_position","#delta #gamma in rad", xp.data(), yp.data(), size);
304 
305 // Drotx plot
306  minV=99999999.; maxV=-minV;
307  for(i=0; i<size; i++){
308  if(_mgacollection[i].drotx<minV) minV=_mgacollection[i].drotx;
309  if(_mgacollection[i].drotx>maxV) maxV=_mgacollection[i].drotx;
310  yp[i]=_mgacollection[i].drotx;
311  }
312  yp[size]=yp[0]; // wraparound point
313 
314  makeGraph(sizeI, smi, sma, minV, maxV,
315  dxh, grx, "delrotX_vs_position", "#delta rotX vs position",
316  "GdelrotX_vs_position","#delta rotX in rad", xp.data(), yp.data(), size);
317 
318 // Droty plot
319  minV=99999999.; maxV=-minV;
320  for(i=0; i<size; i++){
321  if(_mgacollection[i].droty<minV) minV=_mgacollection[i].droty;
322  if(_mgacollection[i].droty>maxV) maxV=_mgacollection[i].droty;
323  yp[i]=_mgacollection[i].droty;
324  }
325  yp[size]=yp[0]; // wraparound point
326 
327  makeGraph(sizeI, smi, sma, minV, maxV,
328  dxh, grx, "delrotY_vs_position", "#delta rotY vs position",
329  "GdelrotY_vs_position","#delta rotY in rad", xp.data(), yp.data(), size);
330 
331 // Drotz plot
332  minV=99999999.; maxV=-minV;
333  for(i=0; i<size; i++){
334  if(_mgacollection[i].drotz<minV) minV=_mgacollection[i].drotz;
335  if(_mgacollection[i].drotz>maxV) maxV=_mgacollection[i].drotz;
336  yp[i]=_mgacollection[i].drotz;
337  }
338  yp[size]=yp[0]; // wraparound point
339 
340  makeGraph(sizeI, smi, sma, minV, maxV,
341  dxh, grx, "delrotZ_vs_position", "#delta rotZ vs position",
342  "GdelrotZ_vs_position","#delta rotZ in rad", xp.data(), yp.data(), size);
343 
344 
345 
346 // Vector plots
347 // First find the maximum length of sqrt(dx*dx+dy*dy): we'll have to
348 // scale these for visibility
349  maxV=-99999999.;
350  float ttemp, rtemp;
351  float maxR=-9999999.;
352  for(i=0; i<size; i++){
353  ttemp= sqrt(_mgacollection[i].dx*_mgacollection[i].dx+
355  rtemp= sqrt(_mgacollection[i].x*_mgacollection[i].x+
357  if(ttemp>maxV) maxV=ttemp;
358  if(rtemp>maxR) maxR=rtemp;
359  }
360 
361  // Don't try to scale rediculously small values
362  float smallestVcm=.001; // 10 microns
363  if(maxV<smallestVcm) maxV=smallestVcm;
364  float scale=0.;
365  float lside=1.1*maxR;
366  if(lside<=0) lside=100.;
367  if(maxV>0){scale=.09*lside/maxV;} // units of pad length!
368  char scalename[50];
369  int ret=snprintf(scalename,50,"#delta #bar{x} length =%f cm",maxV);
370  // If ret<=0 we don't want to print the scale!
371 
372  if(ret>0){
373  dxh=new TH2F("vecdrplot",scalename,80,-lside,lside,80,-lside,lside);
374  }
375  else{
376  dxh=new TH2F("vecdrplot","delta #bar{x} Bad scale",80,-lside,lside,80,-lside,lside);
377  }
378  dxh->GetXaxis()->SetTitle("x in cm");
379  dxh->GetYaxis()->SetTitle("y in cm");
380  dxh->SetStats(kFALSE);
381  dxh->Draw();
382  TArrow* arrow;
383  for(i=0; i<size; i++){
384  ttemp= sqrt(_mgacollection[i].dx*_mgacollection[i].dx+
386 // ttemp=ttemp*scale;
387  float nx=_mgacollection[i].x+scale*_mgacollection[i].dx;
388  float ny=_mgacollection[i].y+scale*_mgacollection[i].dy;
389  arrow = new TArrow(_mgacollection[i].x,
390  _mgacollection[i].y, nx, ny);// ttemp*.3*.05, "->");
391  arrow->SetLineWidth(2); arrow->SetArrowSize(ttemp*.2*.05/maxV);
392  arrow->SetLineColor(1); arrow->SetLineStyle(1);
393  arrow->Paint();
394  dxh->GetListOfFunctions()->Add(static_cast<TObject*>(arrow));
395 // arrow->Draw();
396 // arrow->Write();
397  }
398  dxh->Write();
399 
400  _theFile->Write();
401  _theFile->Close();
402 
403 
404 }
406 void MuonGeometryArrange::makeGraph(int sizeI, float smi, float sma,
407  float minV, float maxV,
408  TH2F* dxh, TGraph* grx, const char* name, const char* title,
409  const char* titleg, const char* axis,
410  const float* xp, const float* yp, int size){
411 
412  if(minV>=maxV || smi>=sma || sizeI<=1 || xp==nullptr || yp==nullptr) return;
413  // out of bounds, bail
414  float diff=maxV-minV;
415  float over=.05*diff;
416  double ylo=minV-over;
417  double yhi=maxV+over;
418  double dsmi, dsma;
419  dsmi=smi; dsma=sma;
420  dxh= new TH2F(name, title,
421  sizeI+2, dsmi, dsma, 50, ylo, yhi);
422  dxh->GetXaxis()->SetTitle("Position around ring");
423  dxh->GetYaxis()->SetTitle(axis);
424  dxh->SetStats(kFALSE);
425  dxh->Draw();
426  grx = new TGraph(size, xp, yp);
427  grx->SetName(titleg);
428  grx->SetTitle(title);
429  grx->SetMarkerColor(2); grx->SetMarkerStyle(3);
430  grx->GetXaxis()->SetLimits(dsmi, dsma);
431  grx->GetXaxis()->SetTitle("position number");
432  grx->GetYaxis()->SetLimits(ylo,yhi);
433  grx->GetYaxis()->SetTitle(axis);
434  grx->Draw("A*");
435  grx->Write();
436  return;
437 }
440  firstEvent_ = true;
441 }
442 
447  const edm::EventSetup& iSetup){
448  if (firstEvent_) {
449 
450  // My stuff
452  inputAlign1 = new MuonAlignment(iSetup, inputMethod1);
455  inputAlign2 = new MuonAlignment(iSetup, inputMethod2);
458  inputAlign2a = new MuonAlignment(iSetup, inputMethod3);
460 
463  auto inputGeometry2Copy2 = inputAlign2a->getAlignableMuon();
464 
465  //setting the levels being used in the geometry comparator
466  edm::LogInfo("MuonGeometryArrange") << "levels: " << _levelStrings.size();
467  for (const auto& level: _levelStrings){
468  theLevels.push_back(inputGeometry2Copy2->objectIdProvider().stringToId(level));
469  edm::LogInfo("MuonGeometryArrange") << "level: " << level;
470  }
471 
472  //compare the goemetries
473  compare(inputGeometry1, inputGeometry2, inputGeometry2Copy2);
474 
475  //write out ntuple
476  //might be better to do within output module
477  _theFile->cd();
478  _alignTree->Write();
479  endHist();
480  // _theFile->Close();
481 
482  firstEvent_ = false;
483  }
484 }
485 
488  Alignable* curAliCopy2){
489 
490  // First sanity
491  if(refAli==nullptr){return;}
492  if(curAli==nullptr){return;}
493 
494  const auto& refComp = refAli->components();
495  const auto& curComp = curAli->components();
496  const auto& curComp2 = curAliCopy2->components();
497  compareGeometries(refAli, curAli, curAliCopy2);
498 
499  int nComp=refComp.size();
500  for(int i=0; i<nComp; i++){
501  compare(refComp[i], curComp[i], curComp2[i]);
502  }
503  return;
504 }
505 
508  Alignable* curAli, Alignable* curCopy){
509  // First sanity
510  if(refAli==nullptr){return;}
511  if(curAli==nullptr){return;}
512  // Is this the Ring we want to align? If so it will contain the
513  // chambers specified in the configuration file
514  if(!isMother(refAli)) return; // Not the desired alignable object
515  // But... There are granddaughters involved--and I don't want to monkey with
516  // the layers of the chambers. So, if the mother of this is also an approved
517  // mother, bail.
518  if(isMother(refAli->mother() )) return;
519  const auto& refComp = refAli->components();
520  const auto& curComp = curCopy->components();
521  if(refComp.size()!=curComp.size()){
522  return;
523  }
524  // GlobalVectors is a vector of GlobalVector which is a 3D vector
525  align::GlobalVectors originalVectors;
526  align::GlobalVectors currentVectors;
527  align::GlobalVectors originalRelativeVectors;
528  align::GlobalVectors currentRelativeVectors;
529 
530 
531  int nComp = refComp.size();
532  int nUsed = 0;
533  // Use the total displacements here:
534  CLHEP::Hep3Vector TotalX, TotalL;
535  TotalX.set(0.,0.,0.); TotalL.set(0., 0., 0.);
536 // CLHEP::Hep3Vector* Rsubtotal, Wsubtotal, DRsubtotal, DWsubtotal;
537  std::vector<CLHEP::Hep3Vector> Positions;
538  std::vector<CLHEP::Hep3Vector> DelPositions;
539 
540  double xrcenter=0.;
541  double yrcenter=0.;
542  double zrcenter=0.;
543  double xccenter=0.;
544  double yccenter=0.;
545  double zccenter=0.;
546 
547  bool useIt;
548  // Create the "center" for the reference alignment chambers, and
549  // load a vector of their centers
550  for(int ich=0; ich<nComp; ich++){
551  useIt=true;
552  if(_weightById){
553  if(!align::readModuleList(curComp[ich]->id(), curComp[ich]->id(), _weightByIdVector))
554  useIt=false;
555  }
556  if(!useIt) continue;
557  align::GlobalVectors curVs;
558  align::createPoints(&curVs, refComp[ich],
560  align::GlobalVector pointsCM = align::centerOfMass(curVs);
561  originalVectors.push_back(pointsCM);
562  nUsed++;
563  xrcenter+= pointsCM.x();
564  yrcenter+= pointsCM.y();
565  zrcenter+= pointsCM.z();
566  }
567  xrcenter=xrcenter/nUsed;
568  yrcenter=yrcenter/nUsed;
569  zrcenter=zrcenter/nUsed;
570 
571  // Create the "center" for the current alignment chambers, and
572  // load a vector of their centers
573  for(int ich=0; ich<nComp; ich++){
574  useIt=true;
575  if(_weightById){
576  if(!align::readModuleList(curComp[ich]->id(), curComp[ich]->id(), _weightByIdVector))
577  useIt=false;
578  }
579  if(!useIt)continue;
580  align::GlobalVectors curVs;
581  align::createPoints(&curVs, curComp[ich],
583  align::GlobalVector pointsCM = align::centerOfMass(curVs);
584  currentVectors.push_back(pointsCM);
585 
586  xccenter+= pointsCM.x();
587  yccenter+= pointsCM.y();
588  zccenter+= pointsCM.z();
589  }
590  xccenter=xccenter/nUsed;
591  yccenter=yccenter/nUsed;
592  zccenter=zccenter/nUsed;
593 
594 
595  // OK, now load the <very approximate> vectors from the ring "centers"
596  align::GlobalVector CCur(xccenter, yccenter, zccenter);
597  align::GlobalVector CRef(xrcenter, yrcenter, zrcenter);
598  int nCompR = currentVectors.size();
599  for(int ich=0; ich<nCompR; ich++){
600  originalRelativeVectors.push_back(originalVectors[ich]-CRef);
601  currentRelativeVectors.push_back(currentVectors[ich]-CCur);
602  }
603 
604  // All right. Now let the hacking begin.
605  // First out of the gate let's try using the raw values and see what
606  // diffRot does for us.
607 
608 
609  align::RotationType rtype3=align::diffRot(currentRelativeVectors,
610  originalRelativeVectors);
611 
612 
614  angles = align::toAngles(rtype3);
615 
616  for(int ich=0; ich<nComp; ich++){
617  if(_weightById){
618  if(!align::readModuleList(curComp[ich]->id(), curComp[ich]->id(), _weightByIdVector))
619  continue;
620  }
621  CLHEP::Hep3Vector Rtotal, Wtotal;
622  Rtotal.set(0.,0.,0.); Wtotal.set(0.,0.,0.);
623  for (int i = 0; i < 100; i++){
624  AlgebraicVector diff = align::diffAlignables(refComp[ich],curComp[ich],
626  CLHEP::Hep3Vector dR(diff[0],diff[1],diff[2]);
627  Rtotal+=dR;
628  CLHEP::Hep3Vector dW(diff[3],diff[4],diff[5]);
629  CLHEP::HepRotation rot(Wtotal.unit(),Wtotal.mag());
630  CLHEP::HepRotation drot(dW.unit(),dW.mag());
631  rot*=drot;
632  Wtotal.set(rot.axis().x()*rot.delta(),
633  rot.axis().y()*rot.delta(), rot.axis().z()*rot.delta());
634  align::moveAlignable(curComp[ich], diff);
635  float tolerance = 1e-7;
636  AlgebraicVector check = align::diffAlignables(refComp[ich],curComp[ich],
638  align::GlobalVector checkR(check[0],check[1],check[2]);
639  align::GlobalVector checkW(check[3],check[4],check[5]);
640  DetId detid(refComp[ich]->id());
641  if ((checkR.mag() > tolerance)||(checkW.mag() > tolerance)){
642 // edm::LogInfo("CompareGeoms") << "Tolerance Exceeded!(alObjId: "
643 // << refAli->alignableObjectId()
644 // << ", rawId: " << refComp[ich]->geomDetId().rawId()
645 // << ", subdetId: "<< detid.subdetId() << "): " << diff;
646  }
647  else{
648  TotalX+=Rtotal;
649  break;
650  } // end of else
651  } // end of for on int i
652  } // end of for on ich
653 
654  // At this point we should have a total displacement and total L
655  TotalX=TotalX/nUsed;
656 
657  // Now start again!
658  AlgebraicVector change(6);
659  change(1)=TotalX.x();
660  change(2)=TotalX.y();
661  change(3)=TotalX.z();
662 
663  change(4)=angles[0];
664  change(5)=angles[1];
665  change(6)=angles[2];
666  align::moveAlignable(curAli, change); // move as a chunk
667 
668  // Now get the components again. They should be in new locations
669  const auto& curComp2 = curAli->components();
670 
671  for(int ich=0; ich<nComp; ich++){
672  CLHEP::Hep3Vector Rtotal, Wtotal;
673  Rtotal.set(0.,0.,0.); Wtotal.set(0.,0.,0.);
674  if(_weightById){
675  if(!align::readModuleList(curComp[ich]->id(), curComp[ich]->id(), _weightByIdVector))
676  continue;
677  }
678 
679  for (int i = 0; i < 100; i++){
680  AlgebraicVector diff = align::diffAlignables(refComp[ich],curComp2[ich],
682  CLHEP::Hep3Vector dR(diff[0],diff[1],diff[2]);
683  Rtotal+=dR;
684  CLHEP::Hep3Vector dW(diff[3],diff[4],diff[5]);
685  CLHEP::HepRotation rot(Wtotal.unit(),Wtotal.mag());
686  CLHEP::HepRotation drot(dW.unit(),dW.mag());
687  rot*=drot;
688  Wtotal.set(rot.axis().x()*rot.delta(), rot.axis().y()*rot.delta(),
689  rot.axis().z()*rot.delta());
690  align::moveAlignable(curComp2[ich], diff);
691  float tolerance = 1e-7;
692  AlgebraicVector check = align::diffAlignables(refComp[ich],curComp2[ich],
694  align::GlobalVector checkR(check[0],check[1],check[2]);
695  align::GlobalVector checkW(check[3],check[4],check[5]);
696  if ((checkR.mag() > tolerance)||(checkW.mag() > tolerance)){}
697  else{break;}
698  } // end of for on int i
699  AlgebraicVector TRtot(6);
700  TRtot(1) = Rtotal.x(); TRtot(2) = Rtotal.y(); TRtot(3) = Rtotal.z();
701  TRtot(4) = Wtotal.x(); TRtot(5) = Wtotal.y(); TRtot(6) = Wtotal.z();
702  fillTree(refComp[ich], TRtot);
703  } // end of for on ich
704 
705 
706 
707 }
708 
710 
712 
713 
714  _id = refAli->id();
715  _level = refAli->alignableObjectId();
716  //need if ali has no mother
717  if (refAli->mother()){
718  _mid = refAli->mother()->geomDetId().rawId();
719  _mlevel = refAli->mother()->alignableObjectId();
720  }
721  else{
722  _mid = -1;
723  _mlevel = -1;
724  }
725  DetId detid(_id);
726  _sublevel = detid.subdetId();
727  int ringPhiPos=-99;
728  if(detid.det()==DetId::Muon && detid.subdetId()== MuonSubdetId::CSC){
729  CSCDetId cscId(refAli->geomDetId());
730  ringPhiPos = cscId.chamber();
731  }
732  _xVal = refAli->globalPosition().x();
733  _yVal = refAli->globalPosition().y();
734  _zVal = refAli->globalPosition().z();
736  _rVal = vec.perp();
737  _phiVal = vec.phi();
738  _etaVal = vec.eta();
740  align::EulerAngles eulerAngles = align::toAngles(rot);
741  _rotxVal = atan2(rot.yz(), rot.zz());
742  float ttt=-rot.xz();
743  if(ttt>1.) ttt=1.;
744  if(ttt<-1.) ttt=-1.;
745  _rotyVal = asin(ttt);
746  _rotzVal = atan2(rot.xy(), rot.xx());
747  _alphaVal = eulerAngles[0];
748  _betaVal = eulerAngles[1];
749  _gammaVal = eulerAngles[2];
750  _dxVal = diff[0];
751  _dyVal = diff[1];
752  _dzVal = diff[2];
753  //getting dR and dPhi
756  _drVal = vCur.perp() - vRef.perp();
757  _dphiVal = vCur.phi() - vRef.phi();
758 
759  _dalphaVal = diff[3];
760  _dbetaVal = diff[4];
761  _dgammaVal = diff[5];
762  _drotxVal=-999.; _drotyVal=-999.; _drotzVal=-999.;
763 
764  align::EulerAngles deuler(3);
765  deuler(1)=_dalphaVal;
766  deuler(2)= _dbetaVal;
767  deuler(3)= _dgammaVal;
768  align::RotationType drot = align::toMatrix(deuler);
769  double xx=rot.xx();
770  double xy=rot.xy();
771  double xz=rot.xz();
772  double yx=rot.yx();
773  double yy=rot.yy();
774  double yz=rot.yz();
775  double zx=rot.zx();
776  double zy=rot.zy();
777  double zz=rot.zz();
778  double detrot=(zz*yy - zy*yz)*xx + (-zz*yx + zx*yz)*xy + (zy*yx - zx*yy)*xz;
779  detrot=1/detrot;
780  double ixx=(zz*yy - zy*yz)*detrot;
781  double ixy=(-zz*xy + zy*xz)*detrot;
782  double ixz=(yz*xy - yy*xz)*detrot;
783  double iyx=(-zz*yx + zx*yz)*detrot;
784  double iyy=(zz*xx - zx*xz)*detrot;
785  double iyz=(-yz*xx + yx*xz)*detrot;
786  double izx=(zy*yx - zx*yy)*detrot;
787  double izy=(-zy*xx + zx*xy)*detrot;
788  double izz=(yy*xx - yx*xy)*detrot;
789  align::RotationType invrot(ixx,ixy,ixz, iyx,iyy,iyz, izx,izy,izz);
790  align::RotationType prot = rot*drot*invrot;
791 // align::RotationType prot = rot*drot;
792  float protx; //, proty, protz;
793  protx = atan2(prot.yz(), prot.zz());
794  _drotxVal = protx;//_rotxVal-protx; //atan2(drot.yz(), drot.zz());
795  ttt=-prot.xz();
796  if(ttt>1.) ttt=1.;
797  if(ttt<-1.) ttt=-1.;
798  _drotyVal = asin(ttt);// -_rotyVal;
799  _drotzVal = atan2(prot.xy(), prot.xx());// - _rotzVal;
800 // Above does not account for 2Pi wraparounds!
801 // Prior knowledge: these are supposed to be small rotations. Therefore:
802  if(_drotxVal>3.141592656) _drotxVal=-6.2831853072+_drotxVal;
803  if(_drotxVal<-3.141592656) _drotxVal=6.2831853072+_drotxVal;
804  if(_drotyVal>3.141592656) _drotyVal=-6.2831853072+_drotyVal;
805  if(_drotyVal<-3.141592656) _drotyVal=6.2831853072+_drotyVal;
806  if(_drotzVal>3.141592656) _drotzVal=-6.2831853072+_drotzVal;
807  if(_drotzVal<-3.141592656) _drotzVal=6.2831853072+_drotzVal;
808 
809  _ldxVal=-999.; _ldyVal=-999.; _ldxVal=-999.;
810  _ldrVal=-999.; _ldphiVal=-999; // set fake
811 
812 // if(refAli->alignableObjectId() == align::AlignableDetUnit){
814  align::LocalVector pointL = refAli->surface().toLocal(dV);
815  //align::LocalVector pointL = (refAli->mother())->surface().toLocal(dV);
816  _ldxVal=pointL.x(); _ldyVal=pointL.y(); _ldzVal=pointL.z();
817  _ldphiVal=pointL.phi(); _ldrVal=pointL.perp();
818 // }
819  //detIdFlag
820  if (refAli->alignableObjectId() == align::AlignableDetUnit){
821  if (_detIdFlag){
822  if ((passIdCut(refAli->id()))||(passIdCut(refAli->mother()->id()))){
823  _useDetId = 1;
824  }
825  else{
826  _useDetId = 0;
827  }
828  }
829  }
830  // det module dimension
831  if (refAli->alignableObjectId() == align::AlignableDetUnit){
832  if (refAli->mother()->alignableObjectId() != align::AlignableDet){
833  _detDim = 1;}
834  else if (refAli->mother()->alignableObjectId() ==
836  }
837  else _detDim = 0;
838 
839 
840 
841  _surWidth = refAli->surface().width();
842  _surLength = refAli->surface().length();
843  align::RotationType rt = refAli->globalRotation();
844  _surRot[0] = rt.xx(); _surRot[1] = rt.xy(); _surRot[2] = rt.xz();
845  _surRot[3] = rt.yx(); _surRot[4] = rt.yy(); _surRot[5] = rt.yz();
846  _surRot[6] = rt.zx(); _surRot[7] = rt.zy(); _surRot[8] = rt.zz();
847 
848  MGACollection holdit;
849  holdit.id=_id; holdit.level=_level; holdit.mid=_mid;
850  holdit.mlevel=_mlevel;
851  holdit.sublevel=_sublevel;
852  holdit.x=_xVal; holdit.y=_yVal; holdit.z=_zVal;
853  holdit.r=_rVal; holdit.phi=_phiVal; holdit.eta=_etaVal;
854  holdit.alpha=_alphaVal; holdit.beta=_betaVal; holdit.gamma=_gammaVal;
855  holdit.dx=_dxVal; holdit.dy=_dyVal; holdit.dz=_dzVal;
856  holdit.dr=_drVal; holdit.dphi=_dphiVal;
857  holdit.dalpha=_dalphaVal; holdit.dbeta=_dbetaVal;
858  holdit.dgamma=_dgammaVal;
859  holdit.useDetId=_useDetId; holdit.detDim=_detDim;
860  holdit.surW=_surWidth; holdit.surL=_surLength;
861  holdit.ldx=_ldxVal; holdit.ldy=_ldyVal; holdit.ldz=_ldzVal;
862  holdit.ldr=_ldrVal; holdit.ldphi=_ldphiVal;
863  holdit.rotx=_rotxVal; holdit.roty=_rotyVal; holdit.rotz=_rotzVal;
864  holdit.drotx=_drotxVal; holdit.droty=_drotyVal; holdit.drotz=_drotzVal;
865  for(int i=0; i<9; i++){holdit.surRot[i]=_surRot[i];}
866  holdit.phipos=ringPhiPos;
867  _mgacollection.push_back(holdit);
868 
869 
870  //Fill
871  _alignTree->Fill();
872 
873 }
874 
877  // Is this the mother ring?
878  if(ali==nullptr) return false; // elementary sanity
879  const auto& aliComp = ali->components();
880 
881  int size=aliComp.size();
882  if(size<=0) return false; // no subcomponents
883 
884  for(int i=0; i<size; i++){
885  if(checkChosen(aliComp[i])) return true; // A ring has CSC chambers
886  } // as subcomponents
887  return false; // 1'st layer of subcomponents weren't CSC chambers
888 }
890 
892  // Check whether the item passed satisfies the criteria given.
893  if(ali==nullptr) return false; // elementary sanity
894  // Is this in the CSC section? If not, bail. Later may extend.
895  if(ali->geomDetId().det()!=DetId::Muon ||
896  ali->geomDetId().subdetId()!=MuonSubdetId::CSC) return false;
897  // If it is a CSC alignable, then check that the station, etc are
898  // those requested.
899  // One might think of aligning more than a single ring at a time,
900  // by using a vector of ring numbers. I don't see the sense in
901  // trying to align more than one station at a time for comparison.
902  CSCDetId cscId(ali->geomDetId());
903 #ifdef jnbdebug
904 std::cout<<"JNB "<<ali->id()<<" "<<cscId.endcap()<<" "
905 <<cscId.station()<<" "<<cscId.ring()<<" "<<cscId.chamber()<<" "
906 <<_endcap<<" "<<_station<<" "<<_ring
907 <<"\n"<<std::flush;
908 #endif
909  if(cscId.endcap()==_endcap && cscId.station()==_station &&
910  cscId.ring()==_ring) {
911  return true;
912  }
913  return false;
914 }
916 
918 
919  // Check to see if this contains CSC components of the appropriate ring
920  // Ring will contain N Alignables which represent chambers, each of which
921  // in turn contains M planes. For our purposes we don't care about the
922  // planes.
923  // Hmm. Interesting question: Do I want to try to fit the chamber as
924  // such, or use the geometry?
925  // I want to fit the chamber, so I'll try to use its presence as the marker.
926  // What specifically identifies a chamber as a chamber, and not as a layer?
927  // The fact that it has layers as sub components, or the fact that it is
928  // the first item with a non-zero ID breakdown? Pick the latter.
929  //
930  if(ali==nullptr) return false;
931  if(checkChosen(ali)) return true; // If this is one of the desired
932  // CSC chambers, accept it
933  const auto& aliComp = ali->components();
934 
935  int size=aliComp.size();
936  if(size<=0) return false; // no subcomponents
937 
938  for(int i=0; i<size; i++){
939  if(checkChosen(aliComp[i])) return true; // A ring has CSC chambers
940  } // as subcomponents
941  return false; // 1'st layer of subcomponents weren't CSC chambers
942 }
944 bool MuonGeometryArrange::passIdCut( uint32_t id ){
945 
946  bool pass = false;
947  DetId detid(id);
948 // if(detid.det()==DetId::Muon && detid.subdetId()== MuonSubdetId::CSC){
949 // CSCDetId cscId(refAli->geomDetId());
950 // if(cscId.layer()!=1) return false; // ONLY FIRST LAYER!
951 // }
952  int nEntries = _detIdFlagVector.size();
953 
954  for (int i = 0; i < nEntries; i++){
955  if (_detIdFlagVector[i] == id) pass = true;
956  }
957 
958  return pass;
959 
960 }
961 
962 
size
Write out results.
T xx() const
align::Scalar width() const
int chamber() const
Definition: CSCDetId.h:68
align::ID id() const
Return the ID of Alignable, i.e. DetId of &#39;first&#39; component GeomDet(Unit).
Definition: Alignable.h:189
T getUntrackedParameter(std::string const &, T const &) const
bool passChosen(Alignable *ali)
std::vector< align::StructureType > theLevels
T perp() const
Definition: PV3DBase.h:72
void compareGeometries(Alignable *refAli, Alignable *curAli, Alignable *curAliCopy2)
int iyy[18][41][3]
bool isMother(Alignable *ali)
const double tolerance
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
MuonGeometryArrange(const edm::ParameterSet &)
Do nothing. Required by framework.
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
T y() const
Definition: PV3DBase.h:63
T yx() const
std::vector< unsigned int > _weightByIdVector
bool checkChosen(Alignable *ali)
AlgebraicVector diffAlignables(Alignable *refAli, Alignable *curAli, const std::string &weightBy, bool weightById, const std::vector< unsigned int > &weightByIdVector)
Definition: AlignTools.cc:10
const RotationType & globalRotation() const
Return the global orientation of the object.
Definition: Alignable.h:141
int ixx[18][41][3]
virtual const Alignables & components() const =0
Return vector of all direct components.
void analyze(const edm::Event &, const edm::EventSetup &) override
void createPoints(GlobalVectors *Vs, Alignable *ali, const std::string &weightBy, bool weightById, const std::vector< unsigned int > &weightByIdVector)
Definition: AlignTools.cc:92
MuonAlignment * inputAlign2a
MuonAlignment * inputAlign2
RotationType diffRot(const GlobalVectors &current, const GlobalVectors &nominal)
Definition: Utilities.cc:73
T zx() const
T xy() const
void createROOTGeometry(const edm::EventSetup &iSetup)
T zz() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
T mag() const
Definition: PV3DBase.h:67
align::RotationType toLocal(const align::RotationType &) const
Return in local frame a rotation given in global frame.
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
AlignableMuon * getAlignableMuon()
Definition: MuonAlignment.h:30
void makeGraph(int sizeI, float smi, float sma, float minV, float maxV, TH2F *dxh, TGraph *grx, const char *name, const char *title, const char *titleg, const char *axis, const float *xp, const float *yp, int numEntries)
T sqrt(T t)
Definition: SSEVec.h:18
void compare(Alignable *refAli, Alignable *curAli, Alignable *curAliCopy2)
T z() const
Definition: PV3DBase.h:64
bool readModuleList(unsigned int, unsigned int, const std::vector< unsigned int > &)
Definition: AlignTools.cc:142
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
T zy() const
std::vector< uint32_t > _detIdFlagVector
EulerAngles toAngles(const RotationType &)
Convert rotation matrix to angles about x-, y-, z-axes (frame rotation).
Definition: Utilities.cc:9
T yy() const
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
Definition: Alignable.h:135
MuonAlignment * inputAlign1
GlobalVector centerOfMass(const GlobalVectors &theVs)
Find the CM of a set of points.
Definition: Utilities.cc:187
void fillTree(Alignable *refAli, const AlgebraicVector &diff)
Definition: DetId.h:18
CLHEP::HepVector AlgebraicVector
AlgebraicVector EulerAngles
Definition: Definitions.h:36
align::Scalar length() const
void fillGapsInSurvey(double shiftErr, double angleErr)
const std::vector< std::string > _levelStrings
AlignableMuon * referenceMuon
std::vector< GlobalVector > GlobalVectors
Definition: Utilities.h:29
T eta() const
Definition: PV3DBase.h:76
AlignableMuon * currentMuon
T xz() const
RotationType toMatrix(const EulerAngles &)
Convert rotation angles about x-, y-, z-axes to matrix.
Definition: Utilities.cc:42
const PositionType & globalPosition() const
Return the global position of the object.
Definition: Alignable.h:138
def check(config)
Definition: trackerTree.py:14
static constexpr int CSC
Definition: MuonSubdetId.h:13
T x() const
Definition: PV3DBase.h:62
void moveAlignable(Alignable *ali, AlgebraicVector diff)
Moves the alignable by the AlgebraicVector.
Definition: AlignTools.cc:81
Alignable * mother() const
Return pointer to container alignable (if any)
Definition: Alignable.h:94
T yz() const
const DetId & geomDetId() const
Definition: Alignable.h:186
void beginJob() override
Read from DB and print survey info.
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:39
std::vector< MGACollection > _mgacollection