Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:25

0001 /** \class AutoParametrizedMagneticFieldProducer
0002  *
0003  *   Description: Producer for parametrized Magnetics Fields, with value scaled depending on current.
0004  *
0005  */
0006 
0007 #include "FWCore/Framework/interface/ESProducer.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 #include "MagneticField/Engine/interface/MagneticField.h"
0012 #include "MagneticField/ParametrizedEngine/interface/ParametrizedMagneticFieldFactory.h"
0013 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0014 
0015 #include "CondFormats/RunInfo/interface/RunInfo.h"
0016 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
0017 
0018 #include <string>
0019 
0020 using namespace std;
0021 using namespace edm;
0022 
0023 namespace magneticfield {
0024   class AutoParametrizedMagneticFieldProducer : public edm::ESProducer {
0025   public:
0026     AutoParametrizedMagneticFieldProducer(const edm::ParameterSet&);
0027     ~AutoParametrizedMagneticFieldProducer() override {}
0028 
0029     std::unique_ptr<MagneticField> produce(const IdealMagneticFieldRecord&);
0030 
0031     int closerNominaCurrent(float current) const;
0032     const std::string version_;
0033     const int currentOverride_;
0034     const std::array<int, 7> nominalCurrents_;
0035     //  std::vector<std::string> nominalLabels_;
0036     edm::ESGetToken<RunInfo, RunInfoRcd> runInfoToken_;
0037   };
0038 }  // namespace magneticfield
0039 
0040 using namespace magneticfield;
0041 
0042 AutoParametrizedMagneticFieldProducer::AutoParametrizedMagneticFieldProducer(const edm::ParameterSet& iConfig)
0043     : version_{iConfig.getParameter<string>("version")},
0044       currentOverride_{iConfig.getParameter<int>("valueOverride")},
0045       nominalCurrents_{{-1, 0, 9558, 14416, 16819, 18268, 19262}}
0046 //  nominalLabels_{["3.8T","0T","2T", "3T", "3.5T", "3.8T", "4T"}}
0047 {
0048   auto cc = setWhatProduced(this, iConfig.getUntrackedParameter<std::string>("label", ""));
0049   if (currentOverride_ < 0) {
0050     runInfoToken_ = cc.consumes();
0051   }
0052 }
0053 
0054 std::unique_ptr<MagneticField> AutoParametrizedMagneticFieldProducer::produce(const IdealMagneticFieldRecord& iRecord) {
0055   // Get value of the current from condition DB
0056   float current = currentOverride_;
0057   string message;
0058   if (current < 0) {
0059     current = iRecord.get(runInfoToken_).m_avg_current;
0060     message = " (from RunInfo DB)";
0061   } else {
0062     message = " (from valueOverride card)";
0063   }
0064   float cnc = closerNominaCurrent(current);
0065 
0066   edm::LogInfo("MagneticField") << "Current: " << current << message << "; using map for: " << cnc;
0067 
0068   vector<double> parameters;
0069 
0070   auto version = version_;
0071   if (cnc == 0) {
0072     version = "Uniform";
0073     parameters.push_back(0);
0074   } else if (version == "Parabolic") {
0075     parameters.push_back(3.8114);        //c1
0076     parameters.push_back(-3.94991e-06);  //b0
0077     parameters.push_back(7.53701e-06);   //b1
0078     parameters.push_back(2.43878e-11);   //a
0079     if (cnc !=
0080         18268) {  // Linear scaling for B!= 3.8T; note that just c1, b0 and b1 have to be scaled to get linear scaling
0081       double scale = double(cnc) / double(18268);
0082       parameters[0] *= scale;
0083       parameters[1] *= scale;
0084       parameters[2] *= scale;
0085     }
0086   } else {
0087     //Other parametrizations are not relevant here and not supported
0088     throw cms::Exception("InvalidParameter") << "version " << version << " is not supported";
0089   }
0090 
0091   return ParametrizedMagneticFieldFactory::get(version, parameters);
0092 }
0093 
0094 int AutoParametrizedMagneticFieldProducer::closerNominaCurrent(float current) const {
0095   int i = 0;
0096   for (; i < (int)nominalCurrents_.size() - 1; i++) {
0097     if (2 * current < nominalCurrents_[i] + nominalCurrents_[i + 1])
0098       return nominalCurrents_[i];
0099   }
0100   return nominalCurrents_[i];
0101 }
0102 
0103 #include "FWCore/Framework/interface/ModuleFactory.h"
0104 DEFINE_FWK_EVENTSETUP_MODULE(AutoParametrizedMagneticFieldProducer);