31 static const double muon_barrel_endcap_boundary = 1.05;
35 m_appliedSystematics(nullptr),
38 m_efficiencyMapReplicaArray(),
39 m_muonquality(
"Medium"),
40 m_calibration_version(
"230222_Winter_r22"),
44 m_experimental(false),
47 m_replicaTriggerList(),
50 m_ReplicaRandomSeed(12345) {
63 declareProperty(
"AllowZeroSF",
m_allowZeroSF,
"If a trigger is not available will return 0 instead of throwing an error. More difficult to spot configuration issues. Use at own risk");
73 if (
year == 2015)
fileName =
"muontrigger_sf_2015_mc20a_v1.root";
74 else if (
year == 2016)
fileName =
"muontrigger_sf_2016_mc20a_v1.root";
75 else if (
year == 2017)
fileName =
"muontrigger_sf_2017_mc20d_v1.root";
76 else if (
year == 2018)
fileName =
"muontrigger_sf_2018_mc20e_v2.root";
77 else if (
year == 2022)
fileName =
"muontrigger_sf_2022_mc21_v05.root";
80 return StatusCode::SUCCESS;
95 ATH_MSG_INFO(
"Note: setting up with user specified input file location " <<
m_custom_dir <<
" - this is not encouraged!");
101 if (
file ==
nullptr || !
file->IsOpen()) {
103 return StatusCode::FAILURE;
108 static const std::vector<std::string>
type {
"data",
"mc" };
109 static const std::vector<std::string> region {
"barrel",
"endcap" };
110 static const std::vector<std::string> systematic {
"nominal",
"stat_up",
"stat_down",
"syst_up",
"syst_down" };
115 if (qualityDirectory ==
nullptr) {
116 ATH_MSG_FATAL(
"MuonTriggerScaleFactors::initialize cannot find directory with selected quality");
117 return StatusCode::FAILURE;
120 TIter nextPeriod(qualityDirectory->GetListOfKeys());
121 while ((periodKey = (TKey*) nextPeriod())) {
122 if (not periodKey->IsFolder())
continue;
123 TDirectory* periodDirectory = qualityDirectory->GetDirectory(periodKey->GetName());
124 std::string
periodName = std::string(periodKey->GetName());
128 TIter nextTrigger(periodDirectory->GetListOfKeys());
129 while ((triggerKey = (TKey*) nextTrigger())) {
130 if (not triggerKey->IsFolder())
continue;
131 TDirectory* triggerDirectory = periodDirectory->GetDirectory(triggerKey->GetName());
132 std::string triggerName = std::string(triggerKey->GetName());
133 if(!std::set<std::string>{
"HLT_mu26_ivarmedium",
"HLT_mu50",
"HLT_mu26_ivarmedium_OR_HLT_mu50"}.count(triggerName) &&
m_binning ==
"coarse"){
134 ATH_MSG_DEBUG(
"Coarse binning not supported for di-muon trigger legs at the moment");
137 for (
const auto& iregion : region) {
138 bool isBarrel = iregion.find(
"barrel") != std::string::npos;
139 for (
const auto& itype :
type) {
140 bool isData = itype.find(
"data") != std::string::npos;
141 std::string
histname = (
"_MuonTrigEff_" +
periodName +
"_" + triggerName +
"_" + quality +
"_" +
"_EtaPhi_" +
m_binning +
"_" + iregion +
"_" + itype);
142 for (
const auto& isys : systematic) {
143 if (itype.find(
"data") != std::string::npos && isys.find(
"syst") != std::string::npos)
continue;
144 std::string
path =
"eff_etaphi_" +
m_binning +
"_" + iregion +
"_" + itype +
"_" + isys;
145 TH2*
hist =
dynamic_cast<TH2*
>(triggerDirectory->Get(
path.c_str()));
148 ATH_MSG_FATAL(
"MuonTriggerScaleFactors::initialize " <<
path <<
" not found under trigger " << triggerName <<
" and period " <<
periodName <<
" for year: " <<
year);
151 hist->SetDirectory(0);
156 return StatusCode::FAILURE;
158 m_efficiencyMap.insert(std::pair<EffiHistoIdent, TH1_Ptr>(HistoId, std::shared_ptr < TH1 > (
hist)));
168 TH1_Ptr tmp_h2 =
TH1_Ptr(
dynamic_cast<TH2F*
>(Nominal_H->Clone(Form(
"tmp_h2_%s", Nominal_H->GetName()))));
169 const int xbins = tmp_h2->GetNbinsX(),
ybins = tmp_h2->GetNbinsY();
170 for (
int x_i = 0; x_i <=
xbins; ++x_i) {
171 for (
int y_i = 0; y_i <=
ybins; ++y_i) {
172 double statErr = std::abs(tmp_h2->GetBinContent(x_i, y_i) - StatUp_H->GetBinContent(x_i, y_i));
173 tmp_h2->SetBinError(x_i, y_i, statErr);
185 return StatusCode::SUCCESS;
202 return StatusCode::FAILURE;
207 return StatusCode::FAILURE;
214 static const int years_to_run[5] = {2015, 2016, 2017, 2018, 2022};
215 for (
const int &
year: years_to_run) {
221 return StatusCode::SUCCESS;
226 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getTriggerScaleFactor This is an experimental function. If you really know what you are doing set UseExperimental property.");
231 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getTriggerScaleFactor Trigger must have value.");
238 ATH_MSG_WARNING(
"What you are trying to do is not correct. For di-muon triggers you should get the efficiency with getTriggerEfficiency and compute the SF by yourself.");
239 else if (
trigger.find(
"HLT_2mu10") != std::string::npos ||
trigger.find(
"HLT_2mu14") != std::string::npos)
240 ATH_MSG_WARNING(
"Di-muon trigger scale factors for single reco muons are not supported!");
248 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getTriggerScaleFactor Trigger must have value.");
254 if (
trigger ==
"HLT_mu8noL1") {
255 ATH_MSG_WARNING(
"What you are trying to do is not correct. For di-muon triggers you should get the efficiency with getTriggerEfficiency and compute the SF by yourself.");
257 else if (
trigger.find(
"HLT_2mu10") != std::string::npos ||
trigger.find(
"HLT_2mu14") != std::string::npos) {
275 std::size_t
pos = sysBaseName.find(
"MCTOY");
276 if (
pos == std::string::npos)
return -1;
277 return atoi(sysBaseName.substr(
pos + 5,
pos + 8).c_str());
282 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getTriggerEfficiency Trigger must have value.");
302 std::string systype =
"";
304 systype =
"syst_down";
308 systype =
"stat_down";
322 if (configuration.
replicaIndex != -1) systype =
"replicas";
339 std::vector<TH1_Ptr> replica_v;
340 const int xbins =
h->GetNbinsX(),
ybins =
h->GetNbinsY();
342 for (
int t = 0;
t < nrep; ++
t) {
343 TH2* replica =
dynamic_cast<TH2*
>(
h->Clone(Form(
"rep%d_%s",
t,
h->GetName())));
345 for (
int x_i = 0; x_i <=
xbins; ++x_i) {
346 for (
int y_i = 0; y_i <=
ybins; ++y_i) {
347 replica->SetBinContent(x_i, y_i, Rndm.Gaus(
h->GetBinContent(x_i, y_i),
h->GetBinError(x_i, y_i)));
350 replica_v.push_back(
TH1_Ptr(replica));
357 return H1.get() !=
nullptr;
362 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getTriggerScaleFactor This is an experimental function. If you really know what you are doing set UseExperimental property.");
372 ATH_MSG_ERROR(
"Could not find efficiency map for muon with eta: " <<
mu_eta <<
" and phi: " <<
mu_phi <<
". Something is inconsistent. Please check your settings for year, mc and trigger." );
376 double mu_phi_corr =
mu_phi;
377 if (mu_phi_corr < eff_h2->GetYaxis()->GetXmin()) mu_phi_corr += 2.0 *
M_PI;
378 if (mu_phi_corr > eff_h2->GetYaxis()->GetXmax()) mu_phi_corr -= 2.0 *
M_PI;
379 return eff_h2->FindFixBin(
mu_eta, mu_phi_corr);
385 return std::hash<std::string>()(
histName);
420 ATH_MSG_WARNING(
"Could not find what you are looking for in the efficiency map. The trigger you are looking for, year and mc are not consistent, or the trigger is unavailable in this data period. Returning efficiency = 0.");
426 ATH_MSG_ERROR(
"Could not find what you are looking for in the efficiency map. The trigger you are looking for, year and mc are not consistent, or the trigger is unavailable in this data period. Please check how you set up the tool.");
431 if (configuration.
replicaIndex >= (
int) cit->second.size()) {
432 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getMuonEfficiency ; index for replicated histograms is out of range.");
439 if (cit.get() ==
nullptr) {
441 ATH_MSG_WARNING(
"Could not find what you are looking for in the efficiency map. The trigger you are looking for, year and mc are not consistent, or the trigger is unavailable in this data period. Returning efficiency = 0.");
445 ATH_MSG_ERROR(
"Could not find what you are looking for in the efficiency map. The trigger you are looking for, year and mc are not consistent, or the trigger is unavailable in this data period. Please check how you set up the tool.");
452 double mu_phi_corr =
mu_phi;
453 if (mu_phi_corr < eff_h2->GetYaxis()->GetXmin()) mu_phi_corr += 2.0 *
M_PI;
454 if (mu_phi_corr > eff_h2->GetYaxis()->GetXmax()) mu_phi_corr -= 2.0 *
M_PI;
456 const int bin = eff_h2->FindFixBin(
mu_eta, mu_phi_corr);
469 if (mucont.
size() != 2) {
470 ATH_MSG_FATAL(
"MuonTriggerScaleFactors::GetTriggerSF;Currently dimuon trigger chains only implemented for events with exactly 2 muons.");
474 Double_t eff_data = 0;
477 std::string data_err =
"";
478 std::string mc_err =
"";
481 data_err =
"nominal";
484 data_err =
"nominal";
485 mc_err =
"syst_down";
487 data_err =
"stat_down";
490 data_err =
"stat_up";
493 data_err =
"nominal";
504 if (configuration.
replicaIndex != -1) data_err =
"replicas";
507 configuration.
isData =
true;
511 configuration.
isData =
false;
516 double event_SF = 1.;
518 if (std::abs(1. - eff_mc) > 0.0001) {
519 event_SF = eff_data / eff_mc;
522 TriggerSF = event_SF;
531 double rate_not_fired_data = 1.;
532 double rate_not_fired_mc = 1.;
534 for (
const auto mu : mucont) {
536 double eff_data = 0., eff_mc = 0.;
545 std::string muon_trigger_name =
trigger;
546 std::string data_err =
"";
547 std::string mc_err =
"";
556 data_err =
"nominal";
559 data_err =
"nominal";
560 mc_err =
"syst_down";
562 data_err =
"stat_down";
565 data_err =
"stat_up";
568 data_err =
"nominal";
579 if (configuration.
replicaIndex != -1) data_err =
"replicas";
582 configuration.
isData =
true;
585 configuration.
isData =
false;
590 rate_not_fired_data *= (1. - eff_data);
591 rate_not_fired_mc *= (1. - eff_mc);
594 double event_SF = 1.;
595 if (1 - rate_not_fired_data == 0) event_SF = 0;
596 if ((mucont.size()) and (std::abs(1. - rate_not_fired_mc) > 0.0001)) {
598 event_SF = (1. - rate_not_fired_data) / (1. - rate_not_fired_mc);
600 TriggerSF = event_SF;
611 double eff_data = 0., eff_mc = 0.;
620 std::string muon_trigger_name =
trigger;
621 std::string data_err =
"";
622 std::string mc_err =
"";
631 data_err =
"nominal";
634 data_err =
"nominal";
635 mc_err =
"syst_down";
637 data_err =
"stat_down";
640 data_err =
"stat_up";
643 data_err =
"nominal";
649 if (configuration.
replicaIndex != -1) data_err =
"replicas";
652 configuration.
isData =
true;
656 configuration.
isData =
false;
663 if (std::abs(eff_mc) > 0.0001)
664 TriggerSF = eff_data / eff_mc;
694 if (
trigger.find(
"2mu10") != std::string::npos)
return "HLT_mu10";
695 if (
trigger.find(
"2mu14") != std::string::npos)
return "HLT_mu14";
696 throw std::runtime_error(
"Unknown dimuon trigger");
701 if (
index != std::string::npos) {
703 if (!rawNumber.empty() && isdigit(rawNumber[0])) {
704 std::stringstream(rawNumber) >>
threshold;
710 ATH_MSG_ERROR(
"MuonTriggerScaleFactors::getThreshold Could not extract threshold for trigger " <<
trigger);
718 if (
run <= 284484)
return 2015;
719 else if (
run <= 311481)
return 2016;
720 else if (
run <= 340453)
return 2017;
721 else if (
run <= 364292)
return 2018;
746 else if (
year == 2016) {
759 else if (
year == 2017) {
770 else if (
year == 2018) {
788 else if (
year == 2022) {
794 ATH_MSG_FATAL(
"RunNumber: " <<
runNumber <<
" not known! Will stop the code to prevent using wrong SFs.");
795 throw std::invalid_argument{
""};
801 if (
info.operator->()==
nullptr) {
803 throw std::invalid_argument{
""};
806 ATH_MSG_DEBUG(
"The current event is a data event. Return runNumber instead.");
807 return info->runNumber();
811 ATH_MSG_FATAL(
"Failed to find the RandomRunNumber decoration. Please call the apply() method from the PileupReweightingTool beforehand in order to get period dependent SFs");
812 throw std::invalid_argument{
""};
813 }
else if (acc_rnd(*
info) == 0) {
814 ATH_MSG_FATAL(
"Pile up tool has given runNumber 0. Exiting the code.");
815 throw std::invalid_argument{
""};
819 return acc_rnd(*
info);
824 TDirectory* tempDir = 0;
826 while (not tempDir) {
829 if (gROOT->GetDirectory((
dirname.str()).c_str())) {
833 tempDir = gROOT->mkdir((
dirname.str()).c_str());
835 ATH_MSG_ERROR(
"getTemporaryDirectory::Temporary directory could not be created");
845 if (!systematic.
empty()) {
847 return sys.find(systematic) !=
sys.end();
875 if (
registry.registerSystematics(*
this) != StatusCode::SUCCESS) {
876 ATH_MSG_ERROR(
"Failed to add systematic to list of recommended systematics.");
877 return StatusCode::FAILURE;
879 return StatusCode::SUCCESS;
898 ATH_MSG_ERROR(
"Unsupported combination of systematics passed to the tool!");
899 return StatusCode::FAILURE;
903 itr =
m_systFilter.insert(std::make_pair(systConfig, filteredSys)).first;
916 return StatusCode::FAILURE;
920 return StatusCode::SUCCESS;