44 * modified by Simone Gennai INFN/Bicocca
55 */
66
7- #include " FWCore/Framework/interface/MakerMacros.h"
8- #include " FWCore/MessageLogger/interface/MessageLogger.h"
9- #include " FWCore/Framework/interface/ESHandle.h"
10- #include " FWCore/ServiceRegistry/interface/Service.h"
7+ // system includes
8+ #include < memory>
9+ #include < numeric>
10+
11+ // user includes
1112#include " DQM/BeamMonitor/plugins/OnlineBeamMonitor.h"
1213#include " DataFormats/Common/interface/Handle.h"
1314#include " DataFormats/Common/interface/View.h"
1415#include " FWCore/Framework/interface/ConsumesCollector.h"
16+ #include " FWCore/Framework/interface/ESHandle.h"
1517#include " FWCore/Framework/interface/EventSetup.h"
1618#include " FWCore/Framework/interface/LuminosityBlock.h"
19+ #include " FWCore/Framework/interface/MakerMacros.h"
1720#include " FWCore/Framework/interface/Run.h"
21+ #include " FWCore/MessageLogger/interface/MessageLogger.h"
22+ #include " FWCore/ServiceRegistry/interface/Service.h"
1823#include " RecoVertex/BeamSpotProducer/interface/BeamFitter.h"
1924#include " RecoVertex/BeamSpotProducer/interface/PVFitter.h"
20- #include < memory>
21-
22- #include < numeric>
2325
2426using namespace std ;
2527using namespace edm ;
@@ -136,16 +138,16 @@ void OnlineBeamMonitor::bookHistograms(DQMStore::IBooker& ibooker,
136138// Slightly better error handler
137139static void print_error (const std::exception& e) { edm::LogError (" BeamSpotOnlineParameters" ) << e.what () << ' \n ' ; }
138140
139- // Method to catch exceptions
140- template <typename T, class Except , class Func , class Response >
141- T try_ (Func f, Response r ) {
141+ // Generic try- catch template
142+ template <typename T, typename Func>
143+ T tryCatch (Func f, T errorValue ) {
142144 try {
143- LogDebug (" BeamSpotOnlineParameters" ) << " I have tried " << std::endl;
145+ LogDebug (" BeamSpotOnlineParameters" ) << " Trying function " << std::endl;
144146 return f ();
145- } catch (Except & e) {
146- LogDebug (" BeamSpotOnlineParameters" ) << " I have caught! " << std::endl;
147- r (e);
148- return static_cast <T>( " -999 " ) ;
147+ } catch (const std::exception & e) {
148+ LogDebug (" BeamSpotOnlineParameters" ) << " Caught exception " << std::endl;
149+ print_error (e);
150+ return errorValue ;
149151 }
150152}
151153
@@ -154,22 +156,45 @@ enum BSparameters {
154156 startTime = 0 , // 0 additional std::string parameters
155157 endTime = 1 , // 1
156158 lumiRange = 2 , // 2
157- END_OF_TYPES = 3 ,
159+ events = 3 , // 3 additional int parameters
160+ maxPV = 4 , // 4
161+ nPV = 5 , // 5
162+ meanPV = 6 , // 6 additional float parameters
163+ meanErrPV = 7 , // 7
164+ rmsPV = 8 , // 8
165+ rmsErrPV = 9 , // 9
166+ END_OF_TYPES = 10 ,
158167};
159168
160- // Functor
161- std::function<std::string(BSparameters, BeamSpotOnlineObjects)> myStringFunctor = [](BSparameters my_param,
162- BeamSpotOnlineObjects m_payload) {
163- std::string ret (" " );
164- switch (my_param) {
165- case startTime:
166- return m_payload.startTime ();
167- case endTime:
168- return m_payload.endTime ();
169- case lumiRange:
170- return m_payload.lumiRange ();
169+ // Unified functor
170+ using BeamSpotFunctor =
171+ std::function<std::variant<std::string, int , float >(BSparameters, const BeamSpotOnlineObjects&)>;
172+
173+ BeamSpotFunctor beamSpotFunctor = [](BSparameters param,
174+ const BeamSpotOnlineObjects& payload) -> std::variant<std::string, int , float > {
175+ switch (param) {
176+ case BSparameters::startTime:
177+ return payload.startTime ();
178+ case BSparameters::endTime:
179+ return payload.endTime ();
180+ case BSparameters::lumiRange:
181+ return payload.lumiRange ();
182+ case BSparameters::events:
183+ return payload.usedEvents ();
184+ case BSparameters::maxPV:
185+ return payload.maxPVs ();
186+ case BSparameters::nPV:
187+ return payload.numPVs ();
188+ case BSparameters::meanPV:
189+ return payload.meanPV ();
190+ case BSparameters::meanErrPV:
191+ return payload.meanErrorPV ();
192+ case BSparameters::rmsPV:
193+ return payload.rmsPV ();
194+ case BSparameters::rmsErrPV:
195+ return payload.rmsErrorPV ();
171196 default :
172- return ret ;
197+ throw std::invalid_argument ( " Unknown BS parameter " ) ;
173198 }
174199};
175200
@@ -188,8 +213,8 @@ void OnlineBeamMonitor::fetchBeamSpotInformation(const Event& iEvent, const Even
188213 ESHandle<BeamSpotOnlineObjects> bsHLTHandle;
189214 ESHandle<BeamSpotOnlineObjects> bsLegacyHandle;
190215 ESHandle<BeamSpotObjects> bsOnlineHandle;
191- // int lastLumiHLT_ = 0;
192- // int lastLumiLegacy_ = 0;
216+
217+ // Additional values for DIP publication
193218 std::string startTimeStamp_ = " 0" ;
194219 std::string startTimeStampHLT_ = " 0" ;
195220 std::string startTimeStampLegacy_ = " 0" ;
@@ -199,24 +224,52 @@ void OnlineBeamMonitor::fetchBeamSpotInformation(const Event& iEvent, const Even
199224 std::string lumiRange_ = " 0 - 0" ;
200225 std::string lumiRangeHLT_ = " 0 - 0" ;
201226 std::string lumiRangeLegacy_ = " 0 - 0" ;
227+ int events_ = 0 ;
228+ int eventsHLT_ = 0 ;
229+ int eventsLegacy_ = 0 ;
230+ int maxPV_ = 0 ;
231+ int maxPVHLT_ = 0 ;
232+ int maxPVLegacy_ = 0 ;
233+ int nPV_ = 0 ;
234+ int nPVHLT_ = 0 ;
235+ int nPVLegacy_ = 0 ;
236+ float meanPV_ = 0 .;
237+ float meanPVHLT_ = 0 .;
238+ float meanPVLegacy_ = 0 .;
239+ float meanErrPV_ = 0 .;
240+ float meanErrPVHLT_ = 0 .;
241+ float meanErrPVLegacy_ = 0 .;
242+ float rmsPV_ = 0 .;
243+ float rmsPVHLT_ = 0 .;
244+ float rmsPVLegacy_ = 0 .;
245+ float rmsErrPV_ = 0 .;
246+ float rmsErrPVHLT_ = 0 .;
247+ float rmsErrPVLegacy_ = 0 .;
202248
203249 if (auto bsHLTHandle = iSetup.getHandle (bsHLTToken_)) {
204250 auto const & spotDB = *bsHLTHandle;
205251
206- // lastLumiHLT_ = spotDB.lastAnalyzedLumi();
207- startTimeStampHLT_ =
208- try_<std::string, std::out_of_range>(std::bind (myStringFunctor, BSparameters::startTime, spotDB), print_error);
209- stopTimeStampHLT_ =
210- try_<std::string, std::out_of_range>(std::bind (myStringFunctor, BSparameters::endTime, spotDB), print_error);
211- lumiRangeHLT_ =
212- try_<std::string, std::out_of_range>(std::bind (myStringFunctor, BSparameters::lumiRange, spotDB), print_error);
252+ auto fetchValue = [&](BSparameters param, auto defaultValue) {
253+ return tryCatch ([&]() { return std::get<decltype (defaultValue)>(beamSpotFunctor (param, spotDB)); }, defaultValue);
254+ };
255+
256+ startTimeStampHLT_ = fetchValue (BSparameters::startTime, std::string (" 0" ));
257+ stopTimeStampHLT_ = fetchValue (BSparameters::endTime, std::string (" 0" ));
258+ lumiRangeHLT_ = fetchValue (BSparameters::lumiRange, std::string (" 0 - 0" ));
259+ eventsHLT_ = fetchValue (BSparameters::events, -999 );
260+ maxPVHLT_ = fetchValue (BSparameters::maxPV, -999 );
261+ nPVHLT_ = fetchValue (BSparameters::nPV, -999 );
262+ meanPVHLT_ = fetchValue (BSparameters::meanPV, -999 .0f );
263+ meanErrPVHLT_ = fetchValue (BSparameters::meanErrPV, -999 .0f );
264+ rmsPVHLT_ = fetchValue (BSparameters::rmsPV, -999 .0f );
265+ rmsErrPVHLT_ = fetchValue (BSparameters::rmsErrPV, -999 .0f );
213266
214267 // translate from BeamSpotObjects to reco::BeamSpot
215268 BeamSpot::Point apoint (spotDB.x (), spotDB.y (), spotDB.z ());
216269
217270 BeamSpot::CovarianceMatrix matrix;
218- for (int i = 0 ; i < 7 ; ++i) {
219- for (int j = 0 ; j < 7 ; ++j) {
271+ for (int i = 0 ; i < reco::BeamSpot::dimension ; ++i) {
272+ for (int j = 0 ; j < reco::BeamSpot::dimension ; ++j) {
220273 matrix (i, j) = spotDB.covariance (i, j);
221274 }
222275 }
@@ -242,23 +295,31 @@ void OnlineBeamMonitor::fetchBeamSpotInformation(const Event& iEvent, const Even
242295 LogError (" OnlineBeamMonitor" ) << " The database BeamSpot (hlt record) is not valid at lumi: "
243296 << iLumi.id ().luminosityBlock ();
244297 }
298+
245299 if (auto bsLegacyHandle = iSetup.getHandle (bsLegacyToken_)) {
246300 auto const & spotDB = *bsLegacyHandle;
247301
302+ auto fetchValue = [&](BSparameters param, auto defaultValue) {
303+ return tryCatch ([&]() { return std::get<decltype (defaultValue)>(beamSpotFunctor (param, spotDB)); }, defaultValue);
304+ };
305+
306+ startTimeStampLegacy_ = fetchValue (BSparameters::startTime, std::string (" 0" ));
307+ stopTimeStampLegacy_ = fetchValue (BSparameters::endTime, std::string (" 0" ));
308+ lumiRangeLegacy_ = fetchValue (BSparameters::lumiRange, std::string (" 0 - 0" ));
309+ eventsLegacy_ = fetchValue (BSparameters::events, -999 );
310+ maxPVLegacy_ = fetchValue (BSparameters::maxPV, -999 );
311+ nPVLegacy_ = fetchValue (BSparameters::nPV, -999 );
312+ meanPVLegacy_ = fetchValue (BSparameters::meanPV, -999 .0f );
313+ meanErrPVLegacy_ = fetchValue (BSparameters::meanErrPV, -999 .0f );
314+ rmsPVLegacy_ = fetchValue (BSparameters::rmsPV, -999 .0f );
315+ rmsErrPVLegacy_ = fetchValue (BSparameters::rmsErrPV, -999 .0f );
316+
248317 // translate from BeamSpotObjects to reco::BeamSpot
249318 BeamSpot::Point apoint (spotDB.x (), spotDB.y (), spotDB.z ());
250319
251- // lastLumiLegacy_ = spotDB.lastAnalyzedLumi();
252- startTimeStampLegacy_ =
253- try_<std::string, std::out_of_range>(std::bind (myStringFunctor, BSparameters::startTime, spotDB), print_error);
254- stopTimeStampLegacy_ =
255- try_<std::string, std::out_of_range>(std::bind (myStringFunctor, BSparameters::endTime, spotDB), print_error);
256- lumiRangeLegacy_ =
257- try_<std::string, std::out_of_range>(std::bind (myStringFunctor, BSparameters::lumiRange, spotDB), print_error);
258-
259320 BeamSpot::CovarianceMatrix matrix;
260- for (int i = 0 ; i < 7 ; ++i) {
261- for (int j = 0 ; j < 7 ; ++j) {
321+ for (int i = 0 ; i < reco::BeamSpot::dimension ; ++i) {
322+ for (int j = 0 ; j < reco::BeamSpot::dimension ; ++j) {
262323 matrix (i, j) = spotDB.covariance (i, j);
263324 }
264325 }
@@ -284,6 +345,7 @@ void OnlineBeamMonitor::fetchBeamSpotInformation(const Event& iEvent, const Even
284345 LogError (" OnlineBeamMonitor" ) << " The database BeamSpot (legacy record) is not valid at lumi: "
285346 << iLumi.id ().luminosityBlock ();
286347 }
348+
287349 if (auto bsOnlineHandle = iEvent.getHandle (bsOnlineToken_)) {
288350 auto const & spotOnline = *bsOnlineHandle;
289351
@@ -300,31 +362,41 @@ void OnlineBeamMonitor::fetchBeamSpotInformation(const Event& iEvent, const Even
300362 sprintf (index, " %s%i" , " _Run" , frun);
301363 tmpname.insert (outputDIPTxt_.length () - 4 , index);
302364 }
303- // int lastLumiAnalyzed_ = iLumi.id().luminosityBlock();
304365
305366 if (beamSpotInfo->beamSpotsMap_ .find (" Online" ) != beamSpotInfo->beamSpotsMap_ .end ()) {
306367 if (beamSpotInfo->beamSpotsMap_ .find (" HLT" ) != beamSpotInfo->beamSpotsMap_ .end () &&
307368 beamSpotInfo->beamSpotsMap_ [" Online" ].x0 () == beamSpotInfo->beamSpotsMap_ [" HLT" ].x0 ()) {
308- // lastLumiAnalyzed_ = lastLumiHLT_;
309369 startTimeStamp_ = startTimeStampHLT_;
310370 stopTimeStamp_ = stopTimeStampHLT_;
311371 lumiRange_ = lumiRangeHLT_;
312-
372+ events_ = eventsHLT_;
373+ maxPV_ = maxPVHLT_;
374+ nPV_ = nPVHLT_;
375+ meanPV_ = meanPVHLT_;
376+ meanErrPV_ = meanErrPVHLT_;
377+ rmsPV_ = rmsPVHLT_;
378+ rmsErrPV_ = rmsErrPVHLT_;
313379 } else if (beamSpotInfo->beamSpotsMap_ .find (" Legacy" ) != beamSpotInfo->beamSpotsMap_ .end () &&
314380 beamSpotInfo->beamSpotsMap_ [" Online" ].x0 () == beamSpotInfo->beamSpotsMap_ [" Legacy" ].x0 ()) {
315- // lastLumiAnalyzed_ = lastLumiLegacy_;
316381 startTimeStamp_ = startTimeStampLegacy_;
317382 stopTimeStamp_ = stopTimeStampLegacy_;
318383 lumiRange_ = lumiRangeLegacy_;
384+ events_ = eventsLegacy_;
385+ maxPV_ = maxPVLegacy_;
386+ nPV_ = nPVLegacy_;
387+ meanPV_ = meanPVLegacy_;
388+ meanErrPV_ = meanErrPVLegacy_;
389+ rmsPV_ = rmsPVLegacy_;
390+ rmsErrPV_ = rmsErrPVLegacy_;
319391 }
320392 }
321393
322394 outFile.open (tmpname.c_str ());
323395
324- outFile << " Runnumber " << frun << " bx " << 0 << std::endl;
396+ // Write out file for DIP publication
397+ outFile << " Runnumber " << frun << std::endl;
325398 outFile << " BeginTimeOfFit " << startTimeStamp_ << " " << 0 << std::endl;
326399 outFile << " EndTimeOfFit " << stopTimeStamp_ << " " << 0 << std::endl;
327- // outFile << "LumiRange " << lumiRange_ << " - " << lastLumiAnalyzed_ << std::endl;
328400 outFile << " LumiRange " << lumiRange_ << std::endl;
329401 outFile << " Type " << spotOnline.type () << std::endl;
330402 outFile << " X0 " << spotOnline.x0 () << std::endl;
@@ -335,17 +407,23 @@ void OnlineBeamMonitor::fetchBeamSpotInformation(const Event& iEvent, const Even
335407 outFile << " dydz " << spotOnline.dydz () << std::endl;
336408 outFile << " BeamWidthX " << spotOnline.BeamWidthX () << std::endl;
337409 outFile << " BeamWidthY " << spotOnline.BeamWidthY () << std::endl;
338- for (int i = 0 ; i < 6 ; ++i) {
410+ for (int i = 0 ; i < reco::BeamSpot::dimension ; ++i) {
339411 outFile << " Cov(" << i << " ,j) " ;
340- for (int j = 0 ; j < 7 ; ++j) {
412+ for (int j = 0 ; j < reco::BeamSpot::dimension ; ++j) {
341413 outFile << spotOnline.covariance (i, j) << " " ;
342414 }
343415 outFile << std::endl;
344416 }
345- outFile << " Cov(6,j) 0 0 0 0 0 0 " << spotOnline.covariance (6 , 6 ) << std::endl;
346417 outFile << " EmittanceX " << spotOnline.emittanceX () << std::endl;
347418 outFile << " EmittanceY " << spotOnline.emittanceY () << std::endl;
348419 outFile << " BetaStar " << spotOnline.betaStar () << std::endl;
420+ outFile << " events " << events_ << std::endl;
421+ outFile << " meanPV " << meanPV_ << std::endl;
422+ outFile << " meanErrPV " << meanErrPV_ << std::endl;
423+ outFile << " rmsPV " << rmsPV_ << std::endl;
424+ outFile << " rmsErrPV " << rmsErrPV_ << std::endl;
425+ outFile << " maxPV " << maxPV_ << std::endl;
426+ outFile << " nPV " << nPV_ << std::endl;
349427
350428 outFile.close ();
351429 }
0 commit comments