Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:21:19

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