CMS 3D CMS Logo

InterpolatorBuilder.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MagneticField/GeomBuilder
4 // Class : InterpolatorBuilder
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Tue, 17 May 2022 20:50:21 GMT
11 //
12 
13 // system include files
14 
15 // user include files
16 #include "InterpolatorBuilder.h"
17 #include "FakeInterpolator.h"
18 
24 
26 //
27 // constants, enums and typedefs
28 //
29 
30 namespace magneticfield {
31  using namespace angle_units::operators;
32 
33  //
34  // static data member definitions
35  //
36 
37  //
38  // constructors and destructor
39  //
40  InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet, bool useMergeFileIfAvailable)
41  : tableSet_(std::move(iTableSet)) {
42  if (not useMergeFileIfAvailable)
43  return;
44  auto indexFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.index");
45  if (not indexFileName.empty()) {
46  auto binaryFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.bin");
47  if (not binaryFileName.empty()) {
48  std::ifstream indexFile(indexFileName);
49  while (indexFile) {
50  std::string magFile;
51  unsigned int offset;
52  indexFile >> magFile >> offset;
53  offsets_.emplace(std::move(magFile), offset);
54  }
55  stream_ = interpolation::binary_ifstream(binaryFileName);
56  }
57  }
58  }
59 
60  //
61  // member functions
62  //
63  std::unique_ptr<MagProviderInterpol> InterpolatorBuilder::build(volumeHandle const* vol) {
64  if (tableSet_ == "fake" || vol->magFile == "fake") {
65  return std::make_unique<magneticfield::FakeInterpolator>();
66  }
67 
68  // If the table is in "local" coordinates, must create a reference
69  // frame that is appropriately rotated along the CMS Z axis.
70 
72 
73  if (vol->masterSector != 1) {
75 
76  // Phi of the master sector
77  double masterSectorPhi = (vol->masterSector - 1) * 1._pi / 6.;
78 
79  GloballyPositioned<float>::RotationType rot(Vector(0, 0, 1), -masterSectorPhi);
80  Vector vpos(vol->placement()->position());
81 
83  vol->placement()->rotation() * rot);
84  }
85 
86  if (not stream_) {
87  auto fullPath = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile);
88  if (fullPath.empty()) {
89  //cause the exception to happen
90  edm::FileInPath mydata("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile);
91  return {};
92  }
93 
95  return std::unique_ptr<MagProviderInterpol>(MFGridFactory::build(strm, rf));
96  }
97 
98  auto find = offsets_.find(vol->magFile);
99  if (find == offsets_.end()) {
100  throw cms::Exception("MissingMagFileEntry") << vol->magFile << " was not an entry in the index file";
101  }
102  stream_->seekg(find->second);
103  if (stream_->fail()) {
104  throw cms::Exception("SeekMagFileEntry") << " failed seekg within merged binary file";
105  }
106  return std::unique_ptr<MagProviderInterpol>(MFGridFactory::build(*stream_, rf));
107  }
108 
109  //
110  // const member functions
111  //
112 
113  //
114  // static member functions
115  //
116 } // namespace magneticfield
ROOT::Math::Plane3D::Vector Vector
Definition: EcalHitMaker.cc:29
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::unique_ptr< MagProviderInterpol > build(volumeHandle const *)
std::optional< interpolation::binary_ifstream > stream_
int masterSector
The sector for which an interpolator for this class of volumes should be built.
static MFGrid * build(const std::string &name, const GloballyPositioned< float > &vol)
Build interpolator for a binary grid file.
Point3DBase< T, GlobalTag > PositionType
InterpolatorBuilder(std::string iTableSet, bool useMergeFileIfAvailable=true)
const PositionType & position() const
std::unordered_map< std::string, unsigned int > offsets_
static std::string findFile(std::string const &)
Definition: FileInPath.cc:442
const RotationType & rotation() const
const GloballyPositioned< float > * placement() const
Position and rotation.
def move(src, dest)
Definition: eostools.py:511
std::string magFile
Name of magnetic field table file.