CMS 3D CMS Logo

CTPPSInterpolatedOpticalFunctionsESSource.cc
Go to the documentation of this file.
1 // authors: Jan Kaspar (jan.kaspar@gmail.com)
2 
8 
10 
14 
18 
20 public:
23 
24  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
25 
26  std::shared_ptr<LHCInterpolatedOpticalFunctionsSetCollection> produce(const CTPPSInterpolatedOpticsRcd &);
27 
28 private:
31  std::shared_ptr<LHCInterpolatedOpticalFunctionsSetCollection> currentData_;
34 };
35 
36 //----------------------------------------------------------------------------------------------------
37 //----------------------------------------------------------------------------------------------------
38 
42  .setConsumes(opticsToken_)
43  .setConsumes(lhcInfoToken_, edm::ESInputTag("", iConfig.getParameter<std::string>("lhcInfoLabel")));
44 }
45 
46 //----------------------------------------------------------------------------------------------------
47 
50 
51  desc.add<std::string>("lhcInfoLabel", "")->setComment("label of the LHCInfo record");
52 
53  descriptions.add("ctppsInterpolatedOpticalFunctionsESSource", desc);
54 }
55 
56 //----------------------------------------------------------------------------------------------------
57 
58 std::shared_ptr<LHCInterpolatedOpticalFunctionsSetCollection> CTPPSInterpolatedOpticalFunctionsESSource::produce(
59  const CTPPSInterpolatedOpticsRcd &iRecord) {
60  // get the input data
61  LHCOpticalFunctionsSetCollection const &ofColl = iRecord.get(opticsToken_);
62 
63  LHCInfo const &lhcInfo = iRecord.get(lhcInfoToken_);
64 
65  // is there anything to do?
67  return currentData_;
68 
69  // is crossing angle reasonable (LHCInfo is correctly filled in DB)?
70  if (lhcInfo.crossingAngle() == 0.) {
71  edm::LogInfo("CTPPSInterpolatedOpticalFunctionsESSource")
72  << "Invalid crossing angle, no optical functions produced.";
73 
74  currentDataValid_ = false;
76  currentData_ = std::make_shared<LHCInterpolatedOpticalFunctionsSetCollection>();
77 
78  return currentData_;
79  }
80 
81  // set new crossing angle
83  edm::LogInfo("CTPPSInterpolatedOpticalFunctionsESSource")
84  << "Crossing angle has changed to " << currentCrossingAngle_ << ".";
85 
86  // is input optics available ?
87  if (ofColl.empty()) {
88  edm::LogInfo("CTPPSInterpolatedOpticalFunctionsESSource")
89  << "No input optics available, no optical functions produced.";
90 
91  currentDataValid_ = false;
92  currentData_ = std::make_shared<LHCInterpolatedOpticalFunctionsSetCollection>();
93 
94  return currentData_;
95  }
96 
97  // regular case with single-xangle input
98  if (ofColl.size() == 1) {
99  const auto &it = ofColl.begin();
100 
101  // does the input xangle correspond to the actual one?
102  if (fabs(currentCrossingAngle_ - it->first) > 1e-6)
103  throw cms::Exception("CTPPSInterpolatedOpticalFunctionsESSource")
104  << "Cannot interpolate: input given only for xangle " << it->first << " while interpolation requested for "
105  << currentCrossingAngle_ << ".";
106 
107  currentData_ = std::make_shared<LHCInterpolatedOpticalFunctionsSetCollection>();
108  for (const auto &rp_p : it->second) {
109  const auto rpId = rp_p.first;
110  LHCInterpolatedOpticalFunctionsSet iof(rp_p.second);
111  iof.initializeSplines();
112  currentData_->emplace(rpId, std::move(iof));
113  }
114 
115  currentDataValid_ = true;
116  }
117 
118  // regular case with multi-xangle input
119  if (ofColl.size() > 1) {
120  // find the closest xangle points for interpolation
121  auto it1 = ofColl.begin();
122  auto it2 = std::next(it1);
123 
124  if (currentCrossingAngle_ > it1->first) {
125  for (; it1 != ofColl.end(); ++it1) {
126  it2 = std::next(it1);
127 
128  if (it2 == ofColl.end()) {
129  it2 = it1;
130  it1 = std::prev(it1);
131  break;
132  }
133 
134  if (it1->first <= currentCrossingAngle_ && currentCrossingAngle_ < it2->first)
135  break;
136  }
137  }
138 
139  const auto &xangle1 = it1->first;
140  const auto &xangle2 = it2->first;
141 
142  const auto &ofs1 = it1->second;
143  const auto &ofs2 = it2->second;
144 
145  // do the interpoaltion RP by RP
146  currentData_ = std::make_shared<LHCInterpolatedOpticalFunctionsSetCollection>();
147  for (const auto &rp_p : ofs1) {
148  const auto rpId = rp_p.first;
149  const auto &rp_it2 = ofs2.find(rpId);
150  if (rp_it2 == ofs2.end())
151  throw cms::Exception("CTPPSInterpolatedOpticalFunctionsESSource") << "RP mismatch between ofs1 and ofs2.";
152 
153  const auto &of1 = rp_p.second;
154  const auto &of2 = rp_it2->second;
155 
156  const size_t num_xi_vals1 = of1.getXiValues().size();
157  const size_t num_xi_vals2 = of2.getXiValues().size();
158 
159  if (num_xi_vals1 != num_xi_vals2)
160  throw cms::Exception("CTPPSInterpolatedOpticalFunctionsESSource") << "Size mismatch between ofs1 and ofs2.";
161 
162  const size_t num_xi_vals = num_xi_vals1;
163 
165  iof.m_z = of1.getScoringPlaneZ();
167  iof.m_xi_values.resize(num_xi_vals);
168 
169  for (size_t fi = 0; fi < of1.getFcnValues().size(); ++fi) {
170  iof.m_fcn_values[fi].resize(num_xi_vals);
171 
172  for (size_t pi = 0; pi < num_xi_vals; ++pi) {
173  double xi = of1.getXiValues()[pi];
174  double xi_control = of2.getXiValues()[pi];
175 
176  if (fabs(xi - xi_control) > 1e-6)
177  throw cms::Exception("CTPPSInterpolatedOpticalFunctionsESSource") << "Xi mismatch between ofs1 and ofs2.";
178 
179  iof.m_xi_values[pi] = xi;
180 
181  double v1 = of1.getFcnValues()[fi][pi];
182  double v2 = of2.getFcnValues()[fi][pi];
183  iof.m_fcn_values[fi][pi] = v1 + (v2 - v1) / (xangle2 - xangle1) * (currentCrossingAngle_ - xangle1);
184  }
185  }
186 
187  iof.initializeSplines();
188 
189  currentDataValid_ = true;
190  currentData_->emplace(rpId, std::move(iof));
191  }
192  }
193 
194  return currentData_;
195 }
196 
197 //----------------------------------------------------------------------------------------------------
198 
T getParameter(std::string const &) const
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:138
std::shared_ptr< LHCInterpolatedOpticalFunctionsSetCollection > currentData_
std::shared_ptr< LHCInterpolatedOpticalFunctionsSetCollection > produce(const CTPPSInterpolatedOpticsRcd &)
const Double_t pi
float const crossingAngle() const
Definition: LHCInfo.cc:182
std::vector< double > m_xi_values
ProductT const & get(ESGetToken< ProductT, DepRecordT > const &iToken) const
void initializeSplines()
builds splines from m_*_values fields
ParameterDescriptionBase * add(U const &iLabel, T const &value)
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
edm::ESGetToken< LHCOpticalFunctionsSetCollection, CTPPSOpticsRcd > opticsToken_
std::vector< std::vector< double > > m_fcn_values
length unit cm
Set of optical functions corresponding to one scoring plane along LHC, including splines for interpol...
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Collection of optical functions for two crossing angle values and various scoring planes...
double m_z
position of the scoring plane, in LHC/TOTEM convention, cm
def move(src, dest)
Definition: eostools.py:511