Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:30:09

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