示例#1
0
    def __init__(self, options):
        self.rebin = options.rebin
        self.options = options
        self.pedfile_fullres_name = options.pedfile_fullres_name
        self.tmpname = options.tmpname
        geometryPSet = open(
            'modules_config/geometry_{det}.txt'.format(det=options.geometry),
            'r')
        geometryParams = eval(geometryPSet.read())
        self.cg = cameraGeometry(geometryParams)
        self.xmax = self.cg.npixx

        if not os.path.exists(self.pedfile_fullres_name):
            print("WARNING: pedestal file with full resolution ",
                  self.pedfile_fullres_name,
                  " not existing. First calculate them...")
            self.calcPedestal(options, 1)
        if not options.justPedestal:
            print("Pulling pedestals...")
            # first the one for clustering with rebin
            ctools = cameraTools(self.cg)
            # then the full resolution one
            pedrf_fr = ROOT.TFile.Open(self.pedfile_fullres_name)
            self.pedmap_fr = pedrf_fr.Get('pedmap').Clone()
            self.pedmap_fr.SetDirectory(0)
            self.pedarr_fr = hist2array(self.pedmap_fr).T
            self.noisearr_fr = ctools.noisearray(self.pedmap_fr).T
            pedrf_fr.Close()
            if options.vignetteCorr:
                self.vignmap = ctools.loadVignettingMap()
            else:
                self.vignmap = np.ones((self.xmax, self.xmax))
示例#2
0
 def __init__(self, img, img_fr, img_fr_zs, img_ori, name, options):
     self.name = name
     self.options = options
     self.rebin = options.rebin
     ct = cameraTools()
     self.image = img
     self.img_ori = img_ori
     self.imagelog = np.zeros((self.image.shape[0], self.image.shape[1]))
     for (x, y), value in np.ndenumerate(self.image):
         if value > 3.0 / math.sqrt(
                 self.rebin):  # tresholding needed for tracking
             self.imagelog[x, y] = math.log(value)
     self.image_fr = img_fr
     self.image_fr_zs = img_fr_zs
     self.contours = []
示例#3
0
 def __init__(self,options):
     self.xmax = 2048
     self.rebin = options.rebin        
     self.options = options
     self.pedfile_fullres_name = options.pedfile_fullres_name
     self.tmpname = options.tmpname
     
     if not os.path.exists(self.pedfile_fullres_name):
         print("WARNING: pedestal file with full resolution ",self.pedfile_fullres_name, " not existing. First calculate them...")
         self.calcPedestal(options,1)
     if not options.justPedestal:
        print("Pulling pedestals...")
        # first the one for clustering with rebin
        ctools = cameraTools()
        # then the full resolution one
        pedrf_fr = ROOT.TFile.Open(self.pedfile_fullres_name)
        self.pedmap_fr = pedrf_fr.Get('pedmap').Clone()
        self.pedmap_fr.SetDirectory(0)
        self.pedarr_fr = hist2array(self.pedmap_fr)
        self.noisearr_fr = ctools.noisearray(self.pedmap_fr)
        pedrf_fr.Close()
示例#4
0
    def reconstruct(self, evrange=(-1, -1, -1)):

        ROOT.gROOT.Macro('rootlogon.C')
        ROOT.gStyle.SetOptStat(0)
        ROOT.gStyle.SetPalette(ROOT.kRainBow)
        savErrorLevel = ROOT.gErrorIgnoreLevel
        ROOT.gErrorIgnoreLevel = ROOT.kWarning

        tf = sw.swift_read_root_file(self.tmpname)
        #tf = ROOT.TFile.Open(self.rfile)
        #c1 = ROOT.TCanvas('c1','',600,600)
        ctools = cameraTools()
        print("Reconstructing event range: ", evrange[1], "-", evrange[2])
        # loop over events (pictures)
        for iobj, key in enumerate(tf.GetListOfKeys()):
            iev = iobj if self.options.daq != 'midas' else int(
                iobj / 2)  # when PMT is present
            #print("max entries = ",self.options.maxEntries)
            if self.options.maxEntries > 0 and iev == max(
                    evrange[0], 0) + self.options.maxEntries:
                break
            if sum(evrange[1:]) > -2:
                if iev < evrange[1] or iev > evrange[2]: continue

            name = key.GetName()
            obj = key.ReadObj()

            # Routine to skip some images if needed
            if iev in self.options.excImages: continue

            if self.options.debug_mode == 1 and iev != self.options.ev:
                continue

            if obj.InheritsFrom('TH2'):
                if self.options.daq == 'btf':
                    run, event = (int(
                        name.split('_')[0].split('run')[-1].lstrip("0")),
                                  int(name.split('_')[-1].lstrip("0")))
                elif self.options.daq == 'h5':
                    run, event = (int(name.split('_')[0].split('run')[-1]),
                                  int(name.split('_')[-1]))
                else:
                    run, event = (int(
                        name.split('_')[1].split('run')[-1].lstrip("0")),
                                  int(name.split('_')[-1].split('ev')[-1]))
                print("Processing Run: ", run, "- Event ", event, "...")
                self.outTree.fillBranch("run", run)
                self.outTree.fillBranch("event", event)
                self.outTree.fillBranch("pedestal_run",
                                        int(self.options.pedrun))

            if self.options.camera_mode:
                if obj.InheritsFrom('TH2'):

                    pic_fullres = obj.Clone(obj.GetName() + '_fr')
                    img_fr = hist2array(pic_fullres)

                    # Upper Threshold full image
                    img_cimax = np.where(img_fr < self.options.cimax, img_fr,
                                         0)

                    # zs on full image + saturation correction on full image
                    if self.options.saturation_corr:
                        #print("you are in saturation correction mode")
                        img_fr_sub = ctools.pedsub(img_cimax, self.pedarr_fr)
                        img_fr_satcor = ctools.satur_corr(img_fr_sub)
                        img_fr_zs = ctools.zsfullres(
                            img_fr_satcor,
                            self.noisearr_fr,
                            nsigma=self.options.nsigma)
                        img_rb_zs = ctools.arrrebin(img_fr_zs, self.rebin)

                    # skip saturation and set satcor =img_fr_sub
                    else:
                        #print("you are in poor mode")
                        img_fr_sub = ctools.pedsub(img_cimax, self.pedarr_fr)
                        img_fr_satcor = img_fr_sub
                        img_fr_zs = ctools.zsfullres(
                            img_fr_satcor,
                            self.noisearr_fr,
                            nsigma=self.options.nsigma)
                        img_rb_zs = ctools.arrrebin(img_fr_zs, self.rebin)

                    # Cluster reconstruction on 2D picture
                    algo = 'DBSCAN'
                    if self.options.type in ['beam', 'cosmics']: algo = 'HOUGH'
                    snprod_inputs = {
                        'picture': img_rb_zs,
                        'pictureHD': img_fr_satcor,
                        'picturezsHD': img_fr_zs,
                        'pictureOri': img_fr,
                        'name': name,
                        'algo': algo
                    }
                    plotpy = options.jobs < 2  # for some reason on macOS this crashes in multicore
                    snprod_params = {
                        'snake_qual': 3,
                        'plot2D': False,
                        'plotpy': False,
                        'plotprofiles': False
                    }
                    snprod = SnakesProducer(snprod_inputs, snprod_params,
                                            self.options)
                    clusters, snakes = snprod.run()
                    self.autotree.fillCameraVariables(img_fr_zs)
                    self.autotree.fillClusterVariables(snakes, 'sc')
                    self.autotree.fillClusterVariables(clusters, 'cl')

            if self.options.pmt_mode:
                if obj.InheritsFrom('TGraph'):
                    # PMT waveform reconstruction
                    from waveform import PeakFinder, PeaksProducer
                    wform = tf.Get('wfm_' + '_'.join(name.split('_')[1:]))
                    # sampling was 5 GHz (5/ns). Rebin by 5 (1/ns)
                    pkprod_inputs = {'waveform': wform}
                    pkprod_params = {
                        'threshold':
                        options.
                        threshold,  # min threshold for a signal (baseline is -20 mV)
                        'minPeakDistance':
                        options.
                        minPeakDistance,  # number of samples (1 sample = 1ns )
                        'prominence':
                        options.
                        prominence,  # noise after resampling very small
                        'width':
                        options.width,  # minimal width of the signal
                        'resample':
                        options.resample,  # to sample waveform at 1 GHz only
                        'rangex': (self.options.time_range[0],
                                   self.options.time_range[1]),
                        'plotpy':
                        options.pmt_plotpy
                    }
                    pkprod = PeaksProducer(pkprod_inputs, pkprod_params,
                                           self.options)

                    peaksfinder = pkprod.run()
                    self.autotree.fillPMTVariables(
                        peaksfinder, 0.2 * pkprod_params['resample'])

            # fill reco tree (just once/event, and the TGraph is analyses as last)
            if (self.options.daq == 'midas' and
                    obj.InheritsFrom('TGraph')) or self.options.daq != 'midas':
                self.outTree.fill()

        ROOT.gErrorIgnoreLevel = savErrorLevel
示例#5
0
    def calcVignettingMap(self,
                          run,
                          pedfile,
                          outfile,
                          maxImages=1000,
                          rebin=12,
                          det='lime',
                          daq='midas'):

        ################ GEOMETRY ###
        geometryPSet = open(
            'modules_config/geometry_{det}.txt'.format(det=det), 'r')
        geometryParams = eval(geometryPSet.read())
        cg = cameraGeometry(geometryParams)
        ctools = cameraTools(cg)
        #############################

        # pedestal map, full reso
        pedrf_fr = ROOT.TFile.Open(pedfile)
        pedmap_fr = pedrf_fr.Get('pedmap').Clone()
        pedmap_fr.SetDirectory(0)
        pedarr_fr = hist2array(pedmap_fr).T
        noisearr_fr = ctools.noisearray(pedmap_fr).T
        pedrf_fr.Close()

        outname_base = os.path.basename(outfile).split('.')[0]
        tf_out = ROOT.TFile.Open(outname_base + '.root', 'recreate')

        N = cg.npixx
        nx = int(N / rebin)
        ny = int(N / rebin)
        normmap = ROOT.TH2D('normmap', 'normmap', nx, 0, N, nx, 0, N)

        mapsum = np.zeros((nx, nx))

        USER = os.environ['USER']
        if sw.checkfiletmp(int(run)):
            infile = "/tmp/%s/histograms_Run%05d.root" % (USER, int(run))
        else:
            print('Downloading file: ' + sw.swift_root_file('Data', int(run)))
            infile = sw.swift_download_root_file(
                sw.swift_root_file('Data', int(run)), int(run))

        tf_in = sw.swift_read_root_file(infile)

        framesize = 216 if det == 'lime' else 0

        #files = ["~/Work/data/cygnus/run03930.root","~/Work/data/cygnus/run03931.root","~/Work/data/cygnus/run03932.root"]
        #for f in files:
        tf_in = ROOT.TFile(f)

        # first calculate the mean
        for i, e in enumerate(tf_in.GetListOfKeys()):
            iev = i if daq != 'midas' else i / 2  # when PMT is present

            if maxImages > -1 and i < len(tf_in.GetListOfKeys()) - maxImages:
                continue
            name = e.GetName()
            obj = e.ReadObj()
            if not obj.InheritsFrom('TH2'): continue
            print("Calc pixel sums with event: ", name)
            arr = hist2array(obj)

            # Upper Threshold full image
            #img_cimax = np.where(arr < 300, arr, 0)
            img_cimax = arr
            img_fr_sub = ctools.pedsub(img_cimax, pedarr_fr)
            img_fr_zs = ctools.zsfullres(img_fr_sub, noisearr_fr,
                                         nsigma=1) * 1e-8

            # for lime, remove the borders of the sensor
            if det == 'lime':
                #img_fr_zs[:framesize,:]=0
                #img_fr_zs[-framesize:,:]=0
                img_fr_zs[:, :framesize] = 0
                img_fr_zs[:, -framesize:] = 0

            img_rb_zs = ctools.arrrebin(img_fr_zs, rebin)
            mapsum = np.add(mapsum, img_rb_zs)
        print(mapsum)

        # calc the normalized map wrt the center area
        CA = 16
        central_square = mapsum[int((N - CA) / rebin / 2):int((N + CA) /
                                                              rebin / 2),
                                int((N - CA) / rebin / 2):int((N + CA) /
                                                              rebin / 2)]
        print(central_square)
        norm = np.mean(central_square)
        print("Now normalizing to the central area value = ", norm)
        mapnorm = mapsum / float(norm)
        if det == 'lime':
            framesize_rb = int(framesize / rebin)
            #mapnorm[:framesize_rb,:]=1
            #mapnorm[-framesize_rb:,:]=1
            mapnorm[:, :framesize_rb] = 1
            mapnorm[:, -framesize_rb:] = 1

        # now save in a persistent ROOT object. Threshold to 1
        for ix in range(nx):
            for iy in range(nx):
                normmap.SetBinContent(ix + 1, iy + 1, min(mapnorm[ix, iy], 1.))
        tf_in.Close()

        tf_out.cd()
        normmap.Write()
        tf_out.Close()
        print(
            "Written the mean map with rebinning {rb}x{rb} into file {outf}.".
            format(rb=rebin, outf=outfile))