Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:09

0001 #!/usr/bin/env python3
0002 
0003 import os
0004 import argparse
0005 from pathlib import Path
0006 
0007 import acts
0008 from acts import (
0009     MaterialValidater,
0010     IntersectionMaterialAssigner,
0011     logging,
0012     GeometryContext,
0013     DetectorBuilder,
0014     GeometryIdGenerator,
0015 )
0016 
0017 from acts.examples import (
0018     Sequencer,
0019     WhiteBoard,
0020     AlgorithmContext,
0021     RootMaterialTrackWriter,
0022     MaterialValidation,
0023 )
0024 
0025 
0026 def runMaterialValidation(s, ntracks, surfaces, outputFile, seed, loglevel):
0027     # IO for material tracks reading
0028     wb = WhiteBoard(acts.logging.INFO)
0029 
0030     rnd = acts.examples.RandomNumbers(seed=seed)
0031 
0032     # Assignment setup : Intersection assigner
0033     materialAssingerConfig = IntersectionMaterialAssigner.Config()
0034     materialAssingerConfig.surfaces = surfaces
0035     materialAssinger = IntersectionMaterialAssigner(materialAssingerConfig, loglevel)
0036 
0037     # Validater setup
0038     materialValidaterConfig = MaterialValidater.Config()
0039     materialValidaterConfig.materialAssigner = materialAssinger
0040     materialValidater = MaterialValidater(materialValidaterConfig, loglevel)
0041 
0042     # Validation Algorithm
0043     materialValidationConfig = MaterialValidation.Config()
0044     materialValidationConfig.materialValidater = materialValidater
0045     materialValidationConfig.outputMaterialTracks = "recorded-material-tracks"
0046     materialValidationConfig.ntracks = ntracks
0047     materialValidationConfig.randomNumberSvc = rnd
0048     materialValidation = MaterialValidation(materialValidationConfig, loglevel)
0049     s.addAlgorithm(materialValidation)
0050 
0051     # Add the mapped material tracks writer
0052     s.addWriter(
0053         RootMaterialTrackWriter(
0054             level=acts.logging.INFO,
0055             inputMaterialTracks=materialValidationConfig.outputMaterialTracks,
0056             filePath=outputFile + ".root",
0057             storeSurface=True,
0058             storeVolume=True,
0059         )
0060     )
0061     # return the sequencer
0062     return s
0063 
0064 
0065 if "__main__" == __name__:
0066     p = argparse.ArgumentParser()
0067 
0068     p.add_argument(
0069         "-n", "--events", type=int, default=1000, help="Number of events to process"
0070     )
0071     p.add_argument(
0072         "-t", "--tracks", type=int, default=100, help="Number of tracks per event"
0073     )
0074     p.add_argument(
0075         "-j", "--threads", type=int, default=-1, help="Number of threads in parallel"
0076     )
0077     p.add_argument(
0078         "-m", "--map", type=str, default="", help="Input file for the material map"
0079     )
0080     p.add_argument("-o", "--output", type=str, default="", help="Output file name")
0081 
0082     p.add_argument(
0083         "--experimental",
0084         action=argparse.BooleanOptionalAction,
0085         help="Construct experimental geometry",
0086     )
0087 
0088     p.add_argument(
0089         "--geomodel-input",
0090         type=str,
0091         default="",
0092         help="Construct experimental geometry from GeoModel",
0093     )
0094 
0095     p.add_argument(
0096         "--geomodel-name-list",
0097         type=str,
0098         nargs="+",
0099         default=[],
0100         help="List of Name List for the Surface Factory",
0101     )
0102 
0103     p.add_argument(
0104         "--geomodel-material-list",
0105         type=str,
0106         nargs="+",
0107         default=[],
0108         help="List of Material List for the Surface Factory",
0109     )
0110 
0111     p.add_argument(
0112         "--geomodel-convert-subvols",
0113         help="Convert the children of the top level full phys vol",
0114         action="store_true",
0115         default=False,
0116     )
0117 
0118     p.add_argument(
0119         "--geomodel-top-node",
0120         type=str,
0121         default="",
0122         help="Top node definition of the GeoModel tree",
0123     )
0124 
0125     p.add_argument(
0126         "--geomodel-table-name",
0127         type=str,
0128         default="ActsBlueprint",
0129         help="Name of the blueprint table",
0130     )
0131 
0132     p.add_argument(
0133         "--geomodel-queries",
0134         nargs="+",
0135         type=str,
0136         default=[],
0137         help="Queries for published GeoModel nodes",
0138     )
0139 
0140     args = p.parse_args()
0141     gContext = GeometryContext()
0142     logLevel = acts.logging.INFO
0143 
0144     materialDecorator = None
0145     if args.map != "":
0146         materialDecorator = acts.IMaterialDecorator.fromFile(args.map)
0147 
0148     if args.experimental:
0149         if len(args.geomodel_input) > 0:
0150             from acts import geomodel as gm
0151 
0152             # Read the geometry model from the database
0153             gmTree = acts.geomodel.readFromDb(args.geomodel_input)
0154 
0155             gmFactoryConfig = gm.GeoModelDetectorObjectFactory.Config()
0156             gmFactoryConfig.materialList = args.geomodel_material_list
0157             gmFactoryConfig.nameList = args.geomodel_name_list
0158             gmFactoryConfig.convertSubVolumes = args.geomodel_convert_subvols
0159             gmFactory = gm.GeoModelDetectorObjectFactory(gmFactoryConfig, logLevel)
0160             # The options
0161             gmFactoryOptions = gm.GeoModelDetectorObjectFactory.Options()
0162             gmFactoryOptions.queries = args.geomodel_queries
0163             # The Cache & construct call
0164             gmFactoryCache = gm.GeoModelDetectorObjectFactory.Cache()
0165             gmFactory.construct(gmFactoryCache, gContext, gmTree, gmFactoryOptions)
0166 
0167             # All surfaces from GeoModel
0168             gmSurfaces = [ss[1] for ss in gmFactoryCache.sensitiveSurfaces]
0169 
0170             # Construct the building hierarchy
0171             gmBlueprintConfig = gm.GeoModelBlueprintCreater.Config()
0172             gmBlueprintConfig.detectorSurfaces = gmSurfaces
0173             gmBlueprintConfig.kdtBinning = [
0174                 acts.AxisDirection.AxisZ,
0175                 acts.AxisDirection.AxisR,
0176             ]
0177 
0178             gmBlueprintOptions = gm.GeoModelBlueprintCreater.Options()
0179             gmBlueprintOptions.table = args.geomodel_table_name
0180             gmBlueprintOptions.topEntry = args.geomodel_top_node
0181             gmBlueprintCreater = gm.GeoModelBlueprintCreater(
0182                 gmBlueprintConfig, logLevel
0183             )
0184             gmBlueprint = gmBlueprintCreater.create(
0185                 gContext, gmTree, gmBlueprintOptions
0186             )
0187 
0188             gmCylindricalBuilder = gmBlueprint.convertToBuilder(logLevel)
0189 
0190             # Top level geo id generator
0191             gmGeoIdConfig = GeometryIdGenerator.Config()
0192             gmGeoIdGenerator = GeometryIdGenerator(
0193                 gmGeoIdConfig, "GeoModelGeoIdGenerator", logging.INFO
0194             )
0195 
0196             # Create the detector builder
0197             gmDetectorConfig = DetectorBuilder.Config()
0198             gmDetectorConfig.name = args.geomodel_top_node + "_DetectorBuilder"
0199             gmDetectorConfig.builder = gmCylindricalBuilder
0200             gmDetectorConfig.geoIdGenerator = gmGeoIdGenerator
0201             gmDetectorConfig.materialDecorator = materialDecorator
0202             gmDetectorConfig.auxiliary = (
0203                 "GeoModel based Acts::Detector from '" + args.geomodel_input + "'"
0204             )
0205 
0206             gmDetectorBuilder = DetectorBuilder(
0207                 gmDetectorConfig, args.geomodel_top_node, logLevel
0208             )
0209             detector = gmDetectorBuilder.construct(gContext)
0210 
0211             materialSurfaces = detector.extractMaterialSurfaces()
0212         else:
0213             from acts.examples.dd4hep import (
0214                 DD4hepDetector,
0215                 DD4hepDetectorOptions,
0216                 DD4hepGeometryService,
0217             )
0218 
0219             from acts.examples.odd import (
0220                 getOpenDataDetector,
0221                 getOpenDataDetectorDirectory,
0222             )
0223 
0224             odd_xml = getOpenDataDetectorDirectory() / "xml" / "OpenDataDetector.xml"
0225 
0226             # Create the dd4hep geometry service and detector
0227             dd4hepConfig = DD4hepGeometryService.Config()
0228             dd4hepConfig.logLevel = acts.logging.INFO
0229             dd4hepConfig.xmlFileNames = [str(odd_xml)]
0230             dd4hepGeometryService = DD4hepGeometryService(dd4hepConfig)
0231             dd4hepDetector = DD4hepDetector(dd4hepGeometryService)
0232 
0233             cOptions = DD4hepDetectorOptions(
0234                 logLevel=acts.logging.INFO, emulateToGraph=""
0235             )
0236             cOptions.materialDecorator = materialDecorator
0237 
0238             # Context and options
0239             geoContext = acts.GeometryContext()
0240             [detector, contextors, store] = dd4hepDetector.finalize(
0241                 geoContext, cOptions
0242             )
0243 
0244             materialSurfaces = detector.extractMaterialSurfaces()
0245 
0246     else:
0247         detector = getOpenDataDetector(materialDecorator)
0248         trackingGeometry = detector.trackingGeometry()
0249 
0250         materialSurfaces = trackingGeometry.extractMaterialSurfaces()
0251 
0252     s = acts.examples.Sequencer(events=args.events, numThreads=args.threads)
0253 
0254     runMaterialValidation(
0255         s, args.tracks, materialSurfaces, args.output, 42, acts.logging.INFO
0256     ).run()