Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:13:13

0001 /// ===========================================================================
0002 /*! \file   CaloStatusMapperLinkDef.h
0003  *  \author Derek Anderson
0004  *  \date   05.22.2024
0005  *
0006  *  A Fun4All QA module to plot no. of towers per event
0007  *  and vs. eta, phi as a function of status.
0008  */
0009 /// ===========================================================================
0010 
0011 #define CLUSTERSTATUSMAPPER_CC
0012 
0013 // module definition
0014 #include "CaloStatusMapper.h"
0015 
0016 // calo base
0017 #include <calobase/TowerInfov2.h>
0018 
0019 // calo trigger
0020 #include <calotrigger/TriggerAnalyzer.h>
0021 
0022 // f4a libraries
0023 #include <fun4all/Fun4AllReturnCodes.h>
0024 #include <fun4all/Fun4AllHistoManager.h>
0025 
0026 // phool libraries
0027 #include <phool/getClass.h>
0028 #include <phool/phool.h>
0029 #include <phool/PHCompositeNode.h>
0030 
0031 // qa utilities
0032 #include <qautils/QAHistManagerDef.h>
0033 
0034 // root libraries
0035 #include <TH1.h>
0036 #include <TH2.h>
0037 
0038 // c++ utiilites
0039 #include <algorithm>
0040 #include <cassert>
0041 #include <iostream>
0042 
0043 // abbreviate namespace for convenience
0044 namespace CSMD = CaloStatusMapperDefs;
0045 
0046 
0047 
0048 // ctor/dtor ==================================================================
0049 
0050 // ----------------------------------------------------------------------------
0051 //! Default module constructor
0052 // ----------------------------------------------------------------------------
0053 CaloStatusMapper::CaloStatusMapper(const std::string& modulename, const bool debug)
0054   : SubsysReco(modulename)
0055 {
0056 
0057   // print debug message
0058   if (debug && (Verbosity() > 1))
0059   {
0060     std::cout << "CaloStatusMapper::CaloStatusMapper(std::string&, bool) Calling ctor" << std::endl;
0061   }
0062 
0063   // make sure node vector is empty
0064   m_inNodes.clear();
0065 
0066 }  // end ctor(std::string&, bool)
0067 
0068 
0069 
0070 // ----------------------------------------------------------------------------
0071 //! Module constructor accepting a configuration
0072 // ----------------------------------------------------------------------------
0073 CaloStatusMapper::CaloStatusMapper(const Config& config)
0074   : SubsysReco(config.moduleName)
0075   , m_config(config)
0076 {
0077 
0078   // print debug message
0079   if (m_config.debug && (Verbosity() > 1))
0080   {
0081     std::cout << "CaloStatusMapper::CaloStatusMapper(Config&) Calling ctor" << std::endl;
0082   }
0083 
0084 }  // end ctor(Config&)
0085 
0086 
0087 
0088 // ----------------------------------------------------------------------------
0089 //! Module destructor
0090 // ----------------------------------------------------------------------------
0091 CaloStatusMapper::~CaloStatusMapper()
0092 {
0093 
0094   // print debug message
0095   if (m_config.debug && (Verbosity() > 1))
0096   {
0097     std::cout << "CaloStatusMapper::~CaloStatusMapper() Calling dtor" << std::endl;
0098   }
0099   delete m_analyzer;
0100 
0101 }  // end dtor
0102 
0103 
0104 
0105 // fun4all methods ============================================================
0106 
0107 // ----------------------------------------------------------------------------
0108 //! Initialize module
0109 // ----------------------------------------------------------------------------
0110 int CaloStatusMapper::Init(PHCompositeNode* /*topNode*/)
0111 {
0112 
0113   if (m_config.debug)
0114   {
0115     std::cout << "CaloStatusMapper::Init(PHCompositeNode*) Initializing" << std::endl;
0116   }
0117 
0118   // initialize trigger analyzer
0119   delete m_analyzer;
0120   m_analyzer = new TriggerAnalyzer();
0121 
0122   // initialize manager/histograms
0123   InitHistManager();
0124   BuildHistograms();
0125 
0126   // make sure event no. is set to 0
0127   m_nEvent = 0;
0128   return Fun4AllReturnCodes::EVENT_OK;
0129 
0130 }  // end 'Init(PHCompositeNode*)'
0131 
0132 
0133 
0134 // ----------------------------------------------------------------------------
0135 //! Grab inputs and fills histograms
0136 // ----------------------------------------------------------------------------
0137 int CaloStatusMapper::process_event(PHCompositeNode* topNode)
0138 {
0139 
0140   if (m_config.debug)
0141   {
0142     std::cout << "CaloStatusMapper::process_event(PHCompositeNode* topNode) Processing Event" << std::endl;
0143   }
0144 
0145   // if needed, check if selected trigger fired
0146   if (m_config.doTrgSelect)
0147   {
0148     m_analyzer -> decodeTriggers(topNode);
0149     bool hasTrigger = JetQADefs::DidTriggerFire(m_config.trgToSelect, m_analyzer);
0150     if (!hasTrigger)
0151     {
0152       return Fun4AllReturnCodes::EVENT_OK;
0153     }
0154   }
0155 
0156   // grab input nodes
0157   GrabNodes(topNode);
0158 
0159   // loop over input nodes
0160   for (size_t iNode = 0; iNode < m_inNodes.size(); ++iNode)
0161   {
0162 
0163     // grab node name & make status base
0164     const std::string nodeName = m_config.inNodeNames[iNode].first;
0165     const std::string statBase = MakeBaseName("Status", nodeName);
0166 
0167     // loop over towers
0168     TowerInfoContainer* towers = m_inNodes[iNode];
0169     for (size_t iTower = 0; iTower < towers->size(); ++iTower)
0170     {
0171 
0172       // grab eta, phi indices
0173       const int32_t key  = towers -> encode_key(iTower);
0174       const int32_t iEta = towers -> getTowerEtaBin(key);
0175       const int32_t iPhi = towers -> getTowerPhiBin(key);
0176 
0177       // get status
0178       const auto tower  = towers -> get_tower_at_channel(iTower);
0179       const auto status = CSMD::GetTowerStatus(tower);
0180       if (status == CSMD::Stat::Unknown)
0181       {
0182         std::cout << PHWHERE << ": Warning! Tower has an unknown status!\n"
0183                   << "  channel = " << iTower << ", key = " << key << "\n"
0184                   << "  node = " << m_config.inNodeNames[iNode].first
0185                   << std::endl; 
0186         continue;
0187       } 
0188 
0189       // make base eta/phi hist name
0190       const std::string statLabel  = m_mapStatLabels[status];
0191       const std::string perEtaBase = MakeBaseName("NPerEta", nodeName, statLabel);
0192       const std::string perPhiBase = MakeBaseName("NPerPhi", nodeName, statLabel);
0193       const std::string phiEtaBase = MakeBaseName("PhiVsEta", nodeName, statLabel);
0194 
0195       // fill histograms accordingly
0196       m_hists[statBase]   -> Fill(status);
0197       m_hists[perEtaBase] -> Fill(iEta);
0198       m_hists[perPhiBase] -> Fill(iPhi);
0199       m_hists[phiEtaBase] -> Fill(iEta, iPhi);
0200 
0201     }  // end tower loop
0202   }  // end node loop
0203 
0204   // increment event no. and return
0205   ++m_nEvent;
0206   return Fun4AllReturnCodes::EVENT_OK;
0207 
0208 }  // end 'process_event(PHCompositeNode*)'
0209 
0210 
0211 
0212 // ----------------------------------------------------------------------------
0213 //! Run final calculations
0214 // ----------------------------------------------------------------------------
0215 int CaloStatusMapper::End(PHCompositeNode* /*topNode*/)
0216 {
0217 
0218   if (m_config.debug)
0219   {
0220     std::cout << "CaloStatusMapper::End(PHCompositeNode* topNode) This is the End..." << std::endl;
0221   }
0222 
0223   // normalize avg. status no.s
0224   for (const auto& nodeName : m_config.inNodeNames)
0225   {
0226     const std::string statBase = MakeBaseName("Status", nodeName.first);
0227     m_hists[statBase] -> Scale(1. / (double) m_nEvent);
0228   }
0229 
0230   // register hists and exit
0231   for (const auto& hist : m_hists) {
0232     m_manager -> registerHisto(hist.second);
0233   }
0234   return Fun4AllReturnCodes::EVENT_OK;
0235 
0236 }  // end 'End(PHCompositeNode*)'
0237 
0238 
0239 
0240 // private methods ============================================================
0241 
0242 // ----------------------------------------------------------------------------
0243 //! Initialize histogram manager
0244 // ----------------------------------------------------------------------------
0245 void CaloStatusMapper::InitHistManager()
0246 {
0247 
0248   // print debug message
0249   if (m_config.debug && (Verbosity() > 0))
0250   {
0251     std::cout << "CaloStatusMapper::InitHistManager() Initializing histogram manager" << std::endl;
0252   }
0253 
0254   m_manager = QAHistManagerDef::getHistoManager();
0255   if (!m_manager)
0256   {
0257     std::cerr << PHWHERE << ": PANIC! Couldn't grab histogram manager!" << std::endl;
0258     assert(m_manager);
0259   }
0260   return;
0261 
0262 }  // end 'InitHistManager()'
0263 
0264 
0265 
0266 // ----------------------------------------------------------------------------
0267 //! Build histograms
0268 // ----------------------------------------------------------------------------
0269 void CaloStatusMapper::BuildHistograms()
0270 {
0271 
0272   // print debug message
0273   if (m_config.debug && (Verbosity() > 0))
0274   {
0275     std::cout << "CaloStatusMapper::BuildHistograms() Creating histograms" << std::endl;
0276   }
0277 
0278   // instantiate histogram definitions
0279   const CSMD::EMCalHistDef emHistDef;
0280   const CSMD::HCalHistDef  hcHistDef;
0281 
0282   // loop over input node names
0283   for (const auto& nodeName : m_config.inNodeNames)
0284   {
0285 
0286     // make status hist name
0287     const std::string statBase = MakeBaseName("Status", nodeName.first);
0288     const std::string statName = CSMD::MakeQAHistName(statBase, m_config.moduleName, m_config.histTag);
0289 
0290     // create status hist
0291     //   - n.b. calo type doesn't matter here
0292     m_hists[statBase] = emHistDef.MakeStatus1D(statName);
0293 
0294     // loop over status labels
0295     for (const auto& statLabel : m_mapStatLabels)
0296     {
0297 
0298       // set relevant bin label for status histogram
0299       m_hists[statBase] -> GetXaxis() -> SetBinLabel(statLabel.first + 1, statLabel.second.data());
0300 
0301       // make base eta/phi hist name
0302       const std::string perEtaBase = MakeBaseName("NPerEta", nodeName.first, statLabel.second);
0303       const std::string perPhiBase = MakeBaseName("NPerPhi", nodeName.first, statLabel.second);
0304       const std::string phiEtaBase = MakeBaseName("PhiVsEta", nodeName.first, statLabel.second);
0305 
0306       // make full eta/phi hist name
0307       const std::string namePerEta = CSMD::MakeQAHistName(perEtaBase, m_config.moduleName, m_config.histTag);
0308       const std::string namePerPhi = CSMD::MakeQAHistName(perPhiBase, m_config.moduleName, m_config.histTag);
0309       const std::string namePhiEta = CSMD::MakeQAHistName(phiEtaBase, m_config.moduleName, m_config.histTag);
0310 
0311       // make eta/phi hists
0312       switch (nodeName.second)
0313       {
0314         case CSMD::Calo::HCal:
0315           m_hists[perEtaBase] = hcHistDef.MakeEta1D(namePerEta);
0316           m_hists[perPhiBase] = hcHistDef.MakePhi1D(namePerPhi);
0317           m_hists[phiEtaBase] = hcHistDef.MakePhiEta2D(namePhiEta);
0318           break;
0319         case CSMD::Calo::EMCal:
0320           [[fallthrough]];
0321         default:
0322           m_hists[perEtaBase] = emHistDef.MakeEta1D(namePerEta);
0323           m_hists[perPhiBase] = emHistDef.MakePhi1D(namePerPhi);
0324           m_hists[phiEtaBase] = emHistDef.MakePhiEta2D(namePhiEta);
0325           break;
0326       }
0327 
0328     }  // end status loop
0329   }  // end node loop
0330   return;
0331 
0332 }  // end 'BuildHistograms()'
0333 
0334 
0335 
0336 // ----------------------------------------------------------------------------
0337 //! Grab input nodes
0338 // ----------------------------------------------------------------------------
0339 void CaloStatusMapper::GrabNodes(PHCompositeNode* topNode)
0340 {
0341 
0342   // print debug message
0343   if (m_config.debug && (Verbosity() > 0))
0344   {
0345     std::cout << "CaloStatusMapper::GrabNodes(PHCompositeNode*) Grabbing input nodes" << std::endl;
0346   }
0347 
0348   // make sure node vector is empty
0349   m_inNodes.clear();
0350 
0351   // loop over nodes to grab
0352   for (auto inNodeName : m_config.inNodeNames)
0353   {
0354     m_inNodes.push_back(
0355       findNode::getClass<TowerInfoContainer>(topNode, inNodeName.first)
0356     );
0357     if (!m_inNodes.back())
0358     {
0359       std::cerr << PHWHERE << ":" << " PANIC! Not able to grab node " << inNodeName.first << "! Aborting!" << std::endl;
0360       assert(m_inNodes.back());
0361     }
0362   }  // end input node name loop
0363   return;
0364 
0365 }  // end 'GrabNodes(PHCompositeNode*)'
0366 
0367 
0368 
0369 // ----------------------------------------------------------------------------
0370 //! Make base histogram name
0371 // ----------------------------------------------------------------------------
0372 std::string CaloStatusMapper::MakeBaseName(
0373   const std::string& base,
0374   const std::string& node,
0375   const std::string& stat) const
0376 {
0377 
0378   if (m_config.debug && (Verbosity() > 2))
0379   {
0380     std::cout << "CaloStatusMapper::MakeBaseName(std::string& x 3) Making base histogram name" << std::endl;
0381   }
0382 
0383   std::string name = base + "_" + node;
0384   if (!stat.empty())
0385   {
0386     name.insert(0, stat + "_");
0387   }
0388   return name;
0389 
0390 }  // end 'MakeBaseName(std::string& x 3)'
0391 
0392 // end ========================================================================