Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:18:42

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