Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:30:57

0001 #!/usr/bin/env python3
0002 import os
0003 from typing import Optional
0004 import argparse
0005 from pathlib import Path
0006 
0007 from acts.examples import (
0008     Sequencer,
0009     WhiteBoard,
0010     AlgorithmContext,
0011     ProcessCode,
0012     RootMaterialTrackReader,
0013     RootMaterialTrackWriter,
0014     CsvTrackingGeometryWriter,
0015     ObjTrackingGeometryWriter,
0016     MaterialMapping,
0017     JsonMaterialWriter,
0018     JsonFormat,
0019 )
0020 
0021 import acts
0022 import acts.examples.dd4hep
0023 
0024 from acts import (
0025     Vector4,
0026     UnitConstants as u,
0027     SurfaceMaterialMapper,
0028     VolumeMaterialMapper,
0029     Navigator,
0030     Propagator,
0031     StraightLineStepper,
0032     MaterialMapJsonConverter,
0033 )
0034 
0035 
0036 import json
0037 import build_geometry_ePIC as geo
0038 
0039 
0040 def runMaterialMapping(
0041     trackingGeometry,
0042     decorators,
0043     outputDir,
0044     inputDir,
0045     inputFile,
0046     mapName="material-map",
0047     mapFormat=JsonFormat.Json,
0048     mapSurface=True,
0049     mapVolume=False,
0050     readCachedSurfaceInformation=False,
0051     mappingStep=1,
0052     s=None,
0053 ):
0054     s = s or Sequencer(numThreads=1)
0055 
0056     for decorator in decorators:
0057         s.addContextDecorator(decorator)
0058 
0059     wb = WhiteBoard(acts.logging.INFO)
0060 
0061     context = AlgorithmContext(0, 0, wb)
0062 
0063     for decorator in decorators:
0064         assert decorator.decorate(context) == ProcessCode.SUCCESS
0065 
0066     # Read material step information from a ROOT TTRee
0067     s.addReader(
0068         RootMaterialTrackReader(
0069             level=acts.logging.INFO,
0070             collection="material-tracks",
0071             fileList=[
0072                 os.path.join(
0073                     inputDir,
0074                     inputFile
0075                 )
0076             ],
0077             readCachedSurfaceInformation=readCachedSurfaceInformation,
0078         )
0079     )
0080 
0081     stepper = StraightLineStepper()
0082 
0083     mmAlgCfg = MaterialMapping.Config(context.geoContext, context.magFieldContext)
0084     mmAlgCfg.trackingGeometry = trackingGeometry
0085     mmAlgCfg.collection = "material-tracks"
0086 
0087     if mapSurface:
0088         navigator = Navigator(
0089             trackingGeometry=trackingGeometry,
0090             resolveSensitive=True, # not required for material mapping
0091             resolveMaterial=True,
0092             resolvePassive=True,
0093         )
0094         propagator = Propagator(stepper, navigator)
0095         mapper = SurfaceMaterialMapper(level=acts.logging.INFO, propagator=propagator)
0096         mmAlgCfg.materialSurfaceMapper = mapper
0097 
0098     if mapVolume:
0099         navigator = Navigator(
0100             trackingGeometry=trackingGeometry,
0101         )
0102         propagator = Propagator(stepper, navigator)
0103         mapper = VolumeMaterialMapper(
0104             level=acts.logging.INFO, propagator=propagator, mappingStep=mappingStep
0105         )
0106         mmAlgCfg.materialVolumeMapper = mapper
0107 
0108     jmConverterCfg = MaterialMapJsonConverter.Config(
0109         processSensitives=True,# not required for material mapping
0110         processApproaches=True,
0111         processRepresenting=True,# not required for material mapping
0112         processBoundaries=True,
0113         processVolumes=True,
0114         context=context.geoContext,
0115     )
0116 
0117     outpath = os.path.join(outputDir, mapName)
0118     jmw = JsonMaterialWriter(
0119         level=acts.logging.VERBOSE,
0120         converterCfg=jmConverterCfg,
0121         fileName=outpath,
0122         writeFormat=mapFormat,
0123     )
0124     print("Done! Generated material map at "+outpath)
0125 
0126     mmAlgCfg.materialWriters = [jmw]
0127 
0128     s.addAlgorithm(MaterialMapping(level=acts.logging.INFO, config=mmAlgCfg))
0129 
0130     outpath = os.path.join(outputDir,mapName + "_tracks.root")
0131             
0132     s.addWriter(
0133         RootMaterialTrackWriter(
0134             level=acts.logging.INFO,
0135             collection=mmAlgCfg.mappingMaterialCollection,
0136             filePath=outpath,
0137             storeSurface=True,
0138             storeVolume=True,
0139         )
0140     )    
0141     print("      Propogated Geantino tracks at "+outpath)
0142 
0143     return s
0144 
0145 
0146 if "__main__" == __name__:
0147 
0148     p = argparse.ArgumentParser(
0149         description="Script to generate material map for ePIC geometry"
0150     )
0151     p.add_argument(
0152         "--xmlFile",
0153         default=os.environ.get("DETECTOR_PATH", "")+"epic_craterlake.xml",
0154         help="input xml file containing ePIC geometry",
0155     )
0156     p.add_argument(
0157         "--stepFile",
0158         type=str,
0159         default="geant4_material_tracks.root",
0160         help="input rootfile containing material steps.",
0161     )
0162     p.add_argument(
0163         "--geoFile",
0164         type=str,
0165         default="geometry-map.json",
0166         help="input json file to define volumes and layers used in material mapping",
0167     )
0168     p.add_argument(
0169         "--matFile",
0170         type=str,
0171         default="material-map.json",
0172         help="output filename for the generated material map, can be json and cbor formats",
0173     )
0174 
0175     # p.add_argument(
0176     #     '-v',"--run_validation",
0177     #     action ="store_true",
0178     #     default=False,
0179     #     help="Run validation",
0180     # )
0181     args = p.parse_args()
0182     print(args)
0183 
0184     if 'json' in args.matFile:
0185         fmt = JsonFormat.Json
0186 
0187     elif 'cbor' in args.matFile:
0188         fmt = JsonFormat.Cbor
0189     else:
0190         print('ERROR(material_mapping_ePIC.py): please provide a material map file in .json or .cbor format')
0191         exit()
0192 
0193     mapName = args.matFile.split('.')[0]
0194 
0195     detector, trackingGeometry, decorators = geo.buildePICGeometry(
0196         args.xmlFile, args.geoFile)
0197 
0198     runMaterialMapping(
0199         trackingGeometry,
0200         decorators,
0201         outputDir = os.getcwd(),
0202         inputDir  = os.getcwd(),
0203         inputFile = args.stepFile,
0204         readCachedSurfaceInformation=False,
0205         mapVolume= False,
0206         mapName  = mapName,
0207         mapFormat=fmt
0208     ).run()
0209 
0210