예제 #1
0
 def getNEvents(self):
     tf = sw.swift_read_root_file(
         self.tmpname)  #tf = ROOT.TFile.Open(self.rfile)
     ret = len(tf.GetListOfKeys()) if self.options.daq != 'midas' else int(
         len(tf.GetListOfKeys()) / 2)
     tf.Close()
     return ret
예제 #2
0
 def getNEvents(self):
     tf = sw.swift_read_root_file(
         self.tmpname)  #tf = ROOT.TFile.Open(self.rfile)
     ret = int(len(tf.GetListOfKeys()) /
               2) if (self.options.daq == 'midas'
                      and self.options.pmt_mode) else len(
                          tf.GetListOfKeys())
     tf.Close()
     return ret
예제 #3
0
    def calcPedestal(self, options, alternativeRebin=-1):
        maxImages = options.maxEntries
        nx = ny = self.xmax
        rebin = self.rebin if alternativeRebin < 0 else alternativeRebin
        nx = int(nx / rebin)
        ny = int(ny / rebin)
        #pedfilename = 'pedestals/pedmap_ex%d_rebin%d.root' % (options.pedexposure,rebin)
        pedfilename = 'pedestals/pedmap_run%s_rebin%d.root' % (options.run,
                                                               rebin)

        pedfile = ROOT.TFile.Open(pedfilename, 'recreate')
        pedmap = ROOT.TH2D('pedmap', 'pedmap', nx, 0, self.xmax, ny, 0,
                           self.xmax)
        pedmapS = ROOT.TH2D('pedmapsigma', 'pedmapsigma', nx, 0, self.xmax, ny,
                            0, self.xmax)

        pedsum = np.zeros((nx, ny))

        tf = sw.swift_read_root_file(self.tmpname)
        #tf = ROOT.TFile.Open(self.rfile)

        # first calculate the mean
        numev = 0
        for i, e in enumerate(tf.GetListOfKeys()):
            iev = i if self.options.daq != 'midas' else i / 2  # when PMT is present
            if iev in self.options.excImages: continue

            if maxImages > -1 and i < len(tf.GetListOfKeys()) - maxImages:
                continue
            name = e.GetName()
            obj = e.ReadObj()
            if not obj.InheritsFrom('TH2'): continue
            print("Calc pedestal mean with event: ", name)
            if rebin > 1:
                obj.RebinX(rebin)
                obj.RebinY(rebin)
            arr = hist2array(obj)
            pedsum = np.add(pedsum, arr)
            numev += 1
        pedmean = pedsum / float(numev)

        # now compute the rms (two separate loops is faster than one, yes)
        pedsqdiff = np.zeros((nx, ny))
        for i, e in enumerate(tf.GetListOfKeys()):
            iev = i if self.options.daq != 'midas' else i / 2  # when PMT is present
            if iev in self.options.excImages: continue

            if maxImages > -1 and i < len(tf.GetListOfKeys()) - maxImages:
                continue
            name = e.GetName()
            obj = e.ReadObj()
            if not obj.InheritsFrom('TH2'): continue
            print("Calc pedestal rms with event: ", name)
            if rebin > 1:
                obj.RebinX(rebin)
                obj.RebinY(rebin)
            arr = hist2array(obj)
            pedsqdiff = np.add(pedsqdiff, np.square(np.add(arr, -1 * pedmean)))
        pedrms = np.sqrt(pedsqdiff / float(numev - 1))

        # now save in a persistent ROOT object
        for ix in range(nx):
            for iy in range(ny):
                pedmap.SetBinContent(ix + 1, iy + 1, pedmean[ix, iy])
                pedmap.SetBinError(ix + 1, iy + 1, pedrms[ix, iy])
                pedmapS.SetBinContent(ix + 1, iy + 1, pedrms[ix, iy])
        tf.Close()

        pedfile.cd()
        pedmap.Write()
        pedmapS.Write()
        pedmean1D = ROOT.TH1D('pedmean', 'pedestal mean', 500, 97, 103)
        pedrms1D = ROOT.TH1D('pedrms', 'pedestal RMS', 500, 0, 5)
        for ix in range(nx):
            for iy in range(ny):
                pedmean1D.Fill(pedmap.GetBinContent(ix, iy))
                pedrms1D.Fill(pedmap.GetBinError(ix, iy))
        pedmean1D.Write()
        pedrms1D.Write()
        pedfile.Close()
        print("Pedestal calculated and saved into ", pedfilename)
예제 #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))