Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Examples/Scripts/Python/material_mapping_core.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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