Exemplo n.º 1
0
    def extract_l_and_w(self, magnet_geofile, full_geometry_file, run=None):
        if not run:
            ship_geo = ConfigRegistry.loadpy(
                '$FAIRSHIP/geometry/geometry_config.py',
                Yheight=10,
                tankDesign=6,
                muShieldDesign=8,
                nuTauTargetDesign=3,
                muShieldGeo=os.path.join(self.geometry_dir, magnet_geofile))

            print
            'Config created with ' + os.path.join(self.geometry_dir,
                                                  magnet_geofile)

            outFile = r.TMemFile('output', 'create')
            run = r.FairRunSim()
            run.SetName('TGeant4')
            run.SetOutputFile(outFile)
            run.SetUserConfig('g4Config.C')
            shipDet_conf.configure(run, ship_geo)
            run.Init()
        run.CreateGeometryFile(
            os.path.join(self.geometry_dir, full_geometry_file))
        sGeo = r.gGeoManager
        muonShield = sGeo.GetVolume('MuonShieldArea')
        L = self.get_magnet_length(muonShield)
        W = self.get_magnet_mass(muonShield)
        g = r.TFile.Open(os.path.join(self.geometry_dir, magnet_geofile),
                         'read')
        params = g.Get("params")
        f = r.TFile.Open(os.path.join(self.geometry_dir, full_geometry_file),
                         'update')
        f.cd()
        length = r.TVectorD(1, array('d', [L]))
        length.Write('length')
        weight = r.TVectorD(1, array('d', [W]))
        weight.Write('weight')
        params.Write("params")

        # Extract coordinates of senstive plane
        nav = r.gGeoManager.GetCurrentNavigator()
        nav.cd("sentsitive_tracker_1")
        tmp = nav.GetCurrentNode().GetVolume().GetShape()
        o = [tmp.GetOrigin()[0], tmp.GetOrigin()[1], tmp.GetOrigin()[2]]
        local = array('d', o)
        globOrigin = array('d', [0, 0, 0])
        nav.LocalToMaster(local, globOrigin)

        sensitive_plane = sGeo.GetVolume('sentsitive_tracker')

        left_end, right_end = globOrigin[2] - sensitive_plane.GetShape().GetDZ(),\
                              globOrigin[2] + sensitive_plane.GetShape().GetDZ()
        return L, W, (left_end, right_end)
Exemplo n.º 2
0
    def run_ship(self, phiRandom=False, followMuon=True, n_events=10, first_event=0):
        """
        phiRandom = False  # only relevant for muon background generator
        followMuon = True  # only transport muons for a fast muon only background
        """
        r.gErrorIgnoreLevel = r.kWarning
        r.gSystem.Load('libpythia8')

        print ('FairShip setup to produce', n_events, 'events')
        r.gRandom.SetSeed(self.theSeed)
        ship_geo = ConfigRegistry.loadpy(
            '$FAIRSHIP/geometry/geometry_config.py',
            Yheight=self.dy,
            tankDesign=self.vessel_design,
            muShieldDesign=self.shield_design,
            muShieldGeo=self.shield_geo_file)

        run = r.FairRunSim()
        run.SetName(self.mcEngine)  # Transport engine
        run.SetOutputFile(self.output_file)  # Output file
        # user configuration file default g4Config.C
        run.SetUserConfig('g4Config.C')
        modules = shipDet_conf.configure(run, ship_geo)
        primGen = r.FairPrimaryGenerator()
        primGen.SetTarget(ship_geo.target.z0 + 50 * u.m, 0.)
        MuonBackgen = r.MuonBackGenerator()
        MuonBackgen.Init(self.input_file, first_event, phiRandom)
        MuonBackgen.SetSmearBeam(3 * u.cm)  # beam size mimicking spiral
        if self.same_seed:
            MuonBackgen.SetSameSeed(self.same_seed)
        primGen.AddGenerator(MuonBackgen)
        if not n_events:
            n_events = MuonBackgen.GetNevents()
        else:
            n_events = min(n_events, MuonBackgen.GetNevents())
        print ('Process ', n_events, ' from input file, with Phi random=', phiRandom)
        if followMuon:
            modules['Veto'].SetFastMuon()
        run.SetGenerator(primGen)
        run.SetStoreTraj(r.kFALSE)
        run.Init()
        print ('Initialised run.')
        # geomGeant4.setMagnetField()
        if hasattr(ship_geo.Bfield, "fieldMap"):
            fieldMaker = geomGeant4.addVMCFields(ship_geo, '', True)
        print ('Start run of {} events.'.format(n_events))
        run.Run(n_events)
        print ('Finished simulation of {} events.'.format(n_events))

        geofile_output_path = os.path.join(self.output_dir,
                                           "geofile_full.fe_{}_n_events_{}.root" .format(first_event, n_events))
        run.CreateGeometryFile(geofile_output_path)
        # save ShipGeo dictionary in geofile
        saveBasicParameters.execute(geofile_output_path, ship_geo)
        return run
def get_geo(geoFile):
    """Generate the geometry and check its lenght and weight.

    Note: As FairRunSim is a C++ singleton it misbehaves if run
    more than once in a process.

    Parameters
    ----------
    geoFile : str
        File with the muon shield parameters (not with the geometry config!)

    Returns
    -------
    L : float
        Magnet length in [cm].
    W : float
        Magnet mass in [kg].

    """
    ship_geo = ConfigRegistry.loadpy('$FAIRSHIP/geometry/geometry_config.py',
                                     Yheight=10,
                                     tankDesign=5,
                                     muShieldDesign=8,
                                     muShieldGeo=geoFile)

    print 'Config created with ' + geoFile

    outFile = r.TMemFile('output', 'create')
    run = r.FairRunSim()
    run.SetName('TGeant4')
    run.SetOutputFile(outFile)
    run.SetUserConfig('g4Config.C')
    shipDet_conf.configure(run, ship_geo)
    run.Init()
    run.CreateGeometryFile('./geo/' + os.path.basename(geoFile))
    sGeo = r.gGeoManager
    muonShield = sGeo.GetVolume('MuonShieldArea')
    L = magnetLength(muonShield)
    W = magnetMass(muonShield)
    return L, W
Exemplo n.º 4
0
    def create_context(self, f_name='magnet_geo_tmp.root'):
        magnet_geofile = self.generate_magnet_geofile(
            f_name, self.default_magnet_config)
        ship_geo = ConfigRegistry.loadpy(
            '$FAIRSHIP/geometry/geometry_config.py',
            Yheight=10,
            tankDesign=5,
            muShieldDesign=8,
            muShieldGeo=os.path.join(self.geometry_dir, magnet_geofile))

        print
        'Config created with ' + os.path.join(self.geometry_dir,
                                              magnet_geofile)

        outFile = r.TMemFile('output', 'create')
        run = r.FairRunSim()
        run.SetName('TGeant4')
        run.SetOutputFile(outFile)
        run.SetUserConfig('g4Config.C')
        shipDet_conf.configure(run, ship_geo)
        run.Init()
        return run
Exemplo n.º 5
0
    def query_params(self, params):
        magnet_geofile = self.generate_magnet_geofile("query_geo.root", params)
        ship_geo = ConfigRegistry.loadpy(
            '$FAIRSHIP/geometry/geometry_config.py',
            Yheight=10,
            tankDesign=5,
            muShieldDesign=8,
            muShieldGeo=os.path.join(self.geometry_dir, magnet_geofile))
        #shipDet_conf.configure(run, ship_geo)

        outFile = r.TMemFile('output', 'create')
        run = r.FairRunSim()
        run.SetName('TGeant4')
        run.SetOutputFile(outFile)
        run.SetUserConfig('g4Config.C')
        shipDet_conf.configure(run, ship_geo)
        run.Init()
        sGeo = r.gGeoManager
        muonShield = sGeo.GetVolume('MuonShieldArea')
        L = self.get_magnet_length(muonShield)
        W = self.get_magnet_mass(muonShield)
        return L, W
Exemplo n.º 6
0
def get_geo(geoFile, workDir='/eos/experiment/ship/user/ffedship/EA_V2/Geometry/', outfile=None):
    if workDir[-1] != '/':
        workDir = workDir + '/'
    if not outfile:
        outfile = workDir + os.path.basename(geoFile)
    else:
        outfile = workDir + outfile
    ship_geo = ConfigRegistry.loadpy(
        '$FAIRSHIP/geometry/geometry_config.py',
        Yheight=10,
        tankDesign=5,
        muShieldDesign=8,
        muShieldGeo=geoFile)

    print 'Get_Geo Message: Config created with ' + geoFile
    outFile = r.TMemFile('output', 'create')  # type: Any
    run = r.FairRunSim()
    run.SetName('TGeant4')
    run.SetOutputFile(outFile)
    run.SetUserConfig('g4Config.C')
    shipDet_conf.configure(run, ship_geo)
    run.Init()
    run.CreateGeometryFile(outfile)
    sGeo = r.gGeoManager
    muonShield = sGeo.GetVolume('MuonShieldArea')
    L = magnetLength(muonShield)
    W = magnetMass(muonShield)
    g = r.TFile.Open(geoFile, 'read')
    params = g.Get("params")
    f = r.TFile.Open(outfile, 'update')
    f.cd()
    length = r.TVectorD(1, array('d', [L]))
    length.Write('length')
    weight = r.TVectorD(1, array('d', [W]))
    weight.Write('weight')
    params.Write("params")
    return L, W
def generate(inputFile, paramFile, outFile, seed=1, nEvents=None):

    firstEvent = 0
    dy = 10.
    vessel_design = 5
    shield_design = 8
    mcEngine = 'TGeant4'
    sameSeed = seed
    theSeed = 1
    phiRandom = False  # only relevant for muon background generator
    followMuon = True  # only transport muons for a fast muon only background
    print('FairShip setup to produce', nEvents, 'events')
    r.gRandom.SetSeed(theSeed)
    ship_geo = ConfigRegistry.loadpy('$FAIRSHIP/geometry/geometry_config.py',
                                     Yheight=dy,
                                     tankDesign=vessel_design,
                                     muShieldDesign=shield_design,
                                     muShieldGeo=paramFile)
    run = r.FairRunSim()
    run.SetName(mcEngine)  # Transport engine
    run.SetOutputFile(outFile)  # Output file
    # user configuration file default g4Config.C
    run.SetUserConfig('g4Config.C')
    modules = shipDet_conf.configure(run, ship_geo)
    primGen = r.FairPrimaryGenerator()
    primGen.SetTarget(ship_geo.target.z0 + 50 * u.m, 0.)
    MuonBackgen = r.MuonBackGenerator()
    MuonBackgen.Init(inputFile, firstEvent, phiRandom)
    MuonBackgen.SetSmearBeam(3 * u.cm)  # beam size mimicking spiral
    if sameSeed:
        MuonBackgen.SetSameSeed(sameSeed)
    primGen.AddGenerator(MuonBackgen)
    nEvents = MuonBackgen.GetNevents() - 1
    print('Process ', nEvents, ' from input file, with Phi random=', phiRandom)
    if followMuon:
        modules['Veto'].SetFastMuon()
    run.SetGenerator(primGen)
    run.SetStoreTraj(r.kFALSE)
    run.Init()
    print('Initialised run.')
    geomGeant4.addVMCFields(ship_geo, '', True)
    print('Start run of {} events.'.format(nEvents))
    run.Run(nEvents)
    print('Finished simulation of {} events.'.format(nEvents))
Exemplo n.º 8
0
def create_csv_field_map(options):
    r.gErrorIgnoreLevel = r.kWarning
    r.gSystem.Load('libpythia8')

    ship_geo = ConfigRegistry.loadpy(
        '$FAIRSHIP/geometry/geometry_config.py',
        Yheight=globalDesigns["dy"],
        tankDesign=globalDesigns["dv"],
        nuTauTargetDesign=globalDesigns["nud"],
        CaloDesign=globalDesigns["caloDesign"],
        strawDesign=globalDesigns["strawDesign"],
        muShieldDesign=options.ds,
        muShieldStepGeo=options.muShieldStepGeo,
        muShieldWithCobaltMagnet=options.muShieldWithCobaltMagnet,
        muShieldGeo=options.geofile)

    ship_geo.muShield.WithConstField = True

    run = r.FairRunSim()
    run.SetName('TGeant4')  # Transport engine
    run.SetOutputFile("tmp_file")  # Output file
    # user configuration file default g4Config.C
    run.SetUserConfig('g4Config.C')
    modules = shipDet_conf.configure(run, ship_geo)
    primGen = r.FairPrimaryGenerator()
    primGen.SetTarget(ship_geo.target.z0 + 70.845 * u.m, 0.)
    #
    run.SetGenerator(primGen)
    run.SetStoreTraj(r.kFALSE)
    run.Init()
    fieldMaker = geomGeant4.addVMCFields(ship_geo, '', True)

    field_center, shield_half_length = ShieldUtils.find_shield_center(ship_geo)
    print("SHIELD ONLY: CENTER: {}, HALFLENGTH: {}, half_X: {}, half_Y: {}".
          format(field_center, shield_half_length,
                 ship_geo.muShield.half_X_max, ship_geo.muShield.half_Y_max))
    fieldMaker.generateFieldMap(
        os.path.expandvars("$FAIRSHIP/files/fieldMap.csv"), 2.5,
        ship_geo.muShield.half_X_max, ship_geo.muShield.half_Y_max,
        shield_half_length, field_center)
def generate(inputFile, geoFile, nEvents, outFile, lofi=False):
    """Generate muon background and transport it through the geometry.

    Parameters
    ----------
    inputFile : str
        File with muon ntuple
    geoFile : str
        File with the muon shield parameters (not with the geometry config!)
    nEvents : int
        Number of events to read from inputFile
    outFile : str
        File in which `cbmsim` tree is saved
    lofi : bool, optional
        Determine fidelity. If True all non-essential Geant4
        processes will be deactivated

    """
    firstEvent = 0
    dy = 10.
    vessel_design = 5
    shield_design = 8
    mcEngine = 'TGeant4'
    sameSeed = 1
    theSeed = 1

    # provisionally for making studies of various muon background sources
    inactivateMuonProcesses = lofi
    phiRandom = False  # only relevant for muon background generator
    followMuon = True  # only transport muons for a fast muon only background

    print 'FairShip setup to produce', nEvents, 'events'
    r.gRandom.SetSeed(theSeed)
    ship_geo = ConfigRegistry.loadpy('$FAIRSHIP/geometry/geometry_config.py',
                                     Yheight=dy,
                                     tankDesign=vessel_design,
                                     muShieldDesign=shield_design,
                                     muShieldGeo=geoFile)

    run = r.FairRunSim()
    run.SetName(mcEngine)  # Transport engine
    run.SetOutputFile(outFile)  # Output file
    # user configuration file default g4Config.C
    run.SetUserConfig('g4Config.C')
    modules = shipDet_conf.configure(run, ship_geo)
    primGen = r.FairPrimaryGenerator()
    primGen.SetTarget(ship_geo.target.z0 + 50 * u.m, 0.)
    MuonBackgen = r.MuonBackGenerator()
    MuonBackgen.Init(inputFile, firstEvent, phiRandom)
    MuonBackgen.SetSmearBeam(3 * u.cm)  # beam size mimicking spiral
    if sameSeed:
        MuonBackgen.SetSameSeed(sameSeed)
    primGen.AddGenerator(MuonBackgen)
    nEvents = min(nEvents, MuonBackgen.GetNevents())
    print 'Process ', nEvents, ' from input file, with Phi random=', phiRandom
    if followMuon:
        modules['Veto'].SetFastMuon()
    run.SetGenerator(primGen)
    run.SetStoreTraj(r.kFALSE)
    run.Init()
    geomGeant4.setMagnetField()
    if inactivateMuonProcesses:
        mygMC = r.TGeant4.GetMC()
        mygMC.ProcessGeantCommand('/process/inactivate muPairProd')
        mygMC.ProcessGeantCommand('/process/inactivate muBrems')
        mygMC.ProcessGeantCommand('/process/inactivate muIoni')
        mygMC.ProcessGeantCommand('/process/inactivate msc')
        mygMC.ProcessGeantCommand('/process/inactivate Decay')
        mygMC.ProcessGeantCommand('/process/inactivate CoulombScat')
        mygMC.ProcessGeantCommand('/process/inactivate muonNuclear')
        mygMC.ProcessGeantCommand('/process/inactivate StepLimiter')
        mygMC.ProcessGeantCommand('/process/inactivate specialCutForMuon')
    run.Run(nEvents)
    print 'Macro finished succesfully.'
Exemplo n.º 10
0
   sys.exit()

if not inputFile.find('_patrec.root') < 0: 
  outFile   = inputFile
  inputFile = outFile.replace('_patrec.root','.root') 
else:
  outFile = inputFile.replace('.root','_patrec.root') 
  if saveDisk: os.system('mv '+inputFile+' '+outFile)
  else :       os.system('cp '+inputFile+' '+outFile)

if not shipPatRec_prev.geoFile:
 tmp = inputFile.replace('ship.','geofile_full.')
 shipPatRec_prev.geoFile = tmp.replace('_rec','')

run = ROOT.FairRunSim()
shipDet_conf.configure(run,shipPatRec_prev.ship_geo)
modules = {}
for x in run.GetListOfModules(): modules[x.GetName()]=x

fgeo   = ROOT.TFile(shipPatRec_prev.geoFile)
sGeo   = fgeo.FAIRGeom


bfield = ROOT.genfit.BellField(shipPatRec_prev.ship_geo.Bfield.max ,shipPatRec_prev.ship_geo.Bfield.z,2,shipPatRec_prev.ship_geo.Yheight/2.*u.m)
fM = ROOT.genfit.FieldManager.getInstance()
fM.init(bfield)
 
geoMat =  ROOT.genfit.TGeoMaterialInterface()
ROOT.genfit.MaterialEffects.getInstance().init(geoMat)

Exemplo n.º 11
0
def run_track_pattern_recognition(input_file, geo_file, output_file, method):
    """
    Runs all steps of track pattern recognition.
    Parameters
    ----------
    input_file : string
        Path to an input .root file with events.
    geo_file : string
        Path to a file with SHiP geometry.
    output_file : string
        Path to an output .root file with quality plots.
    method : string
        Name of a track pattern recognition method.
    """

    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print("An error with opening the ship geo file.")
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3'):
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy(
                "$FAIRSHIP/geometry/geometry_config.py",
                Yheight=dy,
                EcalGeoFile=ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy(
                "$FAIRSHIP/geometry/geometry_config.py",
                EcalGeoFile=ecalGeoFile)

    else:
        upkl = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')

    # Globals
    builtin.ShipGeo = ShipGeo

    ############################################# Load SHiP modules ####################################################

    import shipDet_conf
    run = ROOT.FairRunSim()
    run.SetName("TGeant4")  # Transport engine
    run.SetOutputFile("dummy")  # Output file
    run.SetUserConfig(
        "g4Config_basic.C"
    )  # geant4 transport not used, only needed for the mag field
    rtdb = run.GetRuntimeDb()

    modules = shipDet_conf.configure(run, ShipGeo)
    run.Init()

    #run = ROOT.FairRunSim()
    #modules = shipDet_conf.configure(run,ShipGeo)

    ######################################### Load SHiP magnetic field #################################################

    import geomGeant4
    if hasattr(ShipGeo.Bfield, "fieldMap"):
        fieldMaker = geomGeant4.addVMCFields(ShipGeo,
                                             '',
                                             True,
                                             withVirtualMC=False)
    else:
        print("no fieldmap given, geofile too old, not anymore support")
        exit(-1)
    sGeo = fgeo.FAIRGeom
    geoMat = ROOT.genfit.TGeoMaterialInterface()
    ROOT.genfit.MaterialEffects.getInstance().init(geoMat)
    bfield = ROOT.genfit.FairShipFields()
    bfield.setField(fieldMaker.getGlobalField())
    fM = ROOT.genfit.FieldManager.getInstance()
    fM.init(bfield)

    ############################################# Load inpur data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file, 'update')
    except:
        print("An error with opening the input data file.")
        raise

    sTree = fn.cbmsim
    sTree.Write()

    ############################################# Create hists #########################################################

    h = init_book_hist()

    ########################################## Start Track Pattern Recognition #########################################
    import shipPatRec

    # Init book of hists for the quality measurements
    metrics = {
        'n_hits': [],
        'reconstructible': 0,
        'passed_y12': 0,
        'passed_stereo12': 0,
        'passed_12': 0,
        'passed_y34': 0,
        'passed_stereo34': 0,
        'passed_34': 0,
        'passed_combined': 0,
        'reco_passed': 0,
        'reco_passed_no_clones': 0,
        'frac_y12': [],
        'frac_stereo12': [],
        'frac_12': [],
        'frac_y34': [],
        'frac_stereo34': [],
        'frac_34': [],
        'reco_frac_tot': [],
        'reco_mc_p': [],
        'reco_mc_theta': [],
        'fitted_p': [],
        'fitted_pval': [],
        'fitted_chi': [],
        'fitted_x': [],
        'fitted_y': [],
        'fitted_z': [],
        'fitted_mass': []
    }

    # Start event loop
    nEvents = sTree.GetEntries()

    for iEvent in range(nEvents):

        if iEvent % 1000 == 0:
            print('Event ', iEvent)

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ########################################### Reconstructible tracks #############################################

        reconstructible_tracks = getReconstructibleTracks(
            iEvent, sTree, sGeo, ShipGeo)

        metrics['reconstructible'] += len(reconstructible_tracks)
        for i_reco in reconstructible_tracks:
            h['TracksPassed'].Fill("Reconstructible tracks", 1)
            h['TracksPassedU'].Fill("Reconstructible tracks", 1)

        in_y12 = []
        in_stereo12 = []
        in_12 = []
        in_y34 = []
        in_stereo34 = []
        in_34 = []
        in_combo = []

        found_track_ids = []
        n_tracks = len(reconstructible_tracks)
        n_recognized = 0
        n_clones = 0
        n_ghosts = 0
        n_others = 0

        min_eff = 0.

        ########################################## Recognized tracks ###################################################

        nTracklets = sTree.Tracklets.GetEntriesFast()

        for i_track in range(nTracklets):

            atracklet = sTree.Tracklets[i_track]

            if atracklet.getType() != 1:  # this is a not full track (tracklet)
                continue

            atrack = atracklet.getList()

            if atrack.size() == 0:
                continue

            hits = {
                'X': [],
                'Y': [],
                'Z': [],
                'DetID': [],
                'TrackID': [],
                'Pz': [],
                'Px': [],
                'Py': [],
                'dist2Wire': [],
                'Pdg': []
            }

            for ihit in atrack:
                ahit = sTree.strawtubesPoint[ihit]
                hits['X'] += [ahit.GetX()]
                hits['Y'] += [ahit.GetY()]
                hits['Z'] += [ahit.GetZ()]
                hits['DetID'] += [ahit.GetDetectorID()]
                hits['TrackID'] += [ahit.GetTrackID()]
                hits['Pz'] += [ahit.GetPz()]
                hits['Px'] += [ahit.GetPx()]
                hits['Py'] += [ahit.GetPy()]
                hits['dist2Wire'] += [ahit.dist2Wire()]
                hits['Pdg'] += [ahit.PdgCode()]

            # List to numpy arrays
            for key in hits.keys():
                hits[key] = numpy.array(hits[key])

            # Decoding
            statnb, vnb, pnb, lnb, snb = decodeDetectorID(hits['DetID'])
            is_stereo = ((vnb == 1) + (vnb == 2))
            is_y = ((vnb == 0) + (vnb == 3))
            is_before = ((statnb == 1) + (statnb == 2))
            is_after = ((statnb == 3) + (statnb == 4))

            # Metrics
            metrics['n_hits'] += [get_n_hits(hits['TrackID'])]

            # Tracks passed
            frac_y12, tmax_y12 = fracMCsame(hits['TrackID'][is_before * is_y])
            n_hits_y12 = get_n_hits(hits['TrackID'][is_before * is_y])
            frac_stereo12, tmax_stereo12 = fracMCsame(
                hits['TrackID'][is_before * is_stereo])
            n_hits_stereo12 = get_n_hits(hits['TrackID'][is_before *
                                                         is_stereo])
            frac_12, tmax_12 = fracMCsame(hits['TrackID'][is_before])
            n_hits_12 = get_n_hits(hits['TrackID'][is_before])

            frac_y34, tmax_y34 = fracMCsame(hits['TrackID'][is_after * is_y])
            n_hits_y34 = get_n_hits(hits['TrackID'][is_after * is_y])
            frac_stereo34, tmax_stereo34 = fracMCsame(
                hits['TrackID'][is_after * is_stereo])
            n_hits_stereo34 = get_n_hits(hits['TrackID'][is_after * is_stereo])
            frac_34, tmax_34 = fracMCsame(hits['TrackID'][is_after])
            n_hits_34 = get_n_hits(hits['TrackID'][is_after])
            frac_tot, tmax_tot = fracMCsame(hits['TrackID'])
            n_hits_tot = get_n_hits(hits['TrackID'])

            if tmax_y12 == tmax_stereo12 and tmax_y12 == tmax_y34 and tmax_y12 == tmax_stereo34:
                if frac_y12 >= min_eff and frac_stereo12 >= min_eff and frac_y34 >= min_eff and frac_stereo34 >= min_eff:

                    if tmax_y12 in reconstructible_tracks and tmax_y12 not in found_track_ids:
                        n_recognized += 1
                        found_track_ids.append(tmax_y12)
                    elif tmax_y12 in reconstructible_tracks and tmax_y12 in found_track_ids:
                        n_clones += 1
                    elif tmax_y12 not in reconstructible_tracks:
                        n_others += 1

                else:
                    n_ghosts += 1
            else:
                n_ghosts += 1

            is_reconstructed = 0
            is_reconstructed_no_clones = 0

            if tmax_y12 in reconstructible_tracks:
                metrics['passed_y12'] += 1
                metrics['frac_y12'] += [frac_y12]
                h['TracksPassed'].Fill("Y view station 1&2", 1)
                if tmax_y12 not in in_y12:
                    h['TracksPassedU'].Fill("Y view station 1&2", 1)
                    in_y12.append(tmax_y12)

                if tmax_stereo12 == tmax_y12:
                    metrics['passed_stereo12'] += 1
                    metrics['frac_stereo12'] += [frac_stereo12]
                    h['TracksPassed'].Fill("Stereo station 1&2", 1)
                    if tmax_stereo12 not in in_stereo12:
                        h['TracksPassedU'].Fill("Stereo station 1&2", 1)
                        in_stereo12.append(tmax_stereo12)

                    if tmax_12 == tmax_y12:
                        metrics['passed_12'] += 1
                        metrics['frac_12'] += [frac_12]
                        h['TracksPassed'].Fill("station 1&2", 1)
                        if tmax_12 not in in_12:
                            h['TracksPassedU'].Fill("station 1&2", 1)
                            in_12.append(tmax_12)

                        if tmax_y34 in reconstructible_tracks:
                            metrics['passed_y34'] += 1
                            metrics['frac_y34'] += [frac_y34]
                            h['TracksPassed'].Fill("Y view station 3&4", 1)
                            if tmax_y34 not in in_y34:
                                h['TracksPassedU'].Fill(
                                    "Y view station 3&4", 1)
                                in_y34.append(tmax_y34)

                            if tmax_stereo34 == tmax_y34:
                                metrics['passed_stereo34'] += 1
                                metrics['frac_stereo34'] += [frac_stereo34]
                                h['TracksPassed'].Fill("Stereo station 3&4", 1)
                                if tmax_stereo34 not in in_stereo34:
                                    h['TracksPassedU'].Fill(
                                        "Stereo station 3&4", 1)
                                    in_stereo34.append(tmax_stereo34)

                                if tmax_34 == tmax_y34:
                                    metrics['passed_34'] += 1
                                    metrics['frac_34'] += [frac_34]
                                    h['TracksPassed'].Fill("station 3&4", 1)
                                    if tmax_34 not in in_34:
                                        h['TracksPassedU'].Fill(
                                            "station 3&4", 1)
                                        in_34.append(tmax_34)

                                    if tmax_12 == tmax_34:
                                        metrics['passed_combined'] += 1
                                        h['TracksPassed'].Fill(
                                            "Combined stations 1&2/3&4", 1)
                                        metrics['reco_passed'] += 1
                                        is_reconstructed = 1
                                        if tmax_34 not in in_combo:
                                            h['TracksPassedU'].Fill(
                                                "Combined stations 1&2/3&4", 1)
                                            metrics[
                                                'reco_passed_no_clones'] += 1
                                            in_combo.append(tmax_34)
                                            is_reconstructed_no_clones = 1

            # For reconstructed tracks
            if is_reconstructed == 0:
                continue

            metrics['reco_frac_tot'] += [frac_tot]

            # Momentum
            Pz = hits['Pz']
            Px = hits['Px']
            Py = hits['Py']

            p, px, py, pz = getPtruthFirst(sTree, tmax_tot)
            pt = math.sqrt(px**2 + py**2)

            Z_true = []
            X_true = []
            Y_true = []
            for ahit in sTree.strawtubesPoint:
                if ahit.GetTrackID() == tmax_tot:
                    az, ax, ay = ahit.GetZ(), ahit.GetX(), ahit.GetY()
                    Z_true.append(az)
                    X_true.append(ax)
                    Y_true.append(ay)

            metrics['reco_mc_p'] += [p]
            h['TracksPassed_p'].Fill(p, 1)

            # Direction
            Z = hits['Z'][(hits['TrackID'] == tmax_tot) * is_before]
            X = hits['X'][(hits['TrackID'] == tmax_tot) * is_before]
            Y = hits['Y'][(hits['TrackID'] == tmax_tot) * is_before]
            Z = Z - Z[0]
            X = X - X[0]
            Y = Y - Y[0]
            R = numpy.sqrt(X**2 + Y**2 + Z**2)
            Theta = numpy.arccos(Z[1:] / R[1:])
            theta = numpy.mean(Theta)

            metrics['reco_mc_theta'] += [theta]

            h['n_hits_reco_y12'].Fill(n_hits_y12)
            h['n_hits_reco_stereo12'].Fill(n_hits_stereo12)
            h['n_hits_reco_12'].Fill(n_hits_12)
            h['n_hits_reco_y34'].Fill(n_hits_y34)
            h['n_hits_reco_stereo34'].Fill(n_hits_stereo34)
            h['n_hits_reco_34'].Fill(n_hits_34)
            h['n_hits_reco'].Fill(n_hits_tot)

            h['n_hits_y12'].Fill(p, n_hits_y12)
            h['n_hits_stereo12'].Fill(p, n_hits_stereo12)
            h['n_hits_12'].Fill(p, n_hits_12)
            h['n_hits_y34'].Fill(p, n_hits_y34)
            h['n_hits_stereo34'].Fill(p, n_hits_stereo34)
            h['n_hits_34'].Fill(p, n_hits_34)
            h['n_hits_total'].Fill(p, n_hits_tot)

            h['frac_y12'].Fill(p, frac_y12)
            h['frac_stereo12'].Fill(p, frac_stereo12)
            h['frac_12'].Fill(p, frac_12)
            h['frac_y34'].Fill(p, frac_y34)
            h['frac_stereo34'].Fill(p, frac_stereo34)
            h['frac_34'].Fill(p, frac_34)
            h['frac_total'].Fill(p, frac_tot)

            h['frac_y12_dist'].Fill(frac_y12)
            h['frac_stereo12_dist'].Fill(frac_stereo12)
            h['frac_12_dist'].Fill(frac_12)
            h['frac_y34_dist'].Fill(frac_y34)
            h['frac_stereo34_dist'].Fill(frac_stereo34)
            h['frac_34_dist'].Fill(frac_34)
            h['frac_total_dist'].Fill(frac_tot)

            # Fitted track

            thetrack = sTree.FitTracks[i_track]

            fitStatus = thetrack.getFitStatus()
            thetrack.prune(
                "CFL"
            )  # http://sourceforge.net/p/genfit/code/HEAD/tree/trunk/core/include/Track.h#l280

            nmeas = fitStatus.getNdf()
            pval = fitStatus.getPVal()
            chi2 = fitStatus.getChi2() / nmeas

            metrics['fitted_pval'] += [pval]
            metrics['fitted_chi'] += [chi2]

            h['chi2fittedtracks'].Fill(chi2)
            h['pvalfittedtracks'].Fill(pval)

            try:

                fittedState = thetrack.getFittedState()
                fittedMom = fittedState.getMomMag()
                fittedMom = fittedMom  #*int(charge)

                px_fit, py_fit, pz_fit = fittedState.getMom().x(
                ), fittedState.getMom().y(), fittedState.getMom().z()
                p_fit = fittedMom
                pt_fit = math.sqrt(px_fit**2 + py_fit**2)

                metrics['fitted_p'] += [p_fit]
                perr = (p - p_fit) / p
                h['ptrue-p/ptrue'].Fill(perr)
                h['perr'].Fill(p, perr)
                h['perr_direction'].Fill(numpy.rad2deg(theta), perr)

                pterr = (pt - pt_fit) / pt
                h['pttrue-pt/pttrue'].Fill(pterr)

                pxerr = (px - px_fit) / px
                h['pxtrue-px/pxtrue'].Fill(pxerr)

                pyerr = (py - py_fit) / py
                h['pytrue-py/pytrue'].Fill(pyerr)

                pzerr = (pz - pz_fit) / pz
                h['pztrue-pz/pztrue'].Fill(pzerr)

                if math.fabs(p) > 0.0:
                    h['pvspfitted'].Fill(p, fittedMom)
                fittedtrackDir = fittedState.getDir()
                fittedx = math.degrees(math.acos(fittedtrackDir[0]))
                fittedy = math.degrees(math.acos(fittedtrackDir[1]))
                fittedz = math.degrees(math.acos(fittedtrackDir[2]))
                fittedmass = fittedState.getMass()
                h['momentumfittedtracks'].Fill(fittedMom)
                h['xdirectionfittedtracks'].Fill(fittedx)
                h['ydirectionfittedtracks'].Fill(fittedy)
                h['zdirectionfittedtracks'].Fill(fittedz)
                h['massfittedtracks'].Fill(fittedmass)

                metrics['fitted_x'] += [fittedx]
                metrics['fitted_y'] += [fittedy]
                metrics['fitted_z'] += [fittedz]
                metrics['fitted_mass'] += [fittedmass]

                Z_fit = []
                X_fit = []
                Y_fit = []
                for az in Z_true:
                    rc, pos, mom = extrapolateToPlane(thetrack, az)
                    Z_fit.append(pos.Z())
                    X_fit.append(pos.X())
                    Y_fit.append(pos.Y())

                for i in range(len(Z_true)):
                    xerr = abs(X_fit[i] - X_true[i])
                    yerr = abs(Y_fit[i] - Y_true[i])
                    h['abs(x - x-true)'].Fill(xerr)
                    h['abs(y - y-true)'].Fill(yerr)

                rmse_x = numpy.sqrt(
                    numpy.mean((numpy.array(X_fit) - numpy.array(X_true))**2))
                rmse_y = numpy.sqrt(
                    numpy.mean((numpy.array(Y_fit) - numpy.array(Y_true))**2))
                h['rmse_x'].Fill(rmse_x)
                h['rmse_y'].Fill(rmse_y)

            except:
                print("Problem with fitted state.")

        h['Reco_tracks'].Fill("N total", n_tracks)
        h['Reco_tracks'].Fill("N recognized tracks", n_recognized)
        h['Reco_tracks'].Fill("N clones", n_clones)
        h['Reco_tracks'].Fill("N ghosts", n_ghosts)
        h['Reco_tracks'].Fill("N others", n_others)

    ############################################# Save hists #########################################################

    save_hists(h, output_file)

    return metrics
Exemplo n.º 12
0
def run_checking(input_file, geo_file, dy, reconstructiblerequired,
                 threeprong):

    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print "An error with opening the ship geo file."
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3'):
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy(
                "$FAIRSHIP/geometry/geometry_config.py",
                Yheight=dy,
                EcalGeoFile=ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy(
                "$FAIRSHIP/geometry/geometry_config.py",
                EcalGeoFile=ecalGeoFile)

    else:
        upkl = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')

    ############################################# Load SHiP modules ####################################################

    run = ROOT.FairRunSim()
    modules = shipDet_conf.configure(run, ShipGeo)

    ############################################# Load input data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file, 'update')
    except:
        print "An error with opening the input data file."
        raise

    sTree = fn.cbmsim

    ############################## Initialize SHiP Spectrometer Tracker geometry #######################################

    #zlayer, \
    #zlayerv2, \
    #z34layer, \
    #z34layerv2, \
    #TStation1StartZ, \
    #TStation4EndZ, \
    #VetoStationZ, \
    #VetoStationEndZ = initialize(fgeo, ShipGeo)

    ########################################## Start Checking Geometry #################################################

    all_hits = pandas.DataFrame(
        columns=['event_id', 'det_id', 'xtop', 'ytop', 'z', 'xbot', 'ybot'])
    all_hits_i = 0

    # Start event loop
    nEvents = sTree.GetEntries()

    for iEvent in range(nEvents):

        if iEvent % 100 == 0:
            print 'Event ', iEvent

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ############################################# Get hits #########################################################

        nHits = sTree.strawtubesPoint.GetEntriesFast()
        key = -1

        for i in range(nHits):

            ahit = sTree.strawtubesPoint[i]

            key += 1
            detID = ahit.GetDetectorID()
            top = ROOT.TVector3()
            bot = ROOT.TVector3()

            modules["Strawtubes"].StrawEndPoints(detID, bot, top)

            all_hits.loc[all_hits_i] = [
                iEvent, detID,
                top.x(),
                top.y(),
                top.z(),
                bot.x(),
                bot.y()
            ]
            all_hits_i += 1

    all_hits.to_csv('hits.csv', index=False)
    all_hits['StatNb'] = all_hits['det_id'] // 10000000
    all_hits['ViewNb'] = (all_hits['det_id'] -
                          all_hits['StatNb'] * 10000000) // 1000000
    all_hits['PlaneNb'] = (all_hits['det_id'] - all_hits['StatNb'] * 10000000 -
                           all_hits['ViewNb'] * 1000000) // 100000
    all_hits['LayerNb'] = (all_hits['det_id'] - all_hits['StatNb'] * 10000000 - all_hits['ViewNb'] * 1000000 -\
                           all_hits['PlaneNb'] * 100000) // 10000
    all_hits['StrawNb'] = all_hits['det_id'] - all_hits['StatNb'] * 10000000 - all_hits['ViewNb'] * 1000000 -\
                          all_hits['PlaneNb'] * 100000 - all_hits['LayerNb'] * 10000 - 2000

    dots00 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
             (all_hits['PlaneNb'].values==0) & (all_hits['LayerNb'].values==0)]['ytop'].values
    dots00 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
                 (all_hits['PlaneNb'].values==1) & (all_hits['LayerNb'].values==0)]['ytop'].values
    dots01 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
                 (all_hits['PlaneNb'].values==0) & (all_hits['LayerNb'].values==1)]['ytop'].values
    dots01 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
                 (all_hits['PlaneNb'].values==1) & (all_hits['LayerNb'].values==1)]['ytop'].values

    min_dist = np.hstack([
        np.diff(np.unique(dots00)),
        np.diff(np.unique(dots01)),
        np.diff(np.unique(dots00)),
        np.diff(np.unique(dots01))
    ]).min()

    print('Pitch: ', np.round(min_dist, 3))

    z0 = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==0) & (all_hits['PlaneNb'].values==0) &\
                  (all_hits['LayerNb'].values==0)]['z'].mean()
    z1 = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==0) & (all_hits['PlaneNb'].values==0) &\
                  (all_hits['LayerNb'].values==1)]['z'].mean()

    print('ZShiftLayer: ', np.round(z1 - z0, 3))

    z2 = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==0) & (all_hits['PlaneNb'].values==1) &\
                  (all_hits['LayerNb'].values==0)]['z'].mean()

    print('ZShiftPlane: ', np.round(z2 - z0, 3))

    xtop = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==1) & (all_hits['PlaneNb']==0) &\
                    (all_hits['LayerNb']==0)]['xtop'].values[0]
    ytop = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==1) & (all_hits['PlaneNb']==0) &\
                    (all_hits['LayerNb']==0)]['ytop'].values[0]
    xbot = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==1) & (all_hits['PlaneNb']==0) &\
                    (all_hits['LayerNb']==0)]['xbot'].values[0]
    ybot = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==1) & (all_hits['PlaneNb']==0) &\
                    (all_hits['LayerNb']==0)]['ybot'].values[0]

    print('Angle: ',
          np.round(np.arctan((ytop - ybot) / (xtop - xbot)) * 180 / np.pi, 3))

    z0 = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==0) & (all_hits['PlaneNb'].values==0) &\
            (all_hits['LayerNb'].values==0)]['z'].mean()
    z3 = all_hits[(all_hits['StatNb']==1) & (all_hits['ViewNb']==1) & (all_hits['PlaneNb'].values==0) &\
            (all_hits['LayerNb'].values==0)]['z'].mean()

    print('ZShiftView: ', np.round(z3 - z0, 3))

    dots0 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
             (all_hits['PlaneNb'].values==0) & (all_hits['LayerNb'].values==0)]['ytop'].values
    dots1 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
             (all_hits['PlaneNb'].values==0) & (all_hits['LayerNb'].values==1)]['ytop'].values

    min_dist = np.hstack([np.diff(np.unique(np.hstack([dots0, dots1])))]).min()

    print('YOffsetLayer: ', np.round(min_dist, 3))

    dots0 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
             (all_hits['PlaneNb'].values==0) & (all_hits['LayerNb'].values==0)]['ytop'].values
    dots1 = all_hits[(all_hits['StatNb'].values<3) & ((all_hits['ViewNb'].values==0) + (all_hits['ViewNb'].values==3)) &\
             (all_hits['PlaneNb'].values==1) & (all_hits['LayerNb'].values==0)]['ytop'].values

    min_dist = np.hstack([np.diff(np.unique(np.hstack([dots0, dots1])))]).min()

    print('YOffsetPlane: ', np.round(min_dist, 3))

    return
Exemplo n.º 13
0
def run_track_pattern_recognition(input_file, geo_file, output_file, method):
    """
    Runs all steps of track pattern recognition.

    Parameters
    ----------
    input_file : string
        Path to an input .root file with events.
    geo_file : string
        Path to a file with SHiP geometry.
    output_file : string
        Path to an output .root file with quality plots.
    method : string
        Name of a track pattern recognition method.
    """


    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print "An error with opening the ship geo file."
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3') :
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", Yheight = dy, EcalGeoFile = ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", EcalGeoFile = ecalGeoFile)

    else:
        upkl    = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')
    
    # Globals
    builtin.ShipGeo = ShipGeo

    ############################################# Load SHiP modules ####################################################

    run = ROOT.FairRunSim()
    modules = shipDet_conf.configure(run,ShipGeo)

    ############################################# Load inpur data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file,'update')
    except:
        print "An error with opening the input data file."
        raise

    sTree = fn.cbmsim
    sTree.Write()
    
    ############################################# Create hists #########################################################
    
    h = init_book_hist()

    ########################################## Start Track Pattern Recognition #########################################
    import shipPatRec

    # Init book of hists for the quality measurements
    metrics = {'n_hits': [], 
               'reconstructible': 0, 
               'passed_y12': 0, 'passed_stereo12': 0, 'passed_12': 0, 
               'passed_y34': 0, 'passed_stereo34': 0, 'passed_34': 0, 
               'passed_combined': 0, 'reco_passed': 0, 'reco_passed_no_clones': 0,
               'frac_y12': [], 'frac_stereo12': [], 'frac_12': [], 
               'frac_y34': [], 'frac_stereo34': [], 'frac_34': [], 
               'reco_frac_tot': [], 
               'reco_mc_p': [], 'reco_mc_theta': [], 
               'fitted_p': [], 'fitted_pval': [], 'fitted_chi': [], 
               'fitted_x': [], 'fitted_y': [], 'fitted_z': [], 'fitted_mass': []}

    # Start event loop
    nEvents   = sTree.GetEntries()
   

    for iEvent in range(nEvents):

        if iEvent%1000 == 0:
            print 'Event ', iEvent

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ########################################### Reconstructible tracks #############################################

        reconstructible_tracks = getReconstructibleTracks(iEvent, sTree, sGeo, ShipGeo)
        
        metrics['reconstructible'] += len(reconstructible_tracks)
        for i_reco in reconstructible_tracks:
            h['TracksPassed'].Fill("Reconstructible tracks", 1)
            h['TracksPassedU'].Fill("Reconstructible tracks", 1)
            
        in_y12 = []
        in_stereo12 = []
        in_12 = []
        in_y34 = []
        in_stereo34 = []
        in_34 = []
        in_combo = []
        
        ########################################## Recognized tracks ###################################################
        
        nTracklets = sTree.Tracklets.GetEntriesFast()
        
        for i_track in range(nTracklets):
            
            atracklet = sTree.Tracklets[i_track]
            
            if atracklet.getType() != 1: # this is a not full track (tracklet)
                continue
                
            atrack = atracklet.getList()
            
            if atrack.size() == 0:
                continue
            
            hits = {'X': [], 'Y': [], 'Z': [], 
                    'DetID': [], 'TrackID': [], 
                    'Pz': [], 'Px': [], 'Py': [], 
                    'dist2Wire': [], 'Pdg': []}
            
            for ihit in atrack:
                ahit = sTree.strawtubesPoint[ihit]
                hits['X'] += [ahit.GetX()]
                hits['Y'] += [ahit.GetY()]
                hits['Z'] += [ahit.GetZ()]
                hits['DetID'] += [ahit.GetDetectorID()]
                hits['TrackID'] += [ahit.GetTrackID()]
                hits['Pz'] += [ahit.GetPz()]
                hits['Px'] += [ahit.GetPx()]
                hits['Py'] += [ahit.GetPy()]
                hits['dist2Wire'] += [ahit.dist2Wire()]
                hits['Pdg'] += [ahit.PdgCode()]
                
            # List to numpy arrays
            for key in hits.keys():
                hits[key] = numpy.array(hits[key])
                
            # Decoding
            statnb, vnb, pnb, lnb, snb = decodeDetectorID(hits['DetID'])
            is_stereo = ((vnb == 1) + (vnb == 2))
            is_y = ((vnb == 0) + (vnb == 3))
            is_before = ((statnb == 1) + (statnb == 2))
            is_after = ((statnb == 3) + (statnb == 4)) 
                
            
            # Metrics
            metrics['n_hits'] += [get_n_hits(hits['TrackID'])]
            
            # Tracks passed
            frac_y12, tmax_y12 = fracMCsame(hits['TrackID'][is_before * is_y])
            n_hits_y12 = get_n_hits(hits['TrackID'][is_before * is_y])
            frac_stereo12, tmax_stereo12 = fracMCsame(hits['TrackID'][is_before * is_stereo])
            n_hits_stereo12 = get_n_hits(hits['TrackID'][is_before * is_stereo])
            frac_12, tmax_12 = fracMCsame(hits['TrackID'][is_before])
            n_hits_12 = get_n_hits(hits['TrackID'][is_before])
            
            frac_y34, tmax_y34 = fracMCsame(hits['TrackID'][is_after * is_y])
            n_hits_y34 = get_n_hits(hits['TrackID'][is_after * is_y])
            frac_stereo34, tmax_stereo34 = fracMCsame(hits['TrackID'][is_after * is_stereo])
            n_hits_stereo34 = get_n_hits(hits['TrackID'][is_after * is_stereo])
            frac_34, tmax_34 = fracMCsame(hits['TrackID'][is_after])
            n_hits_34 = get_n_hits(hits['TrackID'][is_after])
            frac_tot, tmax_tot = fracMCsame(hits['TrackID'])
            n_hits_tot = get_n_hits(hits['TrackID'])
            
            is_reconstructed = 0
            is_reconstructed_no_clones = 0
            
            
            if tmax_y12 in reconstructible_tracks:
                metrics['passed_y12'] += 1
                metrics['frac_y12'] += [frac_y12]
                h['TracksPassed'].Fill("Y view station 1&2", 1)
                if tmax_y12 not in in_y12:
                    h['TracksPassedU'].Fill("Y view station 1&2", 1)
                    in_y12.append(tmax_y12)
                
                if tmax_stereo12 == tmax_y12:
                    metrics['passed_stereo12'] += 1
                    metrics['frac_stereo12'] += [frac_stereo12]
                    h['TracksPassed'].Fill("Stereo station 1&2", 1)
                    if tmax_stereo12 not in in_stereo12:
                        h['TracksPassedU'].Fill("Stereo station 1&2", 1)
                        in_stereo12.append(tmax_stereo12)
                    
                    if tmax_12 == tmax_y12:
                        metrics['passed_12'] += 1
                        metrics['frac_12'] += [frac_12]
                        h['TracksPassed'].Fill("station 1&2", 1)
                        if tmax_12 not in in_12:
                            h['TracksPassedU'].Fill("station 1&2", 1)
                            in_12.append(tmax_12)
                        
                        if tmax_y34 in reconstructible_tracks:
                            metrics['passed_y34'] += 1
                            metrics['frac_y34'] += [frac_y34]
                            h['TracksPassed'].Fill("Y view station 3&4", 1)
                            if tmax_y34 not in in_y34:
                                h['TracksPassedU'].Fill("Y view station 3&4", 1)
                                in_y34.append(tmax_y34)
                            
                            if tmax_stereo34 == tmax_y34:
                                metrics['passed_stereo34'] += 1
                                metrics['frac_stereo34'] += [frac_stereo34]
                                h['TracksPassed'].Fill("Stereo station 3&4", 1)
                                if tmax_stereo34 not in in_stereo34:
                                    h['TracksPassedU'].Fill("Stereo station 3&4", 1)
                                    in_stereo34.append(tmax_stereo34)
                                
                                if tmax_34 == tmax_y34:
                                    metrics['passed_34'] += 1
                                    metrics['frac_34'] += [frac_34]
                                    h['TracksPassed'].Fill("station 3&4", 1)
                                    if tmax_34 not in in_34:
                                        h['TracksPassedU'].Fill("station 3&4", 1)
                                        in_34.append(tmax_34)
                                    
                                    if tmax_12 == tmax_34:
                                        metrics['passed_combined'] += 1
                                        h['TracksPassed'].Fill("Combined stations 1&2/3&4", 1)
                                        is_reconstructed = 1
                                        if tmax_34 not in in_combo:
                                            h['TracksPassedU'].Fill("Combined stations 1&2/3&4", 1)
                                            in_combo.append(tmax_34)
                                            is_reconstructed_no_clones = 1
                                        
            # For reconstructed tracks
            if is_reconstructed == 0:
                continue
                
            metrics['reco_passed'] += 1
            metrics['reco_passed_no_clones'] += 1
            metrics['reco_frac_tot'] += [frac_tot]
            
            # Momentum
            Pz = hits['Pz']
            Px = hits['Px']
            Py = hits['Py']
            P = numpy.sqrt(Pz**2 + Px**2 + Py**2)
            P = P[hits['TrackID'] == tmax_tot]
            p = numpy.mean(P)
            
            metrics['reco_mc_p'] += [p]
            h['TracksPassed_p'].Fill(p, 1)
            
            # Direction
            Z = hits['Z'][(hits['TrackID'] == tmax_tot) * is_before]
            X = hits['X'][(hits['TrackID'] == tmax_tot) * is_before]
            Y = hits['Y'][(hits['TrackID'] == tmax_tot) * is_before]
            Z = Z - Z[0]
            X = X - X[0]
            Y = Y - Y[0]
            R = numpy.sqrt(X**2 + Y**2 + Z**2)
            Theta = numpy.arccos(Z[1:] / R[1:])
            theta = numpy.mean(Theta)
            
            metrics['reco_mc_theta'] += [theta]
            
            h['n_hits_reco_y12'].Fill(n_hits_y12)
            h['n_hits_reco_stereo12'].Fill(n_hits_stereo12)
            h['n_hits_reco_12'].Fill(n_hits_12)
            h['n_hits_reco_y34'].Fill(n_hits_y34)
            h['n_hits_reco_stereo34'].Fill(n_hits_stereo34)
            h['n_hits_reco_34'].Fill(n_hits_34)
            h['n_hits_reco'].Fill(n_hits_tot)
            
            h['n_hits_y12'].Fill(p, n_hits_y12)
            h['n_hits_stereo12'].Fill(p, n_hits_stereo12)
            h['n_hits_12'].Fill(p, n_hits_12)
            h['n_hits_y34'].Fill(p, n_hits_y34)
            h['n_hits_stereo34'].Fill(p, n_hits_stereo34)
            h['n_hits_34'].Fill(p, n_hits_34)
            h['n_hits_total'].Fill(p, n_hits_tot)
            
            h['frac_y12'].Fill(p, frac_y12)
            h['frac_stereo12'].Fill(p, frac_stereo12)
            h['frac_12'].Fill(p, frac_12)
            h['frac_y34'].Fill(p, frac_y34)
            h['frac_stereo34'].Fill(p, frac_stereo34)
            h['frac_34'].Fill(p, frac_34)
            h['frac_total'].Fill(p, frac_tot)
            
            # Fitted track
            
            thetrack = sTree.FitTracks[i_track]
            
            fitStatus   = thetrack.getFitStatus()
            thetrack.prune("CFL") # http://sourceforge.net/p/genfit/code/HEAD/tree/trunk/core/include/Track.h#l280

            nmeas = fitStatus.getNdf()
            pval = fitStatus.getPVal()
            chi2 = fitStatus.getChi2() / nmeas
            
            metrics['fitted_pval'] += [pval]
            metrics['fitted_chi'] += [chi2]

            h['chi2fittedtracks'].Fill(chi2)
            h['pvalfittedtracks'].Fill(pval)

            fittedState = thetrack.getFittedState()
            fittedMom = fittedState.getMomMag()
            fittedMom = fittedMom #*int(charge)
            
            metrics['fitted_p'] += [fittedMom]
            perr = (p - fittedMom) / p
            h['ptrue-p/ptrue'].Fill(perr)
            h['perr'].Fill(p, perr)
            h['perr_direction'].Fill(numpy.rad2deg(theta), perr)

            if math.fabs(p) > 0.0 :
                h['pvspfitted'].Fill(p, fittedMom)
            fittedtrackDir = fittedState.getDir()
            fittedx=math.degrees(math.acos(fittedtrackDir[0]))
            fittedy=math.degrees(math.acos(fittedtrackDir[1]))
            fittedz=math.degrees(math.acos(fittedtrackDir[2]))
            fittedmass = fittedState.getMass()
            h['momentumfittedtracks'].Fill(fittedMom)
            h['xdirectionfittedtracks'].Fill(fittedx)
            h['ydirectionfittedtracks'].Fill(fittedy)
            h['zdirectionfittedtracks'].Fill(fittedz)
            h['massfittedtracks'].Fill(fittedmass)
            
            metrics['fitted_x'] += [fittedx]
            metrics['fitted_y'] += [fittedy]
            metrics['fitted_z'] += [fittedz]
            metrics['fitted_mass'] += [fittedmass]
            
    ############################################# Save hists #########################################################
    
    save_hists(h, output_file)

    return metrics
Exemplo n.º 14
0
    sys.exit()

if not inputFile.find('_patrec.root') < 0:
    outFile = inputFile
    inputFile = outFile.replace('_patrec.root', '.root')
else:
    outFile = inputFile.replace('.root', '_patrec.root')
    if saveDisk: os.system('mv ' + inputFile + ' ' + outFile)
    else: os.system('cp ' + inputFile + ' ' + outFile)

if not shipPatRec_prev.geoFile:
    tmp = inputFile.replace('ship.', 'geofile_full.')
    shipPatRec_prev.geoFile = tmp.replace('_rec', '')

run = ROOT.FairRunSim()
shipDet_conf.configure(run, shipPatRec_prev.ship_geo)
modules = {}
for x in run.GetListOfModules():
    modules[x.GetName()] = x

fgeo = ROOT.TFile(shipPatRec_prev.geoFile)
sGeo = fgeo.FAIRGeom

bfield = ROOT.genfit.BellField(shipPatRec_prev.ship_geo.Bfield.max,
                               shipPatRec_prev.ship_geo.Bfield.z, 2,
                               shipPatRec_prev.ship_geo.Yheight / 2. * u.m)
fM = ROOT.genfit.FieldManager.getInstance()
fM.init(bfield)

geoMat = ROOT.genfit.TGeoMaterialInterface()
ROOT.genfit.MaterialEffects.getInstance().init(geoMat)
Exemplo n.º 15
0
# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)  # Transport engine
run.SetOutputFile(outFile)  # Output file
run.SetUserConfig("g4Config.C") # user configuration file default g4Config.C 
rtdb = run.GetRuntimeDb() 
# -----Create geometry----------------------------------------------
# import shipMuShield_only as shipDet_conf # special use case for an attempt to convert active shielding geometry for use with FLUKA
# import shipTarget_only as shipDet_conf
import shipDet_conf
modules = shipDet_conf.configure(run,ship_geo)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()
if simEngine == "Pythia8":
 primGen.SetTarget(ship_geo.target.z0, 0.) 
# -----Pythia8--------------------------------------
 P8gen = ROOT.HNLPythia8Generator()
 import pythia8_conf
 pythia8_conf.configure(P8gen,theHNLmass,theHNLcouplings,inclusive,deepCopy)
 if inputFile: 
# read from external file
  P8gen.SetParameters("ProcessLevel:all = off")
  P8gen.UseExternalFile(inputFile, 0)
 # pion on proton 500GeV
 # P8gen.SetMom(500.*u.GeV)
 # P8gen.SetId(-211)
Exemplo n.º 16
0
# In general, the following parts need not be touched
# ========================================================================

# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)  # Transport engine
run.SetOutputFile(outFile)  # Output file
rtdb = run.GetRuntimeDb() 
# -----Create geometry----------------------------------------------
import shipDet_conf
shipDet_conf.configure(run)
# -----Create PrimaryGenerator--------------------------------------
primGen=ROOT.FairPrimaryGenerator()
boxGen=ROOT.FairBoxGenerator(22,1);
boxGen.SetPRange(momentum, momentum)
boxGen.SetPhiRange(0,0)
boxGen.SetThetaRange(0,0)
#boxGen.SetXYZ(0,0,-9900)
boxGen.SetXYZ(0,0,2400)
primGen.AddGenerator(boxGen)
run.SetGenerator(primGen)

# ------------------------------------------------------------------------

#---Store the visualiztion info of the tracks, this make the output file very large!!
#--- Use it only to display but not for production!
Exemplo n.º 17
0
# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)  # Transport engine
run.SetOutputFile(outFile)  # Output file
run.SetUserConfig("g4Config.C")  # user configuration file default g4Config.C
rtdb = run.GetRuntimeDb()
# -----Create geometry----------------------------------------------
# import shipMuShield_only as shipDet_conf # special use case for an attempt to convert active shielding geometry for use with FLUKA
import shipDet_conf
modules = shipDet_conf.configure(run, ship_geo)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()

if simEngine == "Pythia8":
    primGen.SetTarget(ship_geo.target.z0, 0.)
    # -----Pythia8--------------------------------------
    P8gen = ROOT.HNLPythia8Generator()
    import pythia8_conf
    pythia8_conf.configure(P8gen, inclusive, deepCopy)
    primGen.AddGenerator(P8gen)
if simEngine == "Pythia6":
    # set muon interaction close to decay volume
    primGen.SetTarget(ship_geo.target.z0 + ship_geo.muShield.length, 0.)
    # -----Pythia6-------------------------
    P6gen = ROOT.tPythia6Generator()
Exemplo n.º 18
0
# ========================================================================

# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)  # Transport engine
run.SetOutputFile(outFile)  # Output file
rtdb = run.GetRuntimeDb()
# -----Create geometry----------------------------------------------
import shipDet_conf

shipDet_conf.configure(run)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()
boxGen = ROOT.FairBoxGenerator(22, 1)
boxGen.SetPRange(momentum, momentum)
boxGen.SetPhiRange(0, 0)
boxGen.SetThetaRange(0, 0)
#boxGen.SetXYZ(0,0,-9900)
boxGen.SetXYZ(0, 0, 2400)
primGen.AddGenerator(boxGen)
run.SetGenerator(primGen)

# ------------------------------------------------------------------------

#---Store the visualiztion info of the tracks, this make the output file very large!!
#--- Use it only to display but not for production!
Exemplo n.º 19
0
# In general, the following parts need not be touched
# ========================================================================

# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)# Transport engine
run.SetOutputFile(outFile) # Output file
rtdb = run.GetRuntimeDb() 
# -----Create geometry----------------------------------------------
import shipDet_conf
shipDet_conf.configure(run,ShipGeo)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()
primGen.SetTarget(ShipGeo.target.z0, 0.) 
# -----Pythia8--------------------------------------
P8gen = ROOT.Pythia8Generator()
import pythia8_conf
pythia8_conf.configure(P8gen,inclusive)
primGen.AddGenerator(P8gen)
run.SetGenerator(primGen)

if inclusive: 
  # check presence of HNL
  P8gen.GetPythiaInstance(9900014)
# ------------------------------------------------------------------------
 
Exemplo n.º 20
0
def dmetric(input_file, geo_file, dy, reconstructiblerequired, threeprong):

    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print "An error with opening the ship geo file."
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3') :
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", Yheight = dy, EcalGeoFile = ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", EcalGeoFile = ecalGeoFile)

    else:
        upkl    = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')

    ############################################# Load SHiP modules ####################################################

    run = ROOT.FairRunSim()
    modules = shipDet_conf.configure(run,ShipGeo)

    ############################################# Load input data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file,'update')
    except:
        print "An error with opening the input data file."
        raise

    sTree = fn.cbmsim

    ############################## Initialize SHiP Spectrometer Tracker geometry #######################################

    zlayer, \
    zlayerv2, \
    z34layer, \
    z34layerv2, \
    TStation1StartZ, \
    TStation4EndZ, \
    VetoStationZ, \
    VetoStationEndZ = initialize(ShipGeo)


    ########################################## Start Checking Geometry #################################################

    all_hits = pandas.DataFrame(columns=['event_id', 'det_id', 'track_id', 'xtop', 'ytop', 'z', 'xbot', 'ybot'])
    all_hits_i = 0
    
    # Start event loop
    nEvents   = sTree.GetEntries()

    for iEvent in range(nEvents):

        if iEvent%100 == 0:
            print 'Event ', iEvent

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ############################################# Get hits #########################################################
        
        reco_mc_tracks = getReconstructibleTracks(iEvent,
                                                  sTree,
                                                  sGeo,
                                                  reconstructiblerequired,
                                                  threeprong,
                                                  TStation1StartZ,
                                                  TStation4EndZ,
                                                  VetoStationZ,
                                                  VetoStationEndZ)
        
        #consider only reconstructible events
        if len(reco_mc_tracks) > 1:

            nHits = sTree.strawtubesPoint.GetEntriesFast()
            key = -1

            for i in range(nHits):

                ahit = sTree.strawtubesPoint[i]

                key+=1
                detID = ahit.GetDetectorID()
                trID = ahit.GetTrackID()
                top = ROOT.TVector3()
                bot = ROOT.TVector3()

                modules["Strawtubes"].StrawEndPoints(detID,bot,top)

                all_hits.loc[all_hits_i] = [iEvent, detID, trID, top.x(), top.y(), top.z(), bot.x(), bot.y()]
                all_hits_i += 1
            
    all_hits['StatNb'] = all_hits['det_id'] // 10000000
    all_hits['ViewNb'] = (all_hits['det_id'] - all_hits['StatNb'] * 10000000) // 1000000
    all_hits['PlaneNb'] = (all_hits['det_id'] - all_hits['StatNb'] * 10000000 - all_hits['ViewNb'] * 1000000) // 100000
    all_hits['LayerNb'] = (all_hits['det_id'] - all_hits['StatNb'] * 10000000 - all_hits['ViewNb'] * 1000000 -\
                           all_hits['PlaneNb'] * 100000) // 10000
    all_hits['StrawNb'] = all_hits['det_id'] - all_hits['StatNb'] * 10000000 - all_hits['ViewNb'] * 1000000 -\
                          all_hits['PlaneNb'] * 100000 - all_hits['LayerNb'] * 10000 - 2000
    
    #return daniel's metric
    df_1_view = all_hits[(all_hits.StatNb==1)&(all_hits.ViewNb==0)]
    counts = df_1_view.groupby(['event_id', 'track_id'])['StatNb'].count()
    all_tracks = len(all_hits.groupby(['event_id', 'track_id'])['StatNb'].count())
    
    return 1. * np.sum(counts>1) / all_tracks
Exemplo n.º 21
0
def run_track_pattern_recognition(input_file, geo_file, dy, reconstructiblerequired, threeprong):


    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print "An error with opening the ship geo file."
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3') :
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", Yheight = dy, EcalGeoFile = ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", EcalGeoFile = ecalGeoFile)

    else:
        upkl    = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')

    ############################################# Load SHiP modules ####################################################

    run = ROOT.FairRunSim()
    modules = shipDet_conf.configure(run,ShipGeo)

    ############################################# Load inpur data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file,'update')
    except:
        print "An error with opening the input data file."
        raise

    sTree = fn.cbmsim

    ############################## Initialize SHiP Spectrometer Tracker geometry #######################################

    zlayer, \
    zlayerv2, \
    z34layer, \
    z34layerv2, \
    TStation1StartZ, \
    TStation4EndZ, \
    VetoStationZ, \
    VetoStationEndZ = initialize(fgeo, ShipGeo)


    ########################################## Start Track Pattern Recognition #########################################

    # Init book of hists for the quality measurements
    h = init_book_hist()

    # Start event loop
    nEvents   = sTree.GetEntries()

    for iEvent in range(nEvents):

        if iEvent%10 == 0:
            print 'Event ', iEvent

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ########################################### Smear hits #########################################################

        smeared_hits = smearHits(sTree, ShipGeo, modules, no_amb=None)

        if len(smeared_hits) == 0:
            continue

        ########################################### Do track pattern recognition #######################################

        reco_tracks, theTracks  = execute(smeared_hits, sTree, ShipGeo)

        ########################################### Get MC truth #######################################################

        y = get_track_ids(sTree, smeared_hits)

        fittedtrackids, fittedtrackfrac = get_fitted_trackids(y, reco_tracks)

        reco_mc_tracks = getReconstructibleTracks(iEvent,
                                                  sTree,
                                                  sGeo,
                                                  reconstructiblerequired,
                                                  threeprong,
                                                  TStation1StartZ,
                                                  TStation4EndZ,
                                                  VetoStationZ,
                                                  VetoStationEndZ) # TODO:!!!

        ########################################### Measure quality metrics ############################################

        quality_metrics(smeared_hits, sTree, reco_mc_tracks, reco_tracks, h)


    ############################################### Save results #######################################################

    save_hists(h, 'hists.root')


    return
Exemplo n.º 22
0
def run_track_pattern_recognition(input_file, geo_file, output_file, method):
    """
    Runs all steps of track pattern recognition.
    Parameters
    ----------
    input_file : string
        Path to an input .root file with events.
    geo_file : string
        Path to a file with SHiP geometry.
    output_file : string
        Path to an output .root file with quality plots.
    method : string
        Name of a track pattern recognition method.
    """

    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print "An error with opening the ship geo file."
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3'):
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy(
                "$FAIRSHIP/geometry/geometry_config.py",
                Yheight=dy,
                EcalGeoFile=ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy(
                "$FAIRSHIP/geometry/geometry_config.py",
                EcalGeoFile=ecalGeoFile)

    else:
        upkl = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')

    # Globals
    builtin.ShipGeo = ShipGeo

    ############################################# Load SHiP modules ####################################################

    run = ROOT.FairRunSim()
    modules = shipDet_conf.configure(run, ShipGeo)

    ############################################# Load inpur data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file, 'update')
    except:
        print "An error with opening the input data file."
        raise

    sTree = fn.cbmsim
    sTree.Write()

    ############################################# Create hists #########################################################

    h = init_book_hist()

    ########################################## Start Track Pattern Recognition #########################################
    import shipPatRec

    # Init book of hists for the quality measurements
    metrics = {
        'n_hits': [],
        'reconstructible': 0,
        'passed_y12': 0,
        'passed_stereo12': 0,
        'passed_12': 0,
        'passed_y34': 0,
        'passed_stereo34': 0,
        'passed_34': 0,
        'passed_combined': 0,
        'reco_passed': 0,
        'reco_passed_no_clones': 0,
        'frac_y12': [],
        'frac_stereo12': [],
        'frac_12': [],
        'frac_y34': [],
        'frac_stereo34': [],
        'frac_34': [],
        'reco_frac_tot': [],
        'reco_mc_p': [],
        'reco_mc_theta': [],
        'fitted_p': [],
        'fitted_pval': [],
        'fitted_chi': [],
        'fitted_x': [],
        'fitted_y': [],
        'fitted_z': [],
        'fitted_mass': []
    }

    # Start event loop
    nEvents = sTree.GetEntries()

    for iEvent in range(nEvents):

        if iEvent % 1000 == 0:
            print 'Event ', iEvent

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ########################################### Reconstructible tracks #############################################

        reconstructible_tracks = getReconstructibleTracks(
            iEvent, sTree, sGeo, ShipGeo)

        metrics['reconstructible'] += len(reconstructible_tracks)
        for i_reco in reconstructible_tracks:
            h['TracksPassed'].Fill("Reconstructible tracks", 1)
            h['TracksPassedU'].Fill("Reconstructible tracks", 1)

        in_y12 = []
        in_stereo12 = []
        in_12 = []
        in_y34 = []
        in_stereo34 = []
        in_34 = []
        in_combo = []

        ########################################## Recognized tracks ###################################################

        nTracklets = sTree.Tracklets.GetEntriesFast()

        for i_track in range(nTracklets):

            atracklet = sTree.Tracklets[i_track]

            if atracklet.getType() != 1:  # this is a not full track (tracklet)
                continue

            atrack = atracklet.getList()

            if atrack.size() == 0:
                continue

            hits = {
                'X': [],
                'Y': [],
                'Z': [],
                'DetID': [],
                'TrackID': [],
                'Pz': [],
                'Px': [],
                'Py': [],
                'dist2Wire': [],
                'Pdg': []
            }

            for ihit in atrack:
                ahit = sTree.strawtubesPoint[ihit]
                hits['X'] += [ahit.GetX()]
                hits['Y'] += [ahit.GetY()]
                hits['Z'] += [ahit.GetZ()]
                hits['DetID'] += [ahit.GetDetectorID()]
                hits['TrackID'] += [ahit.GetTrackID()]
                hits['Pz'] += [ahit.GetPz()]
                hits['Px'] += [ahit.GetPx()]
                hits['Py'] += [ahit.GetPy()]
                hits['dist2Wire'] += [ahit.dist2Wire()]
                hits['Pdg'] += [ahit.PdgCode()]

            # List to numpy arrays
            for key in hits.keys():
                hits[key] = numpy.array(hits[key])

            # Decoding
            statnb, vnb, pnb, lnb, snb = decodeDetectorID(hits['DetID'])
            is_stereo = ((vnb == 1) + (vnb == 2))
            is_y = ((vnb == 0) + (vnb == 3))
            is_before = ((statnb == 1) + (statnb == 2))
            is_after = ((statnb == 3) + (statnb == 4))

            # Metrics
            metrics['n_hits'] += [get_n_hits(hits['TrackID'])]

            # Tracks passed
            frac_y12, tmax_y12 = fracMCsame(hits['TrackID'][is_before * is_y])
            n_hits_y12 = get_n_hits(hits['TrackID'][is_before * is_y])
            frac_stereo12, tmax_stereo12 = fracMCsame(
                hits['TrackID'][is_before * is_stereo])
            n_hits_stereo12 = get_n_hits(hits['TrackID'][is_before *
                                                         is_stereo])
            frac_12, tmax_12 = fracMCsame(hits['TrackID'][is_before])
            n_hits_12 = get_n_hits(hits['TrackID'][is_before])

            frac_y34, tmax_y34 = fracMCsame(hits['TrackID'][is_after * is_y])
            n_hits_y34 = get_n_hits(hits['TrackID'][is_after * is_y])
            frac_stereo34, tmax_stereo34 = fracMCsame(
                hits['TrackID'][is_after * is_stereo])
            n_hits_stereo34 = get_n_hits(hits['TrackID'][is_after * is_stereo])
            frac_34, tmax_34 = fracMCsame(hits['TrackID'][is_after])
            n_hits_34 = get_n_hits(hits['TrackID'][is_after])
            frac_tot, tmax_tot = fracMCsame(hits['TrackID'])
            n_hits_tot = get_n_hits(hits['TrackID'])

            is_reconstructed = 0
            is_reconstructed_no_clones = 0

            if tmax_y12 in reconstructible_tracks:
                metrics['passed_y12'] += 1
                metrics['frac_y12'] += [frac_y12]
                h['TracksPassed'].Fill("Y view station 1&2", 1)
                if tmax_y12 not in in_y12:
                    h['TracksPassedU'].Fill("Y view station 1&2", 1)
                    in_y12.append(tmax_y12)

                if tmax_stereo12 == tmax_y12:
                    metrics['passed_stereo12'] += 1
                    metrics['frac_stereo12'] += [frac_stereo12]
                    h['TracksPassed'].Fill("Stereo station 1&2", 1)
                    if tmax_stereo12 not in in_stereo12:
                        h['TracksPassedU'].Fill("Stereo station 1&2", 1)
                        in_stereo12.append(tmax_stereo12)

                    if tmax_12 == tmax_y12:
                        metrics['passed_12'] += 1
                        metrics['frac_12'] += [frac_12]
                        h['TracksPassed'].Fill("station 1&2", 1)
                        if tmax_12 not in in_12:
                            h['TracksPassedU'].Fill("station 1&2", 1)
                            in_12.append(tmax_12)

                        if tmax_y34 in reconstructible_tracks:
                            metrics['passed_y34'] += 1
                            metrics['frac_y34'] += [frac_y34]
                            h['TracksPassed'].Fill("Y view station 3&4", 1)
                            if tmax_y34 not in in_y34:
                                h['TracksPassedU'].Fill(
                                    "Y view station 3&4", 1)
                                in_y34.append(tmax_y34)

                            if tmax_stereo34 == tmax_y34:
                                metrics['passed_stereo34'] += 1
                                metrics['frac_stereo34'] += [frac_stereo34]
                                h['TracksPassed'].Fill("Stereo station 3&4", 1)
                                if tmax_stereo34 not in in_stereo34:
                                    h['TracksPassedU'].Fill(
                                        "Stereo station 3&4", 1)
                                    in_stereo34.append(tmax_stereo34)

                                if tmax_34 == tmax_y34:
                                    metrics['passed_34'] += 1
                                    metrics['frac_34'] += [frac_34]
                                    h['TracksPassed'].Fill("station 3&4", 1)
                                    if tmax_34 not in in_34:
                                        h['TracksPassedU'].Fill(
                                            "station 3&4", 1)
                                        in_34.append(tmax_34)

                                if tmax_12 == tmax_34:
                                    metrics['passed_combined'] += 1
                                    h['TracksPassed'].Fill(
                                        "Combined stations 1&2/3&4", 1)
                                    metrics['reco_passed'] += 1
                                    is_reconstructed = 1
                                    if tmax_34 not in in_combo:
                                        h['TracksPassedU'].Fill(
                                            "Combined stations 1&2/3&4", 1)
                                        metrics['reco_passed_no_clones'] += 1
                                        in_combo.append(tmax_34)
                                        is_reconstructed_no_clones = 1

            # For reconstructed tracks
            if is_reconstructed == 0:
                continue

            metrics['reco_frac_tot'] += [frac_tot]

            # Momentum
            Pz = hits['Pz']
            Px = hits['Px']
            Py = hits['Py']
            P = numpy.sqrt(Pz**2 + Px**2 + Py**2)
            P = P[hits['TrackID'] == tmax_tot]
            p = numpy.mean(P)

            metrics['reco_mc_p'] += [p]
            h['TracksPassed_p'].Fill(p, 1)

            # Direction
            Z = hits['Z'][(hits['TrackID'] == tmax_tot) * is_before]
            X = hits['X'][(hits['TrackID'] == tmax_tot) * is_before]
            Y = hits['Y'][(hits['TrackID'] == tmax_tot) * is_before]
            Z = Z - Z[0]
            X = X - X[0]
            Y = Y - Y[0]
            R = numpy.sqrt(X**2 + Y**2 + Z**2)
            Theta = numpy.arccos(Z[1:] / R[1:])
            theta = numpy.mean(Theta)

            metrics['reco_mc_theta'] += [theta]

            h['n_hits_reco_y12'].Fill(n_hits_y12)
            h['n_hits_reco_stereo12'].Fill(n_hits_stereo12)
            h['n_hits_reco_12'].Fill(n_hits_12)
            h['n_hits_reco_y34'].Fill(n_hits_y34)
            h['n_hits_reco_stereo34'].Fill(n_hits_stereo34)
            h['n_hits_reco_34'].Fill(n_hits_34)
            h['n_hits_reco'].Fill(n_hits_tot)

            h['n_hits_y12'].Fill(p, n_hits_y12)
            h['n_hits_stereo12'].Fill(p, n_hits_stereo12)
            h['n_hits_12'].Fill(p, n_hits_12)
            h['n_hits_y34'].Fill(p, n_hits_y34)
            h['n_hits_stereo34'].Fill(p, n_hits_stereo34)
            h['n_hits_34'].Fill(p, n_hits_34)
            h['n_hits_total'].Fill(p, n_hits_tot)

            h['frac_y12'].Fill(p, frac_y12)
            h['frac_stereo12'].Fill(p, frac_stereo12)
            h['frac_12'].Fill(p, frac_12)
            h['frac_y34'].Fill(p, frac_y34)
            h['frac_stereo34'].Fill(p, frac_stereo34)
            h['frac_34'].Fill(p, frac_34)
            h['frac_total'].Fill(p, frac_tot)

            # Fitted track

            thetrack = sTree.FitTracks[i_track]

            fitStatus = thetrack.getFitStatus()
            thetrack.prune(
                "CFL"
            )  # http://sourceforge.net/p/genfit/code/HEAD/tree/trunk/core/include/Track.h#l280

            nmeas = fitStatus.getNdf()
            pval = fitStatus.getPVal()
            chi2 = fitStatus.getChi2() / nmeas

            metrics['fitted_pval'] += [pval]
            metrics['fitted_chi'] += [chi2]

            h['chi2fittedtracks'].Fill(chi2)
            h['pvalfittedtracks'].Fill(pval)

            fittedState = thetrack.getFittedState()
            fittedMom = fittedState.getMomMag()
            fittedMom = fittedMom  #*int(charge)

            metrics['fitted_p'] += [fittedMom]
            perr = (p - fittedMom) / p
            h['ptrue-p/ptrue'].Fill(perr)
            h['perr'].Fill(p, perr)
            h['perr_direction'].Fill(numpy.rad2deg(theta), perr)

            if math.fabs(p) > 0.0:
                h['pvspfitted'].Fill(p, fittedMom)
            fittedtrackDir = fittedState.getDir()
            fittedx = math.degrees(math.acos(fittedtrackDir[0]))
            fittedy = math.degrees(math.acos(fittedtrackDir[1]))
            fittedz = math.degrees(math.acos(fittedtrackDir[2]))
            fittedmass = fittedState.getMass()
            h['momentumfittedtracks'].Fill(fittedMom)
            h['xdirectionfittedtracks'].Fill(fittedx)
            h['ydirectionfittedtracks'].Fill(fittedy)
            h['zdirectionfittedtracks'].Fill(fittedz)
            h['massfittedtracks'].Fill(fittedmass)

            metrics['fitted_x'] += [fittedx]
            metrics['fitted_y'] += [fittedy]
            metrics['fitted_z'] += [fittedz]
            metrics['fitted_mass'] += [fittedmass]

    ############################################# Save hists #########################################################

    save_hists(h, output_file)

    return metrics
Exemplo n.º 23
0
# ========================================================================

# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)  # Transport engine
run.SetOutputFile(outFile)  # Output file
run.SetUserConfig("g4Config.C") # user configuration file default g4Config.C 
rtdb = run.GetRuntimeDb() 
# -----Create geometry----------------------------------------------
import shipDet_conf
shipDet_conf.configure(run,ship_geo)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()

if simEngine == "Pythia8":
 primGen.SetTarget(ship_geo.target.z0, 0.) 
# -----Pythia8--------------------------------------
 P8gen = ROOT.HNLPythia8Generator()
 import pythia8_conf
 pythia8_conf.configure(P8gen,inclusive,deepCopy)
 primGen.AddGenerator(P8gen)
if simEngine == "Pythia6":
# set muon interaction close to decay volume
 primGen.SetTarget(ship_geo.target.z0+ship_geo.muShield.length, 0.) 
# -----Pythia6-------------------------
 P6gen = ROOT.tPythia6Generator()
Exemplo n.º 24
0
try:
  dy = float( tmp[1]+'.'+tmp[2] )
except:
  dy = 10.

from array import array # gymnastic required by Ecal 
import rootUtils as ut
import shipunit as u
PDG = ROOT.TDatabasePDG.Instance()
from ShipGeoConfig import ConfigRegistry
# init geometry and mag. field
ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", Yheight = dy )
# -----Create geometry----------------------------------------------
import shipDet_conf
run = ROOT.FairRunSim()
modules = shipDet_conf.configure(run,ShipGeo)

ecal = modules['Ecal']
EcalX = array('f',[0.])
EcalY = array('f',[0.])
EcalE = array('i',[0])

rz_inter = -1.,0.
def origin(sTree,it):
 at = sTree.MCTrack[it]
 im = at.GetMotherId()
 if im>0: origin(sTree,im)
 if im<0: 
   # print 'does not come from muon'
   rz_inter = -1.,0.
 if im==0: 
Exemplo n.º 25
0
def run_track_pattern_recognition(input_file, geo_file, output_file, method):
    """
    Runs all steps of track pattern recognition.
    Parameters
    ----------
    input_file : string
        Path to an input .root file with events.
    geo_file : string
        Path to a file with SHiP geometry.
    output_file : string
        Path to an output .root file with quality plots.
    method : string
        Name of a track pattern recognition method.
    """


    ############################################# Load SHiP geometry ###################################################

    # Check geo file
    try:
        fgeo = ROOT.TFile(geo_file)
    except:
        print "An error with opening the ship geo file."
        raise

    sGeo = fgeo.FAIRGeom

    # Prepare ShipGeo dictionary
    if not fgeo.FindKey('ShipGeo'):

        if sGeo.GetVolume('EcalModule3') :
            ecalGeoFile = "ecal_ellipse6x12m2.geo"
        else:
            ecalGeoFile = "ecal_ellipse5x10m2.geo"

        if dy:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", Yheight = dy, EcalGeoFile = ecalGeoFile)
        else:
            ShipGeo = ConfigRegistry.loadpy("$FAIRSHIP/geometry/geometry_config.py", EcalGeoFile = ecalGeoFile)

    else:
        upkl    = Unpickler(fgeo)
        ShipGeo = upkl.load('ShipGeo')
    
    # Globals
    builtin.ShipGeo = ShipGeo

    ############################################# Load SHiP modules ####################################################

    import shipDet_conf
    run = ROOT.FairRunSim()
    run.SetName("TGeant4")  # Transport engine
    run.SetOutputFile("dummy")  # Output file
    run.SetUserConfig("g4Config_basic.C") # geant4 transport not used, only needed for the mag field
    rtdb = run.GetRuntimeDb()

    modules = shipDet_conf.configure(run,ShipGeo)
    run.Init()

    #run = ROOT.FairRunSim()
    #modules = shipDet_conf.configure(run,ShipGeo)


    ######################################### Load SHiP magnetic field #################################################


    import geomGeant4
    if hasattr(ShipGeo.Bfield,"fieldMap"):
      fieldMaker = geomGeant4.addVMCFields(ShipGeo, '', True, withVirtualMC = False)
    else:
      print "no fieldmap given, geofile too old, not anymore support"
      exit(-1)
    sGeo   = fgeo.FAIRGeom
    geoMat =  ROOT.genfit.TGeoMaterialInterface()
    ROOT.genfit.MaterialEffects.getInstance().init(geoMat)
    bfield = ROOT.genfit.FairShipFields()
    bfield.setField(fieldMaker.getGlobalField())
    fM = ROOT.genfit.FieldManager.getInstance()
    fM.init(bfield)


    ############################################# Load inpur data file #################################################

    # Check input file
    try:
        fn = ROOT.TFile(input_file,'update')
    except:
        print "An error with opening the input data file."
        raise

    sTree = fn.cbmsim
    sTree.Write()
    
    ############################################# Create hists #########################################################
    
    h = init_book_hist()

    ########################################## Start Track Pattern Recognition #########################################
    import shipPatRec

    # Init book of hists for the quality measurements
    metrics = {'n_hits': [], 
               'reconstructible': 0, 
               'passed_y12': 0, 'passed_stereo12': 0, 'passed_12': 0, 
               'passed_y34': 0, 'passed_stereo34': 0, 'passed_34': 0, 
               'passed_combined': 0, 'reco_passed': 0, 'reco_passed_no_clones': 0,
               'frac_y12': [], 'frac_stereo12': [], 'frac_12': [], 
               'frac_y34': [], 'frac_stereo34': [], 'frac_34': [], 
               'reco_frac_tot': [], 
               'reco_mc_p': [], 'reco_mc_theta': [], 
               'fitted_p': [], 'fitted_pval': [], 'fitted_chi': [], 
               'fitted_x': [], 'fitted_y': [], 'fitted_z': [], 'fitted_mass': []}

    # Start event loop
    nEvents   = sTree.GetEntries()
   

    for iEvent in range(nEvents):

        if iEvent%1000 == 0:
            print 'Event ', iEvent

        ########################################### Select one event ###################################################

        rc = sTree.GetEvent(iEvent)

        ########################################### Reconstructible tracks #############################################

        reconstructible_tracks = getReconstructibleTracks(iEvent, sTree, sGeo, ShipGeo)
        
        metrics['reconstructible'] += len(reconstructible_tracks)
        for i_reco in reconstructible_tracks:
            h['TracksPassed'].Fill("Reconstructible tracks", 1)
            h['TracksPassedU'].Fill("Reconstructible tracks", 1)
            
        in_y12 = []
        in_stereo12 = []
        in_12 = []
        in_y34 = []
        in_stereo34 = []
        in_34 = []
        in_combo = []

        found_track_ids = []
        n_tracks = len(reconstructible_tracks)
        n_recognized = 0
        n_clones = 0
        n_ghosts = 0
        n_others = 0

        min_eff = 0.
        
        ########################################## Recognized tracks ###################################################
        
        nTracklets = sTree.Tracklets.GetEntriesFast()
        
        for i_track in range(nTracklets):
            
            atracklet = sTree.Tracklets[i_track]
            
            if atracklet.getType() != 1: # this is a not full track (tracklet)
                continue
                
            atrack = atracklet.getList()
            
            if atrack.size() == 0:
                continue
            
            hits = {'X': [], 'Y': [], 'Z': [], 
                    'DetID': [], 'TrackID': [], 
                    'Pz': [], 'Px': [], 'Py': [], 
                    'dist2Wire': [], 'Pdg': []}
            
            for ihit in atrack:
                ahit = sTree.strawtubesPoint[ihit]
                hits['X'] += [ahit.GetX()]
                hits['Y'] += [ahit.GetY()]
                hits['Z'] += [ahit.GetZ()]
                hits['DetID'] += [ahit.GetDetectorID()]
                hits['TrackID'] += [ahit.GetTrackID()]
                hits['Pz'] += [ahit.GetPz()]
                hits['Px'] += [ahit.GetPx()]
                hits['Py'] += [ahit.GetPy()]
                hits['dist2Wire'] += [ahit.dist2Wire()]
                hits['Pdg'] += [ahit.PdgCode()]
                
            # List to numpy arrays
            for key in hits.keys():
                hits[key] = numpy.array(hits[key])
                
            # Decoding
            statnb, vnb, pnb, lnb, snb = decodeDetectorID(hits['DetID'])
            is_stereo = ((vnb == 1) + (vnb == 2))
            is_y = ((vnb == 0) + (vnb == 3))
            is_before = ((statnb == 1) + (statnb == 2))
            is_after = ((statnb == 3) + (statnb == 4)) 
                
            
            # Metrics
            metrics['n_hits'] += [get_n_hits(hits['TrackID'])]
            
            # Tracks passed
            frac_y12, tmax_y12 = fracMCsame(hits['TrackID'][is_before * is_y])
            n_hits_y12 = get_n_hits(hits['TrackID'][is_before * is_y])
            frac_stereo12, tmax_stereo12 = fracMCsame(hits['TrackID'][is_before * is_stereo])
            n_hits_stereo12 = get_n_hits(hits['TrackID'][is_before * is_stereo])
            frac_12, tmax_12 = fracMCsame(hits['TrackID'][is_before])
            n_hits_12 = get_n_hits(hits['TrackID'][is_before])
            
            frac_y34, tmax_y34 = fracMCsame(hits['TrackID'][is_after * is_y])
            n_hits_y34 = get_n_hits(hits['TrackID'][is_after * is_y])
            frac_stereo34, tmax_stereo34 = fracMCsame(hits['TrackID'][is_after * is_stereo])
            n_hits_stereo34 = get_n_hits(hits['TrackID'][is_after * is_stereo])
            frac_34, tmax_34 = fracMCsame(hits['TrackID'][is_after])
            n_hits_34 = get_n_hits(hits['TrackID'][is_after])
            frac_tot, tmax_tot = fracMCsame(hits['TrackID'])
            n_hits_tot = get_n_hits(hits['TrackID'])

            if tmax_y12 == tmax_stereo12 and tmax_y12 == tmax_y34 and tmax_y12 == tmax_stereo34:
                if frac_y12 >= min_eff and frac_stereo12 >= min_eff and frac_y34 >= min_eff and frac_stereo34 >= min_eff:

                    if tmax_y12 in reconstructible_tracks and tmax_y12 not in found_track_ids:
                        n_recognized += 1
                        found_track_ids.append(tmax_y12)
                    elif tmax_y12 in reconstructible_tracks and tmax_y12 in found_track_ids:
                        n_clones += 1
                    elif tmax_y12 not in reconstructible_tracks:
                        n_others += 1

                else:
                    n_ghosts += 1
            else:
                n_ghosts += 1
            
            is_reconstructed = 0
            is_reconstructed_no_clones = 0
            
            
            if tmax_y12 in reconstructible_tracks:
                metrics['passed_y12'] += 1
                metrics['frac_y12'] += [frac_y12]
                h['TracksPassed'].Fill("Y view station 1&2", 1)
                if tmax_y12 not in in_y12:
                    h['TracksPassedU'].Fill("Y view station 1&2", 1)
                    in_y12.append(tmax_y12)
                
                if tmax_stereo12 == tmax_y12:
                    metrics['passed_stereo12'] += 1
                    metrics['frac_stereo12'] += [frac_stereo12]
                    h['TracksPassed'].Fill("Stereo station 1&2", 1)
                    if tmax_stereo12 not in in_stereo12:
                        h['TracksPassedU'].Fill("Stereo station 1&2", 1)
                        in_stereo12.append(tmax_stereo12)
                    
                    if tmax_12 == tmax_y12:
                        metrics['passed_12'] += 1
                        metrics['frac_12'] += [frac_12]
                        h['TracksPassed'].Fill("station 1&2", 1)
                        if tmax_12 not in in_12:
                            h['TracksPassedU'].Fill("station 1&2", 1)
                            in_12.append(tmax_12)
                        
                        if tmax_y34 in reconstructible_tracks:
                            metrics['passed_y34'] += 1
                            metrics['frac_y34'] += [frac_y34]
                            h['TracksPassed'].Fill("Y view station 3&4", 1)
                            if tmax_y34 not in in_y34:
                                h['TracksPassedU'].Fill("Y view station 3&4", 1)
                                in_y34.append(tmax_y34)
                            
                            if tmax_stereo34 == tmax_y34:
                                metrics['passed_stereo34'] += 1
                                metrics['frac_stereo34'] += [frac_stereo34]
                                h['TracksPassed'].Fill("Stereo station 3&4", 1)
                                if tmax_stereo34 not in in_stereo34:
                                    h['TracksPassedU'].Fill("Stereo station 3&4", 1)
                                    in_stereo34.append(tmax_stereo34)
                                
                                if tmax_34 == tmax_y34:
                                    metrics['passed_34'] += 1
                                    metrics['frac_34'] += [frac_34]
                                    h['TracksPassed'].Fill("station 3&4", 1)
                                    if tmax_34 not in in_34:
                                        h['TracksPassedU'].Fill("station 3&4", 1)
                                        in_34.append(tmax_34)
                                    
                                    if tmax_12 == tmax_34:
                                        metrics['passed_combined'] += 1
                                        h['TracksPassed'].Fill("Combined stations 1&2/3&4", 1)
                                        metrics['reco_passed'] += 1
                                        is_reconstructed = 1
                                        if tmax_34 not in in_combo:
                                            h['TracksPassedU'].Fill("Combined stations 1&2/3&4", 1)
                                            metrics['reco_passed_no_clones'] += 1
                                            in_combo.append(tmax_34)
                                            is_reconstructed_no_clones = 1
                                        
            # For reconstructed tracks
            if is_reconstructed == 0:
                continue

            metrics['reco_frac_tot'] += [frac_tot]
            
            # Momentum
            Pz = hits['Pz']
            Px = hits['Px']
            Py = hits['Py']

            p, px, py, pz = getPtruthFirst(sTree, tmax_tot)
            pt = math.sqrt(px**2 + py**2)

            Z_true = []
            X_true = []
            Y_true = []
            for ahit in sTree.strawtubesPoint:
                if ahit.GetTrackID() == tmax_tot:
                    az, ax, ay = ahit.GetZ(),ahit.GetX(),ahit.GetY()
                    Z_true.append(az)
                    X_true.append(ax)
                    Y_true.append(ay)

            
            metrics['reco_mc_p'] += [p]
            h['TracksPassed_p'].Fill(p, 1)
            
            # Direction
            Z = hits['Z'][(hits['TrackID'] == tmax_tot) * is_before]
            X = hits['X'][(hits['TrackID'] == tmax_tot) * is_before]
            Y = hits['Y'][(hits['TrackID'] == tmax_tot) * is_before]
            Z = Z - Z[0]
            X = X - X[0]
            Y = Y - Y[0]
            R = numpy.sqrt(X**2 + Y**2 + Z**2)
            Theta = numpy.arccos(Z[1:] / R[1:])
            theta = numpy.mean(Theta)
            
            metrics['reco_mc_theta'] += [theta]
            
            h['n_hits_reco_y12'].Fill(n_hits_y12)
            h['n_hits_reco_stereo12'].Fill(n_hits_stereo12)
            h['n_hits_reco_12'].Fill(n_hits_12)
            h['n_hits_reco_y34'].Fill(n_hits_y34)
            h['n_hits_reco_stereo34'].Fill(n_hits_stereo34)
            h['n_hits_reco_34'].Fill(n_hits_34)
            h['n_hits_reco'].Fill(n_hits_tot)
            
            h['n_hits_y12'].Fill(p, n_hits_y12)
            h['n_hits_stereo12'].Fill(p, n_hits_stereo12)
            h['n_hits_12'].Fill(p, n_hits_12)
            h['n_hits_y34'].Fill(p, n_hits_y34)
            h['n_hits_stereo34'].Fill(p, n_hits_stereo34)
            h['n_hits_34'].Fill(p, n_hits_34)
            h['n_hits_total'].Fill(p, n_hits_tot)
            
            h['frac_y12'].Fill(p, frac_y12)
            h['frac_stereo12'].Fill(p, frac_stereo12)
            h['frac_12'].Fill(p, frac_12)
            h['frac_y34'].Fill(p, frac_y34)
            h['frac_stereo34'].Fill(p, frac_stereo34)
            h['frac_34'].Fill(p, frac_34)
            h['frac_total'].Fill(p, frac_tot)

            h['frac_y12_dist'].Fill(frac_y12)
            h['frac_stereo12_dist'].Fill(frac_stereo12)
            h['frac_12_dist'].Fill(frac_12)
            h['frac_y34_dist'].Fill(frac_y34)
            h['frac_stereo34_dist'].Fill(frac_stereo34)
            h['frac_34_dist'].Fill(frac_34)
            h['frac_total_dist'].Fill(frac_tot)
            
            # Fitted track
            
            thetrack = sTree.FitTracks[i_track]
            
            fitStatus   = thetrack.getFitStatus()
            thetrack.prune("CFL") # http://sourceforge.net/p/genfit/code/HEAD/tree/trunk/core/include/Track.h#l280

            nmeas = fitStatus.getNdf()
            pval = fitStatus.getPVal()
            chi2 = fitStatus.getChi2() / nmeas
            
            metrics['fitted_pval'] += [pval]
            metrics['fitted_chi'] += [chi2]

            h['chi2fittedtracks'].Fill(chi2)
            h['pvalfittedtracks'].Fill(pval)


            try:

                fittedState = thetrack.getFittedState()
                fittedMom = fittedState.getMomMag()
                fittedMom = fittedMom #*int(charge)

                px_fit,py_fit,pz_fit = fittedState.getMom().x(),fittedState.getMom().y(),fittedState.getMom().z()
                p_fit = fittedMom
                pt_fit = math.sqrt(px_fit**2 + py_fit**2)


                metrics['fitted_p'] += [p_fit]
                perr = (p - p_fit) / p
                h['ptrue-p/ptrue'].Fill(perr)
                h['perr'].Fill(p, perr)
                h['perr_direction'].Fill(numpy.rad2deg(theta), perr)

                pterr = (pt - pt_fit) / pt
                h['pttrue-pt/pttrue'].Fill(pterr)

                pxerr = (px - px_fit) / px
                h['pxtrue-px/pxtrue'].Fill(pxerr)

                pyerr = (py - py_fit) / py
                h['pytrue-py/pytrue'].Fill(pyerr)

                pzerr = (pz - pz_fit) / pz
                h['pztrue-pz/pztrue'].Fill(pzerr)

                if math.fabs(p) > 0.0 :
                    h['pvspfitted'].Fill(p, fittedMom)
                fittedtrackDir = fittedState.getDir()
                fittedx=math.degrees(math.acos(fittedtrackDir[0]))
                fittedy=math.degrees(math.acos(fittedtrackDir[1]))
                fittedz=math.degrees(math.acos(fittedtrackDir[2]))
                fittedmass = fittedState.getMass()
                h['momentumfittedtracks'].Fill(fittedMom)
                h['xdirectionfittedtracks'].Fill(fittedx)
                h['ydirectionfittedtracks'].Fill(fittedy)
                h['zdirectionfittedtracks'].Fill(fittedz)
                h['massfittedtracks'].Fill(fittedmass)

                metrics['fitted_x'] += [fittedx]
                metrics['fitted_y'] += [fittedy]
                metrics['fitted_z'] += [fittedz]
                metrics['fitted_mass'] += [fittedmass]


                Z_fit = []
                X_fit = []
                Y_fit = []
                for az in Z_true:
                    rc,pos,mom = extrapolateToPlane(thetrack, az)
                    Z_fit.append(pos.Z())
                    X_fit.append(pos.X())
                    Y_fit.append(pos.Y())

                for i in range(len(Z_true)):
                    xerr = abs(X_fit[i] - X_true[i])
                    yerr = abs(Y_fit[i] - Y_true[i])
                    h['abs(x - x-true)'].Fill(xerr)
                    h['abs(y - y-true)'].Fill(yerr)

                rmse_x = numpy.sqrt(numpy.mean((numpy.array(X_fit) - numpy.array(X_true))**2))
                rmse_y = numpy.sqrt(numpy.mean((numpy.array(Y_fit) - numpy.array(Y_true))**2))
                h['rmse_x'].Fill(rmse_x)
                h['rmse_y'].Fill(rmse_y)



            except:
                print "Problem with fitted state."


        h['Reco_tracks'].Fill("N total", n_tracks)
        h['Reco_tracks'].Fill("N recognized tracks", n_recognized)
        h['Reco_tracks'].Fill("N clones", n_clones)
        h['Reco_tracks'].Fill("N ghosts", n_ghosts)
        h['Reco_tracks'].Fill("N others", n_others)
            
    ############################################# Save hists #########################################################
    
    save_hists(h, output_file)

    return metrics
Exemplo n.º 26
0
upkl = Unpickler(fgeo)
ShipGeo = upkl.load('ShipGeo')
ecalGeoFile = ShipGeo.ecal.File
dy = ShipGeo.Yheight / u.m

# -----Create geometry----------------------------------------------
import shipDet_conf
run = ROOT.FairRunSim()
run.SetName("TGeant4")  # Transport engine
run.SetOutputFile("dummy")  # Output file
run.SetUserConfig("g4Config_basic.C"
                  )  # geant4 transport not used, only needed for the mag field
rtdb = run.GetRuntimeDb()
# -----Create geometry----------------------------------------------
print('a')
modules = shipDet_conf.configure(run, ShipGeo)
print('b')
run.Init()
print('c')
import geomGeant4
print('d')
if hasattr(ShipGeo.Bfield, "fieldMap"):
    fieldMaker = geomGeant4.addVMCFields(ShipGeo, '', True)
print('e')
sGeo = ROOT.gGeoManager
print('f')
geoMat = ROOT.genfit.TGeoMaterialInterface()
print('g')
ROOT.genfit.MaterialEffects.getInstance().init(geoMat)
print('h')
bfield = ROOT.genfit.FairShipFields()
Exemplo n.º 27
0
# ========================================================================

# -----Timer--------------------------------------------------------
timer = ROOT.TStopwatch()
timer.Start()
# ------------------------------------------------------------------------

# -----Create simulation run----------------------------------------
run = ROOT.FairRunSim()
run.SetName(mcEngine)  # Transport engine
run.SetOutputFile(outFile)  # Output file
run.SetUserConfig("g4Config.C") # user configuration file default g4Config.C 
rtdb = run.GetRuntimeDb() 
# -----Create geometry----------------------------------------------
import shipDet_conf
shipDet_conf.configure(run,ship_geo)
# -----Create PrimaryGenerator--------------------------------------
primGen = ROOT.FairPrimaryGenerator()

if simEngine == "Pythia8":
 primGen.SetTarget(ship_geo.target.z0, 0.) 
# -----Pythia8--------------------------------------
 P8gen = ROOT.Pythia8Generator()
 import pythia8_conf
 pythia8_conf.configure(P8gen,inclusive,deepCopy)
 primGen.AddGenerator(P8gen)
if simEngine == "Pythia6":
# set muon interaction close to decay volume
 primGen.SetTarget(ship_geo.target.z0+ship_geo.muShield.length, 0.) 
# -----Pythia6-------------------------
 P6gen = ROOT.tPythia6Generator()