CMS 3D CMS Logo

CTPPSLHCInfoRandomXangleESSource.cc
Go to the documentation of this file.
1 // Original Author: Jan Kašpar
2 
11 
14 
15 #include "CLHEP/Random/RandFlat.h"
16 #include "CLHEP/Random/JamesRandom.h"
17 
18 #include "TFile.h"
19 #include "TH2D.h"
20 
21 //----------------------------------------------------------------------------------------------------
22 
27 public:
31 
32 private:
34  const edm::IOVSyncValue &,
35  edm::ValidityInterval &) override;
36 
38 
39  unsigned int m_generateEveryNEvents;
40 
41  double m_beamEnergy;
42 
43  std::unique_ptr<CLHEP::HepRandomEngine> m_engine;
44 
45  struct BinData {
46  double min, max;
47  double xangle, betaStar;
48  };
49 
50  std::vector<BinData> xangleBetaStarBins;
51 };
52 
53 //----------------------------------------------------------------------------------------------------
54 //----------------------------------------------------------------------------------------------------
55 
57  : m_label(conf.getParameter<std::string>("label")),
58 
59  m_generateEveryNEvents(conf.getParameter<unsigned int>("generateEveryNEvents")),
60 
61  m_beamEnergy(conf.getParameter<double>("beamEnergy")),
62 
63  m_engine(new CLHEP::HepJamesRandom(conf.getParameter<unsigned int>("seed"))) {
64  // get input beta* vs. xangle histogram
65  const auto &xangleBetaStarHistogramFile = conf.getParameter<std::string>("xangleBetaStarHistogramFile");
66  const auto &xangleBetaStarHistogramObject = conf.getParameter<std::string>("xangleBetaStarHistogramObject");
67 
69  std::unique_ptr<TFile> f_in(TFile::Open(fip.fullPath().c_str()));
70  if (!f_in)
71  throw cms::Exception("PPS") << "Cannot open input file '" << xangleBetaStarHistogramFile << "'.";
72 
73  TH2D *h_xangle_beta_star = (TH2D *)f_in->Get(xangleBetaStarHistogramObject.c_str());
74  if (!h_xangle_beta_star)
75  throw cms::Exception("PPS") << "Cannot load input object '" << xangleBetaStarHistogramObject << "'.";
76 
77  // parse histogram
78  double sum = 0.;
79  for (int x = 1; x <= h_xangle_beta_star->GetNbinsX(); ++x) {
80  for (int y = 1; y <= h_xangle_beta_star->GetNbinsY(); ++y)
81  sum += h_xangle_beta_star->GetBinContent(x, y);
82  }
83 
84  double cw = 0;
85  for (int x = 1; x <= h_xangle_beta_star->GetNbinsX(); ++x) {
86  for (int y = 1; y <= h_xangle_beta_star->GetNbinsY(); ++y) {
87  const double c = h_xangle_beta_star->GetBinContent(x, y);
88  const double xangle = h_xangle_beta_star->GetXaxis()->GetBinCenter(x);
89  const double betaStar = h_xangle_beta_star->GetYaxis()->GetBinCenter(y);
90 
91  if (c > 0.) {
92  const double rc = c / sum;
93  xangleBetaStarBins.push_back({cw, cw + rc, xangle, betaStar});
94  cw += rc;
95  }
96  }
97  }
98 
99  setWhatProduced(this, m_label);
100  findingRecord<LHCInfoRcd>();
101 }
102 
103 //----------------------------------------------------------------------------------------------------
104 
107 
108  desc.add<std::string>("label", "")->setComment("label of the LHCInfo record");
109 
110  desc.add<unsigned int>("seed", 1)->setComment("random seed");
111 
112  desc.add<unsigned int>("generateEveryNEvents", 1)->setComment("how often to generate new xangle");
113 
114  desc.add<std::string>("xangleBetaStarHistogramFile", "")->setComment("ROOT file with xangle distribution");
115  desc.add<std::string>("xangleBetaStarHistogramObject", "")->setComment("xangle distribution object in the ROOT file");
116 
117  desc.add<double>("beamEnergy", 0.)->setComment("beam energy");
118 
119  descriptions.add("ctppsLHCInfoRandomXangleESSource", desc);
120 }
121 
122 //----------------------------------------------------------------------------------------------------
123 
125  const edm::IOVSyncValue &iosv,
126  edm::ValidityInterval &oValidity) {
127  edm::EventID beginEvent = iosv.eventID();
128  edm::EventID endEvent(beginEvent.run(), beginEvent.luminosityBlock(), beginEvent.event() + m_generateEveryNEvents);
129  oValidity = edm::ValidityInterval(edm::IOVSyncValue(beginEvent), edm::IOVSyncValue(endEvent));
130 }
131 
132 //----------------------------------------------------------------------------------------------------
133 
135  auto output = std::make_unique<LHCInfo>();
136 
137  double xangle = 0., betaStar = 0.;
138  const double u = CLHEP::RandFlat::shoot(m_engine.get(), 0., 1.);
139  for (const auto &d : xangleBetaStarBins) {
140  if (d.min <= u && u <= d.max) {
141  xangle = d.xangle;
142  betaStar = d.betaStar;
143  break;
144  }
145  }
146 
147  output->setEnergy(m_beamEnergy);
148  output->setCrossingAngle(xangle);
149  output->setBetaStar(betaStar);
150 
152 }
153 
154 //----------------------------------------------------------------------------------------------------
155 
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:163
edm::ESProducts< std::unique_ptr< LHCInfo > > produce(const LHCInfoRcd &)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
ESProducts< std::remove_reference_t< TArgs >... > products(TArgs &&... args)
Definition: ESProducts.h:128
std::unique_ptr< CLHEP::HepRandomEngine > m_engine
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:17
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:39
static void fillDescriptions(edm::ConfigurationDescriptions &)
CTPPSLHCInfoRandomXangleESSource(const edm::ParameterSet &)
d
Definition: ztail.py:151
xangleBetaStarHistogramFile
Definition: base_cff.py:18
RunNumber_t run() const
Definition: EventID.h:38
xangleBetaStarHistogramObject
Definition: base_cff.py:19
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:91
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const EventID & eventID() const
Definition: IOVSyncValue.h:40
Provides LHCInfo data necessary for CTPPS reconstruction (and direct simulation). ...
def move(src, dest)
Definition: eostools.py:511
EventNumber_t event() const
Definition: EventID.h:40