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 "TH1D.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  double m_betaStar;
43 
44  std::unique_ptr<CLHEP::HepRandomEngine> m_engine;
45 
46  struct BinData {
47  double min, max, xangle;
48  };
49 
50  std::vector<BinData> binData;
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  m_betaStar(conf.getParameter<double>("betaStar")),
63 
64  m_engine(new CLHEP::HepJamesRandom(conf.getParameter<unsigned int>("seed"))) {
65  const auto &xangleHistogramFile = conf.getParameter<std::string>("xangleHistogramFile");
66  const auto &xangleHistogramObject = conf.getParameter<std::string>("xangleHistogramObject");
67 
68  TFile *f_in = TFile::Open(xangleHistogramFile.c_str());
69  TH1D *h_xangle = (TH1D *)f_in->Get(xangleHistogramObject.c_str());
70 
71  double s = 0.;
72  for (int bi = 1; bi <= h_xangle->GetNbinsX(); ++bi)
73  s += h_xangle->GetBinContent(bi);
74 
75  double cw = 0.;
76  for (int bi = 1; bi <= h_xangle->GetNbinsX(); ++bi) {
77  double xangle = h_xangle->GetBinCenter(bi);
78  double w = h_xangle->GetBinContent(bi) / s;
79 
80  binData.push_back({cw, cw + w, xangle});
81 
82  cw += w;
83  }
84 
85  delete f_in;
86 
87  setWhatProduced(this, m_label);
88  findingRecord<LHCInfoRcd>();
89 }
90 
91 //----------------------------------------------------------------------------------------------------
92 
95 
96  desc.add<std::string>("label", "")->setComment("label of the LHCInfo record");
97 
98  desc.add<unsigned int>("seed", 1)->setComment("random seed");
99 
100  desc.add<unsigned int>("generateEveryNEvents", 1)->setComment("how often to generate new xangle");
101 
102  desc.add<std::string>("xangleHistogramFile", "")->setComment("ROOT file with xangle distribution");
103  desc.add<std::string>("xangleHistogramObject", "")->setComment("xangle distribution object in the ROOT file");
104 
105  desc.add<double>("beamEnergy", 0.)->setComment("beam energy");
106  desc.add<double>("betaStar", 0.)->setComment("beta*");
107 
108  descriptions.add("ctppsLHCInfoRandomXangleESSource", desc);
109 }
110 
111 //----------------------------------------------------------------------------------------------------
112 
114  const edm::IOVSyncValue &iosv,
115  edm::ValidityInterval &oValidity) {
116  edm::EventID beginEvent = iosv.eventID();
117  edm::EventID endEvent(beginEvent.run(), beginEvent.luminosityBlock(), beginEvent.event() + m_generateEveryNEvents);
118  oValidity = edm::ValidityInterval(edm::IOVSyncValue(beginEvent), edm::IOVSyncValue(endEvent));
119 }
120 
121 //----------------------------------------------------------------------------------------------------
122 
124  auto output = std::make_unique<LHCInfo>();
125 
126  const double u = CLHEP::RandFlat::shoot(m_engine.get(), 0., 1.);
127 
128  double xangle = 0.;
129  for (const auto &d : binData) {
130  if (d.min <= u && u <= d.max) {
131  xangle = d.xangle;
132  break;
133  }
134  }
135 
136  output->setEnergy(m_beamEnergy);
137  output->setCrossingAngle(xangle);
138  output->setBetaStar(m_betaStar);
139 
141 }
142 
143 //----------------------------------------------------------------------------------------------------
144 
RunNumber_t run() const
Definition: EventID.h:39
T getParameter(std::string const &) const
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:124
EventNumber_t event() const
Definition: EventID.h:41
void setComment(std::string const &value)
edm::ESProducts< std::unique_ptr< LHCInfo > > produce(const LHCInfoRcd &)
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
const double w
Definition: UKUtility.cc:23
const EventID & eventID() const
Definition: IOVSyncValue.h:40
std::unique_ptr< CLHEP::HepRandomEngine > m_engine
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:19
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
ESProducts< std::remove_reference_t< TArgs >... > products(TArgs &&...args)
Definition: ESProducts.h:128
static void fillDescriptions(edm::ConfigurationDescriptions &)
CTPPSLHCInfoRandomXangleESSource(const edm::ParameterSet &)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:91
Provides LHCInfo data necessary for CTPPS reconstruction (and direct simulation). ...
def move(src, dest)
Definition: eostools.py:511