Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-06 07:38:44

0001 // -*- C++ -*-
0002 //
0003 // Package:     MagneticField/GeomBuilder
0004 // Class  :     InterpolatorBuilder
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Tue, 17 May 2022 20:50:21 GMT
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "InterpolatorBuilder.h"
0017 #include "FakeInterpolator.h"
0018 
0019 #include "FWCore/ParameterSet/interface/FileInPath.h"
0020 #include "FWCore/Utilities/interface/Exception.h"
0021 #include "MagneticField/Interpolation/interface/binary_ifstream.h"
0022 #include "MagneticField/Interpolation/interface/MFGridFactory.h"
0023 #include "MagneticField/Interpolation/interface/MFGrid.h"
0024 
0025 #include "DataFormats/Math/interface/angle_units.h"
0026 //
0027 // constants, enums and typedefs
0028 //
0029 
0030 namespace magneticfield {
0031   using namespace angle_units::operators;
0032 
0033   //
0034   // static data member definitions
0035   //
0036 
0037   //
0038   // constructors and destructor
0039   //
0040   InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet, bool useMergeFileIfAvailable)
0041       : tableSet_(std::move(iTableSet)) {
0042     if (not useMergeFileIfAvailable)
0043       return;
0044     auto indexFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.index");
0045     if (not indexFileName.empty()) {
0046       auto binaryFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.bin");
0047       if (not binaryFileName.empty()) {
0048         std::ifstream indexFile(indexFileName);
0049         while (indexFile) {
0050           std::string magFile;
0051           unsigned int offset;
0052           indexFile >> magFile >> offset;
0053           offsets_.emplace(std::move(magFile), offset);
0054         }
0055         stream_ = interpolation::binary_ifstream(binaryFileName);
0056       }
0057     }
0058   }
0059 
0060   //
0061   // member functions
0062   //
0063   std::unique_ptr<MagProviderInterpol> InterpolatorBuilder::build(volumeHandle const* vol) {
0064     if (tableSet_ == "fake" || vol->magFile == "fake") {
0065       return std::make_unique<magneticfield::FakeInterpolator>();
0066     }
0067 
0068     // If the table is in "local" coordinates, must create a reference
0069     // frame that is appropriately rotated along the CMS Z axis.
0070 
0071     GloballyPositioned<float> rf = *(vol->placement());
0072 
0073     if (vol->masterSector != 1) {
0074       typedef Basic3DVector<float> Vector;
0075 
0076       // Phi of the master sector
0077       double masterSectorPhi = (vol->masterSector - 1) * 1._pi / 6.;
0078 
0079       GloballyPositioned<float>::RotationType rot(Vector(0, 0, 1), -masterSectorPhi);
0080       Vector vpos(vol->placement()->position());
0081 
0082       rf = GloballyPositioned<float>(GloballyPositioned<float>::PositionType(rot.multiplyInverse(vpos)),
0083                                      vol->placement()->rotation() * rot);
0084     }
0085 
0086     if (not stream_) {
0087       auto fullPath = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile);
0088       if (fullPath.empty()) {
0089         //cause the exception to happen
0090         edm::FileInPath mydata("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile);
0091         return {};
0092       }
0093 
0094       magneticfield::interpolation::binary_ifstream strm(fullPath);
0095       return std::unique_ptr<MagProviderInterpol>(MFGridFactory::build(strm, rf));
0096     }
0097 
0098     auto find = offsets_.find(vol->magFile);
0099     if (find == offsets_.end()) {
0100       throw cms::Exception("MissingMagFileEntry") << vol->magFile << " was not an entry in the index file";
0101     }
0102     stream_->seekg(find->second);
0103     if (stream_->fail()) {
0104       throw cms::Exception("SeekMagFileEntry") << " failed seekg within merged binary file";
0105     }
0106     return std::unique_ptr<MagProviderInterpol>(MFGridFactory::build(*stream_, rf));
0107   }
0108 
0109   //
0110   // const member functions
0111   //
0112 
0113   //
0114   // static member functions
0115   //
0116 }  // namespace magneticfield