CMS 3D CMS Logo

BeamSpotCompatibilityChecker.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: RecoVertex/BeamSpotProducer
4 // Class: BeamSpotCompatibilityChecker
5 //
13 //
14 // Original Author: Marco Musich
15 // Created: Thu, 23 Apr 2020 09:00:45 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
41 
42 //
43 // class declaration
44 //
45 
46 // ancillary struct to check compatibility
47 namespace {
48 
49  struct Significance {
50  Significance(const double& a, const double& b, const double& errA, const double& errB)
51  : m_A(a), m_B(b), m_ErrA(errA), m_ErrB(errB) {
52  if (m_ErrA == 0 && m_ErrB == 0) {
53  edm::LogError("LogicalError") << "Can't calculate significance when both errors are zero!" << std::endl;
54  }
55  m_combinedError = std::sqrt((m_ErrA * m_ErrA) + (m_ErrB * m_ErrB));
56  }
57 
58  float getSig(bool verbose) {
59  if (verbose) {
60  edm::LogInfo("BeamSpotCompatibilityChecker")
61  << "A= " << m_A << "+/-" << m_ErrA << " "
62  << "B= " << m_B << "+/-" << m_ErrB << " | delta=" << std::abs(m_A - m_B) << "+/-" << m_combinedError
63  << std::endl;
64  }
65  return std::abs(m_A - m_B) / m_combinedError;
66  }
67 
68  private:
69  double m_A;
70  double m_B;
71  double m_ErrA;
72  double m_ErrB;
73  double m_ErrAB;
74  double m_combinedError;
75  };
76 } // namespace
77 
79 public:
81  ~BeamSpotCompatibilityChecker() override = default;
82 
83  void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const final;
84  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
85  static std::array<float, 6> compareBS(const reco::BeamSpot& BSA, const reco::BeamSpot& BSB);
86 
87 private:
88  // ----------member data ---------------------------
89  static constexpr int cmToum = 10000;
90  const double warningThreshold_;
91  const double throwingThreshold_;
92  const bool verbose_;
93  const bool dbFromEvent_;
94  const edm::EDGetTokenT<reco::BeamSpot> bsFromEventToken_; // beamSpot from the event
96  edm::EDGetTokenT<reco::BeamSpot> dbBSfromEventToken_; // beamSpot from the DB (via event)
97 };
98 
99 //
100 // constructors and destructor
101 //
103  : warningThreshold_(iConfig.getParameter<double>("warningThr")),
104  throwingThreshold_(iConfig.getParameter<double>("errorThr")),
105  verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)),
106  dbFromEvent_(iConfig.getParameter<bool>("dbFromEvent")),
107  bsFromEventToken_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("bsFromEvent"))) {
108  //now do what ever initialization is needed
110  throw cms::Exception("ConfigurationError")
111  << __PRETTY_FUNCTION__ << "\n Warning threshold (" << warningThreshold_
112  << ") cannot be smaller than the throwing threshold (" << throwingThreshold_ << ")" << std::endl;
113  }
114  if (dbFromEvent_) {
115  edm::LogWarning("BeamSpotCompatibilityChecker")
116  << "!!!! Warning !!!\nThe Database Beam Spot is going to be taken from the Event via BeamSpotProducer!";
117  dbBSfromEventToken_ = consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("bsFromDB"));
118  } else {
119  bsFromDBToken_ = esConsumes<BeamSpotObjects, BeamSpotObjectsRcd>();
120  }
121 }
122 
123 //
124 // member functions
125 //
126 
127 // ------------ method called for each event ------------
129  edm::Event const& iEvent,
130  edm::EventSetup const& iSetup) const {
131  using namespace edm;
132  reco::BeamSpot spotEvent, spotDB;
133 
134  edm::Handle<reco::BeamSpot> beamSpotFromEventHandle;
135  iEvent.getByToken(bsFromEventToken_, beamSpotFromEventHandle);
136  spotEvent = *beamSpotFromEventHandle;
137 
138  double evt_BSx0 = spotEvent.x0();
139  double evt_BSy0 = spotEvent.y0();
140  double evt_BSz0 = spotEvent.z0();
141  double evt_Beamsigmaz = spotEvent.sigmaZ();
142  double evt_BeamWidthX = spotEvent.BeamWidthX();
143  double evt_BeamWidthY = spotEvent.BeamWidthY();
144 
145  if (!dbFromEvent_) {
147  const BeamSpotObjects* aSpot = beamhandle.product();
148 
149  // translate from BeamSpotObjects to reco::BeamSpot
150  reco::BeamSpot::Point apoint(aSpot->x(), aSpot->y(), aSpot->z());
151 
153  for (int i = 0; i < reco::BeamSpot::dimension; ++i) {
154  for (int j = 0; j < reco::BeamSpot::dimension; ++j) {
155  matrix(i, j) = aSpot->covariance(i, j);
156  }
157  }
158 
159  // this assume beam width same in x and y
160  spotDB = reco::BeamSpot(apoint, aSpot->sigmaZ(), aSpot->dxdz(), aSpot->dydz(), aSpot->beamWidthX(), matrix);
161  } else {
162  // take the DB beamspot from the event (different label)
163  edm::Handle<reco::BeamSpot> beamSpotFromDBHandle;
164  iEvent.getByToken(dbBSfromEventToken_, beamSpotFromDBHandle);
165  spotDB = *beamSpotFromDBHandle;
166  }
167 
168  double db_BSx0 = spotDB.x0();
169  double db_BSy0 = spotDB.y0();
170  double db_BSz0 = spotDB.z0();
171  double db_Beamsigmaz = spotDB.sigmaZ();
172  double db_BeamWidthX = spotDB.BeamWidthX();
173  double db_BeamWidthY = spotDB.BeamWidthY();
174 
175  double deltaX0 = evt_BSx0 - db_BSx0;
176  double deltaY0 = evt_BSy0 - db_BSy0;
177  double deltaZ0 = evt_BSz0 - db_BSz0;
178  double deltaSigmaX = evt_BeamWidthX - db_BeamWidthX;
179  double deltaSigmaY = evt_BeamWidthY - db_BeamWidthY;
180  double deltaSigmaZ = evt_Beamsigmaz - db_Beamsigmaz;
181 
182  if (verbose_) {
183  edm::LogPrint("BeamSpotCompatibilityChecker") << "BS from Event: \n" << spotEvent << std::endl;
184  edm::LogPrint("BeamSpotCompatibilityChecker") << "BS from DB: \n" << spotDB << std::endl;
185  }
186 
187  auto significances = compareBS(spotDB, spotEvent);
188  std::vector<std::string> labels = {"x0", "y0", "z0", "sigmaX", "sigmaY", "sigmaZ"};
189 
190  std::string msg = " |delta X0|=" + std::to_string(std::abs(deltaX0) * cmToum) +
191  " um |delta Y0|=" + std::to_string(std::abs(deltaY0) * cmToum) +
192  " um |delta Z0|=" + std::to_string(std::abs(deltaZ0) * cmToum) +
193  " um |delta sigmaX|=" + std::to_string(std::abs(deltaSigmaX) * cmToum) +
194  " um |delta sigmaY|=" + std::to_string(std::abs(deltaSigmaY) * cmToum) +
195  " um |delta sigmaZ|=" + std::to_string(std::abs(deltaSigmaZ)) + "cm";
196  if (verbose_) {
197  edm::LogPrint("BeamSpotCompatibilityChecker") << msg.c_str() << std::endl;
198  }
199 
200  for (unsigned int i = 0; i < 3; i++) {
201  auto sig = significances.at(i);
202  if (sig > throwingThreshold_) {
203  edm::LogError("BeamSpotCompatibilityChecker") << msg.c_str() << std::endl;
204  throw cms::Exception("BeamSpotCompatibilityChecker")
205  << "[" << __PRETTY_FUNCTION__ << "] \n DB-Event BeamSpot " << labels.at(i) << " distance sigificance " << sig
206  << ", exceeds the threshold of " << throwingThreshold_ << "!" << std::endl;
207  } else if (sig > warningThreshold_) {
208  edm::LogWarning("BeamSpotCompatibilityChecker") << msg.c_str() << std::endl;
209  edm::LogWarning("BeamSpotCompatibilityChecker")
210  << "[" << __PRETTY_FUNCTION__ << "] \n DB-Event BeamSpot " << labels.at(i) << " distance significance "
211  << sig << ", exceeds the threshold of " << warningThreshold_ << "!" << std::endl;
212  }
213  }
214 }
215 
216 std::array<float, 6> BeamSpotCompatibilityChecker::compareBS(const reco::BeamSpot& beamSpotA,
217  const reco::BeamSpot& beamSpotB) {
218  auto xS = Significance(beamSpotA.x0(), beamSpotB.x0(), beamSpotA.x0Error(), beamSpotB.x0Error());
219  auto yS = Significance(beamSpotA.y0(), beamSpotB.y0(), beamSpotA.y0Error(), beamSpotB.y0Error());
220  auto zS = Significance(beamSpotA.z0(), beamSpotB.z0(), beamSpotA.z0Error(), beamSpotB.z0Error());
221  auto xWS = Significance(
222  beamSpotA.BeamWidthX(), beamSpotB.BeamWidthX(), beamSpotA.BeamWidthXError(), beamSpotB.BeamWidthXError());
223  auto yWS = Significance(
224  beamSpotA.BeamWidthY(), beamSpotB.BeamWidthY(), beamSpotA.BeamWidthYError(), beamSpotB.BeamWidthYError());
225  auto zWS = Significance(beamSpotA.sigmaZ(), beamSpotB.sigmaZ(), beamSpotA.sigmaZ0Error(), beamSpotB.sigmaZ0Error());
226 
227  std::array<float, 6> ret = {
228  {xS.getSig(false), yS.getSig(false), zS.getSig(false), xWS.getSig(false), yWS.getSig(false), zWS.getSig(false)}};
229  return ret;
230 }
231 
232 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
235  desc.add<double>("warningThr", 1.)->setComment("Threshold on the signficances to emit a warning");
236  desc.add<double>("errorThr", 3.)->setComment("Threshold on the signficances to abort the job");
237  desc.addUntracked<bool>("verbose", false)->setComment("verbose output");
238  desc.add<edm::InputTag>("bsFromEvent", edm::InputTag(""))
239  ->setComment("edm::InputTag on the BeamSpot from the Event (Reference)");
240  desc.add<bool>("dbFromEvent", false)
241  ->setComment("Switch to take the (target) DB beamspot from the event instead of the EventSetup");
242  desc.add<edm::InputTag>("bsFromDB", edm::InputTag(""))
243  ->setComment("edm::InputTag on the BeamSpot from the Event (Target)\n To be used only if dbFromEvent is True");
244  descriptions.addWithDefaultLabel(desc);
245 }
246 
247 //define this as a plug-in
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:29
double BeamWidthX() const
beam width X
Definition: BeamSpot.h:82
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
double z() const
get Z beam position
double dydz() const
get dydz slope, crossing angle in YZ
double BeamWidthYError() const
error on beam width Y, assume error in X = Y
Definition: BeamSpot.h:101
const edm::EDGetTokenT< reco::BeamSpot > bsFromEventToken_
double covariance(int i, int j) const
get i,j element of the full covariance matrix 7x7
bool verbose
ret
prodAgent to be discontinued
static std::array< float, 6 > compareBS(const reco::BeamSpot &BSA, const reco::BeamSpot &BSB)
edm::ESGetToken< BeamSpotObjects, BeamSpotObjectsRcd > bsFromDBToken_
double x0Error() const
error on x
Definition: BeamSpot.h:86
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:27
double beamWidthX() const
get average transverse beam width
edm::EDGetTokenT< reco::BeamSpot > dbBSfromEventToken_
Log< level::Error, false > LogError
double z0Error() const
error on z
Definition: BeamSpot.h:90
void analyze(edm::StreamID, edm::Event const &, edm::EventSetup const &) const final
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static std::string to_string(const XMLCh *ch)
double sigmaZ0Error() const
error on sigma z
Definition: BeamSpot.h:92
double x0() const
x coordinate
Definition: BeamSpot.h:61
int iEvent
Definition: GenABIO.cc:224
T const * product() const
Definition: ESHandle.h:86
T sqrt(T t)
Definition: SSEVec.h:23
BeamSpotCompatibilityChecker(const edm::ParameterSet &)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double BeamWidthY() const
beam width Y
Definition: BeamSpot.h:84
double x() const
get X beam position
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double y0() const
y coordinate
Definition: BeamSpot.h:63
Log< level::Warning, true > LogPrint
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
double BeamWidthXError() const
error on beam width X, assume error in X = Y
Definition: BeamSpot.h:99
Log< level::Info, false > LogInfo
double y() const
get Y beam position
double sigmaZ() const
sigma z
Definition: BeamSpot.h:76
double b
Definition: hdecay.h:120
double sigmaZ() const
get sigma Z, RMS bunch length
tuple msg
Definition: mps_check.py:286
double z0() const
z coordinate
Definition: BeamSpot.h:65
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
~BeamSpotCompatibilityChecker() override=default
Log< level::Warning, false > LogWarning
double dxdz() const
get dxdz slope, crossing angle in XZ
double y0Error() const
error on y
Definition: BeamSpot.h:88