Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/dd4pod/python/npsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Modified with standard EIC EPIC requirements.
"""
from __future__ import absolute_import, unicode_literals
import argparse
import logging
import sys

Expand All @@ -20,6 +21,17 @@

RUNNER = DD4hepSimulation()

# Parse our own options
parser = argparse.ArgumentParser("Running ePIC/EIC Simulations:", add_help=False)
parser.add_argument("--disableChecksum", action="store_true", default=False,
help="Disable the detector checksum calculations")
args, unknown = parser.parse_known_args()
sys.argv = [sys.argv[0]] + unknown
if "--help" in unknown:
parser.print_help()
print()
print()

# Parse remaining options (command line and steering file override above)
# This is done before updating the settings to workaround issue reported in
# https://github.com/AIDASoft/DD4hep/pull/1376
Expand Down Expand Up @@ -66,6 +78,17 @@ def setupCerenkov(kernel):
RUNNER.action.mapActions['RICHEndcapN'] = 'Geant4OpticalTrackerAction'
RUNNER.action.mapActions['DIRC'] = 'Geant4OpticalTrackerAction'

# Store detector checksum in run action
if not args.disableChecksum:
RUNNER.action.run += [
{
"name": "DetectorChecksumRunAction",
"parameter": {
"ignoreDetectors": []
}
}
]

# Use the optical photon efficiency stacking action for hpDIRC
RUNNER.action.stack = [
{
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

dd4hep_add_plugin(NPDetPlugins
SOURCES
src/DetectorChecksumRunAction.cxx
src/EICInteractionVertexBoost.cxx
src/EICInteractionVertexSmear.cxx
src/OpticalPhotonEfficiencyStackingAction.cxx
INCLUDES $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
USES DD4hep::DDCore DD4hep::DDG4
USES DD4hep::DDCore DD4hep::DDCorePlugins DD4hep::DDG4
)

install(TARGETS NPDetPlugins
Expand Down
107 changes: 107 additions & 0 deletions src/plugins/include/npdet/DetectorChecksumRunAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : M.Frank
//
//==========================================================================
#ifndef DETECTORCHECKSUMRUNACTION_H
#define DETECTORCHECKSUMRUNACTION_H

#include <DDG4/Geant4Random.h>
#include <DDG4/Geant4RunAction.h>
#include <DDG4/RunParameters.h>
#include <chrono>
#include <format>

#if __has_include(<DD4hep/plugins/DetectorChecksum.h>)
#include <DD4hep/plugins/DetectorChecksum.h>
#endif

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {

/// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
namespace sim {

using dd4hep::detail::DetectorChecksum;
using hashMap_t = std::map<std::string, DetectorChecksum::hash_t>;

template <class T = hashMap_t> void RunParameters::ingestParameters(T const& hashMap) {
for (auto& [name, hash] : hashMap) {
// hashes are 64 bit, so we have to cast to string
m_strValues[name + "_hash"] = {std::format("{:x}",hash)};
}
}

class DetectorChecksumRunAction: public Geant4RunAction {
public:
/// Standard constructor with initializing arguments
DetectorChecksumRunAction(Geant4Context* c, const std::string& n)
: Geant4RunAction(c, n) {
declareProperty("ignoreDetectors", m_ignoreDetectors);
};
/// Default destructor
virtual ~DetectorChecksumRunAction() {};

void begin(const G4Run* run);
private:
hashMap_t getHashMap(Detector& detector) const;
std::set<std::string> m_ignoreDetectors;
};

void DetectorChecksumRunAction::begin(const G4Run* /* run */) {
try {
auto* parameters = context()->run().extension<RunParameters>(false);
if (!parameters) {
parameters = new RunParameters();
context()->run().addExtension<RunParameters>(parameters);
}
parameters->ingestParameters(getHashMap(context()->detectorDescription()));
} catch(std::exception &e) {
printout(ERROR,"DetectorChecksumRunAction::begin","Failed to register run parameters: %s", e.what());
}
}

std::map<std::string,DetectorChecksum::hash_t>
DetectorChecksumRunAction::getHashMap(Detector& detector) const {
std::map<std::string, DetectorChecksum::hash_t> hashMap;
#if __has_include(<DD4hep/plugins/DetectorChecksum.h>)
// Determine detector checksum
printout(INFO,"DetectorChecksumRunAction::getHashMap", "determining checksum... (disable with --disableChecksum)");
// FIXME: ctor expects non-const detector
const auto start{std::chrono::steady_clock::now()};
DetectorChecksum checksum(detector);
checksum.debug = 0;
checksum.max_level = -1;
checksum.precision = 3;
checksum.hash_meshes = true;
checksum.hash_readout = true;
bool checksum_det_element_recursive = true;
for (const auto& [name, det] : detector.world().children()) {
if (m_ignoreDetectors.contains(name)) {
continue;
}
checksum.analyzeDetector(det);
DetectorChecksum::hashes_t hash_vec{};
checksum.checksumDetElement(0, det, hash_vec, checksum_det_element_recursive);
DetectorChecksum::hash_t hash =
dd4hep::detail::hash64(&hash_vec[0], hash_vec.size() * sizeof(DetectorChecksum::hash_t));
hashMap[name] = hash;
}
const auto finish{std::chrono::steady_clock::now()};
const std::chrono::duration<double> elapsed_seconds{finish - start};
printout(INFO,"DetectorChecksumRunAction::getHashMap", "duration: %f seconds", elapsed_seconds.count());
#endif
return hashMap;
}

} // End namespace sim
} // End namespace dd4hep

#endif // DETECTORCHECKSUMRUNACTION_H
5 changes: 5 additions & 0 deletions src/plugins/src/DetectorChecksumRunAction.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "DDG4/Factories.h"

#include "npdet/DetectorChecksumRunAction.h"

DECLARE_GEANT4ACTION(DetectorChecksumRunAction)