Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:58

0001 // authors: Jan Kaspar (jan.kaspar@gmail.com)
0002 
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/Framework/interface/ESHandle.h"
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/ESProducer.h"
0007 #include "FWCore/Framework/interface/ModuleFactory.h"
0008 
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 #include "CondFormats/DataRecord/interface/CTPPSInterpolatedOpticsRcd.h"
0012 #include "CondFormats/PPSObjects/interface/LHCInterpolatedOpticalFunctionsSetCollection.h"
0013 
0014 #include "DataFormats/CTPPSDetId/interface/CTPPSDetId.h"
0015 
0016 class CTPPSModifiedOpticalFunctionsESSource : public edm::ESProducer {
0017 public:
0018   CTPPSModifiedOpticalFunctionsESSource(const edm::ParameterSet &);
0019   ~CTPPSModifiedOpticalFunctionsESSource() override {}
0020 
0021   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0022 
0023   std::shared_ptr<LHCInterpolatedOpticalFunctionsSetCollection> produce(const CTPPSInterpolatedOpticsRcd &);
0024 
0025 private:
0026   const edm::ESGetToken<LHCInterpolatedOpticalFunctionsSetCollection, CTPPSInterpolatedOpticsRcd> inputOpticsToken_;
0027 
0028   const std::string scenario_;
0029 
0030   const double factor_;
0031 
0032   const unsigned int rpDecId_45_N_, rpDecId_45_F_, rpDecId_56_N_, rpDecId_56_F_;
0033 };
0034 
0035 //----------------------------------------------------------------------------------------------------
0036 //----------------------------------------------------------------------------------------------------
0037 
0038 CTPPSModifiedOpticalFunctionsESSource::CTPPSModifiedOpticalFunctionsESSource(const edm::ParameterSet &iConfig)
0039     : inputOpticsToken_(setWhatProduced(this, iConfig.getParameter<std::string>("outputOpticsLabel"))
0040                             .consumes(edm::ESInputTag("", iConfig.getParameter<std::string>("inputOpticsLabel")))),
0041       scenario_(iConfig.getParameter<std::string>("scenario")),
0042       factor_(iConfig.getParameter<double>("factor")),
0043       rpDecId_45_N_(iConfig.getParameter<unsigned int>("rpId_45_N")),
0044       rpDecId_45_F_(iConfig.getParameter<unsigned int>("rpId_45_F")),
0045       rpDecId_56_N_(iConfig.getParameter<unsigned int>("rpId_56_N")),
0046       rpDecId_56_F_(iConfig.getParameter<unsigned int>("rpId_56_F")) {}
0047 
0048 //----------------------------------------------------------------------------------------------------
0049 
0050 void CTPPSModifiedOpticalFunctionsESSource::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0051   edm::ParameterSetDescription desc;
0052 
0053   desc.add<std::string>("inputOpticsLabel", "")->setComment("label of the input optics records");
0054   desc.add<std::string>("outputOpticsLabel", "modified")->setComment("label of the output optics records");
0055 
0056   desc.add<std::string>("scenario", "none")->setComment("name of modification scenario");
0057 
0058   desc.add<double>("factor", 0.)->setComment("size of modification (number of sigmas)");
0059 
0060   desc.add<unsigned int>("rpId_45_N", 0)->setComment("decimal RP id for 45 near");
0061   desc.add<unsigned int>("rpId_45_F", 0)->setComment("decimal RP id for 45 far");
0062   desc.add<unsigned int>("rpId_56_N", 0)->setComment("decimal RP id for 56 near");
0063   desc.add<unsigned int>("rpId_56_F", 0)->setComment("decimal RP id for 56 far");
0064 
0065   descriptions.add("ctppsModifiedOpticalFunctionsESSource", desc);
0066 }
0067 
0068 //----------------------------------------------------------------------------------------------------
0069 
0070 std::shared_ptr<LHCInterpolatedOpticalFunctionsSetCollection> CTPPSModifiedOpticalFunctionsESSource::produce(
0071     const CTPPSInterpolatedOpticsRcd &iRecord) {
0072   // get input
0073   LHCInterpolatedOpticalFunctionsSetCollection const &input = iRecord.get(inputOpticsToken_);
0074 
0075   // prepare output
0076   std::shared_ptr<LHCInterpolatedOpticalFunctionsSetCollection> output =
0077       std::make_shared<LHCInterpolatedOpticalFunctionsSetCollection>(input);
0078 
0079   // premare arm/RP id map
0080   struct ArmInfo {
0081     unsigned int rpId_N = 0, rpId_F = 0;
0082   };
0083 
0084   std::map<unsigned int, ArmInfo> armInfo;
0085 
0086   for (const auto &fsp : *output) {
0087     CTPPSDetId rpId(fsp.first);
0088     unsigned int rpDecId = 100 * rpId.arm() + 10 * rpId.station() + rpId.rp();
0089 
0090     if (rpDecId == rpDecId_45_N_)
0091       armInfo[0].rpId_N = fsp.first;
0092     if (rpDecId == rpDecId_45_F_)
0093       armInfo[0].rpId_F = fsp.first;
0094     if (rpDecId == rpDecId_56_N_)
0095       armInfo[1].rpId_N = fsp.first;
0096     if (rpDecId == rpDecId_56_F_)
0097       armInfo[1].rpId_F = fsp.first;
0098   }
0099 
0100   // loop over arms
0101   bool applied = false;
0102 
0103   for (const auto &ap : armInfo) {
0104     const auto &arm = ap.first;
0105 
0106     //printf("* arm %u\n", arm);
0107 
0108     auto &of_N = output->find(ap.second.rpId_N)->second;
0109     auto &of_F = output->find(ap.second.rpId_F)->second;
0110 
0111     const double z_N = of_N.getScoringPlaneZ();
0112     const double z_F = of_F.getScoringPlaneZ();
0113     const double de_z = (arm == 0) ? z_N - z_F : z_F - z_N;
0114 
0115     //printf("  z_N = %.3f m, z_F = %.3f m\n", z_N*1E-2, z_F*1E-2);
0116 
0117     if (of_N.m_xi_values.size() != of_N.m_xi_values.size())
0118       throw cms::Exception("CTPPSModifiedOpticalFunctionsESSource")
0119           << "Different xi sampling of optical functions in near and far RP.";
0120 
0121     // loop over sampling points (in xi)
0122     for (unsigned int i = 0; i < of_N.m_xi_values.size(); ++i) {
0123       const double xi = of_N.m_xi_values[i];
0124 
0125       double x_d_N = of_N.m_fcn_values[LHCOpticalFunctionsSet::exd][i];
0126       double x_d_F = of_F.m_fcn_values[LHCOpticalFunctionsSet::exd][i];
0127 
0128       double L_x_N = of_N.m_fcn_values[LHCOpticalFunctionsSet::eLx][i];
0129       double L_x_F = of_F.m_fcn_values[LHCOpticalFunctionsSet::eLx][i];
0130       double Lp_x = of_N.m_fcn_values[LHCOpticalFunctionsSet::eLpx][i];
0131 
0132       double L_y_N = of_N.m_fcn_values[LHCOpticalFunctionsSet::eLy][i];
0133       double L_y_F = of_F.m_fcn_values[LHCOpticalFunctionsSet::eLy][i];
0134       double Lp_y = of_N.m_fcn_values[LHCOpticalFunctionsSet::eLpy][i];
0135 
0136       //printf("  xi = %.3f, Lp_x = %.3f, %.3f\n", xi, Lp_x, (L_x_F - L_x_N) / de_z);
0137 
0138       // apply modification scenario
0139       if (scenario_ == "none")
0140         applied = true;
0141 
0142       if (scenario_ == "Lx") {
0143         const double a = 3180., b = 40.;  // cm
0144         const double de_L_x = factor_ * (a * xi + b);
0145         L_x_N += de_L_x;
0146         L_x_F += de_L_x;
0147         applied = true;
0148       }
0149 
0150       if (scenario_ == "Lpx") {
0151         const double a = 0.42, b = 0.015;  // dimensionless
0152         const double de_Lp_x = factor_ * (a * xi + b) * Lp_x;
0153         Lp_x += de_Lp_x;
0154         L_x_N -= de_Lp_x * de_z / 2.;
0155         L_x_F += de_Lp_x * de_z / 2.;
0156         applied = true;
0157       }
0158 
0159       if (scenario_ == "xd") {
0160         const double d = 0.08;  // dimensionless
0161         x_d_N += x_d_N * d * factor_;
0162         x_d_F += x_d_F * d * factor_;
0163         applied = true;
0164       }
0165 
0166       // TODO: for test only
0167       if (scenario_ == "Lx-scale") {
0168         L_x_N *= factor_;
0169         L_x_F *= factor_;
0170         applied = true;
0171       }
0172 
0173       if (scenario_ == "Lpy") {
0174         const double a = 2.66, b = 0.015;  // dimensionless
0175         const double de_Lp_y = factor_ * (a * xi + b) * Lp_y;
0176         Lp_y += de_Lp_y;
0177         L_y_N -= de_Lp_y * de_z / 2.;
0178         L_y_F += de_Lp_y * de_z / 2.;
0179         applied = true;
0180       }
0181 
0182       // store updated values
0183       of_N.m_fcn_values[LHCOpticalFunctionsSet::exd][i] = x_d_N;
0184       of_F.m_fcn_values[LHCOpticalFunctionsSet::exd][i] = x_d_F;
0185 
0186       of_N.m_fcn_values[LHCOpticalFunctionsSet::eLx][i] = L_x_N;
0187       of_F.m_fcn_values[LHCOpticalFunctionsSet::eLx][i] = L_x_F;
0188 
0189       of_N.m_fcn_values[LHCOpticalFunctionsSet::eLpx][i] = Lp_x;
0190       of_F.m_fcn_values[LHCOpticalFunctionsSet::eLpx][i] = Lp_x;
0191 
0192       of_N.m_fcn_values[LHCOpticalFunctionsSet::eLy][i] = L_y_N;
0193       of_F.m_fcn_values[LHCOpticalFunctionsSet::eLy][i] = L_y_F;
0194 
0195       of_N.m_fcn_values[LHCOpticalFunctionsSet::eLpy][i] = Lp_y;
0196       of_F.m_fcn_values[LHCOpticalFunctionsSet::eLpy][i] = Lp_y;
0197     }
0198 
0199     // re-initialise splines
0200     of_N.initializeSplines();
0201     of_F.initializeSplines();
0202   }
0203 
0204   // modification applied?
0205   if (!applied)
0206     edm::LogError("CTPPSModifiedOpticalFunctionsESSource") << "Could not apply scenario `" + scenario_ + "'.";
0207 
0208   // save modified output
0209   return output;
0210 }
0211 
0212 //----------------------------------------------------------------------------------------------------
0213 
0214 DEFINE_FWK_EVENTSETUP_MODULE(CTPPSModifiedOpticalFunctionsESSource);