00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "MagneticField/GeomBuilder/src/MagGeoBuilderFromDDD.h"
00010 #include "MagneticField/GeomBuilder/src/volumeHandle.h"
00011 #include "MagneticField/GeomBuilder/src/bSlab.h"
00012 #include "MagneticField/GeomBuilder/src/bRod.h"
00013 #include "MagneticField/GeomBuilder/src/bSector.h"
00014 #include "MagneticField/GeomBuilder/src/bLayer.h"
00015 #include "MagneticField/GeomBuilder/src/eSector.h"
00016 #include "MagneticField/GeomBuilder/src/eLayer.h"
00017 #include "MagneticField/GeomBuilder/src/FakeInterpolator.h"
00018
00019 #include "MagneticField/Layers/interface/MagBLayer.h"
00020 #include "MagneticField/Layers/interface/MagESector.h"
00021
00022 #include "FWCore/ParameterSet/interface/FileInPath.h"
00023
00024 #include "DetectorDescription/Core/interface/DDCompactView.h"
00025 #include "DetectorDescription/Core/interface/DDFilteredView.h"
00026 #include "DetectorDescription/Core/interface/DDFilter.h"
00027
00028 #include "Utilities/BinningTools/interface/ClusterizingHistogram.h"
00029 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00030
00031 #include "MagneticField/Interpolation/interface/MagProviderInterpol.h"
00032 #include "MagneticField/Interpolation/interface/MFGridFactory.h"
00033 #include "MagneticField/Interpolation/interface/MFGrid.h"
00034
00035 #include "MagneticField/VolumeGeometry/interface/MagVolume6Faces.h"
00036 #include "MagneticField/VolumeGeometry/interface/MagExceptions.h"
00037 #include "MagneticField/Layers/interface/MagVerbosity.h"
00038
00039 #include "DataFormats/GeometryVector/interface/Pi.h"
00040
00041 #include "FWCore/Utilities/interface/Exception.h"
00042 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00043
00044 #include <string>
00045 #include <vector>
00046 #include <iostream>
00047 #include <sstream>
00048 #include <algorithm>
00049 #include <iterator>
00050 #include <map>
00051 #include <set>
00052 #include <iomanip>
00053 #include "Utilities/General/interface/precomputed_value_sort.h"
00054
00055
00056 bool MagGeoBuilderFromDDD::debug;
00057
00058 using namespace std;
00059
00060 MagGeoBuilderFromDDD::MagGeoBuilderFromDDD(string tableSet_, bool debug_, bool overrideMasterSector_) :
00061 tableSet (tableSet_),
00062 geometryVersion(0),
00063 overrideMasterSector(overrideMasterSector_)
00064 {
00065 debug = debug_;
00066 if (debug) cout << "Constructing a MagGeoBuilderFromDDD" <<endl;
00067 }
00068
00069 MagGeoBuilderFromDDD::~MagGeoBuilderFromDDD(){
00070 for (handles::const_iterator i=bVolumes.begin();
00071 i!=bVolumes.end(); ++i){
00072 delete (*i);
00073 }
00074
00075 for (handles::const_iterator i=eVolumes.begin();
00076 i!=eVolumes.end(); ++i){
00077 delete (*i);
00078 }
00079 }
00080
00081
00082 void MagGeoBuilderFromDDD::summary(handles & volumes){
00083
00084 int ivolumes = volumes.size();
00085 int isurfaces = ivolumes*6;
00086 int iassigned = 0;
00087 int iunique = 0;
00088 int iref_ass = 0;
00089 int iref_nass = 0;
00090
00091 set<const void *> ptrs;
00092
00093 handles::const_iterator first = volumes.begin();
00094 handles::const_iterator last = volumes.end();
00095
00096 for (handles::const_iterator i=first; i!=last; ++i){
00097 if (int((*i)->shape())>4) continue;
00098 for (int side = 0; side < 6; ++side) {
00099 int references = (*i)->references(side);
00100 if ((*i)->isPlaneMatched(side)) {
00101 ++iassigned;
00102 bool firstOcc = (ptrs.insert(&((*i)->surface(side)))).second;
00103 if (firstOcc) iref_ass+=references;
00104 if (references<2){
00105 cout << "*** Only 1 ref, vol: " << (*i)->name << " # "
00106 << (*i)->copyno << " side: " << side << endl;
00107 }
00108 } else {
00109 iref_nass+=references;
00110 if (references>1){
00111 cout << "*** Ref_nass >1 " <<endl;
00112 }
00113 }
00114 }
00115 }
00116 iunique = ptrs.size();
00117
00118 cout << " volumes " << ivolumes << endl
00119 << " surfaces " << isurfaces << endl
00120 << " assigned " << iassigned << endl
00121 << " unique " << iunique << endl
00122 << " iref_ass " << iref_ass << endl
00123 << " iref_nass " << iref_nass << endl;
00124 }
00125
00126
00127 void MagGeoBuilderFromDDD::build(const DDCompactView & cpva)
00128 {
00129
00130 DDExpandedView fv(cpva);
00131
00132 if (debug) cout << "**********************************************************" <<endl;
00133
00134
00135 map<string, MagProviderInterpol*> bInterpolators;
00136 map<string, MagProviderInterpol*> eInterpolators;
00137
00138
00139 int bVolCount = 0;
00140 int eVolCount = 0;
00141
00142 if (fv.logicalPart().name().name()!="MAGF") {
00143 std::string topNodeName(fv.logicalPart().name().name());
00144
00145
00146 bool doSubDets = fv.firstChild();
00147
00148 bool go=true;
00149 while(go&& doSubDets) {
00150 if (fv.logicalPart().name().name()=="MAGF")
00151 break;
00152 else
00153 go = fv.nextSibling();
00154 }
00155 if (!go) {
00156 throw cms::Exception("NoMAGFinDDD")<<" Neither the top node, nor any child node of the DDCompactView is \"MAGF\" but the top node is instead \""<<topNodeName<<"\"";
00157 }
00158 }
00159
00160 if (debug) { cout << endl << "*** MAGF: " << fv.geoHistory() << endl
00161 << "translation: " << fv.translation() << endl
00162 << " rotation: " << fv.rotation() << endl;
00163 }
00164
00165 bool doSubDets = fv.firstChild();
00166 while (doSubDets){
00167
00168 string name = fv.logicalPart().name().name();
00169 if (debug) cout << endl << "Name: " << name << endl
00170 << " " << fv.geoHistory() <<endl;
00171
00172
00173
00174
00175
00176
00177
00178 bool expand = false;
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 volumeHandle* v = new volumeHandle(fv, expand);
00197
00198
00199
00200
00201 if (overrideMasterSector && v->masterSector!=1) {
00202 v->masterSector=1;
00203 std::string::size_type ibeg, iend;
00204 ibeg = (v->magFile).rfind('/');
00205 iend = (v->magFile).size();
00206 v->magFile = (v->magFile).substr(ibeg+1,iend-ibeg-1);
00207 }
00208
00209
00210 float Z = v->center().z();
00211 float R = v->center().perp();
00212
00213
00214
00215
00216
00217
00218
00219 if ((fabs(Z)<647. || (R>350. && fabs(Z)<662.)) &&
00220 !(fabs(Z)>480 && R<172)
00221
00222
00223
00224 ) {
00225 if (debug) cout << " (Barrel)" <<endl;
00226 bVolumes.push_back(v);
00227
00228
00229
00230
00231
00232
00233
00234
00235 if (v->copyno==v->masterSector) {
00236 buildInterpolator(v, bInterpolators);
00237 ++bVolCount;
00238 }
00239 } else {
00240 if (debug) cout << " (Endcaps)" <<endl;
00241 eVolumes.push_back(v);
00242 if (v->copyno==v->masterSector) {
00243 buildInterpolator(v, eInterpolators);
00244 ++eVolCount;
00245 }
00246 }
00247
00248 doSubDets = fv.nextSibling();
00249 }
00250
00251 if (debug) {
00252 cout << "Number of volumes (barrel): " << bVolumes.size() <<endl
00253 << "Number of volumes (endcap): " << eVolumes.size() <<endl;
00254 cout << "**********************************************************" <<endl;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263 if (debug) {
00264 cout << "-----------------------" << endl;
00265 cout << "SUMMARY: Barrel " << endl;
00266 summary(bVolumes);
00267
00268 cout << endl << "SUMMARY: Endcaps " << endl;
00269 summary(eVolumes);
00270 cout << "-----------------------" << endl;
00271 }
00272
00273
00274
00275
00276
00277 vector<bLayer> layers;
00278 precomputed_value_sort(bVolumes.begin(), bVolumes.end(), ExtractRN());
00279
00280
00281 const float resolution = 1.;
00282 float rmin = bVolumes.front()->RN()-resolution;
00283 float rmax = bVolumes.back()->RN()+resolution;
00284 ClusterizingHistogram hisR( int((rmax-rmin)/resolution) + 1, rmin, rmax);
00285
00286 if (debug) cout << " R layers: " << rmin << " " << rmax << endl;
00287
00288 handles::const_iterator first = bVolumes.begin();
00289 handles::const_iterator last = bVolumes.end();
00290
00291 for (handles::const_iterator i=first; i!=last; ++i){
00292 hisR.fill((*i)->RN());
00293 }
00294 vector<float> rClust = hisR.clusterize(resolution);
00295
00296 handles::const_iterator ringStart = first;
00297 handles::const_iterator separ = first;
00298
00299 for (unsigned int i=0; i<rClust.size() - 1; ++i) {
00300 if (debug) cout << " Layer at RN = " << rClust[i];
00301 float rSepar = (rClust[i] + rClust[i+1])/2.f;
00302 while ((*separ)->RN() < rSepar) ++separ;
00303
00304 bLayer thislayer(ringStart, separ);
00305 layers.push_back(thislayer);
00306 ringStart = separ;
00307 }
00308 {
00309 if (debug) cout << " Layer at RN = " << rClust.back();
00310 bLayer thislayer(separ, last);
00311 layers.push_back(thislayer);
00312 }
00313
00314 if (debug) cout << "Barrel: Found " << rClust.size() << " clusters in R, "
00315 << layers.size() << " layers " << endl << endl;
00316
00317
00318
00319
00320 vector<eSector> sectors;
00321
00322
00323 float phireso = 0.05;
00324 ClusterizingHistogram hisPhi( int((Geom::ftwoPi())/phireso) + 1,
00325 -Geom::fpi(), Geom::fpi());
00326
00327 for (handles::const_iterator i=eVolumes.begin(); i!=eVolumes.end(); ++i){
00328 hisPhi.fill((*i)->minPhi());
00329 }
00330 vector<float> phiClust = hisPhi.clusterize(phireso);
00331 int nESectors = phiClust.size();
00332 if (debug && (nESectors%12)!=0) cout << "ERROR: unexpected # of sectors: " << nESectors << endl;
00333
00334
00335 precomputed_value_sort(eVolumes.begin(), eVolumes.end(), ExtractPhi());
00336
00337
00338 for (int i = 0; i<nESectors; ++i) {
00339 int offset = eVolumes.size()/nESectors;
00340 if (debug) cout << " Sector at phi = "
00341 << (*(eVolumes.begin()+((i)*offset)))->center().phi()
00342 << endl;
00343 sectors.push_back(eSector(eVolumes.begin()+((i)*offset),
00344 eVolumes.begin()+((i+1)*offset)));
00345 }
00346
00347 if (debug) cout << "Endcap: Found "
00348 << sectors.size() << " sectors " << endl;
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 buildMagVolumes(bVolumes, bInterpolators);
00391
00392
00393 for (vector<bLayer>::const_iterator ilay = layers.begin();
00394 ilay!= layers.end(); ++ilay) {
00395 mBLayers.push_back((*ilay).buildMagBLayer());
00396 }
00397
00398 if (debug) {
00399 cout << "*** BARREL ********************************************" << endl
00400 << "Number of different volumes = " << bVolCount << endl
00401 << "Number of interpolators built = " << bInterpolators.size() << endl
00402 << "Number of MagBLayers built = " << mBLayers.size() << endl;
00403
00404 testInside(bVolumes);
00405 }
00406
00407
00408
00409 buildMagVolumes(eVolumes, eInterpolators);
00410
00411
00412 for (vector<eSector>::const_iterator isec = sectors.begin();
00413 isec!= sectors.end(); ++isec) {
00414 mESectors.push_back((*isec).buildMagESector());
00415 }
00416
00417 if (debug) {
00418 cout << "*** ENDCAP ********************************************" << endl
00419 << "Number of different volumes = " << eVolCount << endl
00420 << "Number of interpolators built = " << eInterpolators.size() << endl
00421 << "Number of MagESector built = " << mESectors.size() << endl;
00422
00423 testInside(eVolumes);
00424 }
00425 }
00426
00427
00428 void MagGeoBuilderFromDDD::buildMagVolumes(const handles & volumes, map<string, MagProviderInterpol*> & interpolators) {
00429
00430 for (handles::const_iterator vol=volumes.begin(); vol!=volumes.end();
00431 ++vol){
00432 const MagProviderInterpol* mp = 0;
00433 if (interpolators.find((*vol)->magFile)!=interpolators.end()) {
00434 mp = interpolators[(*vol)->magFile];
00435 } else {
00436 cout << "No interpolator found for file " << (*vol)->magFile
00437 << " vol: " << (*vol)->name << endl;
00438 cout << interpolators.size() <<endl;
00439 }
00440
00441
00442
00443
00444 int volNum;
00445 string name = (*vol)->name;
00446 name.erase(0,name.rfind('_')+1);
00447 stringstream str;
00448 str << name;
00449 str >> volNum;
00450
00451
00452
00453
00454
00455
00456 int key = volNum*100+(*vol)->copyno;
00457 map<int, double>::const_iterator isf = theScalingFactors.find(key);
00458 if (isf == theScalingFactors.end()) {
00459 key = volNum*100;
00460 isf = theScalingFactors.find(key);
00461 }
00462
00463 double sf = 1.;
00464 if (isf != theScalingFactors.end()) {
00465 sf = (*isf).second;
00466
00467 edm::LogInfo("MagneticField|VolumeBasedMagneticFieldESProducer") << "Applying scaling factor " << sf << " to "<< (*vol)->name << "["<< (*vol)->copyno << "] (key:" << key << ")" << endl;
00468 }
00469
00470 const GloballyPositioned<float> * gpos = (*vol)->placement();
00471 (*vol)->magVolume = new MagVolume6Faces(gpos->position(),
00472 gpos->rotation(),
00473 (*vol)->shape(),
00474 (*vol)->sides(),
00475 mp, sf);
00476
00477 if ((*vol)->copyno==(*vol)->masterSector) {
00478 (*vol)->magVolume->ownsFieldProvider(true);
00479 }
00480
00481 (*vol)->magVolume->setIsIron((*vol)->isIron());
00482
00483
00484 (*vol)->magVolume->name = (*vol)->name;
00485 (*vol)->magVolume->copyno = (*vol)->copyno;
00486 }
00487 }
00488
00489
00490 void MagGeoBuilderFromDDD::buildInterpolator(const volumeHandle * vol, map<string, MagProviderInterpol*> & interpolators){
00491
00492
00493 double masterSectorPhi = (vol->masterSector-1)*Geom::pi()/6.;
00494
00495 if (debug) {
00496 cout << "Building interpolator from "
00497 << vol->name << " copyno " << vol->copyno
00498 << " at " << vol->center()
00499 << " phi: " << vol->center().phi()
00500 << " file: " << vol->magFile
00501 << endl;
00502
00503 if ( fabs(vol->center().phi() - masterSectorPhi) > Geom::pi()/9.) {
00504 cout << "***WARNING wrong sector? " << endl;
00505 }
00506 }
00507
00508 if (tableSet == "fake") {
00509 interpolators[vol->magFile] = new magneticfield::FakeInterpolator();
00510 return;
00511 }
00512
00513 string fullPath;
00514
00515 try {
00516 edm::FileInPath mydata("MagneticField/Interpolation/data/"+tableSet+"/"+vol->magFile);
00517 fullPath = mydata.fullPath();
00518 } catch (edm::Exception& exc) {
00519 cerr << "MagGeoBuilderFromDDD: exception in reading table; " << exc.what() << endl;
00520 if (!debug) throw;
00521 return;
00522 }
00523
00524
00525 try{
00526 if (vol->toExpand()){
00527
00528
00529
00530 } else {
00531
00532
00533
00534 GloballyPositioned<float> rf = *(vol->placement());
00535
00536 if (vol->masterSector != 1) {
00537 typedef Basic3DVector<float> Vector;
00538
00539 GloballyPositioned<float>::RotationType rot(Vector(0,0,1), -masterSectorPhi);
00540 Vector vpos(vol->placement()->position());
00541
00542
00543 rf = GloballyPositioned<float>(GloballyPositioned<float>::PositionType(rot.multiplyInverse(vpos)), vol->placement()->rotation()*rot);
00544 }
00545
00546 interpolators[vol->magFile] =
00547 MFGridFactory::build( fullPath, rf);
00548 }
00549 } catch (MagException& exc) {
00550 cout << exc.what() << endl;
00551 interpolators.erase(vol->magFile);
00552 if (!debug) throw;
00553 return;
00554 }
00555
00556
00557 if (debug) {
00558
00559 const MagVolume6Faces tempVolume(vol->placement()->position(),
00560 vol->placement()->rotation(),
00561 vol->shape(),
00562 vol->sides(),
00563 interpolators[vol->magFile]);
00564
00565 const MFGrid* grid = dynamic_cast<const MFGrid*>(interpolators[vol->magFile]);
00566 if (grid!=0) {
00567
00568 Dimensions sizes = grid->dimensions();
00569 cout << "Grid has 3 dimensions "
00570 << " number of nodes is " << sizes.w << " " << sizes.h
00571 << " " << sizes.d << endl;
00572
00573 const double tolerance = 0.03;
00574
00575
00576 size_t dumpCount = 0;
00577 for (int j=0; j < sizes.h; j++) {
00578 for (int k=0; k < sizes.d; k++) {
00579 for (int i=0; i < sizes.w; i++) {
00580 MFGrid::LocalPoint lp = grid->nodePosition( i, j, k);
00581 if (! tempVolume.inside(lp, tolerance)) {
00582 if (++dumpCount < 2) {
00583 MFGrid::GlobalPoint gp = tempVolume.toGlobal(lp);
00584 cout << "GRID ERROR: " << i << " " << j << " " << k
00585 << " local: " << lp
00586 << " global: " << gp
00587 << " R= " << gp.perp() << " phi=" << gp.phi() << endl;
00588 }
00589 }
00590 }
00591 }
00592 }
00593
00594 cout << vol->name << " : Number of grid points outside the MagVolume: " << dumpCount << "/" << sizes.w*sizes.h*sizes.d << endl;
00595 }
00596 }
00597 }
00598
00599
00600
00601 void MagGeoBuilderFromDDD::testInside(handles & volumes) {
00602
00603 cout << "--------------------------------------------------" << endl;
00604 cout << " inside(center) test" << endl;
00605 for (handles::const_iterator vol=volumes.begin(); vol!=volumes.end();
00606 ++vol){
00607 for (handles::const_iterator i=volumes.begin(); i!=volumes.end();
00608 ++i){
00609 if ((*i)==(*vol)) continue;
00610
00611 if ((*i)->magVolume->inside((*vol)->center())) {
00612 cout << "*** ERROR: center of " << (*vol)->name << " is inside "
00613 << (*i)->name <<endl;
00614 }
00615 }
00616
00617 if ((*vol)->magVolume->inside((*vol)->center())) {
00618 cout << (*vol)->name << " OK " << endl;
00619 } else {
00620 cout << "*** ERROR: center of volume is not inside it, "
00621 << (*vol)->name << endl;
00622 }
00623 }
00624 cout << "--------------------------------------------------" << endl;
00625 }
00626
00627
00628 vector<MagBLayer*> MagGeoBuilderFromDDD::barrelLayers() const{
00629 return mBLayers;
00630 }
00631
00632 vector<MagESector*> MagGeoBuilderFromDDD::endcapSectors() const{
00633 return mESectors;
00634 }
00635
00636 vector<MagVolume6Faces*> MagGeoBuilderFromDDD::barrelVolumes() const{
00637 vector<MagVolume6Faces*> v;
00638 v.reserve(bVolumes.size());
00639 for (handles::const_iterator i=bVolumes.begin();
00640 i!=bVolumes.end(); ++i){
00641 v.push_back((*i)->magVolume);
00642 }
00643 return v;
00644 }
00645
00646 vector<MagVolume6Faces*> MagGeoBuilderFromDDD::endcapVolumes() const{
00647 vector<MagVolume6Faces*> v;
00648 v.reserve(eVolumes.size());
00649 for (handles::const_iterator i=eVolumes.begin();
00650 i!=eVolumes.end(); ++i){
00651 v.push_back((*i)->magVolume);
00652 }
00653 return v;
00654 }
00655
00656
00657 float MagGeoBuilderFromDDD::maxR() const{
00658
00659
00660
00661 return 900.;
00662 }
00663
00664 float MagGeoBuilderFromDDD::maxZ() const{
00665
00666 return 1600.;
00667 }
00668
00669
00670 void MagGeoBuilderFromDDD::setScaling(std::vector<int> keys,
00671 std::vector<double> values)
00672 {
00673 if (keys.size() != values.size()) {
00674 throw cms::Exception("InvalidParameter") << "Invalid field scaling parameters 'scalingVolumes' and 'scalingFactors' ";
00675 }
00676 for (unsigned int i=0; i<keys.size(); ++i) {
00677 theScalingFactors[keys[i]] = values[i];
00678 }
00679 }
00680
00681
00682