CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MagGeometry.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * $Date: 2012/03/21 23:01:46 $
5  * $Revision: 1.20 $
6  * \author N. Amapane - INFN Torino
7  */
8 
14 
16 
18 
21 
22 using namespace std;
23 using namespace edm;
24 
25 MagGeometry::MagGeometry(const edm::ParameterSet& config, std::vector<MagBLayer *> tbl,
26  std::vector<MagESector *> tes,
27  std::vector<MagVolume6Faces*> tbv,
28  std::vector<MagVolume6Faces*> tev) :
29  lastVolume(0), theBLayers(tbl), theESectors(tes), theBVolumes(tbv), theEVolumes(tev), geometryVersion(0)
30 {
31 
32  cacheLastVolume = config.getUntrackedParameter<bool>("cacheLastVolume", true);
33 
34  // FIXME: wait geometryVersion to be propagated to all cfgs.
35  if (config.exists("geometryVersion")) {
36  geometryVersion = config.getParameter<int>("geometryVersion");
37  } else {
38  geometryVersion = 90322;
39  }
40 
41  vector<double> rBorders;
42 
43  for (vector<MagBLayer *>::const_iterator ilay = theBLayers.begin();
44  ilay != theBLayers.end(); ++ilay) {
45  if (verbose::debugOut) cout << " Barrel layer at " << (*ilay)->minR() <<endl;
46  //FIXME assume layers are already sorted in minR
47  rBorders.push_back((*ilay)->minR());
48  }
49 
51 
52  if (verbose::debugOut) {
53  for (vector<MagESector *>::const_iterator isec = theESectors.begin();
54  isec != theESectors.end(); ++isec) {
55  cout << " Endcap sector at " << (*isec)->minPhi() << endl;
56  }
57  }
58 
59  //FIXME assume sectors are already sorted in phi
60  //FIXME: PeriodicBinFinderInPhi gets *center* of first bin
61  int nEBins = theESectors.size();
62  theEndcapBinFinder = new PeriodicBinFinderInPhi<float>(theESectors.front()->minPhi()+Geom::pi()/nEBins, nEBins);
63 
64 }
65 
67  delete theBarrelBinFinder;
68  delete theEndcapBinFinder;
69 
70  for (vector<MagBLayer *>::const_iterator ilay = theBLayers.begin();
71  ilay != theBLayers.end(); ++ilay) {
72  delete (*ilay);
73  }
74 
75  for (vector<MagESector *>::const_iterator ilay = theESectors.begin();
76  ilay != theESectors.end(); ++ilay) {
77  delete (*ilay);
78  }
79 }
80 
81 
82 // Return field vector at the specified global point
84  MagVolume * v = 0;
85 
86 
87  v = findVolume(gp);
88  if (v!=0) {
89  return v->fieldInTesla(gp);
90  }
91 
92  // Fall-back case: no volume found
93 
94  if (isnan(gp.mag())) {
95  LogWarning("InvalidInput") << "Input value invalid (not a number): " << gp << endl;
96 
97  } else {
98  LogWarning("MagneticField") << "MagGeometry::fieldInTesla: failed to find volume for " << gp << endl;
99  }
100  return GlobalVector();
101 }
102 
103 
104 // Linear search implementation (just for testing)
105 MagVolume*
106 MagGeometry::findVolume1(const GlobalPoint & gp, double tolerance) const {
107 
108  MagVolume6Faces * found = 0;
109 
110  if (inBarrel(gp)) { // Barrel
111  for (vector<MagVolume6Faces*>::const_iterator v = theBVolumes.begin();
112  v!=theBVolumes.end(); ++v){
113  if ((*v)==0) { //FIXME: remove this check
114  cout << endl << "***ERROR: MagGeometry::findVolume: MagVolume not set" << endl;
115  continue;
116  }
117  if ((*v)->inside(gp,tolerance)) {
118  found = (*v);
119  break;
120  }
121  }
122 
123  } else { // Endcaps
124  for (vector<MagVolume6Faces*>::const_iterator v = theEVolumes.begin();
125  v!=theEVolumes.end(); ++v){
126  if ((*v)==0) { //FIXME: remove this check
127  cout << endl << "***ERROR: MagGeometry::findVolume: MagVolume not set" << endl;
128  continue;
129  }
130  if ((*v)->inside(gp,tolerance)) {
131  found = (*v);
132  break;
133  }
134  }
135  }
136 
137  return found;
138 }
139 
140 // Use hierarchical structure for fast lookup.
141 MagVolume*
142 MagGeometry::findVolume(const GlobalPoint & gp, double tolerance) const{
143  // Check volume cache
144  if (lastVolume!=0 && lastVolume->inside(gp)){
145  return lastVolume;
146  }
147 
148  MagVolume * result=0;
149  if (inBarrel(gp)) { // Barrel
150  double R = gp.perp();
151  int bin = theBarrelBinFinder->binIndex(R);
152 
153  for (int bin1 = bin; bin1 >= max(0,bin-2); --bin1) {
154  if (verbose::debugOut) cout << "Trying layer at R " << theBLayers[bin1]->minR()
155  << " " << R << endl ;
156  result = theBLayers[bin1]->findVolume(gp, tolerance);
157  if (verbose::debugOut) cout << "***In blayer " << bin1-bin << " "
158  << (result==0? " failed " : " OK ") <<endl;
159  if (result != 0) break;
160  }
161 
162  } else { // Endcaps
163  Geom::Phi<float> phi = gp.phi();
164  int bin = theEndcapBinFinder->binIndex(phi);
165  if (verbose::debugOut) cout << "Trying endcap sector at phi "
166  << theESectors[bin]->minPhi() << " " << phi << endl ;
167  result = theESectors[bin]->findVolume(gp, tolerance);
168  if (verbose::debugOut) cout << "***In guessed esector "
169  << (result==0? " failed " : " OK ") <<endl;
170  }
171 
172 
173  if (result==0 && tolerance < 0.0001) {
174  // If search fails, retry with a 300 micron tolerance.
175  // This is a hack for thin gaps on air-iron boundaries,
176  // which will not be present anymore once surfaces are matched.
177  if (verbose::debugOut) cout << "Increasing the tolerance to 0.03" <<endl;
178  result = findVolume(gp, 0.03);
179  }
180 
182 
183  return result;
184 }
185 
186 
187 
188 
189 bool MagGeometry::inBarrel(const GlobalPoint& gp) const {
190  float Z = fabs(gp.z());
191  float R = gp.perp();
192 
193  // FIXME: Get these dimensions from the builder.
194  if (geometryVersion>=90812) {
195  return (Z<350. ||
196  (R>172.4 && Z<633.89) ||
197  (R>308.755 && Z<662.01));
198  } else {
199  return (Z<350. ||
200  (R>172.4 && Z<633.29) ||
201  (R>308.755 && Z<661.01));
202  }
203 }
204 
const double Z[kNumberCalorimeter]
Surface::GlobalVector GlobalVector
Definition: MagGeometry.h:28
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
T perp() const
Definition: PV3DBase.h:71
virtual int binIndex(T R) const
Definition: MagBinFinders.h:68
MagVolume * lastVolume
Definition: MagGeometry.h:63
MagVolume * findVolume1(const GlobalPoint &gp, double tolerance=0.) const
Definition: MagGeometry.cc:106
Geom::Phi< T > phi() const
Definition: PV3DBase.h:68
bool exists(std::string const &parameterName) const
checks if a parameter exists
PeriodicBinFinderInPhi< float > * theEndcapBinFinder
Definition: MagGeometry.h:73
~MagGeometry()
Destructor.
Definition: MagGeometry.cc:66
static bool debugOut
Definition: MagVerbosity.h:14
bool inBarrel(const GlobalPoint &gp) const
Definition: MagGeometry.cc:189
virtual int binIndex(T phi) const
returns an index in the valid range for the bin that contains phi
MagGeometry(const edm::ParameterSet &config, std::vector< MagBLayer * >, std::vector< MagESector * >, std::vector< MagVolume6Faces * >, std::vector< MagVolume6Faces * >)
Constructor.
Definition: MagGeometry.cc:25
T mag() const
Definition: PV3DBase.h:66
virtual bool inside(const GlobalPoint &gp, double tolerance=0.) const =0
MagBinFinders::GeneralBinFinderInR< double > * theBarrelBinFinder
Definition: MagGeometry.h:72
const T & max(const T &a, const T &b)
bool isnan(float x)
Definition: math.h:13
T z() const
Definition: PV3DBase.h:63
tuple result
Definition: query.py:137
std::vector< MagVolume6Faces * > theEVolumes
Definition: MagGeometry.h:70
std::vector< MagVolume6Faces * > theBVolumes
Definition: MagGeometry.h:69
std::vector< MagESector * > theESectors
Definition: MagGeometry.h:66
LocalVector fieldInTesla(const LocalPoint &lp) const
Definition: MagVolume.cc:11
double pi()
Definition: Pi.h:31
GlobalVector fieldInTesla(const GlobalPoint &gp) const
Return field vector at the specified global point.
Definition: MagGeometry.cc:83
tuple cout
Definition: gather_cfg.py:121
MagVolume * findVolume(const GlobalPoint &gp, double tolerance=0.) const
Find a volume.
Definition: MagGeometry.cc:142
bool cacheLastVolume
Definition: MagGeometry.h:75
mathSSE::Vec4< T > v
int geometryVersion
Definition: MagGeometry.h:76
std::vector< MagBLayer * > theBLayers
Definition: MagGeometry.h:65
Definition: DDAxes.h:10