From 7a1167bb10619cae1a10ac6d110c1bbf233ba4af Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Mon, 16 May 2022 17:10:52 -0500 Subject: [PATCH 01/10] Moved binary_ifstream to public interface --- .../Interpolation/interface/binary_ifstream.h | 50 +++++ .../Interpolation/src/GlobalGridWrapper.h | 1 - .../Interpolation/src/MFGridFactory.cc | 4 +- .../Interpolation/src/MagneticFieldGrid.cc | 3 +- .../Interpolation/src/MagneticFieldGrid.h | 1 - .../src/RectangularCartesianMFGrid.cc | 2 +- .../src/RectangularCartesianMFGrid.h | 5 +- .../src/RectangularCylindricalMFGrid.cc | 2 +- .../src/RectangularCylindricalMFGrid.h | 6 +- .../src/SpecialCylindricalMFGrid.cc | 2 +- .../src/SpecialCylindricalMFGrid.h | 6 +- .../src/TrapezoidalCartesianMFGrid.cc | 2 +- .../src/TrapezoidalCartesianMFGrid.h | 6 +- .../src/TrapezoidalCylindricalMFGrid.cc | 2 +- .../src/TrapezoidalCylindricalMFGrid.h | 6 +- .../Interpolation/src/binary_ifstream.cc | 191 +++++++++--------- .../Interpolation/src/binary_ifstream.h | 49 ----- .../Interpolation/src/bstream_iterator.h | 90 ++++----- .../BinaryTablesGeneration/GridFileReader.cpp | 4 +- 19 files changed, 221 insertions(+), 211 deletions(-) create mode 100644 MagneticField/Interpolation/interface/binary_ifstream.h delete mode 100644 MagneticField/Interpolation/src/binary_ifstream.h diff --git a/MagneticField/Interpolation/interface/binary_ifstream.h b/MagneticField/Interpolation/interface/binary_ifstream.h new file mode 100644 index 0000000000000..3bf88a95d8547 --- /dev/null +++ b/MagneticField/Interpolation/interface/binary_ifstream.h @@ -0,0 +1,50 @@ +#ifndef binary_ifstream_H +#define binary_ifstream_H + +#include +#include +#include "FWCore/Utilities/interface/Visibility.h" + +namespace magneticfield::interpolation { + class binary_ifstream { + public: + explicit binary_ifstream(const char* name); + explicit binary_ifstream(const std::string& name); + + ~binary_ifstream(); + + binary_ifstream& operator>>(char& n); + binary_ifstream& operator>>(unsigned char& n); + + binary_ifstream& operator>>(short& n); + binary_ifstream& operator>>(unsigned short& n); + + binary_ifstream& operator>>(int& n); + binary_ifstream& operator>>(unsigned int& n); + + binary_ifstream& operator>>(long& n); + binary_ifstream& operator>>(unsigned long& n); + + binary_ifstream& operator>>(float& n); + binary_ifstream& operator>>(double& n); + + binary_ifstream& operator>>(bool& n); + binary_ifstream& operator>>(std::string& n); + + void close(); + + /// stream state checking + bool good() const; + bool eof() const; + bool fail() const; + bool bad() const; + bool operator!() const; + operator bool() const; + + private: + FILE* file_; + + void init(const char* name); + }; +} // namespace magneticfield::interpolation +#endif diff --git a/MagneticField/Interpolation/src/GlobalGridWrapper.h b/MagneticField/Interpolation/src/GlobalGridWrapper.h index 644fb95e1cf61..a07c59291184a 100644 --- a/MagneticField/Interpolation/src/GlobalGridWrapper.h +++ b/MagneticField/Interpolation/src/GlobalGridWrapper.h @@ -14,7 +14,6 @@ #include -class binary_ifstream; class MagneticFieldGrid; class dso_internal GlobalGridWrapper : public MFGrid { diff --git a/MagneticField/Interpolation/src/MFGridFactory.cc b/MagneticField/Interpolation/src/MFGridFactory.cc index 194cdc1b4bba5..f2ac322324db4 100644 --- a/MagneticField/Interpolation/src/MFGridFactory.cc +++ b/MagneticField/Interpolation/src/MFGridFactory.cc @@ -1,5 +1,5 @@ #include "MagneticField/Interpolation/interface/MFGridFactory.h" -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "DataFormats/GeometrySurface/interface/GloballyPositioned.h" #include "RectangularCartesianMFGrid.h" @@ -14,7 +14,7 @@ using namespace std; MFGrid* MFGridFactory::build(const string& name, const GloballyPositioned& vol) { - binary_ifstream inFile(name); + magneticfield::interpolation::binary_ifstream inFile(name); int gridType; inFile >> gridType; diff --git a/MagneticField/Interpolation/src/MagneticFieldGrid.cc b/MagneticField/Interpolation/src/MagneticFieldGrid.cc index 38bb12338bcf3..9bf0a26ef02ee 100644 --- a/MagneticField/Interpolation/src/MagneticFieldGrid.cc +++ b/MagneticField/Interpolation/src/MagneticFieldGrid.cc @@ -1,11 +1,12 @@ // include header for MagneticFieldGrid (regular + extension for some trapezoids) #include "MagneticFieldGrid.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include using namespace std; void MagneticFieldGrid::load(const string &name) { - binary_ifstream inFile(name); + magneticfield::interpolation::binary_ifstream inFile(name); inFile >> GridType; // reading the header switch (GridType) { diff --git a/MagneticField/Interpolation/src/MagneticFieldGrid.h b/MagneticField/Interpolation/src/MagneticFieldGrid.h index 2736d76576ce7..4a623ca89c4e0 100644 --- a/MagneticField/Interpolation/src/MagneticFieldGrid.h +++ b/MagneticField/Interpolation/src/MagneticFieldGrid.h @@ -19,7 +19,6 @@ // interpolation package #include "FWCore/Utilities/interface/Visibility.h" #include "VectorFieldInterpolation.h" -#include "binary_ifstream.h" // used libs #include diff --git a/MagneticField/Interpolation/src/RectangularCartesianMFGrid.cc b/MagneticField/Interpolation/src/RectangularCartesianMFGrid.cc index 9045ce08f3634..2ec3f5d0c6d1e 100644 --- a/MagneticField/Interpolation/src/RectangularCartesianMFGrid.cc +++ b/MagneticField/Interpolation/src/RectangularCartesianMFGrid.cc @@ -1,5 +1,5 @@ #include "RectangularCartesianMFGrid.h" -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "LinearGridInterpolator3D.h" #include diff --git a/MagneticField/Interpolation/src/RectangularCartesianMFGrid.h b/MagneticField/Interpolation/src/RectangularCartesianMFGrid.h index 32f03c18208f5..aefe3045a711c 100644 --- a/MagneticField/Interpolation/src/RectangularCartesianMFGrid.h +++ b/MagneticField/Interpolation/src/RectangularCartesianMFGrid.h @@ -4,10 +4,13 @@ #include "FWCore/Utilities/interface/Visibility.h" #include "MFGrid3D.h" -class binary_ifstream; +namespace magneticfield::interpolation { + class binary_ifstream; +} class dso_internal RectangularCartesianMFGrid : public MFGrid3D { public: + using binary_ifstream = magneticfield::interpolation::binary_ifstream; RectangularCartesianMFGrid(binary_ifstream& istr, const GloballyPositioned& vol); LocalVector uncheckedValueInTesla(const LocalPoint& p) const override; diff --git a/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.cc b/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.cc index 51f2e9d6f2c7e..1b63af24508f5 100644 --- a/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.cc +++ b/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.cc @@ -1,5 +1,5 @@ #include "RectangularCylindricalMFGrid.h" -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "LinearGridInterpolator3D.h" #include diff --git a/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.h b/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.h index 6aba562ec4166..f7aab443748bf 100644 --- a/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.h +++ b/MagneticField/Interpolation/src/RectangularCylindricalMFGrid.h @@ -4,10 +4,14 @@ #include "FWCore/Utilities/interface/Visibility.h" #include "MFGrid3D.h" -class binary_ifstream; +namespace magneticfield::interpolation { + class binary_ifstream; +} class dso_internal RectangularCylindricalMFGrid : public MFGrid3D { public: + using binary_ifstream = magneticfield::interpolation::binary_ifstream; + RectangularCylindricalMFGrid(binary_ifstream& istr, const GloballyPositioned& vol); LocalVector uncheckedValueInTesla(const LocalPoint& p) const override; diff --git a/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.cc b/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.cc index 7b0d6a322a1e2..5e3d6914628ad 100644 --- a/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.cc +++ b/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.cc @@ -1,5 +1,5 @@ #include "SpecialCylindricalMFGrid.h" -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "LinearGridInterpolator3D.h" #include diff --git a/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.h b/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.h index 03943ac8c98ce..f040f81587324 100644 --- a/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.h +++ b/MagneticField/Interpolation/src/SpecialCylindricalMFGrid.h @@ -11,10 +11,14 @@ #include "FWCore/Utilities/interface/Visibility.h" #include "MFGrid3D.h" -class binary_ifstream; +namespace magneticfield::interpolation { + class binary_ifstream; +} class dso_internal SpecialCylindricalMFGrid : public MFGrid3D { public: + using binary_ifstream = magneticfield::interpolation::binary_ifstream; + /// Constructor. /// gridType = 5 => 1/sin(phi); i.e. master sector is #4 /// gridType = 6 => 1/cos(phi); i.e. master sector is #1 diff --git a/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.cc b/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.cc index 4e44156f86b51..8de09167904ed 100644 --- a/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.cc +++ b/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.cc @@ -4,7 +4,7 @@ */ #include "TrapezoidalCartesianMFGrid.h" -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "LinearGridInterpolator3D.h" #include "MagneticField/VolumeGeometry/interface/MagExceptions.h" #include diff --git a/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.h b/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.h index f6391b737e4ed..5bb599cd3e03f 100644 --- a/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.h +++ b/MagneticField/Interpolation/src/TrapezoidalCartesianMFGrid.h @@ -14,10 +14,14 @@ #include "Trapezoid2RectangleMappingX.h" #include "FWCore/Utilities/interface/Visibility.h" -class binary_ifstream; +namespace magneticfield::interpolation { + class binary_ifstream; +} class dso_internal TrapezoidalCartesianMFGrid : public MFGrid3D { public: + using binary_ifstream = magneticfield::interpolation::binary_ifstream; + TrapezoidalCartesianMFGrid(binary_ifstream& istr, const GloballyPositioned& vol); LocalVector uncheckedValueInTesla(const LocalPoint& p) const override; diff --git a/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.cc b/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.cc index 9623f7b11b89c..38c2de57b1900 100644 --- a/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.cc +++ b/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.cc @@ -1,5 +1,5 @@ #include "TrapezoidalCylindricalMFGrid.h" -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "LinearGridInterpolator3D.h" #include "MagneticField/VolumeGeometry/interface/MagExceptions.h" diff --git a/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.h b/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.h index c6eb29306cd9b..9abe95657cf37 100644 --- a/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.h +++ b/MagneticField/Interpolation/src/TrapezoidalCylindricalMFGrid.h @@ -5,10 +5,14 @@ #include "Trapezoid2RectangleMappingX.h" #include "FWCore/Utilities/interface/Visibility.h" -class binary_ifstream; +namespace magneticfield::interpolation { + class binary_ifstream; +} class dso_internal TrapezoidalCylindricalMFGrid : public MFGrid3D { public: + using binary_ifstream = magneticfield::interpolation::binary_ifstream; + TrapezoidalCylindricalMFGrid(binary_ifstream& istr, const GloballyPositioned& vol); LocalVector uncheckedValueInTesla(const LocalPoint& p) const override; diff --git a/MagneticField/Interpolation/src/binary_ifstream.cc b/MagneticField/Interpolation/src/binary_ifstream.cc index d48716c9bb113..7e65919d7e8a3 100644 --- a/MagneticField/Interpolation/src/binary_ifstream.cc +++ b/MagneticField/Interpolation/src/binary_ifstream.cc @@ -1,102 +1,105 @@ -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include #include -struct binary_ifstream_error {}; +namespace magneticfield::interpolation { + struct binary_ifstream_error {}; -binary_ifstream::binary_ifstream(const char* name) : file_(nullptr) { init(name); } + binary_ifstream::binary_ifstream(const char* name) : file_(nullptr) { init(name); } -binary_ifstream::binary_ifstream(const std::string& name) : file_(nullptr) { init(name.c_str()); } + binary_ifstream::binary_ifstream(const std::string& name) : file_(nullptr) { init(name.c_str()); } -void binary_ifstream::init(const char* name) { - file_ = fopen(name, "rb"); - if (file_ == nullptr) { - std::cout << "file " << name << " cannot be opened for reading" << std::endl; - throw binary_ifstream_error(); + void binary_ifstream::init(const char* name) { + file_ = fopen(name, "rb"); + if (file_ == nullptr) { + std::cout << "file " << name << " cannot be opened for reading" << std::endl; + throw binary_ifstream_error(); + } } -} - -binary_ifstream::~binary_ifstream() { close(); } -void binary_ifstream::close() { - if (file_ != nullptr) - fclose(file_); - file_ = nullptr; -} - -binary_ifstream& binary_ifstream::operator>>(char& n) { - n = static_cast(fgetc(file_)); - return *this; -} - -binary_ifstream& binary_ifstream::operator>>(unsigned char& n) { - n = static_cast(fgetc(file_)); - return *this; -} - -binary_ifstream& binary_ifstream::operator>>(short& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} -binary_ifstream& binary_ifstream::operator>>(unsigned short& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} -binary_ifstream& binary_ifstream::operator>>(int& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} -binary_ifstream& binary_ifstream::operator>>(unsigned int& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} - -binary_ifstream& binary_ifstream::operator>>(long& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} -binary_ifstream& binary_ifstream::operator>>(unsigned long& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} - -binary_ifstream& binary_ifstream::operator>>(float& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} -binary_ifstream& binary_ifstream::operator>>(double& n) { - fread(&n, sizeof(n), 1, file_); - return *this; -} - -binary_ifstream& binary_ifstream::operator>>(bool& n) { - n = static_cast(fgetc(file_)); - return *this; -} - -binary_ifstream& binary_ifstream::operator>>(std::string& n) { - unsigned int nchar; - (*this) >> nchar; - char* tmp = new char[nchar + 1]; - unsigned int nread = fread(tmp, 1, nchar, file_); - if (nread != nchar) - std::cout << "binary_ifstream error: read less then expected " << std::endl; - n.assign(tmp, nread); - delete[] tmp; - return *this; -} - -bool binary_ifstream::good() const { return !bad() && !eof(); } - -bool binary_ifstream::eof() const { return feof(file_); } - -bool binary_ifstream::fail() const { return file_ == nullptr || ferror(file_) != 0; } - -// don't know the difference between fail() and bad() (yet) -bool binary_ifstream::bad() const { return fail(); } - -bool binary_ifstream::operator!() const { return fail() || bad() || eof(); } - -//binary_ifstream::operator bool() const {return !fail() && !bad();} - -binary_ifstream::operator bool() const { return good(); } + + binary_ifstream::~binary_ifstream() { close(); } + void binary_ifstream::close() { + if (file_ != nullptr) + fclose(file_); + file_ = nullptr; + } + + binary_ifstream& binary_ifstream::operator>>(char& n) { + n = static_cast(fgetc(file_)); + return *this; + } + + binary_ifstream& binary_ifstream::operator>>(unsigned char& n) { + n = static_cast(fgetc(file_)); + return *this; + } + + binary_ifstream& binary_ifstream::operator>>(short& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + binary_ifstream& binary_ifstream::operator>>(unsigned short& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + binary_ifstream& binary_ifstream::operator>>(int& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + binary_ifstream& binary_ifstream::operator>>(unsigned int& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + + binary_ifstream& binary_ifstream::operator>>(long& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + binary_ifstream& binary_ifstream::operator>>(unsigned long& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + + binary_ifstream& binary_ifstream::operator>>(float& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + binary_ifstream& binary_ifstream::operator>>(double& n) { + fread(&n, sizeof(n), 1, file_); + return *this; + } + + binary_ifstream& binary_ifstream::operator>>(bool& n) { + n = static_cast(fgetc(file_)); + return *this; + } + + binary_ifstream& binary_ifstream::operator>>(std::string& n) { + unsigned int nchar; + (*this) >> nchar; + char* tmp = new char[nchar + 1]; + unsigned int nread = fread(tmp, 1, nchar, file_); + if (nread != nchar) + std::cout << "binary_ifstream error: read less then expected " << std::endl; + n.assign(tmp, nread); + delete[] tmp; + return *this; + } + + bool binary_ifstream::good() const { return !bad() && !eof(); } + + bool binary_ifstream::eof() const { return feof(file_); } + + bool binary_ifstream::fail() const { return file_ == nullptr || ferror(file_) != 0; } + + // don't know the difference between fail() and bad() (yet) + bool binary_ifstream::bad() const { return fail(); } + + bool binary_ifstream::operator!() const { return fail() || bad() || eof(); } + + //binary_ifstream::operator bool() const {return !fail() && !bad();} + + binary_ifstream::operator bool() const { return good(); } + +} // namespace magneticfield::interpolation diff --git a/MagneticField/Interpolation/src/binary_ifstream.h b/MagneticField/Interpolation/src/binary_ifstream.h deleted file mode 100644 index 83bbe9de3a25f..0000000000000 --- a/MagneticField/Interpolation/src/binary_ifstream.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef binary_ifstream_H -#define binary_ifstream_H - -#include -#include -#include "FWCore/Utilities/interface/Visibility.h" - -class binary_ifstream { -public: - explicit binary_ifstream(const char* name); - explicit binary_ifstream(const std::string& name); - - ~binary_ifstream(); - - binary_ifstream& operator>>(char& n); - binary_ifstream& operator>>(unsigned char& n); - - binary_ifstream& operator>>(short& n); - binary_ifstream& operator>>(unsigned short& n); - - binary_ifstream& operator>>(int& n); - binary_ifstream& operator>>(unsigned int& n); - - binary_ifstream& operator>>(long& n); - binary_ifstream& operator>>(unsigned long& n); - - binary_ifstream& operator>>(float& n); - binary_ifstream& operator>>(double& n); - - binary_ifstream& operator>>(bool& n); - binary_ifstream& operator>>(std::string& n); - - void close(); - - /// stream state checking - bool good() const; - bool eof() const; - bool fail() const; - bool bad() const; - bool operator!() const; - operator bool() const; - -private: - FILE* file_; - - void init(const char* name); -}; - -#endif diff --git a/MagneticField/Interpolation/src/bstream_iterator.h b/MagneticField/Interpolation/src/bstream_iterator.h index 049a5600d032b..0b03e9a58205d 100644 --- a/MagneticField/Interpolation/src/bstream_iterator.h +++ b/MagneticField/Interpolation/src/bstream_iterator.h @@ -1,80 +1,68 @@ #ifndef bstream_iterator_H #define bstream_iterator_H -#include "binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "binary_ofstream.h" #include #include "FWCore/Utilities/interface/Visibility.h" template -class bistream_iterator : - public std::iterator -{ +class bistream_iterator : public std::iterator { public: + bistream_iterator() : stream_(0) {} - bistream_iterator() : stream_(0) {} + bistream_iterator(binary_ifstream& s) : stream_(&s) { read(); } - bistream_iterator( binary_ifstream& s) : stream_(&s) { - read(); - } - - const T& operator*() const {return value_;} + const T& operator*() const { return value_; } - const T* operator->() const {return &value_;} + const T* operator->() const { return &value_; } - bistream_iterator& operator++() {read(); return *this;} + bistream_iterator& operator++() { + read(); + return *this; + } - bistream_iterator& operator++(int) { - bistream_iterator tmp; - read(); - return tmp; - } + bistream_iterator& operator++(int) { + bistream_iterator tmp; + read(); + return tmp; + } - bool operator==(const bistream_iterator& rhs) { - return stream_ == rhs.stream_; - } + bool operator==(const bistream_iterator& rhs) { return stream_ == rhs.stream_; } - bool operator!=(const bistream_iterator& rhs) { - return !operator==(rhs); - } + bool operator!=(const bistream_iterator& rhs) { return !operator==(rhs); } private: - - binary_ifstream* stream_; - T value_; - - void read() { - if (stream_ != 0) { - // if (!(*stream_ >> value_)) stream_ = 0; - if (!(*stream_ >> value_)) { - stream_ = 0; - // std::cout << "istream_iterator: stream turned bad, set stream_ to zero" << std::endl; - } - } + binary_ifstream* stream_; + T value_; + + void read() { + if (stream_ != 0) { + // if (!(*stream_ >> value_)) stream_ = 0; + if (!(*stream_ >> value_)) { + stream_ = 0; + // std::cout << "istream_iterator: stream turned bad, set stream_ to zero" << std::endl; + } } - + } }; -template -class dso_internal bostream_iterator : - public std::iterator { +template +class dso_internal bostream_iterator : public std::iterator { public: + bostream_iterator(binary_ofstream& s) : stream_(&s) {} - bostream_iterator( binary_ofstream& s) : stream_(&s) {} - - bostream_iterator& operator=( const T& t) { - *stream_ << t; - return *this; - } + bostream_iterator& operator=(const T& t) { + *stream_ << t; + return *this; + } - bostream_iterator& operator*() {return *this;} - bostream_iterator& operator++() {return *this;} - bostream_iterator& operator++(int) {return *this;} + bostream_iterator& operator*() { return *this; } + bostream_iterator& operator++() { return *this; } + bostream_iterator& operator++(int) { return *this; } private: - - binary_ofstream* stream_; - + binary_ofstream* stream_; }; #endif diff --git a/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp b/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp index 679fba0ad8bba..5d3f57b7be2c9 100644 --- a/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp +++ b/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp @@ -1,7 +1,7 @@ // Small Program to read Grid Files // by droll (29/02/04) // essential files -#include "MagneticField/Interpolation/src/binary_ifstream.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" // used libs #include @@ -33,7 +33,7 @@ int main(int argc, char **argv) { bool fullDump = argv[2]; - binary_ifstream inFile(filename); + magneticfield::interpolation::binary_ifstream inFile(filename); if (!inFile) { cout << "file open failed!" << endl; return EXIT_FAILURE; From d0450e04377d26ceb209aac42502f8a10f9ee553 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Wed, 18 May 2022 10:27:55 -0500 Subject: [PATCH 02/10] Allow passing binary_ifstream directly to MFGridFactory --- MagneticField/Interpolation/interface/MFGridFactory.h | 7 +++++++ MagneticField/Interpolation/src/MFGridFactory.cc | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/MagneticField/Interpolation/interface/MFGridFactory.h b/MagneticField/Interpolation/interface/MFGridFactory.h index bf728fd487884..ca78ced89305f 100644 --- a/MagneticField/Interpolation/interface/MFGridFactory.h +++ b/MagneticField/Interpolation/interface/MFGridFactory.h @@ -13,10 +13,17 @@ class MFGrid; template class GloballyPositioned; +namespace magneticfield::interpolation { + class binary_ifstream; +} + class MFGridFactory { public: + using binary_ifstream = magneticfield::interpolation::binary_ifstream; + /// Build interpolator for a binary grid file static MFGrid* build(const std::string& name, const GloballyPositioned& vol); + static MFGrid* build(binary_ifstream& name, const GloballyPositioned& vol); /// Build a 2pi phi-symmetric interpolator for a binary grid file static MFGrid* build(const std::string& name, const GloballyPositioned& vol, double phiMin, double phiMax); diff --git a/MagneticField/Interpolation/src/MFGridFactory.cc b/MagneticField/Interpolation/src/MFGridFactory.cc index f2ac322324db4..43477ba93a2e4 100644 --- a/MagneticField/Interpolation/src/MFGridFactory.cc +++ b/MagneticField/Interpolation/src/MFGridFactory.cc @@ -15,6 +15,11 @@ using namespace std; MFGrid* MFGridFactory::build(const string& name, const GloballyPositioned& vol) { magneticfield::interpolation::binary_ifstream inFile(name); + + return build(inFile, vol); +} + +MFGrid* MFGridFactory::build(binary_ifstream& inFile, const GloballyPositioned& vol) { int gridType; inFile >> gridType; From ea9835f73dbd41f90a6dd11f9eabb8100e00dc45 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Wed, 18 May 2022 10:33:23 -0500 Subject: [PATCH 03/10] Added separate InterpolatorBuilder class --- .../GeomBuilder/src/DD4hep_MagGeoBuilder.cc | 64 +++----------- .../GeomBuilder/src/DD4hep_MagGeoBuilder.h | 3 +- .../GeomBuilder/src/InterpolatorBuilder.cc | 85 +++++++++++++++++++ .../GeomBuilder/src/InterpolatorBuilder.h | 50 +++++++++++ 4 files changed, 151 insertions(+), 51 deletions(-) create mode 100644 MagneticField/GeomBuilder/src/InterpolatorBuilder.cc create mode 100644 MagneticField/GeomBuilder/src/InterpolatorBuilder.h diff --git a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc index 8aeef2c1ac69d..11bd3f2fd5896 100644 --- a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc +++ b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc @@ -6,19 +6,16 @@ #include "DD4hep_MagGeoBuilder.h" #include "bLayer.h" #include "eSector.h" -#include "FakeInterpolator.h" +#include "InterpolatorBuilder.h" #include "MagneticField/Layers/interface/MagBLayer.h" #include "MagneticField/Layers/interface/MagESector.h" -#include "FWCore/ParameterSet/interface/FileInPath.h" - #include "DetectorDescription/DDCMS/interface/DDFilteredView.h" #include "Utilities/BinningTools/interface/ClusterizingHistogram.h" #include "MagneticField/Interpolation/interface/MagProviderInterpol.h" -#include "MagneticField/Interpolation/interface/MFGridFactory.h" #include "MagneticField/Interpolation/interface/MFGrid.h" #include "MagneticField/VolumeGeometry/interface/MagVolume6Faces.h" @@ -147,6 +144,7 @@ void MagGeoBuilder::build(const cms::DDDetector* det) { LogError("MagGeoBuilder") << "Filtered view has no node. Cannot build."; return; } + InterpolatorBuilder interpolatorBuilder(tableSet_); while (doSubDets) { string name = fv.volume().volume().name(); LogTrace("MagGeoBuilder") << "Name: " << name; @@ -205,7 +203,7 @@ void MagGeoBuilder::build(const cms::DDDetector* det) { // not replicated in phi) // ASSUMPTION: copyno == sector. if (v->copyno == v->masterSector) { - auto i = buildInterpolator(v); + auto i = buildInterpolator(v, interpolatorBuilder); if (i) { bInterpolators[v->magFile] = i; } @@ -215,7 +213,7 @@ void MagGeoBuilder::build(const cms::DDDetector* det) { LogTrace("MagGeoBuilder") << " (Endcaps)"; eVolumes_.push_back(v); if (v->copyno == v->masterSector) { - auto i = buildInterpolator(v); + auto i = buildInterpolator(v, interpolatorBuilder); if (i) { eInterpolators[v->magFile] = i; } @@ -436,15 +434,14 @@ void MagGeoBuilder::buildMagVolumes(const handles& volumes, } } -MagProviderInterpol* MagGeoBuilder::buildInterpolator(const volumeHandle* vol) const { +MagProviderInterpol* MagGeoBuilder::buildInterpolator(const volumeHandle* vol, InterpolatorBuilder& builder) const { MagProviderInterpol* retValue = nullptr; - // Phi of the master sector - double masterSectorPhi = (vol->masterSector - 1) * 1._pi / 6.; - LogTrace("MagGeoBuilder") << "Building interpolator from " << vol->volumeno << " copyno " << vol->copyno << " at " << vol->center() << " phi: " << static_cast(vol->center().phi()) / 1._pi << " pi, file: " << vol->magFile << " master: " << vol->masterSector; if (debug_) { + // Phi of the master sector + double masterSectorPhi = (vol->masterSector - 1) * 1._pi / 6.; double delta = std::abs(vol->center().phi() - masterSectorPhi); if (delta > (1._pi / 9.)) { LogTrace("MagGeoBuilder") << "***WARNING wrong sector? Vol delta from master sector is " << delta / 1._pi @@ -452,47 +449,14 @@ MagProviderInterpol* MagGeoBuilder::buildInterpolator(const volumeHandle* vol) c } } - if (tableSet_ == "fake" || vol->magFile == "fake") { - return new magneticfield::FakeInterpolator(); - } - - string fullPath = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); - if (fullPath.empty()) { - //get the exact error info - try { - edm::FileInPath mydata("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); - } catch (edm::Exception& exc) { - cerr << "MagGeoBuilder: exception in reading table; " << exc.what() << endl; - if (!debug_) - throw; - } - return nullptr; - } - try { - if (vol->toExpand()) { - //FIXME: see discussion on mergeCylinders above. - // interpolators[vol->magFile] = - // MFGridFactory::build( fullPath, *(vol->placement()), vol->minPhi(), vol->maxPhi()); - } else { - // If the table is in "local" coordinates, must create a reference - // frame that is appropriately rotated along the CMS Z axis. - - GloballyPositioned rf = *(vol->placement()); - - if (vol->masterSector != 1) { - typedef Basic3DVector Vector; - - GloballyPositioned::RotationType rot(Vector(0, 0, 1), -masterSectorPhi); - Vector vpos(vol->placement()->position()); - - rf = GloballyPositioned(GloballyPositioned::PositionType(rot.multiplyInverse(vpos)), - vol->placement()->rotation() * rot); - } - - retValue = MFGridFactory::build(fullPath, rf); - } - } catch (MagException& exc) { + retValue = builder.build(vol).release(); + } catch (edm::Exception& exc) { + cerr << "MagGeoBuilder: exception in reading table; " << exc.what() << endl; + if (!debug_) + throw; + return nullptr; + } catch (MagException const& exc) { LogTrace("MagGeoBuilder") << exc.what(); if (!debug_) throw; diff --git a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h index 3ceba58873bc8..ea2fcb4b46241 100644 --- a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h +++ b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h @@ -31,6 +31,7 @@ class MagESector; class MagVolume6Faces; namespace magneticfield { + class InterpolatorBuilder; class MagGeoBuilder { public: @@ -63,7 +64,7 @@ namespace magneticfield { private: // Build interpolator for the volume with "correct" rotation - MagProviderInterpol* buildInterpolator(const volumeHandle* vol) const; + MagProviderInterpol* buildInterpolator(const volumeHandle* vol, InterpolatorBuilder&) const; // Build all MagVolumes setting the MagProviderInterpol void buildMagVolumes(const handles& volumes, diff --git a/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc b/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc new file mode 100644 index 0000000000000..f599c91009e9d --- /dev/null +++ b/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc @@ -0,0 +1,85 @@ +// -*- C++ -*- +// +// Package: MagneticField/GeomBuilder +// Class : InterpolatorBuilder +// +// Implementation: +// [Notes on implementation] +// +// Original Author: Christopher Jones +// Created: Tue, 17 May 2022 20:50:21 GMT +// + +// system include files + +// user include files +#include "InterpolatorBuilder.h" +#include "FakeInterpolator.h" + +#include "FWCore/ParameterSet/interface/FileInPath.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" +#include "MagneticField/Interpolation/interface/MFGridFactory.h" +#include "MagneticField/Interpolation/interface/MFGrid.h" + +#include "DataFormats/Math/interface/angle_units.h" +// +// constants, enums and typedefs +// + +namespace magneticfield { + using namespace angle_units::operators; + + // + // static data member definitions + // + + // + // constructors and destructor + // + InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet) : tableSet_(std::move(iTableSet)) {} + + // + // member functions + // + std::unique_ptr InterpolatorBuilder::build(volumeHandle const* vol) { + if (tableSet_ == "fake" || vol->magFile == "fake") { + return std::make_unique(); + } + + auto fullPath = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); + if (fullPath.empty()) { + //cause the exception to happen + edm::FileInPath mydata("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); + return {}; + } + + // If the table is in "local" coordinates, must create a reference + // frame that is appropriately rotated along the CMS Z axis. + + GloballyPositioned rf = *(vol->placement()); + + if (vol->masterSector != 1) { + typedef Basic3DVector Vector; + + // Phi of the master sector + double masterSectorPhi = (vol->masterSector - 1) * 1._pi / 6.; + + GloballyPositioned::RotationType rot(Vector(0, 0, 1), -masterSectorPhi); + Vector vpos(vol->placement()->position()); + + rf = GloballyPositioned(GloballyPositioned::PositionType(rot.multiplyInverse(vpos)), + vol->placement()->rotation() * rot); + } + + magneticfield::interpolation::binary_ifstream strm(fullPath); + return std::unique_ptr(MFGridFactory::build(strm, rf)); + } + + // + // const member functions + // + + // + // static member functions + // +} // namespace magneticfield diff --git a/MagneticField/GeomBuilder/src/InterpolatorBuilder.h b/MagneticField/GeomBuilder/src/InterpolatorBuilder.h new file mode 100644 index 0000000000000..7b307441910c5 --- /dev/null +++ b/MagneticField/GeomBuilder/src/InterpolatorBuilder.h @@ -0,0 +1,50 @@ +#ifndef MagneticField_GeomBuilder_InterpolatorBuilder_h +#define MagneticField_GeomBuilder_InterpolatorBuilder_h +// -*- C++ -*- +// +// Package: MagneticField/GeomBuilder +// Class : InterpolatorBuilder +// +/**\class InterpolatorBuilder InterpolatorBuilder.h "InterpolatorBuilder.h" + + Description: [one line class summary] + + Usage: + + +*/ +// +// Original Author: Christopher Jones +// Created: Tue, 17 May 2022 20:50:20 GMT +// + +// system include files +#include + +// user include files +#include "MagneticField/Interpolation/interface/MagProviderInterpol.h" +#include "DD4hep_volumeHandle.h" + +// forward declarations + +namespace magneticfield { + class InterpolatorBuilder { + public: + InterpolatorBuilder(std::string iTableSet); + + InterpolatorBuilder(const InterpolatorBuilder&) = delete; // stop default + const InterpolatorBuilder& operator=(const InterpolatorBuilder&) = delete; // stop default + + // ---------- const member functions --------------------- + + // ---------- static member functions -------------------- + + // ---------- member functions --------------------------- + std::unique_ptr build(volumeHandle const*); + + private: + // ---------- member data -------------------------------- + std::string tableSet_; + }; +} // namespace magneticfield +#endif From a46c0e2b53455122b91720b0fcffcb3718fd7444 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Thu, 19 May 2022 10:40:28 -0500 Subject: [PATCH 04/10] Added seek interface to binary_ifstream --- MagneticField/Interpolation/interface/binary_ifstream.h | 3 +++ MagneticField/Interpolation/src/binary_ifstream.cc | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/MagneticField/Interpolation/interface/binary_ifstream.h b/MagneticField/Interpolation/interface/binary_ifstream.h index 3bf88a95d8547..f35ae0c37f1e1 100644 --- a/MagneticField/Interpolation/interface/binary_ifstream.h +++ b/MagneticField/Interpolation/interface/binary_ifstream.h @@ -41,6 +41,9 @@ namespace magneticfield::interpolation { bool operator!() const; operator bool() const; + long tellg(); + binary_ifstream& seekg(long); + private: FILE* file_; diff --git a/MagneticField/Interpolation/src/binary_ifstream.cc b/MagneticField/Interpolation/src/binary_ifstream.cc index 7e65919d7e8a3..945ad61c7bfdc 100644 --- a/MagneticField/Interpolation/src/binary_ifstream.cc +++ b/MagneticField/Interpolation/src/binary_ifstream.cc @@ -102,4 +102,11 @@ namespace magneticfield::interpolation { binary_ifstream::operator bool() const { return good(); } + long binary_ifstream::tellg() { return ftell(file_); } + + binary_ifstream& binary_ifstream::seekg(long to) { + fseek(file_, to, 0); + return *this; + } + } // namespace magneticfield::interpolation From 98be07d00e6271ae88c342156d95ca0548787b9e Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Mon, 23 May 2022 09:26:33 -0500 Subject: [PATCH 05/10] Added move interfaces to binanry_ifstream --- .../Interpolation/interface/binary_ifstream.h | 5 +++++ MagneticField/Interpolation/src/binary_ifstream.cc | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/MagneticField/Interpolation/interface/binary_ifstream.h b/MagneticField/Interpolation/interface/binary_ifstream.h index f35ae0c37f1e1..d15cab1555301 100644 --- a/MagneticField/Interpolation/interface/binary_ifstream.h +++ b/MagneticField/Interpolation/interface/binary_ifstream.h @@ -11,6 +11,11 @@ namespace magneticfield::interpolation { explicit binary_ifstream(const char* name); explicit binary_ifstream(const std::string& name); + binary_ifstream(const binary_ifstream&) = delete; + binary_ifstream(binary_ifstream&&); + binary_ifstream& operator=(const binary_ifstream&) = delete; + binary_ifstream& operator=(binary_ifstream&&); + ~binary_ifstream(); binary_ifstream& operator>>(char& n); diff --git a/MagneticField/Interpolation/src/binary_ifstream.cc b/MagneticField/Interpolation/src/binary_ifstream.cc index 945ad61c7bfdc..82cf648731169 100644 --- a/MagneticField/Interpolation/src/binary_ifstream.cc +++ b/MagneticField/Interpolation/src/binary_ifstream.cc @@ -18,6 +18,16 @@ namespace magneticfield::interpolation { } } + binary_ifstream::binary_ifstream(binary_ifstream&& iOther): file_(iOther.file_) { + iOther.file_ = nullptr; + } + + binary_ifstream& binary_ifstream::operator=(binary_ifstream&& iOther) { + binary_ifstream temp{std::move(iOther)}; + std::swap(file_, temp.file_); + return *this; + } + binary_ifstream::~binary_ifstream() { close(); } void binary_ifstream::close() { if (file_ != nullptr) @@ -105,7 +115,8 @@ namespace magneticfield::interpolation { long binary_ifstream::tellg() { return ftell(file_); } binary_ifstream& binary_ifstream::seekg(long to) { - fseek(file_, to, 0); + //if there is a problem, calling fail() will return true; + fseek(file_, to, SEEK_SET); return *this; } From 60aed87855bb85ae4c18b528f4742705e7811a9a Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Mon, 23 May 2022 09:27:22 -0500 Subject: [PATCH 06/10] Added interpolation field merged file handling --- .../GeomBuilder/src/InterpolatorBuilder.cc | 48 +++++++++++++---- .../GeomBuilder/src/InterpolatorBuilder.h | 5 ++ .../Interpolation/src/MFGridFactory.cc | 2 - .../BinaryTablesGeneration/mergeFieldTable.cc | 54 +++++++++++++++++++ .../Interpolation/test/BuildFile.xml | 5 ++ 5 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 MagneticField/Interpolation/test/BinaryTablesGeneration/mergeFieldTable.cc diff --git a/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc b/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc index f599c91009e9d..5c8cb4e5861a1 100644 --- a/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc +++ b/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc @@ -17,6 +17,7 @@ #include "FakeInterpolator.h" #include "FWCore/ParameterSet/interface/FileInPath.h" +#include "FWCore/Utilities/interface/Exception.h" #include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "MagneticField/Interpolation/interface/MFGridFactory.h" #include "MagneticField/Interpolation/interface/MFGrid.h" @@ -36,7 +37,22 @@ namespace magneticfield { // // constructors and destructor // - InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet) : tableSet_(std::move(iTableSet)) {} + InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet) : tableSet_(std::move(iTableSet)) { + auto indexFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.index"); + if (not indexFileName.empty()) { + auto binaryFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.bin"); + if (not binaryFileName.empty()) { + std::ifstream indexFile(indexFileName); + while (indexFile) { + std::string magFile; + unsigned int offset; + indexFile >> magFile >> offset; + offsets_.emplace(std::move(magFile), offset); + } + stream_ = interpolation::binary_ifstream(binaryFileName); + } + } + } // // member functions @@ -46,13 +62,6 @@ namespace magneticfield { return std::make_unique(); } - auto fullPath = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); - if (fullPath.empty()) { - //cause the exception to happen - edm::FileInPath mydata("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); - return {}; - } - // If the table is in "local" coordinates, must create a reference // frame that is appropriately rotated along the CMS Z axis. @@ -71,8 +80,27 @@ namespace magneticfield { vol->placement()->rotation() * rot); } - magneticfield::interpolation::binary_ifstream strm(fullPath); - return std::unique_ptr(MFGridFactory::build(strm, rf)); + if (not stream_) { + auto fullPath = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); + if (fullPath.empty()) { + //cause the exception to happen + edm::FileInPath mydata("MagneticField/Interpolation/data/" + tableSet_ + "/" + vol->magFile); + return {}; + } + + magneticfield::interpolation::binary_ifstream strm(fullPath); + return std::unique_ptr(MFGridFactory::build(strm, rf)); + } + + auto find = offsets_.find(vol->magFile); + if (find == offsets_.end()) { + throw cms::Exception("MissingMagFileEntry") << vol->magFile << " was not an entry in the index file"; + } + stream_->seekg(find->second); + if (stream_->fail()) { + throw cms::Exception("SeekMagFileEntry") << " failed seekg within merged binary file"; + } + return std::unique_ptr(MFGridFactory::build(*stream_, rf)); } // diff --git a/MagneticField/GeomBuilder/src/InterpolatorBuilder.h b/MagneticField/GeomBuilder/src/InterpolatorBuilder.h index 7b307441910c5..02fc83cd40e8a 100644 --- a/MagneticField/GeomBuilder/src/InterpolatorBuilder.h +++ b/MagneticField/GeomBuilder/src/InterpolatorBuilder.h @@ -20,9 +20,12 @@ // system include files #include +#include +#include // user include files #include "MagneticField/Interpolation/interface/MagProviderInterpol.h" +#include "MagneticField/Interpolation/interface/binary_ifstream.h" #include "DD4hep_volumeHandle.h" // forward declarations @@ -45,6 +48,8 @@ namespace magneticfield { private: // ---------- member data -------------------------------- std::string tableSet_; + std::unordered_map offsets_; + std::optional stream_; }; } // namespace magneticfield #endif diff --git a/MagneticField/Interpolation/src/MFGridFactory.cc b/MagneticField/Interpolation/src/MFGridFactory.cc index 43477ba93a2e4..d5bc521a290e5 100644 --- a/MagneticField/Interpolation/src/MFGridFactory.cc +++ b/MagneticField/Interpolation/src/MFGridFactory.cc @@ -15,7 +15,6 @@ using namespace std; MFGrid* MFGridFactory::build(const string& name, const GloballyPositioned& vol) { magneticfield::interpolation::binary_ifstream inFile(name); - return build(inFile, vol); } @@ -49,7 +48,6 @@ MFGrid* MFGridFactory::build(binary_ifstream& inFile, const GloballyPositioned + +#include +#include + +int main(int argc, char** argv) { + if (argc != 3) { + return 0; + } + + std::string directoryName = argv[1]; + + std::string baseName = argv[2]; + + const std::filesystem::path readDirectory{directoryName}; + + if (not is_directory(readDirectory)) { + std::cerr << "Error: " << readDirectory << " is not a directory"; + return 1; + } + + std::ofstream mergedFile(baseName + ".bin", std::ios::binary); + std::ofstream indexFile(baseName + ".index"); + + unsigned long int offset = 0; + for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{readDirectory}) { + if (dir_entry.is_regular_file()) { + std::string fileName = relative(dir_entry.path(), readDirectory); + //std::cout <<" "< buffer; + while (toRead.read(buffer.data(), buffer.size())) { + mergedFile.write(buffer.data(), buffer.size()); + } + if (toRead.bad()) { + std::cerr << " problem copying file " << dir_entry.path(); + return 1; + } + if (not toRead.eof()) { + std::cerr << " failed to read end of file " << dir_entry.path(); + return 1; + } + int lastRead = toRead.gcount(); + if (lastRead != 1024) { + mergedFile.write(buffer.data(), lastRead); + } + } + } + return 0; +} diff --git a/MagneticField/Interpolation/test/BuildFile.xml b/MagneticField/Interpolation/test/BuildFile.xml index 8a66eb2fb7942..755698ddd1f19 100644 --- a/MagneticField/Interpolation/test/BuildFile.xml +++ b/MagneticField/Interpolation/test/BuildFile.xml @@ -19,4 +19,9 @@ + + + + + From 03f6cd435b72d6c1a1b8ef8fb9cee64c4f9c023b Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Mon, 23 May 2022 10:28:55 -0500 Subject: [PATCH 07/10] Fix formating in binary_ifstream --- MagneticField/Interpolation/interface/binary_ifstream.h | 2 +- MagneticField/Interpolation/src/binary_ifstream.cc | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/MagneticField/Interpolation/interface/binary_ifstream.h b/MagneticField/Interpolation/interface/binary_ifstream.h index d15cab1555301..e2eff84761f0e 100644 --- a/MagneticField/Interpolation/interface/binary_ifstream.h +++ b/MagneticField/Interpolation/interface/binary_ifstream.h @@ -15,7 +15,7 @@ namespace magneticfield::interpolation { binary_ifstream(binary_ifstream&&); binary_ifstream& operator=(const binary_ifstream&) = delete; binary_ifstream& operator=(binary_ifstream&&); - + ~binary_ifstream(); binary_ifstream& operator>>(char& n); diff --git a/MagneticField/Interpolation/src/binary_ifstream.cc b/MagneticField/Interpolation/src/binary_ifstream.cc index 82cf648731169..57d94f9620915 100644 --- a/MagneticField/Interpolation/src/binary_ifstream.cc +++ b/MagneticField/Interpolation/src/binary_ifstream.cc @@ -18,9 +18,7 @@ namespace magneticfield::interpolation { } } - binary_ifstream::binary_ifstream(binary_ifstream&& iOther): file_(iOther.file_) { - iOther.file_ = nullptr; - } + binary_ifstream::binary_ifstream(binary_ifstream&& iOther) : file_(iOther.file_) { iOther.file_ = nullptr; } binary_ifstream& binary_ifstream::operator=(binary_ifstream&& iOther) { binary_ifstream temp{std::move(iOther)}; From 75ceeb4c54970a4a6710a7e10c9a21376f1a92b4 Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Mon, 23 May 2022 13:11:33 -0500 Subject: [PATCH 08/10] Extended GridFileReader to also use index files --- .../BinaryTablesGeneration/GridFileReader.cpp | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp b/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp index 5d3f57b7be2c9..86690a3bf8a44 100644 --- a/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp +++ b/MagneticField/Interpolation/test/BinaryTablesGeneration/GridFileReader.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "DataFormats/Math/interface/approx_exp.h" inline int bits(int a) { unsigned int aa = abs(a); @@ -24,8 +25,8 @@ inline int bits(int a) { using namespace std; using namespace approx_math; int main(int argc, char **argv) { - if (argc > 3) { - cout << "SYNOPSIS:" << endl << " GridFileReader input.bin [fullDump=true|false]" << endl; + if (argc < 3 or argc > 4) { + cout << "SYNOPSIS:" << endl << " GridFileReader input.bin [fullDump=true|false] [file.index]" << endl; cout << "Example:" << endl << " GridFileReader grid.217.bin false" << endl; return 1; } @@ -33,12 +34,45 @@ int main(int argc, char **argv) { bool fullDump = argv[2]; - magneticfield::interpolation::binary_ifstream inFile(filename); + string indexFile; + if (argc == 4) { + indexFile = argv[3]; + } + + std::optional tempFile; + if (indexFile.empty()) { + tempFile = magneticfield::interpolation::binary_ifstream(filename); + } else { + auto pos = indexFile.find('.'); + tempFile = magneticfield::interpolation::binary_ifstream(indexFile.substr(0, pos) + ".bin"); + } + auto inFile = std::move(*tempFile); if (!inFile) { cout << "file open failed!" << endl; return EXIT_FAILURE; } + if (not indexFile.empty()) { + std::ifstream index(indexFile); + + bool found = true; + while (index) { + std::string magFile; + unsigned int offset; + index >> magFile >> offset; + + if (magFile == filename) { + inFile.seekg(offset); + break; + } + } + if (not found) { + cout << "failed to find entry " << filename << " in index file " << indexFile << endl; + return EXIT_FAILURE; + } + cout << "Using index file: " << indexFile << endl; + } + cout << "Data File: " << filename << endl; // reading iterator (number of entries) From 1e773e6b7bf23daa609eb004bbfcca81886b3a0c Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Tue, 24 May 2022 08:30:02 -0500 Subject: [PATCH 09/10] Added documentation for mergeFileTable --- .../BinaryTablesGeneration/mergeFileTable.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 MagneticField/Interpolation/test/BinaryTablesGeneration/mergeFileTable.md diff --git a/MagneticField/Interpolation/test/BinaryTablesGeneration/mergeFileTable.md b/MagneticField/Interpolation/test/BinaryTablesGeneration/mergeFileTable.md new file mode 100644 index 0000000000000..18bfe7119dd73 --- /dev/null +++ b/MagneticField/Interpolation/test/BinaryTablesGeneration/mergeFileTable.md @@ -0,0 +1,18 @@ +# mergeFileTable Usage + +## Usage + +``` + mergeFileTable +``` + +The two arguments to the program are +1. `` the path of the directory holding the `s*` subdirectories that define the table set. +2. `` the base name of the the `.bin` and `.index` files to be generated. In general, this should always be `merged` in order to properly be read in cmsRun. + +## Action + +The program reads all the `*.bin` files in all the subdirectories and concatenates them into one `.bin` file. It also generates a `.index` file which contains the offsets in the newly generated `.bin` file for each `*.bin` file that was concatenated. + +The use of the merged file during cmsRun processing rather than reading the individual files provides a factor of 5 speedup in the initialization of the magnetic field. + From c00fa60c5ea5473caa28cf98295429c813354e1e Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Tue, 24 May 2022 08:30:54 -0500 Subject: [PATCH 10/10] Added parameter to disable use of merged files for MagField Extended or added fillDescriptions where needed. --- ...umeBasedMagneticField_dd4hep_160812_cfi.py | 1 - ...4hep_VolumeBasedMagneticFieldESProducer.cc | 33 ++++++++++++++++++- ...olumeBasedMagneticFieldESProducerFromDB.cc | 7 ++-- .../GeomBuilder/src/DD4hep_MagGeoBuilder.cc | 10 ++++-- .../GeomBuilder/src/DD4hep_MagGeoBuilder.h | 3 +- .../GeomBuilder/src/InterpolatorBuilder.cc | 5 ++- .../GeomBuilder/src/InterpolatorBuilder.h | 2 +- 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/MagneticField/Engine/python/volumeBasedMagneticField_dd4hep_160812_cfi.py b/MagneticField/Engine/python/volumeBasedMagneticField_dd4hep_160812_cfi.py index a5c19e8f322c4..501803e66896c 100644 --- a/MagneticField/Engine/python/volumeBasedMagneticField_dd4hep_160812_cfi.py +++ b/MagneticField/Engine/python/volumeBasedMagneticField_dd4hep_160812_cfi.py @@ -24,7 +24,6 @@ VolumeBasedMagneticFieldESProducer = cms.ESProducer("DD4hep_VolumeBasedMagneticFieldESProducer", VBFConfig_160812, - DDDetector = cms.ESInputTag('', 'magfield'), appendToDataLabel = cms.string(''), ) diff --git a/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducer.cc b/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducer.cc index b54ceb5b23edd..f0f614eeb0bf0 100644 --- a/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducer.cc +++ b/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducer.cc @@ -36,10 +36,13 @@ namespace magneticfield { std::unique_ptr produce(const IdealMagneticFieldRecord& iRecord); + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + private: edm::ParameterSet pset_; const bool debug_; const bool useParametrizedTrackerField_; + const bool useMergeFileIfAvailable_; const MagFieldConfig conf_; const std::string version_; edm::ESGetToken paramFieldToken_; @@ -53,6 +56,7 @@ DD4hep_VolumeBasedMagneticFieldESProducer::DD4hep_VolumeBasedMagneticFieldESProd : pset_{iConfig}, debug_{iConfig.getUntrackedParameter("debugBuilder", false)}, useParametrizedTrackerField_{iConfig.getParameter("useParametrizedTrackerField")}, + useMergeFileIfAvailable_{iConfig.getParameter("useMergeFileIfAvailable")}, conf_{iConfig, debug_}, version_{iConfig.getParameter("version")} { LogTrace("MagGeoBuilder") << "info:Constructing a DD4hep_VolumeBasedMagneticFieldESProducer"; @@ -71,7 +75,7 @@ std::unique_ptr DD4hep_VolumeBasedMagneticFieldESProducer::produc LogTrace("MagGeoBuilder") << "DD4hep_VolumeBasedMagneticFieldESProducer::produce() " << version_; } - MagGeoBuilder builder(conf_.version, conf_.geometryVersion, debug_); + MagGeoBuilder builder(conf_.version, conf_.geometryVersion, debug_, useMergeFileIfAvailable_); // Set scaling factors if (!conf_.keys.empty()) { @@ -106,4 +110,31 @@ std::unique_ptr DD4hep_VolumeBasedMagneticFieldESProducer::produc false); } +void DD4hep_VolumeBasedMagneticFieldESProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { + edm::ParameterSetDescription desc; + desc.addUntracked("debugBuilder", false); + desc.add("useMergeFileIfAvailable", true); + desc.add("useParametrizedTrackerField"); + desc.addUntracked("label", ""); + desc.add("version"); + desc.add("paramLabel"); + + //from MagFieldConfig + desc.add("geometryVersion"); + { + edm::ParameterSetDescription sub; + sub.add("volumes"); + sub.add("sectors"); + sub.add("master"); + sub.add("path"); + desc.addVPSet("gridFiles", sub); + } + desc.add >("scalingVolumes"); + desc.add >("scalingFactors"); + //default used to be compatible with older configurations + desc.add >("paramData", std::vector()); + + descriptions.addDefault(desc); +} + DEFINE_FWK_EVENTSETUP_MODULE(DD4hep_VolumeBasedMagneticFieldESProducer); diff --git a/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc b/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc index db3413c2a1cea..4a9e5d0e6bdfa 100644 --- a/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc +++ b/MagneticField/GeomBuilder/plugins/dd4hep/DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc @@ -74,12 +74,14 @@ namespace magneticfield { edm::ESGetToken mayConsumeBlobToken_; const bool debug_; + const bool useMergeFileIfAvailable_; }; } // namespace magneticfield DD4hep_VolumeBasedMagneticFieldESProducerFromDB::DD4hep_VolumeBasedMagneticFieldESProducerFromDB( const edm::ParameterSet& iConfig) - : debug_(iConfig.getUntrackedParameter("debugBuilder")) { + : debug_(iConfig.getUntrackedParameter("debugBuilder")), + useMergeFileIfAvailable_(iConfig.getParameter("useMergeFileIfAvailable")) { std::string const myConfigLabel = "VBMFESChoice"; usesResources({edm::ESSharedResourceNames::kDD4hep}); @@ -162,7 +164,7 @@ std::unique_ptr DD4hep_VolumeBasedMagneticFieldESProducerFromDB:: } // Full VolumeBased map + parametrization - MagGeoBuilder builder(conf->version, conf->geometryVersion, debug_); + MagGeoBuilder builder(conf->version, conf->geometryVersion, debug_, useMergeFileIfAvailable_); // Set scaling factors if (!conf->keys.empty()) { @@ -216,6 +218,7 @@ std::string_view DD4hep_VolumeBasedMagneticFieldESProducerFromDB::closerNominalL void DD4hep_VolumeBasedMagneticFieldESProducerFromDB::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { edm::ParameterSetDescription desc; desc.addUntracked("debugBuilder", false); + desc.add("useMergeFileIfAvailable", true); desc.add("valueOverride", -1)->setComment("Force value of current (in A); take the value from DB if < 0."); desc.addUntracked("label", ""); diff --git a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc index 11bd3f2fd5896..b737d6b3996ed 100644 --- a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc +++ b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc @@ -42,8 +42,12 @@ using namespace magneticfield; using namespace edm; using namespace angle_units::operators; -MagGeoBuilder::MagGeoBuilder(string tableSet, int geometryVersion, bool debug) - : tableSet_(tableSet), geometryVersion_(geometryVersion), theGridFiles_(nullptr), debug_(debug) { +MagGeoBuilder::MagGeoBuilder(string tableSet, int geometryVersion, bool debug, bool mergeFile) + : tableSet_(tableSet), + geometryVersion_(geometryVersion), + theGridFiles_(nullptr), + debug_(debug), + useMergeFileIfAvailable_(mergeFile) { LogTrace("MagGeoBuilder") << "Constructing a MagGeoBuilder"; } @@ -144,7 +148,7 @@ void MagGeoBuilder::build(const cms::DDDetector* det) { LogError("MagGeoBuilder") << "Filtered view has no node. Cannot build."; return; } - InterpolatorBuilder interpolatorBuilder(tableSet_); + InterpolatorBuilder interpolatorBuilder(tableSet_, useMergeFileIfAvailable_); while (doSubDets) { string name = fv.volume().volume().name(); LogTrace("MagGeoBuilder") << "Name: " << name; diff --git a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h index ea2fcb4b46241..9cdc6aea59ef5 100644 --- a/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h +++ b/MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h @@ -35,7 +35,7 @@ namespace magneticfield { class MagGeoBuilder { public: - MagGeoBuilder(std::string tableSet, int geometryVersion, bool debug = false); + MagGeoBuilder(std::string tableSet, int geometryVersion, bool debug = false, bool useMergeFileIfAvailable = true); ~MagGeoBuilder(); @@ -89,6 +89,7 @@ namespace magneticfield { const TableFileMap* theGridFiles_; // Non-owned pointer assumed to be valid until build() is called const bool debug_; + const bool useMergeFileIfAvailable_; }; } // namespace magneticfield #endif diff --git a/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc b/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc index 5c8cb4e5861a1..53ceb3673b04d 100644 --- a/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc +++ b/MagneticField/GeomBuilder/src/InterpolatorBuilder.cc @@ -37,7 +37,10 @@ namespace magneticfield { // // constructors and destructor // - InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet) : tableSet_(std::move(iTableSet)) { + InterpolatorBuilder::InterpolatorBuilder(std::string iTableSet, bool useMergeFileIfAvailable) + : tableSet_(std::move(iTableSet)) { + if (not useMergeFileIfAvailable) + return; auto indexFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.index"); if (not indexFileName.empty()) { auto binaryFileName = edm::FileInPath::findFile("MagneticField/Interpolation/data/" + tableSet_ + "/merged.bin"); diff --git a/MagneticField/GeomBuilder/src/InterpolatorBuilder.h b/MagneticField/GeomBuilder/src/InterpolatorBuilder.h index 02fc83cd40e8a..05f98f0476dc5 100644 --- a/MagneticField/GeomBuilder/src/InterpolatorBuilder.h +++ b/MagneticField/GeomBuilder/src/InterpolatorBuilder.h @@ -33,7 +33,7 @@ namespace magneticfield { class InterpolatorBuilder { public: - InterpolatorBuilder(std::string iTableSet); + InterpolatorBuilder(std::string iTableSet, bool useMergeFileIfAvailable = true); InterpolatorBuilder(const InterpolatorBuilder&) = delete; // stop default const InterpolatorBuilder& operator=(const InterpolatorBuilder&) = delete; // stop default