Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 /// ===========================================================================
0002 /*! \file   TrksInJetQA.cc
0003  *  \author Derek Anderson
0004  *  \date   03.25.2024
0005  *
0006  *  A "small" Fun4All module to produce QA plots for tracks,
0007  *  hits, and more.
0008  */
0009 /// ===========================================================================
0010 
0011 #define TRKSINJETQA_CC
0012 
0013 // module defintion
0014 #include "TrksInJetQA.h"
0015 
0016 // root libraries
0017 #include <TStyle.h>
0018 
0019 // ctor/dtor ==================================================================
0020 
0021 // ----------------------------------------------------------------------------
0022 //! Default ctor
0023 // ----------------------------------------------------------------------------
0024 TrksInJetQA::TrksInJetQA(const std::string& name)
0025   : SubsysReco(name)
0026   , m_moduleName(name)
0027 {};
0028 
0029 // ----------------------------------------------------------------------------
0030 //! Default dtor
0031 // ----------------------------------------------------------------------------
0032 TrksInJetQA::~TrksInJetQA()
0033 {
0034   // print debug messages
0035   if (m_config.doDebug && (m_config.verbose > 4))
0036   {
0037     std::cout << "TrksInJetQA::~TrksInJetQA() Calling dtor" << std::endl;
0038   }
0039 
0040   // clean up any dangling pointers
0041   // - n.b. deleting null ptrs is legal, setting it to null is not
0042   //   needed in the dtor
0043   delete m_outFile;
0044 }  // end dtor
0045 
0046 // public methods =============================================================
0047 
0048 // ----------------------------------------------------------------------------
0049 //! Configure module and all submodules
0050 // ----------------------------------------------------------------------------
0051 void TrksInJetQA::Configure(const TrksInJetQAConfig& config,
0052                             std::optional<TrksInJetQAHist> hist)
0053 {
0054   // print debug messages
0055   if (config.doDebug && (config.verbose > 3))
0056   {
0057     std::cout << "TrksInJetQA::~TrksInJetQA() Calling dtor" << std::endl;
0058   }
0059   m_config = config;
0060 
0061   if (hist.has_value())
0062   {
0063     m_hist = hist.value();
0064   }
0065 }  // end 'Configure(TrksInJetQAConfig, std::optional<TrksInJetQAHist>)'
0066 
0067 // fun4all methods ============================================================
0068 
0069 // ----------------------------------------------------------------------------
0070 //! Initialize module
0071 // ----------------------------------------------------------------------------
0072 int TrksInJetQA::Init(PHCompositeNode* /*topNode*/)
0073 {
0074   // print debug message
0075   if (m_config.doDebug && (m_config.verbose > 0))
0076   {
0077     std::cout << "TrksInJetQA::Init(PHCompositeNode* /*topNode*/) Initializing" << std::endl;
0078   }
0079 
0080   // initialize output & histograms
0081   InitOutput();
0082   InitHistograms();
0083 
0084   // register histograms with manager if needed
0085   if (m_config.outMode == OutMode::QA)
0086   {
0087     RegisterHistograms();
0088   }
0089 
0090   // initialize trigger analyzer and exit
0091   delete m_analyzer; // make cppcheck happy
0092   m_analyzer = new TriggerAnalyzer();
0093   return Fun4AllReturnCodes::EVENT_OK;
0094 }  // end 'Init(PHCompositeNode*)'
0095 
0096 // ----------------------------------------------------------------------------
0097 //! Run sub-modules and fill histograms
0098 // ----------------------------------------------------------------------------
0099 int TrksInJetQA::process_event(PHCompositeNode* topNode)
0100 {
0101   // print debug message
0102   if (m_config.doDebug && (m_config.verbose > 2))
0103   {
0104     std::cout << "TrksInJetQA::process_event(PHCompositeNode* topNode) Processing Event" << std::endl;
0105   }
0106 
0107   // if needed, check if selected trigger fired
0108   if (m_doTrgSelect)
0109   {
0110     m_analyzer->decodeTriggers(topNode);
0111     bool hasTrigger = JetQADefs::DidTriggerFire(m_trgToSelect, m_analyzer);
0112     if (!hasTrigger)
0113     {
0114       return Fun4AllReturnCodes::EVENT_OK;
0115     }
0116   }
0117 
0118   // run submodules
0119   if (m_config.doInJet)
0120   {
0121     m_inJet->Fill(topNode);
0122   }
0123   if (m_config.doInclusive)
0124   {
0125     m_inclusive->Fill(topNode);
0126   }
0127   return Fun4AllReturnCodes::EVENT_OK;
0128 }  // end 'process_event(PHCompositeNode*)'
0129 
0130 // ----------------------------------------------------------------------------
0131 //! Run final calculations
0132 // ----------------------------------------------------------------------------
0133 int TrksInJetQA::End(PHCompositeNode* /*topNode*/)
0134 {
0135   // print debug message
0136   if (m_config.doDebug && (m_config.verbose > 0))
0137   {
0138     std::cout << "TrksInJetQA::End(PHCompositeNode* /*topNode*/) This is the End..." << std::endl;
0139   }
0140 
0141   // save hists to file if needed
0142   if (m_config.outMode == OutMode::File)
0143   {
0144     // save histograms
0145     if (m_config.doInJet)
0146     {
0147       m_inJet->SaveHistograms(m_outFile, "InJet");
0148     }
0149     if (m_config.doInclusive)
0150     {
0151       m_inclusive->SaveHistograms(m_outFile, "Inclusive");
0152     }
0153 
0154     // close file
0155     m_outFile->cd();
0156     m_outFile->Close();
0157   }
0158   return Fun4AllReturnCodes::EVENT_OK;
0159 }  // end 'End(PHCompositeNode*)'
0160 
0161 // private methods ============================================================
0162 
0163 // ----------------------------------------------------------------------------
0164 //! Initialize outputs
0165 // ----------------------------------------------------------------------------
0166 /*! Will initialize either output file to write to
0167  *  (OutMode::File) or QAHistManager (OutMode::QA).
0168  */
0169 void TrksInJetQA::InitOutput()
0170 {
0171   // print debug message
0172   if (m_config.doDebug && (m_config.verbose > 1))
0173   {
0174     std::cout << "TrksInJetQA::InitOutput() Initializing outputs..." << std::endl;
0175   }
0176 
0177   // initialize relevent outputs
0178   switch (m_config.outMode)
0179   {
0180   case OutMode::File:
0181     m_outFile = new TFile(m_outFileName.data(), "RECREATE");
0182     if (!m_outFile)
0183     {
0184       std::cerr << PHWHERE << ": PANIC: couldn't create output file!" << std::endl;
0185       assert(m_outFile);
0186     }
0187     break;
0188 
0189   case OutMode::QA:
0190     delete m_manager;
0191 
0192     gStyle->SetOptTitle(0);
0193     m_manager = QAHistManagerDef::getHistoManager();
0194     if (!m_manager)
0195     {
0196       std::cerr << PHWHERE << ": PANIC: couldn't grab histogram manager!" << std::endl;
0197       assert(m_manager);
0198     }
0199     break;
0200 
0201   default:
0202     std::cerr << PHWHERE << ": PANIC: unknown output mode specified!\n"
0203               << "  Please set .outMode = OutMode::File OR OutMode::QA!"
0204               << std::endl;
0205     assert((m_config.outMode == OutMode::File) || (m_config.outMode == OutMode::QA));
0206     break;
0207   }
0208 }  // end 'InitOutput()'
0209 
0210 // ----------------------------------------------------------------------------
0211 //! Initialize histograms
0212 // ----------------------------------------------------------------------------
0213 void TrksInJetQA::InitHistograms()
0214 {
0215   // print debug message
0216   if (m_config.doDebug && (m_config.verbose > 1))
0217   {
0218     std::cout << "TrksInJetQA::InitHistograms() Initializing histograms..." << std::endl;
0219   }
0220 
0221   // histograms are always prefixed by the module name
0222   std::string prefix = "h_";
0223   prefix += m_moduleName;
0224 
0225   // if additional prefix provided, add it
0226   if (m_histPrefix.has_value())
0227   {
0228     prefix += m_histPrefix.value();
0229     prefix += "_";
0230   }
0231 
0232   // make suffixes
0233   std::string inJetSuffix = "InJet";
0234   std::string inclusiveSuffix = "Inclusive";
0235   if (m_histSuffix.has_value() && !m_histSuffix.value().empty())
0236   {
0237     inJetSuffix += "_";
0238     inJetSuffix += m_histSuffix.value();
0239     inclusiveSuffix += "_";
0240     inclusiveSuffix += m_histSuffix.value();
0241   }
0242 
0243   // initialize submodules, as needed
0244   if (m_config.doInJet)
0245   {
0246     m_inJet = std::make_unique<TrksInJetQAInJetFiller>(m_config, m_hist);
0247     m_inJet->MakeHistograms(prefix, inJetSuffix);
0248   }
0249   if (m_config.doInclusive)
0250   {
0251     m_inclusive = std::make_unique<TrksInJetQAInclusiveFiller>(m_config, m_hist);
0252     m_inclusive->MakeHistograms(prefix, inclusiveSuffix);
0253   }
0254 }  // end 'InitHistograms()'
0255 
0256 // ----------------------------------------------------------------------------
0257 //! Register histograms with QAHistManager
0258 // ----------------------------------------------------------------------------
0259 void TrksInJetQA::RegisterHistograms()
0260 {
0261   // print debug message
0262   if (m_config.doDebug && (m_config.verbose > 1))
0263   {
0264     std::cout << "TrksInJetQA::RegisterHistograms() Registering histograms..." << std::endl;
0265   }
0266 
0267   std::vector<TH1D*> vecHist1D;
0268   std::vector<TH2D*> vecHist2D;
0269   if (m_config.doInJet)
0270   {
0271     m_inJet->GrabHistograms(vecHist1D, vecHist2D);
0272   }
0273   if (m_config.doInclusive)
0274   {
0275     m_inclusive->GrabHistograms(vecHist1D, vecHist2D);
0276   }
0277 
0278   // register w/ manager
0279   for (TH1D* hist1D : vecHist1D)
0280   {
0281     m_manager->registerHisto(hist1D);
0282   }
0283   for (TH2D* hist2D : vecHist2D)
0284   {
0285     m_manager->registerHisto(hist2D);
0286   }
0287 }  // end 'RegisterHistograms()'
0288 
0289 // end ========================================================================