Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python3
0002 
0003 import argparse
0004 
0005 import acts
0006 from acts import (
0007     MaterialMapper,
0008     IntersectionMaterialAssigner,
0009     BinnedSurfaceMaterialAccumulater,
0010     MaterialMapJsonConverter,
0011     logging,
0012     GeometryContext,
0013     DetectorBuilder,
0014     GeometryIdGenerator,
0015 )
0016 
0017 from acts.examples import (
0018     Sequencer,
0019     WhiteBoard,
0020     AlgorithmContext,
0021     RootMaterialTrackReader,
0022     RootMaterialTrackWriter,
0023     CoreMaterialMapping,
0024     JsonMaterialWriter,
0025     RootMaterialWriter,
0026     JsonFormat,
0027 )
0028 
0029 from acts.examples.odd import getOpenDataDetector, getOpenDataDetectorDirectory
0030 
0031 
0032 def runMaterialMapping(surfaces, inputFile, outputFile, outputMap, loglevel):
0033     # Create a sequencer
0034     print("Creating the sequencer with 1 thread (inter event information needed)")
0035 
0036     s = Sequencer(numThreads=1)
0037 
0038     # IO for material tracks reading
0039     wb = WhiteBoard(acts.logging.INFO)
0040 
0041     # Read material step information from a ROOT TTRee
0042     s.addReader(
0043         RootMaterialTrackReader(
0044             level=acts.logging.INFO,
0045             outputMaterialTracks="material-tracks",
0046             fileList=[inputFile],
0047             readCachedSurfaceInformation=False,
0048         )
0049     )
0050 
0051     # Assignment setup : Intersection assigner
0052     materialAssingerConfig = IntersectionMaterialAssigner.Config()
0053     materialAssingerConfig.surfaces = surfaces
0054     materialAssinger = IntersectionMaterialAssigner(materialAssingerConfig, loglevel)
0055 
0056     # Accumulation setup : Binned surface material accumulater
0057     materialAccumulaterConfig = BinnedSurfaceMaterialAccumulater.Config()
0058     materialAccumulaterConfig.materialSurfaces = surfaces
0059     materialAccumulater = BinnedSurfaceMaterialAccumulater(
0060         materialAccumulaterConfig, loglevel
0061     )
0062 
0063     # Mapper setup
0064     materialMapperConfig = MaterialMapper.Config()
0065     materialMapperConfig.assignmentFinder = materialAssinger
0066     materialMapperConfig.surfaceMaterialAccumulater = materialAccumulater
0067     materialMapper = MaterialMapper(materialMapperConfig, loglevel)
0068 
0069     # Add the map writer(s)
0070     mapWriters = []
0071     # json map writer
0072     context = AlgorithmContext(0, 0, wb)
0073     jmConverterCfg = MaterialMapJsonConverter.Config(
0074         processSensitives=True,
0075         processApproaches=True,
0076         processRepresenting=True,
0077         processBoundaries=True,
0078         processVolumes=True,
0079         context=context.geoContext,
0080     )
0081     mapWriters.append(
0082         JsonMaterialWriter(
0083             level=loglevel,
0084             converterCfg=jmConverterCfg,
0085             fileName=outputMap + "",
0086             writeFormat=JsonFormat.Json,
0087         )
0088     )
0089     mapWriters.append(RootMaterialWriter(level=loglevel, filePath=outputMap + ".root"))
0090 
0091     # Mapping Algorithm
0092     coreMaterialMappingConfig = CoreMaterialMapping.Config()
0093     coreMaterialMappingConfig.materialMapper = materialMapper
0094     coreMaterialMappingConfig.inputMaterialTracks = "material-tracks"
0095     coreMaterialMappingConfig.mappedMaterialTracks = "mapped-material-tracks"
0096     coreMaterialMappingConfig.unmappedMaterialTracks = "unmapped-material-tracks"
0097     coreMaterialMappingConfig.materiaMaplWriters = mapWriters
0098     coreMaterialMapping = CoreMaterialMapping(coreMaterialMappingConfig, loglevel)
0099     s.addAlgorithm(coreMaterialMapping)
0100 
0101     # Add the mapped material tracks writer
0102     s.addWriter(
0103         RootMaterialTrackWriter(
0104             level=acts.logging.INFO,
0105             inputMaterialTracks="mapped-material-tracks",
0106             filePath=outputFile + "_mapped.root",
0107             storeSurface=True,
0108             storeVolume=True,
0109         )
0110     )
0111 
0112     # Add the unmapped material tracks writer
0113     s.addWriter(
0114         RootMaterialTrackWriter(
0115             level=acts.logging.INFO,
0116             inputMaterialTracks="unmapped-material-tracks",
0117             filePath=outputFile + "_unmapped.root",
0118             storeSurface=True,
0119             storeVolume=True,
0120         )
0121     )
0122 
0123     return s
0124 
0125 
0126 if "__main__" == __name__:
0127     p = argparse.ArgumentParser()
0128 
0129     p.add_argument(
0130         "-n", "--events", type=int, default=1000, help="Number of events to process"
0131     )
0132     p.add_argument(
0133         "-i", "--input", type=str, default="", help="Input file with material tracks"
0134     )
0135     p.add_argument(
0136         "-o", "--output", type=str, default="", help="Output file (core) name"
0137     )
0138     p.add_argument(
0139         "-m", "--map", type=str, default="", help="Output file for the material map"
0140     )
0141 
0142     p.add_argument(
0143         "--matconfig", type=str, default="", help="Material configuration file"
0144     )
0145 
0146     p.add_argument(
0147         "--experimental",
0148         action=argparse.BooleanOptionalAction,
0149         help="Construct experimental geometry",
0150     )
0151 
0152     p.add_argument(
0153         "--geomodel-input",
0154         type=str,
0155         default="",
0156         help="Construct experimental geometry from GeoModel",
0157     )
0158 
0159     p.add_argument(
0160         "--geomodel-name-list",
0161         type=str,
0162         nargs="+",
0163         default=[],
0164         help="List of Name List for the Surface Factory",
0165     )
0166 
0167     p.add_argument(
0168         "--geomodel-material-list",
0169         type=str,
0170         nargs="+",
0171         default=[],
0172         help="List of Material List for the Surface Factory",
0173     )
0174 
0175     p.add_argument(
0176         "--geomodel-convert-subvols",
0177         help="Convert the children of the top level full phys vol",
0178         action="store_true",
0179         default=False,
0180     )
0181 
0182     p.add_argument(
0183         "--geomodel-top-node",
0184         type=str,
0185         default="",
0186         help="Top node definition of the GeoModel tree",
0187     )
0188 
0189     p.add_argument(
0190         "--geomodel-table-name",
0191         type=str,
0192         default="ActsBlueprint",
0193         help="Name of the blueprint table",
0194     )
0195 
0196     p.add_argument(
0197         "--geomodel-queries",
0198         nargs="+",
0199         type=str,
0200         default=[],
0201         help="Queries for published GeoModel nodes",
0202     )
0203 
0204     args = p.parse_args()
0205     gContext = GeometryContext()
0206     logLevel = logging.INFO
0207 
0208     if args.experimental:
0209         if len(args.geomodel_input) > 0:
0210             from acts import geomodel as gm
0211 
0212             # Read the geometry model from the database
0213             gmTree = acts.geomodel.readFromDb(args.geomodel_input)
0214 
0215             gmFactoryConfig = gm.GeoModelDetectorObjectFactory.Config()
0216             gmFactoryConfig.materialList = args.geomodel_material_list
0217             gmFactoryConfig.nameList = args.geomodel_name_list
0218             gmFactoryConfig.convertSubVolumes = args.geomodel_convert_subvols
0219             gmFactory = gm.GeoModelDetectorObjectFactory(gmFactoryConfig, logLevel)
0220             # The options
0221             gmFactoryOptions = gm.GeoModelDetectorObjectFactory.Options()
0222             gmFactoryOptions.queries = args.geomodel_queries
0223             # The Cache & construct call
0224             gmFactoryCache = gm.GeoModelDetectorObjectFactory.Cache()
0225             gmFactory.construct(gmFactoryCache, gContext, gmTree, gmFactoryOptions)
0226 
0227             # All surfaces from GeoModel
0228             gmSurfaces = [ss[1] for ss in gmFactoryCache.sensitiveSurfaces]
0229 
0230             # Construct the building hierarchy
0231             gmBlueprintConfig = gm.GeoModelBlueprintCreater.Config()
0232             gmBlueprintConfig.detectorSurfaces = gmSurfaces
0233             gmBlueprintConfig.kdtBinning = [
0234                 acts.AxisDirection.AxisZ,
0235                 acts.AxisDirection.AxisR,
0236             ]
0237 
0238             gmBlueprintOptions = gm.GeoModelBlueprintCreater.Options()
0239             gmBlueprintOptions.table = args.geomodel_table_name
0240             gmBlueprintOptions.topEntry = args.geomodel_top_node
0241             gmBlueprintCreater = gm.GeoModelBlueprintCreater(
0242                 gmBlueprintConfig, logLevel
0243             )
0244             gmBlueprint = gmBlueprintCreater.create(
0245                 gContext, gmTree, gmBlueprintOptions
0246             )
0247 
0248             gmCylindricalBuilder = gmBlueprint.convertToBuilder(logLevel)
0249 
0250             # Top level geo id generator
0251             gmGeoIdConfig = GeometryIdGenerator.Config()
0252             gmGeoIdGenerator = GeometryIdGenerator(
0253                 gmGeoIdConfig, "GeoModelGeoIdGenerator", logging.INFO
0254             )
0255 
0256             # Create the detector builder
0257             gmDetectorConfig = DetectorBuilder.Config()
0258             gmDetectorConfig.name = args.geomodel_top_node + "_DetectorBuilder"
0259             gmDetectorConfig.builder = gmCylindricalBuilder
0260             gmDetectorConfig.geoIdGenerator = gmGeoIdGenerator
0261             gmDetectorConfig.auxiliary = (
0262                 "GeoModel based Acts::Detector from '" + args.geomodel_input + "'"
0263             )
0264 
0265             gmDetectorBuilder = DetectorBuilder(
0266                 gmDetectorConfig, args.geomodel_top_node, logLevel
0267             )
0268             detector = gmDetectorBuilder.construct(gContext)
0269 
0270             materialSurfaces = detector.extractMaterialSurfaces()
0271         else:
0272             from acts.examples.dd4hep import (
0273                 DD4hepDetector,
0274                 DD4hepDetectorOptions,
0275                 DD4hepGeometryService,
0276             )
0277 
0278             odd_xml = getOpenDataDetectorDirectory() / "xml" / "OpenDataDetector.xml"
0279 
0280             # Create the dd4hep geometry service and detector
0281             dd4hepConfig = DD4hepGeometryService.Config()
0282             dd4hepConfig.logLevel = acts.logging.INFO
0283             dd4hepConfig.xmlFileNames = [str(odd_xml)]
0284             dd4hepGeometryService = DD4hepGeometryService(dd4hepConfig)
0285             dd4hepDetector = DD4hepDetector(dd4hepGeometryService)
0286 
0287             cOptions = DD4hepDetectorOptions(
0288                 logLevel=acts.logging.INFO, emulateToGraph=""
0289             )
0290 
0291             # Context and options
0292             geoContext = acts.GeometryContext()
0293             [detector, contextors, store] = dd4hepDetector.finalize(
0294                 geoContext, cOptions
0295             )
0296 
0297             materialSurfaces = detector.extractMaterialSurfaces()
0298     else:
0299         matDeco = None
0300         if args.matconfig != "":
0301             matDeco = acts.IMaterialDecorator.fromFile(args.matconfig)
0302 
0303         detector = getOpenDataDetector(matDeco)
0304         trackingGeometry = detector.trackingGeometry()
0305 
0306         materialSurfaces = trackingGeometry.extractMaterialSurfaces()
0307 
0308     runMaterialMapping(
0309         materialSurfaces, args.input, args.output, args.map, logLevel
0310     ).run()