Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-29 08:25:04

0001 // Copyright 2022, Dmitry Romanov
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 //
0004 //
0005 
0006 #include <DD4hep/Detector.h>
0007 #include <Evaluator/DD4hepUnits.h>
0008 #include <JANA/JApplication.h>
0009 #include <JANA/JApplicationFwd.h>
0010 #include <JANA/JException.h>
0011 #include <JANA/Utils/JTypeInfo.h>
0012 #include <fmt/format.h>
0013 #include <spdlog/logger.h>
0014 #include <array>
0015 #include <gsl/pointers>
0016 #include <gsl/util>
0017 #include <memory>
0018 #include <stdexcept>
0019 #include <string>
0020 #include <vector>
0021 
0022 #include "MPGDHitReconstructionConfig.h"
0023 #include "algorithms/digi/MPGDTrackerDigiConfig.h"
0024 #include "extensions/jana/JOmniFactoryGeneratorT.h"
0025 #include "factories/digi/MPGDTrackerDigi_factory.h"
0026 #include "factories/digi/SiliconTrackerDigi_factory.h"
0027 #include "factories/tracking/MPGDHitReconstruction_factory.h"
0028 #include "factories/tracking/TrackerHitReconstruction_factory.h"
0029 #include "services/geometry/dd4hep/DD4hep_service.h"
0030 #include "services/log/Log_service.h"
0031 
0032 // 2D-STRIP DIGITIZATION
0033 // - Is produced by "MPGDTrackerDigi".
0034 // - Relies on 2DStrip version of "compact" geometry file.
0035 // PIXEL DIGITIZATION
0036 // - Is produced by "SiliconTrackerDigi".
0037 
0038 extern "C" {
0039 void InitPlugin(JApplication* app) {
0040   InitJANAPlugin(app);
0041 
0042   using namespace eicrecon;
0043 
0044   // ***** PIXEL or 2DSTRIP DIGITIZATION?
0045   // - This determines which of the MPGDTrackerDigi or SiliconTrackerDigi
0046   //  factory is used.
0047   // - It's encoded in XML constants "<detector>_2DStrip", which can be
0048   //  conveniently set in the XMLs of the MPGDs.
0049   // - It can be reset from command line via the "MPGD:SiFactoryPattern" option,
0050   //  which is a bit pattern: 0x1=CyMBaL, 0x2=OuterBarrel, ...
0051   // - It defaults (when neither XML constant nor command line option) to
0052   //  SiliconTrackerDigi.
0053   // Default
0054   unsigned int SiFactoryPattern = 0x3; // Full-scale SiliconTrackerDigi
0055   // XML constant
0056   auto log_service               = app->GetService<Log_service>();
0057   auto mLog                      = log_service->logger("tracking");
0058   const int nMPGDs               = 2;
0059   const char* MPGD_names[nMPGDs] = {"InnerMPGDBarrel", "MPGDOuterBarrel"};
0060   for (int mpgd = 0; mpgd < nMPGDs; mpgd++) {
0061     std::string MPGD_name    = gsl::at(MPGD_names, mpgd);
0062     std::string constantName = MPGD_name + std::string("_2DStrip");
0063     try {
0064       auto detector = app->GetService<DD4hep_service>()->detector();
0065       int constant  = detector->constant<int>(constantName);
0066       if (constant == 1) {
0067         SiFactoryPattern &= ~(0x1 << mpgd);
0068         mLog->info(R"(2DStrip XML loaded for "{}")", MPGD_name);
0069       } else {
0070         mLog->info(R"(pixel XML loaded for "{}")", MPGD_name);
0071       }
0072     } catch (const std::runtime_error&) {
0073       // Variable not present apply legacy pixel readout
0074     }
0075   }
0076   // Command line option
0077   std::string SiFactoryPattern_str;
0078   app->SetDefaultParameter("MPGD:SiFactoryPattern", SiFactoryPattern_str,
0079                            "Hexadecimal Pattern of MPGDs digitized via \"SiliconTrackerDigi\"");
0080   if (!SiFactoryPattern_str.empty()) {
0081     try {
0082       SiFactoryPattern = std::stoul(SiFactoryPattern_str, nullptr, 16);
0083     } catch (const std::invalid_argument& e) {
0084       throw JException(
0085           R"(Option "MPGD:SiFactoryPattern": Error ("%s") parsing input
0086         string: '%s'.)",
0087           e.what(), SiFactoryPattern_str.c_str());
0088     }
0089   }
0090   for (int mpgd = 0; mpgd < nMPGDs; mpgd++) {
0091     std::string MPGD_name(MPGD_names[mpgd]);
0092     if (SiFactoryPattern & (0x1 << mpgd)) {
0093       mLog->info(R"(pixel digitization will be applied to "{}")", MPGD_name);
0094     } else {
0095       mLog->info(R"(2DStrip digitization will be applied to "{}")", MPGD_name);
0096     }
0097   }
0098 
0099   // ***** "MPGDBarrel" (=CyMBaL)
0100   // Digitization
0101   if ((SiFactoryPattern & 0x1) != 0U) {
0102     app->Add(new JOmniFactoryGeneratorT<SiliconTrackerDigi_factory>(
0103         "MPGDBarrelRawHits", {"EventHeader", "MPGDBarrelHits"},
0104         {"MPGDBarrelRawHits", "MPGDBarrelRawHitLinks", "MPGDBarrelRawHitAssociations"},
0105         {
0106             .threshold      = 100 * dd4hep::eV,
0107             .timeResolution = 10,
0108         },
0109         app));
0110   } else {
0111     // Configuration parameters
0112     MPGDTrackerDigiConfig digi_cfg;
0113     digi_cfg.readout             = "MPGDBarrelHits";
0114     digi_cfg.threshold           = 100 * dd4hep::eV;
0115     digi_cfg.timeResolution      = 10;
0116     digi_cfg.gain                = 10000;
0117     digi_cfg.stripResolutions[0] = digi_cfg.stripResolutions[1] = 150 * dd4hep::um;
0118     // Get #channels from XML
0119     const char* constantNames[] = {"MMnStripsPhi", "MMnStripsZ"};
0120     for (int phiZ = 0; phiZ < 2; phiZ++) {
0121       std::string constantName = std::string(gsl::at(constantNames, phiZ));
0122       try {
0123         auto detector                        = app->GetService<DD4hep_service>()->detector();
0124         gsl::at(digi_cfg.stripNumbers, phiZ) = detector->constant<int>(constantName);
0125       } catch (...) {
0126         throw JException(
0127             R"(MPGD "%s": Error retrieving #channels from XML: no "%s" constant found.)",
0128             digi_cfg.readout.c_str(), constantName.c_str());
0129       }
0130     }
0131     app->Add(new JOmniFactoryGeneratorT<MPGDTrackerDigi_factory>(
0132         "MPGDBarrelRawHits", {"EventHeader", "MPGDBarrelHits"},
0133         {"MPGDBarrelRawHits", "MPGDBarrelRawHitLinks", "MPGDBarrelRawHitAssociations"}, digi_cfg,
0134         app));
0135   }
0136 
0137   // Convert raw digitized hits into hits with geometry info (ready for tracking)
0138   if ((SiFactoryPattern & 0x1) != 0U) {
0139     app->Add(new JOmniFactoryGeneratorT<TrackerHitReconstruction_factory>(
0140         "MPGDBarrelRecHits", {"MPGDBarrelRawHits"}, // Input data collection tags
0141         {"MPGDBarrelRecHits"},                      // Output data tag
0142         {
0143             .timeResolution = 10,
0144         },
0145         app));
0146   } else {
0147     MPGDHitReconstructionConfig reco_cfg;
0148     reco_cfg.readout             = "MPGDBarrelHits";
0149     reco_cfg.timeResolution      = 10;
0150     reco_cfg.stripResolutions[0] = reco_cfg.stripResolutions[1] = 150 * dd4hep::um;
0151     app->Add(new JOmniFactoryGeneratorT<MPGDHitReconstruction_factory>(
0152         "MPGDBarrelRecHits", {"MPGDBarrelRawHits"}, // Input data collection tags
0153         {"MPGDBarrelRecHits"},                      // Output data tag
0154         reco_cfg, app));
0155   }
0156 
0157   // ***** OuterMPGDBarrel
0158   // Digitization
0159   if ((SiFactoryPattern & 0x2) != 0U) {
0160     app->Add(new JOmniFactoryGeneratorT<SiliconTrackerDigi_factory>(
0161         "OuterMPGDBarrelRawHits", {"EventHeader", "OuterMPGDBarrelHits"},
0162         {"OuterMPGDBarrelRawHits", "OuterMPGDBarrelRawHitLinks",
0163          "OuterMPGDBarrelRawHitAssociations"},
0164         {
0165             .threshold      = 100 * dd4hep::eV,
0166             .timeResolution = 10,
0167         },
0168         app));
0169   } else {
0170     MPGDTrackerDigiConfig digi_cfg;
0171     digi_cfg.readout             = "OuterMPGDBarrelHits";
0172     digi_cfg.threshold           = 100 * dd4hep::eV;
0173     digi_cfg.timeResolution      = 5;
0174     digi_cfg.gain                = 10000;
0175     digi_cfg.stripResolutions[0] = digi_cfg.stripResolutions[1] = 150 * dd4hep::um;
0176     // Get #channels from XML
0177     std::string constantName = std::string("MPGDOuterBarrelnStrips");
0178     try {
0179       auto detector            = app->GetService<DD4hep_service>()->detector();
0180       digi_cfg.stripNumbers[0] = digi_cfg.stripNumbers[1] = detector->constant<int>(constantName);
0181     } catch (...) {
0182       throw JException(R"(MPGD "%s": Error retrieving #channels from XML: no "%s" constant found.)",
0183                        digi_cfg.readout.c_str(), constantName.c_str());
0184     }
0185     app->Add(new JOmniFactoryGeneratorT<MPGDTrackerDigi_factory>(
0186         "OuterMPGDBarrelRawHits", {"EventHeader", "OuterMPGDBarrelHits"},
0187         {"OuterMPGDBarrelRawHits", "OuterMPGDBarrelRawHitLinks",
0188          "OuterMPGDBarrelRawHitAssociations"},
0189         digi_cfg, app));
0190   }
0191 
0192   // Convert raw digitized hits into hits with geometry info (ready for tracking)
0193   if ((SiFactoryPattern & 0x2) != 0U) {
0194     app->Add(new JOmniFactoryGeneratorT<TrackerHitReconstruction_factory>(
0195         "OuterMPGDBarrelRecHits", {"OuterMPGDBarrelRawHits"}, // Input data collection tags
0196         {"OuterMPGDBarrelRecHits"},                           // Output data tag
0197         {
0198             .timeResolution = 10,
0199         },
0200         app));
0201   } else {
0202     MPGDHitReconstructionConfig reco_cfg;
0203     reco_cfg.readout             = "OuterMPGDBarrelHits";
0204     reco_cfg.timeResolution      = 10;
0205     reco_cfg.stripResolutions[0] = reco_cfg.stripResolutions[1] = 150 * dd4hep::um;
0206     app->Add(new JOmniFactoryGeneratorT<MPGDHitReconstruction_factory>(
0207         "OuterMPGDBarrelRecHits", {"OuterMPGDBarrelRawHits"}, // Input data collection tags
0208         {"OuterMPGDBarrelRecHits"},                           // Output data tag
0209         reco_cfg, app));
0210   }
0211 
0212   // ***** "BackwardMPGDEndcap"
0213   // Digitization
0214   app->Add(new JOmniFactoryGeneratorT<SiliconTrackerDigi_factory>(
0215       "BackwardMPGDEndcapRawHits", {"EventHeader", "BackwardMPGDEndcapHits"},
0216       {"BackwardMPGDEndcapRawHits", "BackwardMPGDEndcapRawHitLinks",
0217        "BackwardMPGDEndcapRawHitAssociations"},
0218       {
0219           .threshold      = 100 * dd4hep::eV,
0220           .timeResolution = 10,
0221       },
0222       app));
0223 
0224   // Convert raw digitized hits into hits with geometry info (ready for tracking)
0225   app->Add(new JOmniFactoryGeneratorT<TrackerHitReconstruction_factory>(
0226       "BackwardMPGDEndcapRecHits", {"BackwardMPGDEndcapRawHits"}, // Input data collection tags
0227       {"BackwardMPGDEndcapRecHits"},                              // Output data tag
0228       {
0229           .timeResolution = 10,
0230       },
0231       app));
0232 
0233   // ""ForwardMPGDEndcap"
0234   // Digitization
0235   app->Add(new JOmniFactoryGeneratorT<SiliconTrackerDigi_factory>(
0236       "ForwardMPGDEndcapRawHits", {"EventHeader", "ForwardMPGDEndcapHits"},
0237       {"ForwardMPGDEndcapRawHits", "ForwardMPGDEndcapRawHitLinks",
0238        "ForwardMPGDEndcapRawHitAssociations"},
0239       {
0240           .threshold      = 100 * dd4hep::eV,
0241           .timeResolution = 10,
0242       },
0243       app));
0244 
0245   // Convert raw digitized hits into hits with geometry info (ready for tracking)
0246   app->Add(new JOmniFactoryGeneratorT<TrackerHitReconstruction_factory>(
0247       "ForwardMPGDEndcapRecHits", {"ForwardMPGDEndcapRawHits"}, // Input data collection tags
0248       {"ForwardMPGDEndcapRecHits"},                             // Output data tag
0249       {
0250           .timeResolution = 10,
0251       },
0252       app));
0253 }
0254 } // extern "C"