File indexing completed on 2025-08-05 08:13:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define BEAMBACKGROUNDFILTERANDQA_CC
0013
0014
0015 #include "BeamBackgroundFilterAndQA.h"
0016 #include "BeamBackgroundFilterAndQADefs.h"
0017
0018
0019 #include <calobase/TowerInfoContainer.h>
0020
0021
0022 #include <fun4all/Fun4AllReturnCodes.h>
0023 #include <fun4all/Fun4AllHistoManager.h>
0024 #include <ffaobjects/FlagSavev1.h>
0025
0026
0027 #include <phool/getClass.h>
0028 #include <phool/phool.h>
0029 #include <phool/PHCompositeNode.h>
0030
0031
0032 #include <qautils/QAHistManagerDef.h>
0033
0034
0035 #include <TH1.h>
0036
0037
0038 #include <cassert>
0039 #include <iostream>
0040
0041
0042 namespace bbfqd = BeamBackgroundFilterAndQADefs;
0043
0044
0045
0046
0047
0048
0049
0050
0051 BeamBackgroundFilterAndQA::BeamBackgroundFilterAndQA(const std::string& name, const bool debug)
0052 : SubsysReco(name)
0053 , m_manager(nullptr)
0054 , m_flags(name)
0055 {
0056
0057
0058 if (debug && (Verbosity() > 0))
0059 {
0060 std::cout << "BeamBackgroundFilterAndQA::BeamBackgroundFilterAndQA(const std::string &name) Calling ctor" << std::endl;
0061 }
0062
0063 }
0064
0065
0066
0067
0068
0069
0070 BeamBackgroundFilterAndQA::BeamBackgroundFilterAndQA(const Config& config)
0071 : SubsysReco(config.moduleName)
0072 , m_manager(nullptr)
0073 , m_flags(config.moduleName)
0074 , m_config(config)
0075 {
0076
0077
0078 if (m_config.debug && (Verbosity() > 0))
0079 {
0080 std::cout << "BeamBackgroundFilterAndQA::BeamBackgroundFilterAndQA(const std::string &name) Calling ctor" << std::endl;
0081 }
0082
0083 }
0084
0085
0086
0087
0088
0089
0090 BeamBackgroundFilterAndQA::~BeamBackgroundFilterAndQA()
0091 {
0092
0093
0094 if (m_config.debug && (Verbosity() > 0))
0095 {
0096 std::cout << "BeamBackgroundFilterAndQA::~BeamBackgroundFilterAndQA() Calling dtor" << std::endl;
0097 }
0098
0099
0100
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110 int BeamBackgroundFilterAndQA::Init(PHCompositeNode* topNode)
0111 {
0112
0113 if (m_config.debug)
0114 {
0115 std::cout << "BeamBackgroundFilterAndQA::Init(PHCompositeNode *topNode) Initializing" << std::endl;
0116 }
0117
0118
0119 InitFilters();
0120 InitFlags(topNode);
0121 BuildHistograms();
0122
0123
0124 if (m_config.doQA)
0125 {
0126 InitHistManager();
0127 RegisterHistograms();
0128 }
0129 return Fun4AllReturnCodes::EVENT_OK;
0130
0131 }
0132
0133
0134
0135
0136
0137
0138 int BeamBackgroundFilterAndQA::process_event(PHCompositeNode* topNode)
0139 {
0140
0141 if (m_config.debug)
0142 {
0143 std::cout << "BeamBackgroundFilterAndQA::process_event(PHCompositeNode *topNode) Processing event" << std::endl;
0144 }
0145
0146
0147 SetDefaultFlags();
0148
0149
0150 const bool hasBeamBkgd = ApplyFilters(topNode);
0151
0152
0153 UpdateFlags(topNode);
0154
0155
0156 if (m_config.debug)
0157 {
0158 m_flags.printint();
0159 }
0160
0161
0162 if (hasBeamBkgd && m_config.doEvtAbort)
0163 {
0164 return Fun4AllReturnCodes::ABORTEVENT;
0165 }
0166 else
0167 {
0168 return Fun4AllReturnCodes::EVENT_OK;
0169 }
0170
0171 }
0172
0173
0174
0175
0176
0177
0178 int BeamBackgroundFilterAndQA::End(PHCompositeNode* )
0179 {
0180
0181 if (m_config.debug)
0182 {
0183 std::cout << "BeamBackgroundFilterAndQA::End(PHCompositeNode *topNode) This is the end..." << std::endl;
0184 }
0185
0186
0187 return Fun4AllReturnCodes::EVENT_OK;
0188
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198 void BeamBackgroundFilterAndQA::InitFilters()
0199 {
0200
0201
0202 if (m_config.debug && (Verbosity() > 1))
0203 {
0204 std::cout << "BeamBackgroundFilterAndQA::InitFilters() Initializing background filters" << std::endl;
0205 }
0206
0207 m_filters["Null"] = std::make_unique<NullFilter>( m_config.null, "Null" );
0208 m_filters["StreakSideband"] = std::make_unique<StreakSidebandFilter>( m_config.sideband, "StreakSideband" );
0209
0210 return;
0211
0212 }
0213
0214
0215
0216
0217
0218
0219 void BeamBackgroundFilterAndQA::InitFlags(PHCompositeNode* topNode)
0220 {
0221
0222
0223 if (m_config.debug && (Verbosity() > 1))
0224 {
0225 std::cout << "BeamBackgroundFilterAndQA::InitFlags() Initializing flags" << std::endl;
0226 }
0227
0228
0229 PHNodeIterator itNode(topNode);
0230 PHCompositeNode* parNode = dynamic_cast<PHCompositeNode*>(itNode.findFirst("PHCompositeNode", "PAR"));
0231 if (!parNode)
0232 {
0233 std::cerr << PHWHERE << " WARNING: No PAR node found! Cannot add node for background flags to node tree!" << std::endl;
0234 }
0235 else
0236 {
0237 m_flags.SaveToNodeTree(parNode, m_config.flagPrefix);
0238 }
0239
0240
0241 SetDefaultFlags();
0242 return;
0243
0244 }
0245
0246
0247
0248
0249
0250
0251 void BeamBackgroundFilterAndQA::InitHistManager()
0252 {
0253
0254
0255 if (m_config.debug && (Verbosity() > 0))
0256 {
0257 std::cout << "BeamBackgroundFilterAndQA::InitHistManager() Initializing histogram manager" << std::endl;
0258 }
0259
0260 m_manager = QAHistManagerDef::getHistoManager();
0261 if (!m_manager)
0262 {
0263 std::cerr << PHWHERE << ": PANIC! Couldn't grab histogram manager!" << std::endl;
0264 assert(m_manager);
0265 }
0266 return;
0267
0268 }
0269
0270
0271
0272
0273
0274
0275 void BeamBackgroundFilterAndQA::BuildHistograms()
0276 {
0277
0278
0279 if (m_config.debug && (Verbosity() > 0))
0280 {
0281 std::cout << "BeamBackgroundFilterAndQA::BuildHistograms() Creating histograms" << std::endl;
0282 }
0283
0284
0285 std::vector<std::string> varNames = {"nevts_overall"};
0286 for (const std::string& filterToApply : m_config.filtersToApply)
0287 {
0288 varNames.push_back("nevts_" + filterToApply);
0289 }
0290
0291
0292 std::vector<std::string> histNames = bbfqd::MakeQAHistNames(varNames, m_config.moduleName, m_config.histTag);
0293
0294
0295 for (std::size_t iVar = 0; iVar < varNames.size(); ++iVar)
0296 {
0297 m_hists[varNames[iVar]] = new TH1D(histNames[iVar].data(), "", 3, -0.5, 2.5);
0298 m_hists[varNames[iVar]]->GetXaxis()->SetBinLabel(1, "All");
0299 m_hists[varNames[iVar]]->GetXaxis()->SetBinLabel(2, "No beam bkgd.");
0300 m_hists[varNames[iVar]]->GetXaxis()->SetBinLabel(3, "Beam bkgd.");
0301 }
0302
0303
0304 for (const std::string& filterToApply : m_config.filtersToApply)
0305 {
0306 m_filters.at(filterToApply)->BuildHistograms(m_config.moduleName, m_config.histTag);
0307 }
0308 return;
0309
0310 }
0311
0312
0313
0314
0315
0316
0317 void BeamBackgroundFilterAndQA::RegisterHistograms()
0318 {
0319
0320
0321 if (m_config.debug && (Verbosity() > 0))
0322 {
0323 std::cout << "BeamBackgroundFilterAndQA::RegisterHistograms() Registering histograms w/ manager" << std::endl;
0324 }
0325
0326
0327 for (auto& hist : m_hists)
0328 {
0329 m_manager->registerHisto( hist.second );
0330 }
0331
0332
0333 for (const std::string& filterToApply : m_config.filtersToApply)
0334 {
0335 m_filters.at(filterToApply)->RegisterHistograms(m_manager);
0336 }
0337 return;
0338
0339 }
0340
0341
0342
0343
0344
0345
0346 void BeamBackgroundFilterAndQA::SetDefaultFlags()
0347 {
0348
0349
0350 if (m_config.debug && (Verbosity() > 1))
0351 {
0352 std::cout << "BeamBackgroundFilterAndQA::SetDefaultFlags() Setting defuault flag values" << std::endl;
0353 }
0354
0355
0356 for (const std::string& filterToApply : m_config.filtersToApply)
0357 {
0358 m_flags.set_int_param(MakeFlagName(filterToApply), 0);
0359 }
0360 m_flags.set_int_param(MakeFlagName(), 0);
0361 return;
0362
0363 }
0364
0365
0366
0367
0368
0369
0370 void BeamBackgroundFilterAndQA::UpdateFlags(PHCompositeNode* topNode)
0371 {
0372
0373
0374 if (m_config.debug && (Verbosity() > 0))
0375 {
0376 std::cout << "BeamBackgroundFilterAndQA::UpdateFlags(PHCompositeNode*) Updating flags on the node tree" << std::endl;
0377 }
0378
0379 PHNodeIterator itNode(topNode);
0380 PHCompositeNode* parNode = dynamic_cast<PHCompositeNode*>(itNode.findFirst("PHCompositeNode", "PAR"));
0381 if (parNode)
0382 {
0383 m_flags.UpdateNodeTree(parNode, m_config.flagPrefix);
0384 }
0385 return;
0386
0387 }
0388
0389
0390
0391
0392
0393
0394 bool BeamBackgroundFilterAndQA::ApplyFilters(PHCompositeNode* topNode)
0395 {
0396
0397
0398 if (m_config.debug && (Verbosity() > 0))
0399 {
0400 std::cout << "BeamBackgroundFilterAndQA::ApplyFilters(PHCompositeNode*) Creating histograms" << std::endl;
0401 }
0402
0403
0404 bool hasBkgd = false;
0405 for (const std::string& filterToApply : m_config.filtersToApply)
0406 {
0407 const bool filterFoundBkgd = m_filters.at(filterToApply)->ApplyFilter(topNode);
0408 if (filterFoundBkgd)
0409 {
0410 m_hists["nevts_" + filterToApply]->Fill(bbfqd::Status::HasBkgd);
0411 m_flags.set_int_param(MakeFlagName(filterToApply), 1);
0412 }
0413 else
0414 {
0415 m_hists["nevts_" + filterToApply]->Fill(bbfqd::Status::NoBkgd);
0416 }
0417 m_hists["nevts_" + filterToApply]->Fill(bbfqd::Status::Evt);
0418 hasBkgd += filterFoundBkgd;
0419 }
0420
0421
0422 m_hists["nevts_overall"]->Fill(bbfqd::Status::Evt);
0423 if (hasBkgd)
0424 {
0425 m_hists["nevts_overall"]->Fill(bbfqd::Status::HasBkgd);
0426 m_flags.set_int_param(MakeFlagName(), 1);
0427 }
0428 else
0429 {
0430 m_hists["nevts_overall"]->Fill(bbfqd::Status::NoBkgd);
0431 }
0432 return hasBkgd;
0433
0434 }
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445 std::string BeamBackgroundFilterAndQA::MakeFlagName(const std::string& filter)
0446 {
0447
0448
0449 if (m_config.debug && (Verbosity() > 2))
0450 {
0451 std::cout << "BeamBackgroundFilterAndQA::MakeFlagName(std::string&) Creating flag name" << std::endl;
0452 }
0453
0454
0455
0456 if (filter.empty())
0457 {
0458 return m_config.flagPrefix;
0459 }
0460 else
0461 {
0462 return m_config.flagPrefix + "_" + filter + "Filter";
0463 }
0464
0465 }
0466
0467