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: 2009/03/03 13:07:49 $
5  * $Revision: 1.17 $
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)
30 {
31 
32  cacheLastVolume = config.getUntrackedParameter<bool>("cacheLastVolume", true);
33  v_85l = (config.getParameter<std::string>("version")=="grid_85l_030919");
34 
35  vector<double> rBorders;
36 
37  for (vector<MagBLayer *>::const_iterator ilay = theBLayers.begin();
38  ilay != theBLayers.end(); ++ilay) {
39  if (verbose::debugOut) cout << " Barrel layer at " << (*ilay)->minR() <<endl;
40  //FIXME assume layers are already sorted in minR
41  rBorders.push_back((*ilay)->minR());
42  }
43 
45 
46  if (verbose::debugOut) {
47  for (vector<MagESector *>::const_iterator isec = theESectors.begin();
48  isec != theESectors.end(); ++isec) {
49  cout << " Endcap sector at " << (*isec)->minPhi() << endl;
50  }
51  }
52 
53  //FIXME assume sectors are already sorted in phi
54  //FIXME: PeriodicBinFinderInPhi gets *center* of first bin
55  theEndcapBinFinder = new PeriodicBinFinderInPhi<float>(theESectors.front()->minPhi()+Geom::pi()/12., 12);
56 
57 }
58 
60  delete theBarrelBinFinder;
61  delete theEndcapBinFinder;
62 
63  for (vector<MagBLayer *>::const_iterator ilay = theBLayers.begin();
64  ilay != theBLayers.end(); ++ilay) {
65  delete (*ilay);
66  }
67 
68  for (vector<MagESector *>::const_iterator ilay = theESectors.begin();
69  ilay != theESectors.end(); ++ilay) {
70  delete (*ilay);
71  }
72 }
73 
74 
75 // Return field vector at the specified global point
77  MagVolume * v = 0;
78 
79  // Map version 85l is Z-symmetric; -> implement Z reflection
80  if (v_85l && gp.z()>0) {
81  GlobalPoint gpSym(gp.x(), gp.y(), -gp.z());
82  // Check volume cache
83  v = findVolume(gpSym);
84 
85  if (v!=0) {
86  GlobalVector bresult = v->fieldInTesla(gpSym);
87  return GlobalVector(-bresult.x(), -bresult.y(), bresult.z());
88  }
89 
90  } else { // No reflection
91  v = findVolume(gp);
92  if (v!=0) {
93  return v->fieldInTesla(gp);
94  }
95  }
96 
97  // Fall-back case: no volume found
98 
99  if (isnan(gp.mag())) {
100  LogWarning("InvalidInput") << "Input value invalid (not a number): " << gp << endl;
101 
102  } else {
103  LogWarning("MagneticField") << "MagGeometry::fieldInTesla: failed to find volume for " << gp << endl;
104  }
105  return GlobalVector();
106 }
107 
108 
109 // Linear search implementation (just for testing)
110 MagVolume*
111 MagGeometry::findVolume1(const GlobalPoint & gp, double tolerance) const {
112 
113  MagVolume6Faces * found = 0;
114 
115  if (inBarrel(gp)) { // Barrel
116  for (vector<MagVolume6Faces*>::const_iterator v = theBVolumes.begin();
117  v!=theBVolumes.end(); ++v){
118  if ((*v)==0) { //FIXME: remove this check
119  cout << endl << "***ERROR: MagGeometry::findVolume: MagVolume not set" << endl;
120  continue;
121  }
122  if ((*v)->inside(gp,tolerance)) {
123  found = (*v);
124  break;
125  }
126  }
127 
128  } else { // Endcaps
129  for (vector<MagVolume6Faces*>::const_iterator v = theEVolumes.begin();
130  v!=theEVolumes.end(); ++v){
131  if ((*v)==0) { //FIXME: remove this check
132  cout << endl << "***ERROR: MagGeometry::findVolume: MagVolume not set" << endl;
133  continue;
134  }
135  if ((*v)->inside(gp,tolerance)) {
136  found = (*v);
137  break;
138  }
139  }
140  }
141 
142  return found;
143 }
144 
145 // Use hierarchical structure for fast lookup.
146 MagVolume*
147 MagGeometry::findVolume(const GlobalPoint & gp, double tolerance) const{
148  // Check volume cache
149  if (lastVolume!=0 && lastVolume->inside(gp)){
150  return lastVolume;
151  }
152 
153  MagVolume * result=0;
154  if (inBarrel(gp)) { // Barrel
155  double R = gp.perp();
156  int bin = theBarrelBinFinder->binIndex(R);
157 
158  for (int bin1 = bin; bin1 >= max(0,bin-2); --bin1) {
159  if (verbose::debugOut) cout << "Trying layer at R " << theBLayers[bin1]->minR()
160  << " " << R << endl ;
161  result = theBLayers[bin1]->findVolume(gp, tolerance);
162  if (verbose::debugOut) cout << "***In blayer " << bin1-bin << " "
163  << (result==0? " failed " : " OK ") <<endl;
164  if (result != 0) break;
165  }
166 
167  } else { // Endcaps
168  Geom::Phi<float> phi = gp.phi();
169  int bin = theEndcapBinFinder->binIndex(phi);
170  if (verbose::debugOut) cout << "Trying endcap sector at phi "
171  << theESectors[bin]->minPhi() << " " << phi << endl ;
172  result = theESectors[bin]->findVolume(gp, tolerance);
173  if (verbose::debugOut) cout << "***In guessed esector "
174  << (result==0? " failed " : " OK ") <<endl;
175  }
176 
177 
178  if (result==0 && tolerance < 0.0001) {
179  // If search fails, retry with a 300 micron tolerance.
180  // This is a hack for thin gaps on air-iron boundaries,
181  // which will not be present anymore once surfaces are matched.
182  if (verbose::debugOut) cout << "Increasing the tolerance to 0.03" <<endl;
183  result = findVolume(gp, 0.03);
184  }
185 
187 
188  return result;
189 }
190 
191 
192 
193 
194 bool MagGeometry::inBarrel(const GlobalPoint& gp) const {
195  float Z = fabs(gp.z());
196  float R = gp.perp();
197 
198  // FIXME: Get these dimensions from the builder.
199  // For this we can wait the next generation of tables, when the picture
200  // may be more complicated
201  if (v_85l){
202  return (Z<634.49 || (R>308.755 && Z<661.01));
203  } else {
204  return (Z<350. ||
205  (R>172.4 && Z<633.29) ||
206  (R>308.755 && Z<661.01));
207  }
208 }
209 
210 
212  return v_85l;
213 }
214 
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
bool isZSymmetric() const
Definition: MagGeometry.cc:211
T perp() const
Definition: PV3DBase.h:66
virtual int binIndex(T R) const
Definition: MagBinFinders.h:68
MagVolume * lastVolume
Definition: MagGeometry.h:62
MagVolume * findVolume1(const GlobalPoint &gp, double tolerance=0.) const
Definition: MagGeometry.cc:111
Geom::Phi< T > phi() const
Definition: PV3DBase.h:63
T y() const
Definition: PV3DBase.h:57
PeriodicBinFinderInPhi< float > * theEndcapBinFinder
Definition: MagGeometry.h:72
~MagGeometry()
Destructor.
Definition: MagGeometry.cc:59
static bool debugOut
Definition: MagVerbosity.h:14
bool inBarrel(const GlobalPoint &gp) const
Definition: MagGeometry.cc:194
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:61
virtual bool inside(const GlobalPoint &gp, double tolerance=0.) const =0
MagBinFinders::GeneralBinFinderInR< double > * theBarrelBinFinder
Definition: MagGeometry.h:71
const T & max(const T &a, const T &b)
bool isnan(float x)
Definition: math.h:13
T z() const
Definition: PV3DBase.h:58
tuple result
Definition: query.py:137
std::vector< MagVolume6Faces * > theEVolumes
Definition: MagGeometry.h:69
std::vector< MagVolume6Faces * > theBVolumes
Definition: MagGeometry.h:68
std::vector< MagESector * > theESectors
Definition: MagGeometry.h:65
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:76
tuple cout
Definition: gather_cfg.py:41
MagVolume * findVolume(const GlobalPoint &gp, double tolerance=0.) const
Find a volume.
Definition: MagGeometry.cc:147
T x() const
Definition: PV3DBase.h:56
bool cacheLastVolume
Definition: MagGeometry.h:74
mathSSE::Vec4< T > v
std::vector< MagBLayer * > theBLayers
Definition: MagGeometry.h:64
Definition: DDAxes.h:10