File indexing completed on 2024-04-06 12:22:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
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
0028
0029
0030 namespace magneticfield {
0031 using namespace angle_units::operators;
0032
0033
0034
0035
0036
0037
0038
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
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
0069
0070
0071 GloballyPositioned<float> rf = *(vol->placement());
0072
0073 if (vol->masterSector != 1) {
0074 typedef Basic3DVector<float> Vector;
0075
0076
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
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
0111
0112
0113
0114
0115
0116 }