Esempio n. 1
0
def eventdisplay(inputfile, outputfile, histoname):
	#inputfile = raw_input("Insert root file: ")
	inputfile = TFile(datapath+inputfile)
	tree = TTree()
	inputfile.GetObject("B4", tree)

	#outputfile = raw_input("Insert root output file: ")
	displayfile = TFile(outputfile+".root","RECREATE")

	#loop over events
	for Event in range(int(tree.GetEntries())):

		tree.GetEntry(Event)

		#Set values of the tree
		PrimaryParticleName = tree.PrimaryParticleName # MC truth: primary particle Geant4 name
		PrimaryParticleEnergy = tree.PrimaryParticleEnergy
		EnergyTot = tree.EnergyTot # Total energy deposited in calorimeter
		Energyem = tree.Energyem # Energy deposited by the em component
		EnergyScin = tree.EnergyScin # Energy deposited in Scin fibers (not Birk corrected)
		EnergyCher = tree.EnergyCher # Energy deposited in Cher fibers (not Birk corrected)
		NofCherenkovDetected = tree.NofCherenkovDetected # Total Cher p.e. detected
		BarrelR_VectorSignals = tree.VectorSignalsR  # Vector of energy deposited in Scin fibers (Birk corrected)
		BarrelL_VectorSignals = tree.VectorSignalsL  # Vector of energy deposited in Scin fibers (Birk corrected)
		BarrelR_VectorSignalsCher = tree.VectorSignalsCherR # Vector of Cher p.e. detected in Cher fibers
		BarrelL_VectorSignalsCher = tree.VectorSignalsCherL 	
		VectorR = tree.VectorR
		VectorL = tree.VectorL		
		
		ROOTHistograms.create_eventdisplay_scin(PrimaryParticleName, BarrelR_VectorSignals, BarrelL_VectorSignals, histoname) 
Esempio n. 2
0
def getsignals():
    inputfile = TFile(path)
    tree = TTree()
    inputfile.GetObject("B4", tree)

    outputfile = raw_input("Insert root output file: ")
    file = TFile(outputfile, "RECREATE")

    SignalScin = TH1F("SignalScin", "SignalScin", 100, 0., 30000.)
    SignalCher = TH1F("SignalCher", "SignalCher", 100, 0., 50000.)
    Energydep = TH1F("EnergyDep", "Energydep", 10000, 0., 100000.)
    #loop over events
    for Event in range(int(tree.GetEntries())):

        tree.GetEntry(Event)

        #Set values of the tree
        PrimaryParticleName = tree.PrimaryParticleName  # MC truth: primary particle Geant4 name
        PrimaryParticleEnergy = tree.PrimaryParticleEnergy
        EnergyTot = tree.EnergyTot  # Total energy deposited in calorimeter
        Energyem = tree.Energyem  # Energy deposited by the em component
        EnergyScin = tree.EnergyScin  # Energy deposited in Scin fibers (not Birk corrected)
        EnergyCher = tree.EnergyCher  # Energy deposited in Cher fibers (not Birk corrected)
        NofCherenkovDetected = tree.NofCherenkovDetected  # Total Cher p.e. detected
        BarrelR_VectorSignals = tree.VectorSignalsR  # Vector of energy deposited in Scin fibers (Birk corrected)
        BarrelL_VectorSignals = tree.VectorSignalsL  # Vector of energy deposited in Scin fibers (Birk corrected)
        BarrelR_VectorSignalsCher = tree.VectorSignalsCherR  # Vector of Cher p.e. detected in Cher fibers
        BarrelL_VectorSignalsCher = tree.VectorSignalsCherL  # Vecotr of Cher p.e. detected in Cher fibers
        VectorR = tree.VectorR  # Energy deposted
        VectorL = tree.VectorL  # Energy deposited

        S = sum(BarrelR_VectorSignals) + sum(BarrelL_VectorSignals)
        C = sum(BarrelR_VectorSignalsCher) + sum(BarrelL_VectorSignalsCher)
        E = sum(VectorR) + sum(VectorL)
        print S
        SignalScin.Fill(S)
        SignalCher.Fill(C)
        Energydep.Fill(E)

    SignalScin.Write()
    SignalCher.Write()
    Energydep.Write()

    file.Close()
Esempio n. 3
0
def jetdisplay():
    outputfile = "wwlj"
    displayfile = TFile(outputfile + ".root", "RECREATE")

    inputfiles = ["wwlj10k_leakage_029/wwlj.root"]
    #for geant4.10.5
    inputfiles = ["resultsgeant4.10.5/wwlj/wwlj.root"]
    inputfiles = ["resultsgeant4.10.5/jetscan_leakage_X0/wwlj/wwlj.root"]
    #end of geant4.10.5

    for counter, inputfile in enumerate(inputfiles):
        inputfile = TFile(inputfile)
        print "Analyzing: " + str(inputfile) + " \n"
        tree = TTree()
        inputfile.GetObject("MyTree", tree)

        #graphEjet1 = TH1F("energyjet1", "energyjet1", 100, 0., 200.)
        #graphEjet2 = TH1F("energyjet2", "energyjet2", 100, 0., 200.)

        #graphEcherjet1 = TH1F("energycher1", "energycherjet", 100, 0., 200.)
        #graph3 = TH1F("energyscinjet", "energyscinjet", 100, 0., 200.)

        #graphmass = TH1F("mass_jet", "mass_jet", 100, 0., 200.)
        graphtest = TH1F("test", "test", 80, -40., 40.)
        graphenergy = TH1F("energy", "energy", 90, 60., 120.)
        graphenergytruth = TH1F("energytruth", "energytruth", 90, 60., 120.)
        graphjs = TH1F("energyjs", "energyjs", 200, 0., 100.)
        graphjc = TH1F("energyjc", "energyjc", 200, 0., 100.)
        graphdiff = TH1F("diff_mass", "diff_mass", 40, -20., 20.)
        #loop over events
        for Event in range(tree.GetEntries()):

            tree.GetEntry(Event)
            #print "Event "+str(Event)
            nmuon = tree.nmuon
            nneu = tree.nneu
            mjjr = tree.mjjr
            mjjt = tree.mjjt
            edep = tree.edep
            muene_che = tree.muene_che
            muene_sci = tree.muene_sci
            emcomp1 = tree.emcomp1
            emcomp2 = tree.emcomp2
            eleak = tree.eleak
            eleakn = tree.eleakn
            drmmu = tree.drmmu
            emu = tree.emu
            enumu = tree.enumu

            j1t_E = tree.j1t_E
            j1t_m = tree.j1t_m
            j1t_theta = tree.j1t_theta
            j1t_pt = tree.j1t_pt
            j1t_eta = tree.j1t_eta
            j1t_phi = tree.j1t_phi
            j2t_E = tree.j2t_E
            j2t_m = tree.j2t_m
            j2t_theta = tree.j2t_theta
            j2t_pt = tree.j2t_pt
            j2t_eta = tree.j2t_eta
            j2t_phi = tree.j2t_phi

            j1r_E = tree.j1r_E
            j1r_m = tree.j1r_m
            j1r_theta = tree.j1r_theta
            j1r_pt = tree.j1r_pt
            j1r_eta = tree.j1r_eta
            j1r_phi = tree.j1r_phi
            j2r_E = tree.j2r_E
            j2r_m = tree.j2r_m
            j2r_theta = tree.j2r_theta
            j2r_pt = tree.j2r_pt
            j2r_eta = tree.j2r_eta
            j2r_phi = tree.j2r_phi

            deltaj1 = 0.04406 * j1r_E + 0.1158
            deltaj1 = 0.04135 * j1r_E + 0.08789
            deltaj1 = 0.04911 * j1r_E + 0.05723
            j1 = TLorentzVector()
            j1.SetPtEtaPhiE(j1r_pt + deltaj1 * np.sin(j1r_theta), j1r_eta,
                            j1r_phi, j1r_E + deltaj1)
            deltaj2 = 0.04406 * j2r_E + 0.1158
            deltaj2 = 0.04135 * j2r_E + 0.08789
            deltaj2 = 0.04911 * j2r_E + 0.05723
            j2 = TLorentzVector()
            j2.SetPtEtaPhiE(j2r_pt + deltaj2 * np.sin(j2r_theta), j2r_eta,
                            j2r_phi, j2r_E + deltaj2)
            newmass = (j1 + j2).M()

            j1s_E = tree.j1s_E
            j1s_m = tree.j1s_m
            j1s_theta = tree.j1s_theta
            j1s_pt = tree.j1s_pt
            j1s_eta = tree.j1s_eta
            j1s_phi = tree.j1s_phi
            j2s_E = tree.j2s_E
            j2s_m = tree.j2s_m
            j2s_theta = tree.j2s_theta
            j2s_pt = tree.j2s_pt
            j2s_eta = tree.j2s_eta
            j2s_phi = tree.j2s_phi

            j1c_E = tree.j1c_E
            j1c_m = tree.j1c_m
            j1c_theta = tree.j1c_theta
            j1c_pt = tree.j1c_pt
            j1c_eta = tree.j1c_eta
            j1c_phi = tree.j1c_phi
            j2c_E = tree.j2c_E
            j2c_m = tree.j2c_m
            j2c_theta = tree.j2c_theta
            j2c_pt = tree.j2c_pt
            j2c_eta = tree.j2c_eta
            j2c_phi = tree.j2c_phi

            cut1 = nmuon == 1 and nneu == 1
            cut2 = abs(j1t_phi - j2t_phi) > 0.1
            cut3 = enumu + j1t_E + j2t_E > 162.45
            cut4 = eleak / 1000. - emu + muene_sci < 5.
            cut5 = j1r_E + j2r_E > 68.0
            cut6 = muene_sci < 4.0

            if cut1 and cut2 and cut3 and cut4 and cut5 and cut6:
                graphtest.Fill(j1r_E - j1t_E)
                graphtest.Fill(j2r_E - j2t_E)
                deltaj1 = 0.02175 * j1r_E + 0.0808
                deltaj2 = 0.02175 * j2r_E + 0.0808
                graphenergy.Fill(newmass)
                #graphenergy.Fill(j2r_E+deltaj2)
                #graphenergytruth.Fill(j1t_E)
                #graphenergytruth.Fill(j2t_E+j1t_E)
                graphenergytruth.Fill(mjjt)
                graphdiff.Fill(newmass - mjjt)
                graphjs.Fill(j2s_E)
                graphjs.Fill(j1s_E)
                graphjc.Fill(j2c_E)
                graphjc.Fill(j1c_E)

        displayfile.cd()
        #graphtest.Write()
        graphenergy.Write()
        graphenergytruth.Write()
        graphdiff.Write()
Esempio n. 4
0
class Run(Analysis):
    """ Run class containing all the information for a single run. """

    NTelPlanes = 4

    def __init__(self, number=None, testcampaign=None, load_tree=True, verbose=None):
        """
        :param number: if None is provided it creates a dummy run
        :param testcampaign: if None is provided ...
        :param load_tree: load the ROOT TTree
        :param verbose: turn on more output
        """
        # Basics
        super(Run, self).__init__(testcampaign, verbose=verbose, pickle_dir='Run')
        self.Number = number

        # Directories / Test Campaign
        self.IrradiationFile = join(self.Dir, self.MainConfig.get('MISC', 'irradiation file'))

        # Configuration & Root Files
        self.Config = self.load_run_config()
        self.RootFileDir = self.load_rootfile_dirname()
        self.RootFilePath = self.load_rootfile_path()

        # Run Info
        self.InfoFile = join(self.TCDir, 'run_log.json')
        self.Info = self.load_run_info()
        self.RootFile = None
        self.Tree = TTree()
        self.TreeName = self.Config.get('BASIC', 'treename')
        self.DUTs = [self.dut(i + 1, self.Info) for i in range(self.get_n_diamonds())] if self.Number is not None else None

        # Settings
        self.Plane = Plane()
        self.TriggerPlanes = self.load_trigger_planes()

        # General Information
        self.Flux = self.get_flux()
        self.Type = self.get_type()

        # Times
        self.LogStart = self.load_log_start()
        self.LogEnd = self.load_log_stop()
        self.Duration = self.LogEnd - self.LogStart

        self.Converter = Converter(self)
        if self.set_run(number, load_tree):
            # tree info
            self.TimeOffset = None
            self.Time = self.load_time_vec()
            self.StartEvent = 0
            self.NEvents = int(self.Tree.GetEntries())
            self.EndEvent = self.NEvents - 1
            self.StartTime = self.get_time_at_event(self.StartEvent)
            self.EndTime = self.get_time_at_event(self.EndEvent)
            self.TotalTime = self.load_total_time()
            self.TotalMinutes = self.TotalTime / 60000.
            self.Duration = timedelta(seconds=self.TotalTime)
            self.LogEnd = self.LogStart + self.Duration  # overwrite if we know exact duration
            self.NPlanes = self.load_n_planes()

        self.TInit = time() - self.InitTime

    def __str__(self):
        return f'{self.__class__.__name__} {self.Number}{self.evt_str} ({self.TCString})'

    def __repr__(self):
        return self.__str__()

    def __call__(self, number, load_tree=False):
        self.set_run(number, load_tree)
        return self

    def __gt__(self, other):
        return self.Number > (other.Number if isinstance(other, Run) else other)

    @property
    def evt_str(self):
        return f' with {make_ev_str(self.Info["events"])} ev' if 'events' in self.Info else f' with {make_ev_str(self.NEvents)} ev' if self.Tree.Hash() else ''

    def set_run(self, number, load_tree):
        if number is None:
            return False
        if number < 0 and type(number) is not int:
            critical('incorrect run number')

        self.Number = number
        self.load_run_info()
        self.Flux = self.get_flux()

        # check for conversion
        if load_tree:
            self.Converter.convert_run()
            self.load_rootfile()
        else:
            return False
        if not self.rootfile_is_valid():
            self.Converter.convert_run()
            self.load_rootfile()
        return True

    def get_type(self):
        return self.Config.get('BASIC', 'type') if self.Number is not None else None

    def set_estimate(self, n=None):
        self.Tree.SetEstimate(choose(n, -1))

    def is_volt_scan(self):
        return any(name in self.Info['runtype'] for name in ['voltage', 'hv'])

    # ----------------------------------------
    # region INIT
    @property
    def dut(self):
        return DUT

    def load_rootfile(self, prnt=True):
        self.info('Loading information for rootfile: {file}'.format(file=basename(self.RootFilePath)), endl=False, prnt=prnt)
        self.RootFile = TFile(self.RootFilePath)
        self.Tree = self.RootFile.Get(self.TreeName)
        return self.Tree

    def load_run_config(self):
        base_file_name = join(get_base_dir(), 'config', self.TCString, 'RunConfig.ini')
        if not file_exists(base_file_name):
            critical('RunConfig.ini does not exist for {0}! Please create it in config/{0}!'.format(self.TCString))
        parser = Config(base_file_name)  # first read the main config file with general information for all splits
        if parser.has_section('SPLIT') and self.Number is not None:
            split_runs = [0] + loads(parser.get('SPLIT', 'runs')) + [inf]
            config_nr = next(i for i in range(1, len(split_runs)) if split_runs[i - 1] <= self.Number < split_runs[i])
            parser.read(join(get_base_dir(), 'config', self.TCString, 'RunConfig{nr}.ini'.format(nr=config_nr)))  # add the content of the split config
        return parser

    @staticmethod
    def make_root_filename(run):
        return f'TrackedRun{run:0>3}.root'

    def make_root_subdir(self):
        return join('root', 'pads' if self.get_type() == 'pad' else self.get_type())

    def load_rootfile_path(self, run=None):
        run = choose(run, self.Number)
        return None if run is None else join(self.RootFileDir, self.make_root_filename(run))

    def load_rootfile_dirname(self):
        return ensure_dir(join(self.TCDir, self.make_root_subdir())) if self.Number is not None else None

    def load_trigger_planes(self):
        return array(self.Config.get_list('BASIC', 'trigger planes', [1, 2]))

    def get_n_diamonds(self, run_number=None):
        run_info = self.load_run_info(run_number)
        return len([key for key in run_info if key.startswith('dia') and key[-1].isdigit()])

    def load_dut_numbers(self):
        return [i + 1 for i in range(len([key for key in self.Info.keys() if key.startswith('dia') and key[-1].isdigit()]))]

    def load_dut_type(self):
        dut_type = self.Config.get('BASIC', 'type') if self.Number is not None else None
        if dut_type not in ['pixel', 'pad', None]:
            critical("The DUT type {0} has to be either 'pixel' or 'pad'".format(dut_type))
        return dut_type

    def load_default_info(self):
        with open(join(self.Dir, 'Runinfos', 'defaultInfo.json')) as f:
            return load(f)

    def load_run_info_file(self):
        if not file_exists(self.InfoFile):
            critical('Run Log File: "{f}" does not exist!'.format(f=self.InfoFile))
        with open(self.InfoFile) as f:
            return load(f)

    def load_run_info(self, run_number=None):
        data = self.load_run_info_file()

        run_number = self.Number if run_number is None else run_number
        if run_number is not None:
            run_info = data.get(str(run_number))
            if run_info is None:  # abort if the run is still not found
                critical('Run {} not found in json run log file!'.format(run_number))
            self.Info = run_info
            self.Info['masked pixels'] = [0] * 4
            self.translate_diamond_names()
            return run_info
        else:
            self.Info = self.load_default_info()
            return self.Info

    def load_dut_names(self):
        return [self.Info['dia{nr}'.format(nr=i)] for i in range(1, self.get_n_diamonds() + 1)]

    def load_biases(self):
        return [int(self.Info['dia{nr}hv'.format(nr=i)]) for i in range(1, self.get_n_diamonds() + 1)]

    def load_log_start(self):
        return conv_log_time(self.Info['starttime0'])

    def load_log_stop(self):
        return conv_log_time(self.Info['endtime'])

    def load_total_time(self):
        return (self.Time[-1] - self.Time[0]) / 1000

    def load_n_planes(self):
        if self.has_branch('cluster_col'):
            self.Tree.Draw('@cluster_col.size()', '', 'goff', 1)
            return int(self.Tree.GetV1()[0])
        else:
            return 4

    def load_time_vec(self):
        t = get_time_vec(self.Tree)
        t0 = datetime.fromtimestamp(t[0] / 1000) if t[0] < 1e12 else None
        self.TimeOffset = None if t0 is None or t0.year > 2000 and t0.day == self.LogStart.day else t[0] - time_stamp(self.LogStart) * 1000
        return t if self.TimeOffset is None else t - self.TimeOffset

    def load_plane_efficiency(self, plane):
        return self.load_plane_efficiencies()[plane - 1]

    def load_plane_efficiencies(self):
        return [ufloat(e, .03) for e in self.Config.get_list('BASIC', 'plane efficiencies', default=[.95, .95])]
    # endregion INIT
    # ----------------------------------------

    # ----------------------------------------
    # region MASK
    def load_mask_file_path(self):
        mask_dir = self.MainConfig.get('MAIN', 'maskfile directory') if self.MainConfig.has_option('MAIN', 'maskfile directory') else join(self.DataDir, self.TCDir, 'masks')
        if not dir_exists(mask_dir):
            warning('Mask file directory does not exist ({})!'.format(mask_dir))
        return join(mask_dir, basename(self.Info['maskfile']))

    def load_mask(self, plane=None):
        mask_file = self.load_mask_file_path()
        if basename(mask_file).lower() in ['no mask', 'none', 'none!', ''] or self.Number is None:
            return
        try:
            data = genfromtxt(mask_file, [('id', 'U10'), ('pl', 'i'), ('x', 'i'), ('y', 'i')])
            if 'cornBot' not in data['id']:
                warning('Invalid mask file: "{}". Not taking any mask!'.format(mask_file))
            mask = [[data[where((data['pl'] == pl) & (data['id'] == n))][0][i] for n in ['cornBot', 'cornTop'] for i in [2, 3]] for pl in sorted(set(data['pl']))]
            mask = [[max(1, m[0]), max(1, m[1]), min(self.Plane.NCols - 2, m[2]), min(self.Plane.NRows - 2, m[3])] for m in mask]  # outer pixels are ignored
            return mask if plane is None else mask[plane - 1] if plane - 1 < len(mask) else None
        except Exception as err:
            warning(err)
            warning('Could not read mask file... not taking any mask!')

    def get_mask_dim(self, plane=1, mm=True):
        return Plane.get_mask_dim(self.load_mask(plane), mm)

    def get_mask_dims(self, mm=True):
        return array([self.get_mask_dim(pl, mm) for pl in [1, 2]])

    def get_unmasked_area(self, plane):
        return None if self.Number is None else Plane.get_area(self.load_mask(plane))

    def find_for_in_comment(self):
        for name in ['for1', 'for2']:
            if name not in self.Info:
                for cmt in self.Info['comments'].split('\r\n'):
                    cmt = cmt.replace(':', '')
                    cmt = cmt.split(' ')
                    if str(cmt[0].lower()) == name:
                        self.Info[name] = int(cmt[1])
        return 'for1' in self.Info
    # endregion MASK
    # ----------------------------------------

    # ----------------------------------------
    # region HELPERS
    def translate_diamond_names(self):
        for key, value in [(key, value) for key, value in self.Info.items() if key.startswith('dia') and key[-1].isdigit()]:
            self.Info[key] = self.translate_dia(value)

    def register_new_dut(self):
        if input('Do you want to add a new diamond? [y,n] ').lower() in ['y', 'yes']:
            dut_type = int(input('Enter the DUT type (1 for pCVD, 2 for scCVD, 3 for silicon): ')) - 1
            dut_name = input('Enter the name of the DUT (no "_"): ')
            alias = input(f'Enter the alias (no "_", for default {dut_name.lower()} press enter): ')
            self.add_alias(alias, dut_name, dut_type)
            self.add_dut_info(dut_name)
            return True
        else:
            return False

    @staticmethod
    def add_alias(alias, dut_name, dut_type):
        alias_file = join(Dir, 'config', 'DiamondAliases.ini')
        with open(alias_file, 'r+') as f:
            lines = [line.strip(' \n') for line in f.readlines()]
            i0 = lines.index(['# pCVD', '# scCVD', '# Silicon'][dut_type])
            i = next(i for i, line in enumerate(lines[i0:], i0) if line.strip() == '')
            lines.insert(i, f'{(alias if alias else dut_name).lower()} = {dut_name}')
            f.seek(0)
            f.writelines([f'{line}\n' for line in lines])
            info(f'added entry: {(alias if alias else dut_name).lower()} = {dut_name} in {alias_file}')

    def add_dut_info(self, dut_name):
        dia_info_file = join(Dir, 'Runinfos', 'dia_info.json')
        data = load_json(dia_info_file)
        if dut_name in data:
            return warning('The entered DUT name already exists!')
        tc = get_input(f'Enter the beam test [YYYYMM]', self.TCString)
        data[dut_name] = {'irradiation': {tc: get_input(f'Enter the irradiation for {tc}', '0')},
                          'boardnumber': {tc: get_input(f'Enter the board number for {tc}')},
                          'thickness': get_input('Enter the thickness'),
                          'size': get_input('Enter the lateral size ([x, y])'),
                          'manufacturer': get_input('Enter the manufacturer')}
        with open(dia_info_file, 'w') as f:
            dump(data, f, indent=2)
        info(f'added {dut_name} to {dia_info_file}')

    def translate_dia(self, dia):
        name, suf = dia.split('_')[0].lower(), '_'.join(dia.split('_')[1:])
        if name not in Config(join(self.Dir, 'config', 'DiamondAliases.ini')).options('ALIASES'):
            warning(f'{dia} was not found in config/DiamondAliases.ini!')
            if not self.register_new_dut():
                critical(f'unknown diamond {dia}')
        parser = Config(join(self.Dir, 'config', 'DiamondAliases.ini'))
        return '_'.join([parser.get('ALIASES', name)] + ([suf] if suf else []))

    def reload_run_config(self, run_number):
        self.Number = run_number
        self.Config = self.load_run_config()
        self.Info = self.load_run_info()
        self.RootFileDir = self.load_rootfile_dirname()
        self.RootFilePath = self.load_rootfile_path()
        return self.Config

    def rootfile_is_valid(self, file_path=None):
        tfile = self.RootFile if file_path is None else TFile(file_path)
        ttree = self.Tree if file_path is None else tfile.Get(self.TreeName)
        is_valid = not tfile.IsZombie() and tfile.ClassName() == 'TFile' and ttree and ttree.ClassName() == 'TTree'
        if not is_valid:
            warning('Invalid TFile or TTree! Deleting file {}'.format(tfile.GetName()))
            remove_file(tfile.GetName())
        return is_valid

    def calculate_plane_flux(self, plane=1, corr=True):
        """estimate the flux [kHz/cm²] through a trigger plane based on Poisson statistics."""
        rate, eff, area = self.Info[f'for{plane}'], self.load_plane_efficiency(plane), self.get_unmasked_area(plane)
        return -log(1 - rate / Plane.Frequency) * Plane.Frequency / area / 1000 / (eff if corr else ufloat(1, .05))  # count zero hits of Poisson

    def find_n_events(self, n, cut, start=0):
        evt_numbers = self.get_tree_vec(var='Entry$', cut=cut, nentries=self.NEvents, firstentry=start)
        return int(evt_numbers[:n][-1] + 1 - start)

    def get_max_run(self):
        return int(max(self.load_run_info_file(), key=int))
    # endregion HELPERS
    # ----------------------------------------

    # ----------------------------------------
    # region GET
    def get_flux(self, plane=None, corr=True):
        if self.Number is None:
            return
        if not self.find_for_in_comment():
            # warning('no plane rates in the data...')
            return self.Info['measuredflux'] / (mean(self.load_plane_efficiencies()) if corr else 1)
        return self.get_mean_flux(corr) if plane is None else self.calculate_plane_flux(plane, corr)

    def get_mean_flux(self, corr=True):
        return mean([self.get_flux(pl, corr) for pl in [1, 2]])

    def get_time(self):
        return ufloat(time_stamp(self.LogStart + self.Duration / 2), self.Duration.seconds / 2)

    def get_channel_name(self, channel):
        self.Tree.GetEntry()
        return self.Tree.sensor_name[channel]

    def get_time_at_event(self, event):
        """ For negative event numbers it will return the time stamp at the startevent. """
        return self.Time[min(event, self.EndEvent)] / 1000.

    def get_event_at_time(self, seconds, rel=True):
        """ Returns the event nunmber at time dt from beginning of the run. Accuracy: +- 1 Event """
        if seconds - (0 if rel else self.StartTime) >= self.TotalTime or seconds == -1:  # return time of last event if input is too large
            return self.NEvents - 1
        return where(self.Time <= 1000 * (seconds + (self.StartTime if rel else 0)))[0][-1]

    def get_tree_vec(self, var, cut='', dtype=None, nentries=None, firstentry=0):
        return get_tree_vec(self.Tree, var, cut, dtype, nentries, firstentry)

    def get_tree_tuple(self):
        return (self.Tree, self.RootFile) if self.Tree is not None else False

    def get_time_vec(self):
        return self.Time if hasattr(self, 'Time') else None

    def get_bias_strings(self):
        return [str(b) for b in self.load_biases()]

    @save_pickle('HR', suf_args=0)
    def get_high_rate_run(self, high=True):
        from src.run_selection import RunSelector
        return int(RunSelector(testcampaign=self.TCString).get_high_rate_run(self.Number, high))

    def get_low_rate_run(self):
        return self.get_high_rate_run(high=False)
    # endregion GET
    # ----------------------------------------

    # ----------------------------------------
    # region SHOW
    def show_info(self):
        print('Run information for', self)
        for key, value in sorted(self.Info.items()):
            print(f'{key:<13}: {value}')

    def has_branch(self, name):
        return bool(self.Tree.GetBranch(name))

    def info(self, msg, endl=True, blank_lines=0, prnt=True):
        return info(msg, endl, prnt=self.Verbose and prnt, blank_lines=blank_lines)

    def add_to_info(self, t, txt='Done', prnt=True):
        return add_to_info(t, txt, prnt=self.Verbose and prnt)
Esempio n. 5
0
filename = str(file) + "B4.root"
inputFile = TFile(str(filename))  #root input file
tree = TTree()  #Tree name B4
inputFile.GetObject("B4", tree)

#---------------------------------------------------------------------------------------------------
#Section used only if you want to estimate the calibration constants and Chi factor of a module.
#DREAM is calibrated with electrons. The input file must be from electron events with the same energy.
#To estimate h/e Cher and scin values and the Chi factor a second root file with pions (+ or -) is needed.

#To later perform h/e Cher and scin computation and Chi factor
print "You have given an electron file event, now pass a pion one for h/e estimation.\n"
namepionfile = raw_input("Insert name of ROOT pion (+ or -) file: ")

#Set parameters
NofEvents = tree.GetEntries()
fastchercalibconstant = 0  #Done without building clusters
fastscincalibconstant = 0  #Done without building clusters
fastChi = 0
chercalibconstant = 0
scincalibconstant = 0
Chi = 0
firstcounter = 0
secondcounter = 0

#Estimate Cherenkov and Scintillation calibration constants
for Event in range(int(NofEvents)):

    tree.GetEntry(Event)

    #Set values of tree
Esempio n. 6
0
def makeRooDataSet(type, infile_name, outfile_name, tree_name, nevents):
    """ Make RooDataSets from TTrees"""

    inputfile = TFile.Open(infile_name, "READ")
    print "Importing tree"
    tree = TTree()
    inputfile.GetObject(tree_name, tree)  #get the tree from the data file

    #define variables for the RooDataSet
    m_mumu = RooRealVar("m_mumu", "m_mumu", 0.0, 4.0)
    y_mumu = RooRealVar("y_mumu", "y_mumu", 0.0, 2.0)
    pt_mumu = RooRealVar("pt_mumu", "pt_mumu", 0.0, 260.0)
    eta_gamma = RooRealVar("eta_gamma", "eta_gamma", -3.5, 3.5)
    pt_gamma = RooRealVar("pt_gamma", "pt_gamma", 0.0, 100.0)
    m_gamma = RooRealVar("m_gamma", "m_gamma", -0.1, 0.1)

    m_chi_rf1S = RooRealVar("m_chi_rf1S", "m_chi_rf1S", 0.0, 7.0)
    m_chi_rf2S = RooRealVar("m_chi_rf2S", "m_chi_rf2S", -1.0, 1.0)
    #Qvalue        = RooRealVar("Qvalue","Q", -15., 15.)
    s = RooRealVar("s", "s", -10., 10.)

    ctpv = RooRealVar("ctpv", "ctpv", -1.0, 3.5)
    ctpv_error = RooRealVar("ctpv_err", "ctpv_err", -1.0, 1.0)
    pi0_abs_mass = RooRealVar("pi0_abs_mass", "pi0_abs_mass", 0.0, 2.2)
    psi1S_nsigma = RooRealVar("psi1S_nsigma", "psi1S_nsigma", 0.0, 1.0)
    psi2S_nsigma = RooRealVar("psi2S_nsigma", "psi2S_nsigma", 0.0, 1.0)
    psi3S_nsigma = RooRealVar("psi3S_nsigma", "psi3S_nsigma", 0.0, 1.0)
    rho_conv = RooRealVar("rho_conv", "rho_conv", 0.0, 70.0)
    dz = RooRealVar("dz", "dz", -1.0, 1.0)
    probFit1S = RooRealVar("probFit1S", "probFit1S", 0, 1)
    probFit2S = RooRealVar("probFit2S", "probFit2S", 0, 1)

    dataArgSet = RooArgSet(m_mumu, y_mumu, pt_mumu, eta_gamma, pt_gamma,
                           m_gamma, m_chi_rf1S)

    dataArgSet.add(m_chi_rf2S)
    dataArgSet.add(s)
    dataArgSet.add(ctpv)
    dataArgSet.add(ctpv_error)
    dataArgSet.add(pi0_abs_mass)
    dataArgSet.add(psi1S_nsigma)
    dataArgSet.add(psi2S_nsigma)
    dataArgSet.add(rho_conv)
    dataArgSet.add(dz)
    dataArgSet.add(probFit1S)
    dataArgSet.add(probFit2S)

    print "Creating DataSet"
    dataSet = RooDataSet("chicds", "Chic RooDataSet", dataArgSet)

    entries = tree.GetEntries()
    print entries

    if nevents is not 0:
        entries = nevents

    for ientry in range(0, entries):
        tree.GetEntry(ientry)

        # unfort ntuples are slightly different for chic and chib

        if type == 'chic':

            m_mumu.setVal(tree.dimuon_mass)
            y_mumu.setVal(tree.dimuon_rapidity)
            pt_mumu.setVal(tree.dimuon_pt)
            eta_gamma.setVal(tree.photon_eta)
            pt_gamma.setVal(tree.photon_pt)
            #m_gamma.setVal(tree.photon_p4.M())
            m_chi_rf1S.setVal(tree.rf1S_chic_mass)
            #m_chi_rf1S.setVal(tree.rf2S_chi_p4.M())
            #Qvalue.setVal((tree.chi_p4).M() - tree.dimuon_p4.M())
            #Qvalue.setVal((tree.chi_p4).M()**2 - tree.dimuon_p4.M()**2)
            #Qvalue.setVal((tree.rf1S_chic_mass**2 -tree.dimuon_mass**2)
            #              / (3.5107**2 - 3.0969**2 ) -1)

            #           this should be the correct one if the refitted variable was available
            #           s.setVal((tree.rf1S_chic_mass**2 - tree.rf1S_dimuon_p4.M()**2)/ (3.5107**2 - 3.0969**2 ) -1)

            s.setVal((tree.rf1S_chic_mass**2 - 3.0969**2) /
                     (3.5107**2 - 3.0969**2) - 1)

            psi1S_nsigma.setVal(tree.psi1S_nsigma)
            psi2S_nsigma.setVal(0)
            psi3S_nsigma.setVal(0)

        elif type == 'chib':

            m_mumu.setVal(tree.dimuon_mass)
            y_mumu.setVal(tree.dimuon_rapidity)
            pt_mumu.setVal(tree.dimuon_pt)
            eta_gamma.setVal(tree.photon_eta)
            pt_gamma.setVal(tree.photon_pt)
            m_chi_rf1S.setVal(tree.rf1S_chib_mass)
            m_chi_rf2S.setVal(tree.rf2S_chib_mass)
            Qvalue.setVal(tree.chib_mass - tree.dimuon_mass)
            psi1S_nsigma.setVal(tree.Y1S_nsigma)
            psi2S_nsigma.setVal(tree.Y2S_nsigma)
            psi3S_nsigma.setVal(tree.Y3S_nsigma)

        ctpv.setVal(tree.ct_pv)
        ctpv_error.setVal(tree.ct_pv_error)
        #pi0_abs_mass.setVal(tree.pi0_abs_mass)

        rho_conv.setVal(tree.Conv)
        dz.setVal(tree.Dz)
        probFit1S.setVal(tree.probfit1S)
        #probFit2S.setVal(tree.probFit2S)

        #if (tree.chic_pdgId == 20443):dataSet.add(dataArgSet)
        dataSet.add(dataArgSet)

    outfile = TFile(outfile_name, 'recreate')
    dataSet.Write()
Esempio n. 7
0
def jetdisplay():
	outputfile = "Jetdisplay"
	displayfile = TFile(outputfile+".root","RECREATE")

	inputfile = "wwlj1k.root"

	inputfile = TFile(inputfile)
	print "Analyzing: "+str(inputfile)+" \n"
	tree = TTree()
	inputfile.GetObject("B4", tree)	

	graph = TH1F("energyjet", "energyjet", 100, 0., 200.)
	graph2 = TH1F("energycherjet", "energycherjet", 100, 0., 200.)
	graph3 = TH1F("energyscinjet", "energyscinjet", 100, 0., 200.)
	graphmass = TH1F("mass_jet", "mass_jet", 100, 0., 200.)

	graph4 = TH1F("energy", "energy", 100, 0., 200.)
	graph5 = TH1F("energycher", "energycher", 100, 0., 200.)
	graph6 = TH1F("energyscin", "energyscin", 100, 0., 200.)

	#loop over events
	for Event in range(tree.GetEntries()):	

		tree.GetEntry(Event)	

		#Set values of the tree
		PrimaryParticleName = tree.PrimaryParticleName # MC truth: primary particle Geant4 name
		PrimaryParticleEnergy = tree.PrimaryParticleEnergy
		EnergyTot = tree.EnergyTot # Total energy deposited in calorimeter
		Energyem = tree.Energyem # Energy deposited by the em component
		EnergyScin = tree.EnergyScin # Energy deposited in Scin fibers (not Birk corrected)
		EnergyCher = tree.EnergyCher # Energy deposited in Cher fibers (not Birk corrected)
		NofCherenkovDetected = tree.NofCherenkovDetected # Total Cher p.e. detected
		BarrelR_VectorSignals = tree.VectorSignalsR  # Vector of energy deposited in Scin fibers (Birk corrected)
		BarrelL_VectorSignals = tree.VectorSignalsL  # Vector of energy deposited in Scin fibers (Birk corrected)
		BarrelR_VectorSignalsCher = tree.VectorSignalsCherR # Vector of Cher p.e. detected in Cher fibers
		BarrelL_VectorSignalsCher = tree.VectorSignalsCherL 	
		VectorR = tree.VectorR
		VectorL = tree.VectorL
		
		Calib_BarrelL_VectorSignals = calibration.calibscin(BarrelL_VectorSignals)
		Calib_BarrelR_VectorSignals = calibration.calibscin(BarrelR_VectorSignals)
		Calib_BarrelL_VectorSignalsCher = calibration.calibcher(BarrelL_VectorSignalsCher)
		Calib_BarrelR_VectorSignalsCher = calibration.calibcher(BarrelR_VectorSignalsCher)

		energy = float(sum(Calib_BarrelR_VectorSignals)+sum(Calib_BarrelL_VectorSignals))
		energycher = float(sum(Calib_BarrelR_VectorSignalsCher)+sum(Calib_BarrelL_VectorSignalsCher))
		threshold = 0.0 #(GeV)	

		if energy>70.:
			
			#event displays with signals (p.e.)
			#if Event < 1:
				#displayfile.cd()
				#ROOTHistograms.create_eventdisplay_scin("Jet", BarrelR_VectorSignals, BarrelL_VectorSignals, "signal"+str(Event)) 
				#ROOTHistograms.create_eventdisplay_cher("Jet", BarrelR_VectorSignalsCher, BarrelL_VectorSignalsCher, "signal"+str(Event))
		
			#event displays with energy (GeV)	
			if Event<10:
					displayfile.cd()
					ROOTHistograms.create_eventdisplay_scin("Jet_energy", Calib_BarrelR_VectorSignals, Calib_BarrelL_VectorSignals, "energy"+str(Event), threshold) 
					ROOTHistograms.create_eventdisplay_cher("Jet_energy", Calib_BarrelR_VectorSignalsCher, Calib_BarrelL_VectorSignalsCher, "energy"+str(Event), threshold)	
			
			inputparticles_scin = []
			inputparticles_cher = []

			#right part
			for towerindex in range(75*36):	
				theta, phi, eta = newmap.maptower(towerindex, "right")
				energy_scin = Calib_BarrelR_VectorSignals[towerindex]
				pt_scin = energy_scin*np.sin(theta*math.pi/180.)

				energy_cher = Calib_BarrelR_VectorSignalsCher[towerindex]
				pt_cher = energy_cher*np.sin(theta*math.pi/180.)
				
				towerscin = TLorentzVector()
				towerscin.SetPtEtaPhiM(pt_scin, eta, phi*math.pi/180., 0.)
				towercher = TLorentzVector()
				towercher.SetPtEtaPhiM(pt_cher, eta, phi*math.pi/180., 0.)	

				if energy_scin > threshold:
					#print "truth towers: "+str(energy_scin)+" "+str(eta)+" "+str(phi)
					inputparticles_scin.append(fastjet.PseudoJet(towerscin.Px(), towerscin.Py(), towerscin.Pz(), towerscin.E()))
					inputparticles_cher.append(fastjet.PseudoJet(towercher.Px(), towercher.Py(), towercher.Pz(), towercher.E()))
			
			#left part
			for towerindex in range(75*36):	
				theta, phi, eta = newmap.maptower(towerindex, "left")
				energy_scin = Calib_BarrelL_VectorSignals[towerindex]
				pt_scin = energy_scin*np.sin(theta*math.pi/180.)
				
				energy_cher = Calib_BarrelL_VectorSignalsCher[towerindex]
				pt_cher = energy_cher*np.sin(theta*math.pi/180.)
				
				towerscin = TLorentzVector()
				towerscin.SetPtEtaPhiM(pt_scin, eta, phi*math.pi/180., 0.)
				towercher = TLorentzVector()
				towercher.SetPtEtaPhiM(pt_cher, eta, phi*math.pi/180., 0.)	

				if energy_scin > threshold:
					#print "truth towers: "+str(energy_scin)+" "+str(eta)+" "+str(phi)
					inputparticles_scin.append(fastjet.PseudoJet(towerscin.Px(), towerscin.Py(), towerscin.Pz(), towerscin.E()))
					inputparticles_cher.append(fastjet.PseudoJet(towercher.Px(), towercher.Py(), towercher.Pz(), towercher.E()))

			jet_def = fastjet.JetDefinition(fastjet.ee_genkt_algorithm, 2*math.pi, 1.)
			
			clust_seq = fastjet.ClusterSequence(inputparticles_scin, jet_def)	

			print "Event: "+str(Event)+" energy (GeV): "+str(energy)+" n-jets: "+str(len(clust_seq.exclusive_jets(int(2))))+" truth: "+str(len(inputparticles_scin))
			
			clust_seq_cher = fastjet.ClusterSequence(inputparticles_cher, jet_def)

			jet1_scin = fastjet.sorted_by_E(clust_seq.exclusive_jets(int(2)))[0]
			jet2_scin = fastjet.sorted_by_E(clust_seq.exclusive_jets(int(2)))[1]

			jet1_cher = fastjet.sorted_by_E(clust_seq_cher.exclusive_jets(int(2)))[0]
			jet2_cher = fastjet.sorted_by_E(clust_seq_cher.exclusive_jets(int(2)))[1]

			print "DeltaR jet1_scin: "+str(jet1_scin.delta_R(jet1_cher))+" "+str(jet1_scin.delta_R(jet2_cher))

			c = 0.34 #chi factor

			jet1, jet2 = mergejet(jet1_scin, jet2_scin, jet1_cher, jet2_cher)

			graph.Fill(jet1.e()+jet2.e())
			graph3.Fill(jet1_scin.e()+jet2_scin.e())
			graph2.Fill(jet1_cher.e()+jet2_cher.e())
			j = jet1+jet2
			graphmass.Fill(j.m())
			
			graph4.Fill((energy-c*energycher)/(1.-c))
			graph5.Fill(energycher)
			graph6.Fill(energy)
	
	graph.Write()
	graph2.Write()
	graph3.Write()
	graph4.Write()
	graph5.Write()
	graph6.Write()
	graphmass.Write()
Esempio n. 8
0
File: lat.py Progetto: gothman5/LAT
def main(argv):

    print("=======================================")
    print("LAT started:",time.strftime('%X %x %Z'))
    startT = time.clock()
    # gROOT.ProcessLine("gErrorIgnoreLevel = 3001;") # suppress ROOT error messages
    global batMode
    intMode, batMode, rangeMode, fileMode, gatMode, singleMode, pathMode, cutMode = False, False, False, False, False, False, False, False
    dontUseTCuts = False
    dsNum, subNum, runNum, plotNum = -1, -1, -1, 1
    pathToInput, pathToOutput, manualInput, manualOutput, customPar = ".", ".", "", "", ""

    if len(argv)==0: return
    for i,opt in enumerate(argv):
        if opt == "-r":
            rangeMode, dsNum, subNum = True, int(argv[i+1]), int(argv[i+2])
            print("Scanning DS-%d sub-range %d" % (dsNum, subNum))
        if opt == "-p":
            pathMode, manualInput, manualOutput = True, argv[i+1], argv[i+2]
            print("Manually set input/output files:\nInput: %s\nOutput: %s" % (manualInput, manualOutput))
        if opt == "-d":
            pathToInput, pathToOutput = argv[i+1], argv[i+2]
            print("Custom paths: Input %s,  Output %s" % (pathToInput,pathToOutput))
        if opt == "-f":
            fileMode, dsNum, runNum = True, int(argv[i+1]), int(argv[i+2])
            print("Scanning DS-%d, run %d" % (dsNum, runNum))
        if opt == "-g":
            gatMode, runNum = True, int(argv[i+1])
            print("GATDataSet mode.  Scanning run %d" % (runNum))
        if opt == "-s":
            singleMode, pathToInput = True, argv[i+1]
            print("Single file mode.  Scanning {}".format(pathToInput))
        if opt == "-i":
            intMode, plotNum = True, int(argv[i+1])
            print("Interactive mode selected. Use \"p\" for previous and \"q\" to exit.")
        if opt == "-x":
            dontUseTCuts = True
            print("DC TCuts deactivated.  Retaining all events ...")
        if opt == "-c":
            cutMode, customPar = True, str(argv[i+1])
            print("Using custom cut parameter: {}".format(customPar))
        if opt == "-b":
            batMode = True
            import matplotlib
            # if os.environ.get('DISPLAY','') == '':
                # print('No display found. Using non-interactive Agg backend')
            matplotlib.use('Agg')
            print("Batch mode selected.  A new file will be created.")
    import matplotlib.pyplot as plt
    from matplotlib import gridspec
    import matplotlib.ticker as mtick
    plt.style.use('pltTalks.mplstyle')
    from matplotlib.colors import LogNorm, Normalize

    # File I/O
    inFile, outFile, bltFile = TFile(), TFile(), TFile()
    gatTree, bltTree, out = TTree(), TTree(), TTree()
    theCut, inPath, outPath = "", "", ""

    # Set input and output files
    if rangeMode:
        inPath = "%s/waveSkimDS%d_%d.root" % (pathToInput, dsNum, subNum)
        outPath = "%s/latSkimDS%d_%d.root" % (pathToOutput, dsNum, subNum)
    if fileMode:
        inPath = "%s/waveSkimDS%d_run%d.root" % (pathToInput, dsNum, runNum)
        outPath = "%s/latSkimDS%d_run%d.root" % (pathToOutput, dsNum, runNum)
    if pathMode:
        inPath, outPath = manualInput, manualOutput
    if gatMode:
        ds = GATDataSet()
        gatPath = ds.GetPathToRun(runNum,GATDataSet.kGatified)
        bltPath = ds.GetPathToRun(runNum,GATDataSet.kBuilt)
        outPath = "%s/lat_run%d.root" % (pathToOutput, runNum)
    if pathMode and gatMode:
        outPath = manualOutput


    # Initialize trees
    if rangeMode or fileMode or pathMode:
        inFile = TFile(inPath)
        gatTree = inFile.Get("skimTree")
        print(gatTree.GetEntries(),"entries in input tree.")
    elif gatMode:
        inFile = TFile(gatPath)
        bltFile = TFile(bltPath)
        gatTree = inFile.Get("mjdTree")
        bltTree = bltFile.Get("MGTree")
        gatTree.AddFriend(bltTree)
    if singleMode:
        inFile = TFile(pathToInput)
        gatTree = inFile.Get("skimTree")

    # apply cut to tree
    if (rangeMode or fileMode or pathMode) and not dontUseTCuts:
        try:
            theCut = inFile.Get("theCut").GetTitle()
        except ReferenceError:
            theCut = ""
    if cutMode:
        # theCut += customPar
        # theCut = "(channel==672 || channel==674) && mH==2" # sync chan: 672, extp chan: 674
        # theCut += " && fitSlo < 10"
        # theCut = "trapENFCal > 1 && trapENFCal < 10 && riseNoise > 2"
        theCut = "trapENFCal > 20 && trapENFCal < 100 && riseNoise > 2"
        print("WARNING: Custom cut in use! : ",theCut)

    gatTree.Draw(">>elist", theCut, "entrylist")
    elist = gDirectory.Get("elist")
    gatTree.SetEntryList(elist)
    nList = elist.GetN()
    print("Using cut:\n",theCut)
    print("Found",gatTree.GetEntries(),"input entries.")
    print("Found",nList,"entries passing cuts.")

    # Output: In batch mode (-b) only, create an output file+tree & append new branches.
    if batMode and not intMode:
        outFile = TFile(outPath, "RECREATE")
        print("Attempting tree copy to",outPath)
        out = gatTree.CopyTree("")
        out.Write()
        print("Wrote",out.GetEntries(),"entries.")
        cutUsed = TNamed("theCut",theCut)
        cutUsed.Write()

    waveS1, waveS2 = std.vector("double")(), std.vector("double")()
    waveS3, waveS4, waveS5 = std.vector("double")(), std.vector("double")(), std.vector("double")()
    bcMax, bcMin = std.vector("double")(), std.vector("double")()
    bandMax, bandTime = std.vector("double")(), std.vector("double")()
    den10, den50, den90 = std.vector("double")(), std.vector("double")(), std.vector("double")()
    oppie = std.vector("double")()
    fitMu, fitAmp, fitSlo = std.vector("double")(), std.vector("double")(), std.vector("double")()
    fitTau, fitBL = std.vector("double")(), std.vector("double")()
    matchMax, matchWidth, matchTime = std.vector("double")(), std.vector("double")(), std.vector("double")()
    pol0, pol1, pol2, pol3 = std.vector("double")(), std.vector("double")(), std.vector("double")(), std.vector("double")()
    fails, fitChi2, fitLL = std.vector("int")(), std.vector("double")(), std.vector("double")()
    riseNoise = std.vector("double")()
    t0_SLE, t0_ALE, lat, latF = std.vector("double")(), std.vector("double")(), std.vector("double")(), std.vector("double")()
    latAF, latFC, latAFC = std.vector("double")(), std.vector("double")(), std.vector("double")()
    nMS = std.vector("int")()
    tE50, latE50, wfStd = std.vector("double")(), std.vector("double")(), std.vector("double")()
    wfAvgBL, wfRMSBL = std.vector("double")(), std.vector("double")()
    fitErr = std.vector("int")()

    # It's not possible to put the "out.Branch" call into a class initializer (waveLibs::latBranch). You suck, ROOT.
    b1, b2 = out.Branch("waveS1",waveS1), out.Branch("waveS2",waveS2)
    b3, b4, b5 = out.Branch("waveS3",waveS3), out.Branch("waveS4",waveS4), out.Branch("waveS5",waveS5)
    b7, b8 = out.Branch("bcMax",bcMax), out.Branch("bcMin",bcMin)
    b9, b10 = out.Branch("bandMax",bandMax), out.Branch("bandTime",bandTime)
    b11, b12, b13 = out.Branch("den10",den10), out.Branch("den50",den50), out.Branch("den90",den90)
    b14 = out.Branch("oppie",oppie)
    b15, b16, b17 = out.Branch("fitMu", fitMu), out.Branch("fitAmp", fitAmp), out.Branch("fitSlo", fitSlo)
    b18, b19 = out.Branch("fitTau",fitTau), out.Branch("fitBL",fitBL)
    b20, b21, b22 = out.Branch("matchMax", matchMax), out.Branch("matchWidth", matchWidth), out.Branch("matchTime", matchTime)
    b23, b24, b25, b26 = out.Branch("pol0", pol0), out.Branch("pol1", pol1), out.Branch("pol2", pol2), out.Branch("pol3", pol3)
    b27, b28, b29 = out.Branch("fails",fails), out.Branch("fitChi2",fitChi2), out.Branch("fitLL",fitLL)
    b30 = out.Branch("riseNoise",riseNoise)
    b31, b32, b33, b34 = out.Branch("t0_SLE",t0_SLE), out.Branch("t0_ALE",t0_ALE), out.Branch("lat",lat), out.Branch("latF",latF)
    b35, b36, b37 = out.Branch("latAF",latAF), out.Branch("latFC",latFC), out.Branch("latAFC",latAFC)
    b38 = out.Branch("nMS",nMS)
    b39, b40, b41 = out.Branch("tE50", tE50), out.Branch("latE50", latE50), out.Branch("wfStd", wfStd)
    b42, b43 = out.Branch("wfAvgBL", wfAvgBL), out.Branch("wfRMSBL", wfRMSBL)
    b44 = out.Branch("fitErr",fitErr)

    # make a dictionary that can be iterated over (avoids code repetition in the loop)
    brDict = {
        "waveS1":[waveS1, b1], "waveS2":[waveS2, b2],
        "waveS3":[waveS3, b3], "waveS4":[waveS4, b4], "waveS5":[waveS5, b5],
        "bcMax":[bcMax, b7], "bcMin":[bcMin, b8],
        "bandMax":[bandMax, b9], "bandTime":[bandTime, b10],
        "den10":[den10, b11], "den50":[den50, b12], "den90":[den90, b13],
        "oppie":[oppie, b14],
        "fitMu":[fitMu, b15], "fitAmp":[fitAmp, b16], "fitSlo":[fitSlo, b17],
        "fitTau":[fitTau, b18], "fitBL":[fitBL,b19],
        "matchMax":[matchMax, b20], "matchWidth":[matchWidth, b21], "matchTime":[matchTime, b22],
        "pol0":[pol0, b23], "pol1":[pol1, b24], "pol2":[pol2, b25], "pol3":[pol3, b26],
        "fails":[fails,b27], "fitChi2":[fitChi2,b28], "fitLL":[fitLL,b29],
        "riseNoise":[riseNoise,b30],
        "t0_SLE":[t0_SLE,b31], "t0_ALE":[t0_ALE,b32], "lat":[lat,b33], "latF":[latF,b34],
        "latAF":[latAF,b35], "latFC":[latFC,b36], "latAFC":[latAFC,b37],
        "nMS":[nMS,b38], "tE50":[tE50,b39], "latE50":[latE50,b40], "wfStd":[wfStd,b41],
        "wfAvgBL":[wfAvgBL,b42], "wfRMSBL":[wfRMSBL,b43],
        "fitErr":[fitErr,b44]
    }

    # Make a figure (-i option: select different plots)
    # fig = plt.figure(figsize=(12,9), facecolor='w')
    fig = plt.figure()
    if plotNum==0 or plotNum==7 or plotNum==8:
        p0 = plt.subplot(111)  # 0-raw waveform, 7-new trap filters
    elif plotNum==1 or plotNum==2:
        p0 = plt.subplot(211)  # 1-wavelet, 2-time points, bandpass filters, tail slope
        p1 = plt.subplot(212)
    elif plotNum==3:
        p0 = plt.subplot2grid((2,5), (0,0), colspan=3)  # oppie / freq-domain matched filter
        p1 = plt.subplot2grid((2,5), (0,3), colspan=2)
        p2 = plt.subplot2grid((2,5), (1,0), colspan=3)
    elif plotNum==4:
        p0 = plt.subplot(111)  # time-domain matched filter
    elif plotNum==5:
        p0 = plt.subplot(111)  # bandpass / bandTime
    elif plotNum==6:
        p0 = plt.subplot2grid((6,10), (0,0), colspan=10, rowspan=3) # waveform fit
        p1 = plt.subplot2grid((6,10), (3,0), colspan=10, rowspan=1) # residual
        p2 = plt.subplot2grid((6,10), (4,0), colspan=2, rowspan=2) # traces
        p3 = plt.subplot2grid((6,10), (4,2), colspan=2, rowspan=2)
        p4 = plt.subplot2grid((6,10), (4,4), colspan=2, rowspan=2)
        p5 = plt.subplot2grid((6,10), (4,6), colspan=2, rowspan=2)
        p6 = plt.subplot2grid((6,10), (4,8), colspan=2, rowspan=2)
    elif plotNum==9:
        p0 = plt.subplot2grid((5,1), (0,0)) # 9- wpt on wf fit residual
        p1 = plt.subplot2grid((5,1), (1,0), rowspan=2)
        p2 = plt.subplot2grid((5,1), (3,0), rowspan=2)
    if not batMode: plt.show(block=False)


    # Load a fast signal template - used w/ the freq-domain matched filter
    # print("Generating signal template ...")
    tSamp, tR, tZ, tAmp, tST, tSlo = 5000, 0, 15, 100, 2500, 10
    # tOrig, tOrigTS = wl.MakeSiggenWaveform(tSamp,tR,tZ,tAmp,tST,tSlo) # Damn you to hell, PDSF
    templateFile = np.load("%s/data/lat_template.npz" % os.environ['LATDIR'])
    if dsNum==2 or dsNum==6: templateFile = np.load("%s/data/lat_ds2template.npz" % os.environ['LATDIR'])
    tOrig, tOrigTS = templateFile['arr_0'], templateFile['arr_1']


    # Load stuff from DS1 forced acq. runs
    npzfile = np.load("%s/data/fft_forcedAcqDS1.npz" % os.environ['LATDIR'])
    noise_asd, noise_xFreq, avgPwrSpec, xPwrSpec, data_forceAcq, data_fft = npzfile['arr_0'],npzfile['arr_1'],npzfile['arr_2'],npzfile['arr_3'],npzfile['arr_4'],npzfile['arr_5']


    # Loop over events
    print("Starting event loop ...")
    iList = -1
    while True:
        iList += 1
        if intMode==True and iList != 0:
            value = input()
            if value=='q': break        # quit
            if value=='p': iList -= 2   # go to previous
            if (value.isdigit()):
                iList = int(value)      # go to entry number
        elif intMode==False and batMode==False:
            plt.pause(0.00001)          # rapid-draw mode
        if iList >= nList: break        # bail out, goose!

        entry = gatTree.GetEntryNumber(iList);
        gatTree.LoadTree(entry)
        gatTree.GetEntry(entry)
        nChans = gatTree.channel.size()
        event = MGTEvent()
        if gatMode: event = bltTree.event

        # Reset all branch vectors
        # NOTE: The events sometimes contain 'straggler' hits that do not pass the
        # given TCut.  This line sets ALL the new parameters to -88888 by default.
        # If you see this value in a plot, then you must be including hits that
        # passed the cut in wave-skim but did not pass the (different?) cut in LAT.
        for key in brDict: brDict[key][0].assign(nChans,-88888)
        brDict["fails"][0].assign(nChans,0) # set error code to 'true' by default
        errorCode = [0,0,0,0]


        # Loop over hits passing cuts
        numPass = gatTree.Draw("channel",theCut,"GOFF",1,iList)
        chans = gatTree.GetV1()
        chanList = list(set(int(chans[n]) for n in range(numPass)))
        hitList = (iH for iH in range(nChans) if gatTree.channel.at(iH) in chanList)  # a 'generator expression'
        for iH in hitList:

            # ------------------------------------------------------------------------
            # Waveform processing

            # load data
            run = gatTree.run
            chan = gatTree.channel.at(iH)
            dataENFCal = gatTree.trapENFCal.at(iH)
            dataENM = gatTree.trapENM.at(iH)
            dataTSMax = gatTree.trapENMSample.at(iH)*10. - 4000
            wf = MGTWaveform()
            iEvent = 0
            if gatMode:
                wf = event.GetWaveform(iH)
                iEvent = entry
            else:
                wf = gatTree.MGTWaveforms.at(iH)
                iEvent = gatTree.iEvent

            # print("%d:  run %d  chan %d  trapENFCal %.2f" % (iList, run, chan, dataENFCal))

            # be absolutely sure you're matching the right waveform to this hit
            if wf.GetID() != chan:
                print("ERROR -- Vector matching failed.  iList %d  run %d  iEvent %d" % (iList,run,iEvent))
                return

            # Let's start the show - grab a waveform.
            # Remove first 4 samples when we have multisampling
            # Remove last 2 samples to get rid of the ADC spike at the end of all wf's.
            truncLo, truncHi = 0, 2
            if dsNum==6 or dsNum==2: truncLo = 4
            signal = wl.processWaveform(wf,truncLo,truncHi)
            data = signal.GetWaveRaw()
            data_blSub = signal.GetWaveBLSub()
            dataTS = signal.GetTS()
            dataBL,dataNoise = signal.GetBaseNoise()

            # wavelet packet transform
            wp = pywt.WaveletPacket(data_blSub, 'db2', 'symmetric', maxlevel=4)
            nodes = wp.get_level(4, order='freq')
            wpCoeff = np.array([n.data for n in nodes],'d')
            wpCoeff = abs(wpCoeff)

            # wavelet parameters
            # First get length of wavelet on the time axis, the scale axis will always be the same
            # due to the number of levels in the wavelet
            wpLength = len(wpCoeff[1,:])
            waveS1[iH] = np.sum(wpCoeff[0:1,1:wpLength//4+1]) # python3 : floor division (//) returns an int
            waveS2[iH] = np.sum(wpCoeff[0:1,wpLength//4+1:wpLength//2+1])
            waveS3[iH] = np.sum(wpCoeff[0:1,wpLength//2+1:3*wpLength//4+1])
            waveS4[iH] = np.sum(wpCoeff[0:1,3*wpLength//4+1:-1])
            waveS5[iH] = np.sum(wpCoeff[2:-1,1:-1])
            S6 = np.sum(wpCoeff[2:9,1:wpLength//4+1])
            S7 = np.sum(wpCoeff[2:9,wpLength//4+1:wpLength//2+1])
            S8 = np.sum(wpCoeff[2:9,wpLength//2+1:3*wpLength//4+1])
            S9 = np.sum(wpCoeff[2:9,3*wpLength//4+1:-1])
            S10 = np.sum(wpCoeff[9:,1:wpLength//4+1])
            S11 = np.sum(wpCoeff[9:,wpLength//4+1:wpLength//2+1])
            S12 = np.sum(wpCoeff[9:,wpLength//2+1:3*wpLength//4+1])
            S13 = np.sum(wpCoeff[9:,3*wpLength//4+1:-1])
            sumList = [S6, S7, S8, S9, S10, S11, S12, S13]
            bcMax[iH] = np.max(sumList)
            bcMin[iH] = 1. if np.min(sumList) < 1 else np.min(sumList)

            # reconstruct waveform w/ only lowest frequency.
            new_wp = pywt.WaveletPacket(data=None, wavelet='db2', mode='symmetric')
            new_wp['aaa'] = wp['aaa'].data
            data_wlDenoised = new_wp.reconstruct(update=False)
            # resize in a smart way
            diff = len(data_wlDenoised) - len(data_blSub)
            if diff > 0: data_wlDenoised = data_wlDenoised[diff:]

            # waveform high/lowpass filters - parameters are a little arbitrary

            B1,A1 = butter(2, [1e5/(1e8/2),1e6/(1e8/2)], btype='bandpass')
            data_bPass = lfilter(B1, A1, data_blSub)

            # used in the multisite tagger
            B2, A2 = butter(1, 0.08)
            data_filt = filtfilt(B2, A2, data_blSub)
            data_filtDeriv = wl.wfDerivative(data_filt)
            filtAmp = np.amax(data_filtDeriv) # scale the max to match the amplitude
            data_filtDeriv = data_filtDeriv * (dataENM / filtAmp)

            B3, A3 = butter(2,1e6/(1e8/2), btype='lowpass')
            data_lPass = lfilter(B3, A3, data_blSub)

            idx = np.where((dataTS > dataTS[0]+100) & (dataTS < dataTS[-1]-100))
            windowingOffset = dataTS[idx][0] - dataTS[0]

            bandMax[iH] = np.amax(data_bPass[idx])
            bandTime[iH] = dataTS[ np.argmax(data_bPass[idx])] - windowingOffset


            # timepoints of low-pass waveforms
            tpc = MGWFTimePointCalculator();
            tpc.AddPoint(.2)
            tpc.AddPoint(.5)
            tpc.AddPoint(.9)
            mgtLowPass = wl.MGTWFFromNpArray(data_lPass)
            tpc.FindTimePoints(mgtLowPass)
            den10[iH] = tpc.GetFromStartRiseTime(0)*10
            den50[iH] = tpc.GetFromStartRiseTime(1)*10
            den90[iH] = tpc.GetFromStartRiseTime(2)*10


            # ================ xgauss waveform fitting ================

            amp, mu, sig, tau, bl = dataENM, dataTSMax, 600., -72000., dataBL
            floats = np.asarray([amp, mu, sig, tau, bl])
            temp = xgModelWF(dataTS, floats)
            if not batMode: MakeTracesGlobal()

            # get the noise of the denoised wf
            denoisedNoise,_,_ = wl.baselineParameters(data_wlDenoised)

            # NOTE: fit is to wavelet-denoised data, BECAUSE there are no HF components in the model,
            # AND we'll still calculate fitChi2 w/r/t the data, not the denoised data.
            # datas = [dataTS, data, dataNoise] # fit data
            datas = [dataTS, data_wlDenoised + dataBL, denoisedNoise] # fit wavelet-denoised data w/ Bl added back in

            # Set bounds - A,mu,sig,tau,bl.
            # bnd = ((None,None),(None,None),(None,None),(None,None),(None,None))   # often gets caught at sig=0
            bnd = ((None,None),(None,None),(2.,None),(-72001.,-71999.),(None,None)) # gets caught much less often.

            # L-BGFS-B with numerical gradient.
            start = time.clock()
            result = op.minimize(lnLike, floats, args=datas, method="L-BFGS-B", options=None, bounds=bnd)
            fitSpeed = time.clock() - start

            fitErr[iH] = 0
            if not result["success"]:
                # print("fit fail: ", result["message"])
                fitErr[iH] = 1
                errorCode[0] = 1

            amp, mu, sig, tau, bl = result["x"]

            # save parameters

            fitMu[iH], fitAmp[iH], fitSlo[iH], fitTau[iH], fitBL[iH] = mu, amp, sig, tau, bl
            floats = np.asarray([amp, mu, sig, tau, bl])
            fit = xgModelWF(dataTS, floats)

            # print("%d/%d iH %d  e %-10.2f  fs %-8.2f  f %d" % (iList, nList, iH, dataENFCal, fitSlo[iH], fitErr[iH]))

            # log-likelihood of this fit
            fitLL[iH] = result["fun"]

            # chi-square of this fit
            # Textbook is (observed - expected)^2 / expected,
            # but we'll follow MGWFCalculateChiSquare.cc and do (observed - expected)^2 / NDF.
            # NOTE: we're doing the chi2 against the DATA, though the FIT is to the DENOISED DATA.
            fitChi2[iH] = np.sum(np.square(data-fit)) / (len(data)-1)/dataNoise


            # get wavelet coeff's for rising edge only.  normalize to bcMin
            # view this w/ plot 1

            # find the window of rising edge
            fit_blSub = fit - bl
            fitMaxTime = dataTS[np.argmax(fit_blSub)]
            fitStartTime = dataTS[0]
            idx = np.where(fit_blSub < 0.1)
            if len(dataTS[idx] > 0): fitStartTime = dataTS[idx][-1]
            fitRiseTime50 = (fitMaxTime + fitStartTime)/2.

            # bcMin is 32 samples long in the x-direction.
            # if we make the window half as wide, it'll have the same # of coeff's as bcMin.
            # this is still 'cheating' since we're not summing over the same rows.
            numXRows = wpCoeff.shape[1]
            wpCtrRise = int((fitRiseTime50 - dataTS[0]) / (dataTS[-1] - dataTS[0]) * numXRows)
            wpLoRise = wpCtrRise - 8
            if wpLoRise < 0: wpLoRise = 0
            wpHiRise = wpCtrRise + 8
            if wpHiRise > numXRows: wpHiRise = numXRows

            # sum all HF wavelet components for this edge.
            riseNoise[iH] = np.sum(wpCoeff[2:-1,wpLoRise:wpHiRise]) / bcMin[iH]

            # print("%d %d %d %d e %-5.2f  bmax %-6.2f  bmin %-6.2f  mu %-5.2f  a %-5.2f  s %-5.2f  bl %-5.2f  rn %.2f" % (run,iList,iH,chan,dataENFCal,bcMax[iH],bcMin[iH],fitMu[iH],fitAmp[iH],fitSlo[iH],fitBL[iH],riseNoise[iH]))

            # =========================================================

            # optimal matched filter (freq. domain)
            # we use the pysiggen fast template (not the fit result) to keep this independent of the wf fitter.

            # pull in the template, shift it, and make sure it's the same length as the data
            guessTS = tOrigTS - 15000.
            idx = np.where((guessTS > -5) & (guessTS < dataTS[-1]))
            guessTS, guess = guessTS[idx], tOrig[idx]
            if len(guess)!=len(data):
                if len(guess)>len(data):
                    guess, guessTS = guess[0:len(data)], guessTS[0:len(data)]
                else:
                    guess = np.pad(guess, (0,len(data)-len(guess)), 'edge')
                    guessTS = np.pad(guessTS, (0,len(data)-len(guessTS)), 'edge')

            data_fft = np.fft.fft(data_blSub) # can also try taking fft of the low-pass data
            temp_fft = np.fft.fft(guess)

            datafreq = np.fft.fftfreq(data.size) * 1e8
            power_vec = np.interp(datafreq, noise_xFreq, noise_asd) # load power spectra from file

            # Apply the filter
            optimal = data_fft * temp_fft.conjugate() / power_vec
            optimal_time = 2 * np.fft.ifft(optimal)

            # Normalize the output
            df = np.abs(datafreq[1] - datafreq[0]) # freq. bin size
            sigmasq = 2 * (temp_fft * temp_fft.conjugate() / power_vec).sum() * df
            sigma = np.sqrt(np.abs(sigmasq))
            SNR = abs(optimal_time) / (sigma)
            oppie[iH] = np.amax(SNR)


            # time-domain matched filter.  use the baseline-subtracted wf as data, and fit_blSub too.

            # make a longer best-fit waveform s/t it can be shifted L/R.
            matchTS = np.append(dataTS, np.arange(dataTS[-1], dataTS[-1] + 20000, 10)) # add 2000 samples
            match = xgModelWF(matchTS, [amp, mu+10000., sig, tau, bl]) # shift mu accordingly
            match = match[::-1] - bl # time flip and subtract off bl

            # line up the max of the 'match' (flipped wf) with the max of the best-fit wf
            # this kills the 1-1 matching between matchTS and dataTS (each TS has some offset)
            matchMaxTime = matchTS[np.argmax(match)]
            matchTS = matchTS + (fitMaxTime - matchMaxTime)

            # resize match, matchTS to have same # samples as data, dataTS.
            # this is the only case we really care about
            # ("too early" and "too late" also happen, but the shift is larger than the trigger walk, making it unphysical)
            if matchTS[0] <= dataTS[0] and matchTS[-1] >= dataTS[-1]:
                idx = np.where((matchTS >= dataTS[0]) & (matchTS <= dataTS[-1]))
                match, matchTS = match[idx], matchTS[idx]
                sizeDiff = len(dataTS)-len(matchTS)
                if sizeDiff < 0:
                    match, matchTS = match[:sizeDiff], matchTS[:sizeDiff]
                elif sizeDiff > 0:
                    match = np.hstack((match, np.zeros(sizeDiff)))
                    matchTS = np.hstack((matchTS, dataTS[-1*sizeDiff:]))
                if len(match) != len(data):
                    print("FIXME: match filter array manip is still broken.")

            # compute match filter parameters
            matchMax[iH], matchWidth[iH], matchTime[iH] = -888, -888, -888
            if len(match)==len(data):
                smoothMF = gaussian_filter(match * data_blSub, sigma=5.)
                matchMax[iH] = np.amax(smoothMF)
                matchTime[iH] = matchTS[ np.argmax(smoothMF) ]
                idx = np.where(smoothMF > matchMax[iH]/2.)
                if len(matchTS[idx]>1):
                    matchWidth[iH] = matchTS[idx][-1] - matchTS[idx][0]


            # Fit tail slope to polynomial.  Guard against fit fails

            idx = np.where(dataTS >= fitMaxTime)
            tail, tailTS = data[idx], dataTS[idx]
            popt1,popt2 = 0,0
            try:
                popt1,_ = op.curve_fit(wl.tailModelPol, tailTS, tail)
                pol0[iH], pol1[iH], pol2[iH], pol3[iH] = popt1[0], popt1[1], popt1[2], popt1[3]
            except:
                # print("curve_fit tailModelPol failed, run %i  event %i  channel %i" % (run, iList, chan))
                errorCode[2] = 1
                pass

            # =========================================================

            # new trap filters.
            # params: t0_SLE, t0_ALE, lat, latF, latAF, latFC, latAFC

            # calculate trapezoids

            # standard trapezoid - prone to walking, less sensitive to noise.  use to find energy
            eTrap = wl.trapFilter(data_blSub, 400, 250, 7200.)
            eTrapTS = np.arange(0, len(eTrap)*10., 10)
            eTrapInterp = interpolate.interp1d(eTrapTS, eTrap)

            # short trapezoid - triggers more quickly, sensitive to noise.  use to find t0
            sTrap = wl.trapFilter(data_blSub, 100, 150, 7200.)
            sTrapTS = np.arange(0, len(sTrap)*10., 10)

            # asymmetric trapezoid - used to find the t0 only
            aTrap = wl.asymTrapFilter(data_blSub, 4, 10, 200, True) # (0.04us, 0.1us, 2.0us)
            aTrapTS = np.arange(0, len(aTrap)*10., 10)

            # find leading edges (t0 times)

            # limit the range from 0 to 10us, and use an ADC threshold of 1.0 as suggested by DCR
            t0_SLE[iH],_ = wl.walkBackT0(sTrap, eTrapTS[-1]+7000-4000-2000, 1., 0, 1000) # (in ns) finds leading edge from short trap
            t0_ALE[iH],_ = wl.walkBackT0(aTrap, eTrapTS[-1]+7000-4000-2000, 1., 0, 1000) # (in ns) finds leading edge from asymmetric trap

            # standard energy trapezoid w/ a baseline padded waveform
            data_pad = np.pad(data_blSub,(200,0),'symmetric')
            pTrap = wl.trapFilter(data_pad, 400, 250, 7200.)
            pTrapTS = np.linspace(0, len(pTrap)*10, len(pTrap))
            pTrapInterp = interpolate.interp1d(pTrapTS, pTrap)

            # calculate energy parameters
            # standard amplitude.  basically trapEM, but w/o NL correction if the input WF doesn't have it.
            lat[iH] = np.amax(eTrap)

            # Calculate DCR suggested amplitude, using the 50% to the left and right of the maximum point
            t0_F50,t0fail1 = wl.walkBackT0(pTrap, thresh=lat[iH]*0.5, rmin=0, rmax=len(pTrap)-1)
            t0_B50,t0fail2 = wl.walkBackT0(pTrap, thresh=lat[iH]*0.5, rmin=0, rmax=len(pTrap)-1, forward=True)
            t0_E50 = (t0_F50 + t0_B50)/2.0

            #TODO -- if it's necessary due to the trigger walk, we could potentially add a way to recursively increase the threshold until a timepoint is found, however it will still always fail for most noise events
            if not t0fail1 or not t0fail2:
                latE50[iH] = 0 # Set amplitude to 0 if one of the evaluations failed
            else:
                latE50[iH] = pTrapInterp(t0_E50) # Maybe I should call this latDCR50 to confuse people
            tE50[iH] = t0_B50 - t0_F50 # Save the difference between the middle points, can be used as a cut later

            # standard amplitude with t0 from the shorter traps
            # If either fixed pickoff time (t0) is < 0, use the first sample as the amplitude (energy).
            latF[iH] = eTrapInterp( np.amax([t0_SLE[iH]-7000+4000+2000, 0.]) ) # This should be ~trapEF
            latAF[iH] = eTrapInterp( np.amax([t0_ALE[iH]-7000+4000+2000, 0.]) )

            # amplitude from padded trapezoid, with t0 from short traps and a correction function
            # function is under development.  currently: f() = exp(p0 + p1*E), p0 ~ 7.8, p1 ~ -0.45 and -0.66
            # functional walk back distance is *either* the minimum of the function value, or 5500 (standard value)

            # t0_corr = -7000+6000+2000 # no correction
            t0_corr = -7000+6000+2000 - np.amin([np.exp(7.8 - 0.45*lat[iH]),1000.])
            t0A_corr = -7000+6000+2000 - np.amin([np.exp(7.8 - 0.66*lat[iH]),1000.])

            latFC[iH] = pTrapInterp( np.amax([t0_SLE[iH] + t0_corr, 0.]) )
            latAFC[iH] = pTrapInterp( np.amax([t0_ALE[iH] + t0A_corr, 0.]) )


            # =========================================================

            # the genius multisite event tagger - plot 8

            # decide a threshold
            dIdx = np.argmax(data_filtDeriv)
            dMax = data_filtDeriv[dIdx]
            dRMS,_,_ = wl.baselineParameters(data_filtDeriv)
            # msThresh = np.amax([dMax * .2, dRMS * 5.])
            # msThresh = dMax * .15
            msThresh = 50.  # I don't know.  this seems like a good value

            # run peak detect algorithm
            maxtab,_ = wl.peakdet(data_filtDeriv, msThresh)

            # profit
            msList = []
            for iMax in range(len(maxtab)):
                idx = int(maxtab[iMax][0])
                val = maxtab[iMax][1]
                msList.append(dataTS[idx])
                # print("%d  idx %d  TS %d  val %.2f  thresh %.2f" % (iList, idx, dataTS[idx], val, msThresh))
            nMS[iH] = len(maxtab)

            # =========================================================
            # wfStd analysis
            wfAvgBL[iH] = dataBL
            wfRMSBL[iH] = dataNoise
            wfStd[iH] = np.std(data[5:-5])

            # ------------------------------------------------------------------------
            # End waveform processing.

            # Calculate error code
            fails[iH] = 0
            for i,j in enumerate(errorCode):
                if j==1: fails[iH] += int(j)<<i
            # print("fails:",fails[iH])

            # Make plots!
            if batMode: continue
            if plotNum==0: # raw data
                p0.cla()
                p0.plot(dataTS,data,'b')
                p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f" % (run,iList,chan,dataENFCal))
                p0.set_xlabel("Time (ns)", ha='right', x=1.)
                p0.set_ylabel("Voltage (ADC)", ha='right', y=1.)

            if plotNum==1: # wavelet plot
                p0.cla()
                p0.margins(x=0)
                p0.plot(dataTS,data_blSub,color='blue',label='data (%.2f keV)' % dataENFCal)
                p0.plot(dataTS,data_wlDenoised,color='cyan',label='denoised',alpha=0.7)
                p0.axvline(fitRiseTime50,color='green',label='fit 50%',linewidth=2)
                p0.plot(dataTS,fit_blSub,color='red',label='bestfit',linewidth=2)
                # p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f  flo %.0f  fhi %.0f  fhi-flo %.0f" % (run,iList,chan,dataENFCal,fitStartTime,fitMaxTime,fitMaxTime-fitStartTime))
                p0.legend(loc='best')
                p0.set_xlabel("Time (ns)", ha='right', x=1.)
                p0.set_ylabel("Voltage (ADC)", ha='right', y=1.)

                p1.cla()
                p1.imshow(wpCoeff, interpolation='nearest', aspect="auto", origin="lower",extent=[0, 1, 0, len(wpCoeff)],cmap='viridis')
                p1.axvline(float(wpLoRise)/numXRows,color='orange',linewidth=2)
                p1.axvline(float(wpHiRise)/numXRows,color='orange',linewidth=2)
                # p1.set_title("waveS5 %.2f  bcMax %.2f  bcMin %.2f  riseNoise %.2f" % (waveS5[iH], bcMax[iH], bcMin[iH], riseNoise[iH]))
                # p1.set_xlabel("Time (%wf)", ha='right', x=1.)
                p1.set_ylabel("WPT Coefficients", ha='right', y=1.)

            if plotNum==2: # time points, bandpass filters, tail slope
                p0.cla()
                p0.plot(dataTS,data,color='blue',label='data')
                p0.axvline(den10[iH],color='black',label='lpTP')
                p0.axvline(den50[iH],color='black')
                p0.axvline(den90[iH],color='black')
                p0.plot(dataTS,fit,color='magenta',label='bestfit')
                if errorCode[2]!=1: p0.plot(tailTS, wl.tailModelPol(tailTS, *popt1), color='orange',linewidth=2, label='tailPol')
                p0.legend(loc='best')
                p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f" % (run,iEvent,chan,dataENFCal))

                p1.cla()
                p1.plot(dataTS,data_lPass,color='blue',label='lowpass')
                p1.plot(dataTS,data_filtDeriv,color='green',label='filtDeriv')
                p1.plot(dataTS,data_filt,color='black',label='filtfilt')
                p1.plot(dataTS,data_bPass,color='red',label='bpass')
                p1.axvline(bandTime[iH],color='orange',label='bandTime')
                p1.legend(loc='best')

            if plotNum==3: # freq-domain matched filter
                p0.cla()
                p0.plot(dataTS,data,'b')
                p0.plot(dataTS,temp,'r')
                p0.plot(dataTS,fit,color='cyan')
                p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f" % (run,iEvent,chan,dataENFCal))

                data_asd, data_xFreq = plt.psd(data, Fs=1e8, NFFT=2048, pad_to=2048, visible=False)
                temp_asd, temp_xFreq = plt.psd(temp, Fs=1e8, NFFT=2048, pad_to=2048, visible=False)

                p1.cla()
                p1.loglog(data_xFreq, np.sqrt(data_asd), 'b')
                p1.loglog(noise_xFreq, np.sqrt(noise_asd), 'g')
                p1.loglog(temp_xFreq, np.sqrt(temp_asd), 'r')
                p1.set_xlabel('Frequency (Hz)')
                p1.set_ylabel('ASD')
                p1.grid('on')

                p2.cla()
                p2.plot(dataTS, SNR)
                p2.set_title('oppie %.1f' % (oppie[iH]))
                p2.set_xlabel('Offset time (s)')
                p2.set_ylabel('SNR')

            if plotNum==4: # time domain match filter plot
                p0.cla()
                p0.plot(dataTS,data_blSub,color='blue',label='data',alpha=0.7)
                p0.plot(dataTS,fit_blSub,color='red',label='bestfit',linewidth=3)
                p0.axvline(matchTime[iH],color='orange',label='matchTime',linewidth=2)
                p0.plot(matchTS,smoothMF,color='magenta',label='smoothMF',linewidth=3)
                p0.plot(matchTS,match,color='cyan',label='match',linewidth=3)
                p0.set_xlabel('Time (s)')
                p0.set_ylabel('Voltage (arb)')
                p0.legend(loc='best')
                p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f  matchMax %.2f  matchTime %.2f  matchWidth %.2f" % (run,iEvent,chan,dataENFCal,matchMax[iH],matchTime[iH],matchWidth[iH]))

            if plotNum==5: # bandTime plot
                p0.cla()
                p0.plot(dataTS,data_blSub,color='blue',label='data',alpha=0.7)
                p0.plot(dataTS,data_lPass,color='magenta',label='lowpass',linewidth=4)
                p0.plot(dataTS,data_bPass,color='red',label='bpass',linewidth=4)
                p0.axvline(bandTime[iH],color='orange',label='bandTime',linewidth=4)
                p0.legend(loc='best')
                p0.set_xlabel('Time (ns)')
                p0.set_ylabel('ADC (arb)')
                p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f" % (run,iEvent,chan,dataENFCal))

            if plotNum==6: # waveform fit plot
                p0.cla()
                p0.plot(dataTS,data,color='blue',label='data')
                # p0.plot(dataTS,data_wlDenoised,color='cyan',label='wlDenoised',alpha=0.5)
                p0.plot(dataTS,temp,color='orange',label='xgauss guess')
                p0.plot(dataTS,fit,color='red',label='xgauss fit')
                p0.set_title("Run %d  evt %d  chan %d  trapENFCal %.1f  trapENM %.1f  deltaBL %.1f\n  amp %.2f  mu %.2f  sig %.2f  tau %.2f  chi2 %.2f  spd %.3f" % (run,iList,chan,dataENFCal,dataENM,dataBL-bl,amp,mu,sig,tau,fitChi2[iH],fitSpeed))
                p0.legend(loc='best')
                p1.cla()
                p1.plot(dataTS,data-fit,color='blue',label='residual')
                p1.legend(loc='best')
                p2.cla()
                p2.plot(ampTr[1:],label='amp',color='red')
                p2.legend(loc='best')
                p3.cla()
                p3.plot(muTr[1:],label='mu',color='green')
                p3.legend(loc='best')
                p4.cla()
                p4.plot(sigTr[1:],label='sig',color='blue')
                p4.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
                p4.legend(loc='best')
                p5.cla()
                p5.plot(tauTr[1:],label='tau',color='black')
                p5.legend(loc='best')
                p6.cla()
                p6.plot(blTr[1:],label='bl',color='magenta')
                p6.legend(loc='best')

                print(gatTree.fitSlo.at(iH), sig)

            if plotNum==7: # new traps plot
                p0.cla()
                p0.plot(dataTS, data_blSub, color='blue', label='data')
                p0.plot(sTrapTS, sTrap, color='red', label='sTrap')
                p0.axvline(t0_SLE[iH], color='red')
                p0.plot(aTrapTS, aTrap, color='orange', label='aTrap')
                p0.axvline(t0_ALE[iH], color='orange')
                p0.plot(eTrapTS, eTrap, color='green', label='eTrap')
                p0.axhline(lat[iH],color='green')
                p0.plot(pTrapTS, pTrap, color='magenta', label='pTrap')
                p0.axhline(latAFC[iH], color='magenta')
                p0.axhline(latE50[iH], color='cyan')
                p0.set_title("trapENFCal %.2f  trapENM %.2f || latEM %.2f  latEF %.2f  latEAF %.2f  latEFC %.2f  latEAFC %.2f  latE50 %.2f" % (dataENFCal,dataENM,lat[iH],latF[iH],latAF[iH],latFC[iH],latAFC[iH], latE50[iH]))
                p0.legend(loc='best')

            if plotNum==8: # multisite tag plot
                p0.cla()
                p0.plot(dataTS, data_blSub, color='blue', label='data')
                p0.plot(dataTS, data_filtDeriv, color='red', label='filtDeriv')
                for mse in msList: p0.axvline(mse, color='green')
                p0.axhline(msThresh,color='red')
                p0.legend()

            if plotNum==9: # wavelet vs wf fit residual plot

                # wavelet packet transform on wf fit residual
                fitResid = data-fit
                wpRes = pywt.WaveletPacket(fitResid, 'db2', 'symmetric', maxlevel=4)
                nodesRes = wpRes.get_level(4, order='freq')
                wpCoeffRes = np.array([n.data for n in nodesRes], 'd')
                wpCoeffRes = abs(wpCoeffRes)
                R6 = np.sum(wpCoeffRes[2:9,1:wpLength//4+1])
                R7 = np.sum(wpCoeffRes[2:9,wpLength//4+1:wpLength//2+1])
                R8 = np.sum(wpCoeffRes[2:9,wpLength//2+1:3*wpLength//4+1])
                R9 = np.sum(wpCoeffRes[2:9,3*wpLength//4+1:-1])
                R10 = np.sum(wpCoeffRes[9:,1:wpLength//4+1])
                R11 = np.sum(wpCoeffRes[9:,wpLength//4+1:wpLength//2+1])
                R12 = np.sum(wpCoeffRes[9:,wpLength//2+1:3*wpLength//4+1])
                R13 = np.sum(wpCoeffRes[9:,3*wpLength//4+1:-1])
                RsumList = [R6, R7, R8, R9, R10, R11, R12, R13]
                bcMinRes = 1. if np.min(RsumList) < 1 else np.min(RsumList)
                riseNoiseRes = np.sum(wpCoeffRes[2:-1,wpLoRise:wpHiRise]) / bcMinRes
                rnCut = 1.1762 + 0.00116 * np.log(1 + np.exp((dataENFCal-7.312)/0.341))

                p0.cla()
                p0.margins(x=0)
                p0.plot(dataTS,data_blSub,color='blue',label='data')
                # p0.plot(dataTS,data_wlDenoised,color='cyan',label='denoised',alpha=0.7)
                # p0.axvline(fitRiseTime50,color='green',label='fit 50%',linewidth=2)
                p0.plot(dataTS,fit_blSub,color='red',label='bestfit',linewidth=2)
                # p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f  flo %.0f  fhi %.0f  fhi-flo %.0f" % (run,iList,chan,dataENFCal,fitStartTime,fitMaxTime,fitMaxTime-fitStartTime))
                # p0.legend(loc='best')
                p0.set_title("Run %d  Entry %d  Channel %d  ENFCal %.2f  flo %.0f  fhi %.0f  fhi-flo %.0f  approxFitE %.2f" % (run,iList,chan,dataENFCal,fitStartTime,fitMaxTime,fitMaxTime-fitStartTime,fitAmp[iH]*0.4))

                p1.cla()
                p1.plot(dataTS,fitResid,color='blue')

                p2.cla()
                p2.set_title("riseNoise %.2f  rnCut %.2f  riseNoiseRes %.2f  bcMinRes %.2f  bcMin %.2f  max %.2f" % (riseNoise[iH],rnCut,riseNoiseRes,bcMinRes,bcMin[iH],wpCoeffRes.max()))
                p2.imshow(wpCoeffRes, interpolation='nearest', aspect="auto", origin="lower",extent=[0, 1, 0, len(wpCoeff)],cmap='viridis')

            plt.tight_layout()
            plt.pause(0.000001)
            # ------------------------------------------------------------------------

        # End loop over hits, fill branches
        if batMode:
            for key in brDict:
                brDict[key][1].Fill()
            if iList%5000 == 0 and iList!=0:
                out.Write("",TObject.kOverwrite)
                print("%d / %d entries saved (%.2f %% done), time: %s" % (iList,nList,100*(float(iList)/nList),time.strftime('%X %x %Z')))

    # End loop over events
    if batMode and not intMode:
        out.Write("",TObject.kOverwrite)
        print("Wrote",out.GetBranch("channel").GetEntries(),"entries in the copied tree,")
        print("and wrote",b1.GetEntries(),"entries in the new branches.")

    stopT = time.clock()
    print("Stopped:",time.strftime('%X %x %Z'),"\nProcess time (min):",(stopT - startT)/60)
    print(float(nList)/((stopT-startT)/60.),"entries per minute.")
def main():

    INPUT_FILE_INCLUDING_PATH = "../../../root_generator/tree/root2_tree.root"

    FileNameList = read_file_name(INPUT_FILE_INCLUDING_PATH)
    BranchListAll = get_branch_list_all(FileNameList[2])
    BranchListEachTree = get_branch_list_each_tree(FileNameList[2])

    DicNumpyArray_branch = {}
    for numpyarray in BranchListAll:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    print(DicNumpyArray_branch)

    DicNumpyArray_branch_w = {}
    for numpyarray_w in BranchListAll:
        a_w = numpy.array([0], 'd')
        DicNumpyArray_branch_w[numpyarray_w] = a_w
    DicNumpyArray_branch_w = collections.OrderedDict(
        sorted(DicNumpyArray_branch_w.items()))
    print(DicNumpyArray_branch_w)

    WCuts = WhetherAddCut(BranchListEachTree)
    if (WCuts == None):
        print("\n")
        print(
            "*********************************************************************************************"
        )
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print("\n")
        for i in range(len(BranchListEachTree)):
            print("compenets below :")
            print("The Tree name is : ", BranchListEachTree.keys()[i])
            for j in range(len(BranchListEachTree.values()[i])):
                print("        it contains : ",
                      BranchListEachTree.values()[i][j])
            print("\n")
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print(
            "*********************************************************************************************"
        )
    else:
        print("\n")
        print("...Cut added and regenerating tree root file...")
        gBenchmark.Start("Regerating tree root")

        f = TFile(FileNameList[2], "READ")
        dirlist = f.GetListOfKeys()
        ITER = dirlist.MakeIterator()
        key = ITER.Next()
        outfileName = FileNameList[0] + "_cut_tree.root"
        outfile = TFile(outfileName, "RECREATE")
        ijk = 0
        while key:
            tree = key.ReadObj()
            tree_f = TTree(tree.GetName() + "_f", tree.GetName() + "_f")
            ENTRY = tree.GetEntries()
            #print(ENTRY)
            for i in range(len(DicNumpyArray_branch)):
                tree.SetBranchAddress(DicNumpyArray_branch.keys()[i],
                                      DicNumpyArray_branch.values()[i])
                tree_f.Branch(DicNumpyArray_branch_w.keys()[i],
                              DicNumpyArray_branch_w.values()[i],
                              DicNumpyArray_branch_w.keys()[i] + "/D")
            print("for tree", tree.GetName())
            for j in range(ENTRY):
                tree.GetEntry(j)
                if (j % 5000 == 0):
                    print("now looping", j, "th Events, total of ", ENTRY,
                          "events")
                for k in range(len(DicNumpyArray_branch)):
                    DicNumpyArray_branch_w.values(
                    )[k][0] = DicNumpyArray_branch.values()[k][0]
                if (  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME
                    (
                        DicNumpyArray_branch.values()[0][0] < 0.5
                    )  # [i][0]  means "i"th branch of the tree, [0] don't change   #FIXME#FIXME#FIXME#FIXME
                        &
                    (
                        DicNumpyArray_branch.values()[1][0] < 0.5
                    )  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME
                ):
                    ijk = ijk + 1
                    tree_f.Fill()
                else:
                    continue
            print(" Event left after filtering : ", ijk, "!!!!!!!!!")
            print("\n")
            ijk = 0
            if (tree_f.GetEntries() == 0):
                print("!!!!!!! ", tree_f.GetName(),
                      " is Empty, would not be written !!!!!")
            else:
                tree_f.Write()

            key = ITER.Next()

        print("")
        print("////////////////////////////////////////////////")
        print("outputfile : ")
        print(outfileName)
        print("////////////////////////////////////////////////")
        print("")

        outfile.Close()
        f.Close()
        print(
            "*********************************************************************************************"
        )
        gBenchmark.Show("Regerating tree root")
        print(
            "*********************************************************************************************"
        )
Esempio n. 10
0
def main(argv):

    startT = time.clock()
    inDir, outDir = ".", "."
    inFile, inFileName, outFileName = TFile(), "", ""
    gatTree = TTree()
    dsNum, subNum, runNum, theCut, inFileName = -1, -1, -1, "", ""
    saveLAT, savePacket, saveWave = False, False, False

    if len(argv) == 0: return
    for i, opt in enumerate(argv):
        if opt == "-f":
            # dsNum, subNum, inDir, outDir = int(argv[i+1]), int(argv[i+2]), str(argv[i+3]), str(argv[i+4])
            inDir, inFileName, outDir = str(argv[i + 1]), str(
                argv[i + 2]), str(argv[i + 3])
            # inFileName = "waveSkimDS{}_{}.root".format(dsNum, subNum)
        if opt == "-r":
            inDir, inFileName, outDir = str(argv[i + 1]), str(
                argv[i + 2]), str(argv[i + 3])
            # dsNum, runNum, inDir, outDir = int(argv[i+1]), int(argv[i+2]), str(argv[i+3]), str(argv[i+4])
            # inFileName = "waveSkimDS{}_run{}.root".format(dsNum, runNum)

    # inFileName = inDir + inFile
    print("Scanning File: {}".format(inFileName))
    inFile = TFile("%s/%s" % (inDir, inFileName))
    gatTree = inFile.Get("skimTree")
    theCut = inFile.Get("theCut").GetTitle()

    # Make files smaller for tests
    # theCut += " && sumEHL > 236 && sumEHL < 240 && mHL==2 && trapENFCal < 5"
    # Select only pulsers
    # theCut += " && EventDC1Bits > 0"

    print "Using cut:\n", theCut
    gatTree.Draw(">>elist", theCut, "entrylist")
    elist = gDirectory.Get("elist")
    gatTree.SetEntryList(elist)
    nList = elist.GetN()
    print "Found", gatTree.GetEntries(), "input entries."
    print "Found", nList, "entries passing cuts."
    # Gimmicky but works... this bypasses creating the branches...
    gatTree.GetEntry(0)

    # Mess of various branches
    channel = std.vector("int")()
    trapENFCal = std.vector("double")()
    trapENM = std.vector("double")()

    # Create map of branches to put into dataframe
    # This map is only for branches that we want to keep!
    keepMapBase = {
        'trapENFCal': gatTree.trapENFCal,
        'trapENM': gatTree.trapENM,
        "channel": gatTree.channel,
        "run": gatTree.run,
        "mHL": gatTree.mHL
    }

    # Combine dictionaries, if keepMapLAT is empty it won't add any branches
    keepMap = dict(keepMapBase)
    # keepMap.update(keepMapLAT)

    dataList = []
    print 'Writing to: ', '%s/proc%s.h5' % (outDir, inFileName.split('.')[0])

    iList, removeNBeg, removeNEnd = -1, 500, 500
    # Loop over events
    while True:
        iList += 1
        if iList >= nList: break
        # if iList >= 5000: break
        entry = gatTree.GetEntryNumber(iList)
        gatTree.LoadTree(entry)
        gatTree.GetEntry(entry)
        nChans = gatTree.channel.size()
        numPass = gatTree.Draw("channel", theCut, "GOFF", 1, iList)
        chans = gatTree.GetV1()
        chanList = list(set(int(chans[n]) for n in xrange(numPass)))
        hitList = (iH for iH in xrange(nChans)
                   if gatTree.channel.at(iH) in chanList
                   )  # a 'generator expression'
        for iH in hitList:
            dataMap = {}
            wf = gatTree.MGTWaveforms.at(iH)
            signal = wl.processWaveform(wf, removeNBeg, removeNEnd)
            wave = np.array(signal.GetWaveRaw(), dtype=np.int16)
            for key, branch in keepMap.items():
                # Save branches that aren't vector<Template> (so far only run and mHL)
                if key == 'run' or key == 'mHL': dataMap[key] = int(branch)
                elif key == 'channel': dataMap[key] = int(branch.at(iH))
                else: dataMap[key] = float(branch.at(iH))
            dataMap['waveform'] = wave.tolist()
            dataList.append(dataMap)

        if iList % 5000 == 0 and iList != 0:
            print "%d / %d entries saved (%.2f %% done), time: %s" % (
                iList, nList, 100 *
                (float(iList) / nList), time.strftime('%X %x %Z'))

    df = pd.DataFrame.from_dict(dataList)
    print(df.head())
    print(df.info())
    print(np.unique(df.channel))
    print(np.unique(df.mHL))
    print(np.unique(df.run))
    # Suppress stupid warning
    warnings.filterwarnings(action="ignore",
                            module="pandas",
                            message="^\nyour performance")

    # Chunk write like a sucker
    chunksize = 50000
    start = 0
    end = chunksize - 1
    i = 0
    # for i in len(df):
    while end < df.shape[0]:
        # chunk = df.iloc[(i*chunksize):min((i+1)*chunksize,len(df))]
        chunk = df.iloc[start:end]
        try:
            chunk.to_hdf('{}/proc{}_{}.h5'.format(outDir,
                                                  inFileName.split('.')[0], i),
                         key='skimTree',
                         data_columns=[
                             'trapENFCal', 'trapENM', 'channel', 'mHL',
                             'waveform'
                         ],
                         format='fixed',
                         mode='w',
                         complevel=9)
        except (Exception) as e:
            print e
            print chunk
            print chunk.info()
        start += chunksize
        end += chunksize
        i += 1

    # df.to_hdf('%s/proc%s.h5' % (outDir,inFileName.split('.')[0]), key="skimTree", data_columns=['trapENFCal', 'trapENM','channel','mHL','waveform'], format='fixed', mode='w', complevel=9)

    stopT = time.clock()
    print("Stopped:", time.strftime('%X %x %Z'), "\nProcess time (min):",
          (stopT - startT) / 60)
    print(float(nList) / ((stopT - startT) / 60.), "entries per minute.")
Esempio n. 11
0
class TopovarWriter(object):


  ## Constructor 
  def __init__(self, outfile, variables = [], infile = None):
    # Default values
    self.__intree = None
    self.__outtree = None
    self.__holder = {}
    self.__variables = copy.copy(variables)
    self.__outfile = TFile(outfile, 'recreate')
    # Clone a tree if a infile name is passed
    if infile:
      self.__infile = TFile(infile)
      self.__intree = self.__infile.Get(Common.Inputtree)
      self.__setActiveBranches()
      # Swiching to output file
      self.__outfile.cd()
      self.__outtree = self.__intree.CloneTree(0)
    else:
      # Swiching to output file
      self.__outfile.cd()
      self.__outtree = TTree(Common.Inputtree,Common.Inputtree)


  ## Destructor
  def __del__(self):
    if self.__infile:
      self.__infile.Close()
    if self.__outfile:
      self.__outfile.Close()


  ## Return list of variables
  def getVariables(self):
    return self.__variables


  ## Return list of variables to be added
  def getAddedVariables(self):
    return self.__holder.keys()


  ## Get size of the intree
  def getInTreeEntries(self):
    if not self.__intree:
      raise TopovarWriterError('No input tree exist.')
    return self.__intree.GetEntries()


  ## Get size of the outtree
  def getOutTreeEntries(self):
    return self.__outtree.GetEntries()


  ## Read a even in the chain  
  def read(self, entry, compress = False, variables = None):
    # Set variable list (by default uses the initial variable list)
    # This allow to select a subset of the initial variable list
    if not variables:
      variables = self.__variables
    else:
      variables = copy.deepcopy(variables)
      variables.insert(0,Common.EventWeight)
    # Check for the intree exist
    if not self.__intree:
      raise TopovarWriterError('No input tree exist.')
    # Check if the entry is in the chain
    if entry < 0 or entry >= self.__intree.GetEntries():
      raise TopovarWriterError('Entry out of range of the in tree.')
    # Set the chain entry
    self.__intree.GetEntry(entry)
    # Check for compress format
    if compress:
      # Compact format return a list of variables
      event = []
      for variable in variables:
        event.append(getattr(self.__intree, variable))
      return event
    else:
      # No compact format return event object
      event = Event()
      for variable in variables:
        # Variable value is process first (Normalization, etc..)
        setattr(event, variable, getattr(self.__intree, variable))
      return event

    
  ## Add a new topovar to the tree
  def addVariable(self, variable, type, default = -999):
    # Check for pre-existent variables
    update = False
    if variable in self.__variables:
      self.__message('Updating a pre-existent variable.')
      update = True
    # Adding variable to the tree (only int and float are supported)
    if type == 'int':
      self.__holder[variable] = array.array('i', [default])
      if not update:
        self.__outtree.Branch(variable, self.__holder[variable], '%s/I' % variable)
      else:
        self.__outtree.SetBranchAddress(variable, self.__holder[variable])
    elif type == 'double':
      self.__holder[variable] = array.array('d', [default])
      if not update:
        self.__outtree.Branch(variable, self.__holder[variable], '%s/D' % variable)
      else:
        self.__outtree.SetBranchAddress(variable, self.__holder[variable])
    else:
      raise TopovarWriterError('Unsupported type, topovar can only be int or double.') 


  ## Fill the tree
  def fill(self, *args, **kwargs):
    # Reading of the values
    values = {}
    if len(args) > 1:
      raise TopovarWriterError('Too many positional arguments.')
    if args:
      values.update(args[0])
    values.update(kwargs)
    # Setup all the variable values
    for key in values:
      # Check if the variable was added
      if not key in self.__holder:
        raise TopovarWriterError('The variable %s was not added to the tree.' % key)
      self.__holder[key][0] = values[key]
    # Fill the tree
    self.__outtree.Fill()

  def __setActiveBranches(self):
    # Set only selected variables
    if len(self.__variables) != 0:
      self.__variables.insert(0, Common.EventWeight)     
      self.__intree.SetBranchStatus('*', False)
      for variable in self.__variables:
        self.__intree.SetBranchStatus(variable, True)
    else:
    # Set all the variables
      self.__intree.SetBranchStatus('*', True)
      branches = self.__intree.GetListOfBranches()
      for i in xrange(branches.GetEntries()):
        self.__variables.append(branches[i].GetName())


  ## Write the tree to the file
  def write(self):
    self.__outtree.Write()

  
  ## Auxiliary function for log
  def __message(self, msg):
    print '%s: %s' % (self.__class__.__name__, msg)
Esempio n. 12
0
def MakeHistsToFit2(file, f_out_str):

    E_arr = array('d', [])
    E_arr_err = array('d', [])
    effic_arr = array('d', [])
    effic_arr_err = array('d', [])

    c1 = TCanvas("c1", "c1", 1600, 900)
    c1.SetGridx(1)
    c1.SetGridy(1)

    #Find which energy files exist, create file list and energy array
    f_wogamma_list = []
    curr_E_val = 0.1
    while curr_E_val <= 3.0:
        normfile = REL_DIR + "tree_pi+pi-p_nofit_" + str(curr_E_val) + ".root"
        if (os.path.isfile(normfile)):
            E_arr.append(curr_E_val)
            E_arr_err.append(0)
            f_wogamma_list.append(TFile.Open(normfile, 'read'))
            # print "File found!!!"
            # print "Was looking for file: " + normfile
            # print "Current E val: " + str(curr_E_val)
        # else:
        # print "File not found!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        # print "Was looking for file: " + normfile
        # print "Current E val: " + str(curr_E_val)
        curr_E_val += 0.05

    #Get normalization factor
    norm_arr = array('d', [])
    for i in range(0, len(f_wogamma_list)):
        my_tr = TTree()
        my_tr = f_wogamma_list[i].Get("pi+pi-p_nofit_Tree")
        norm_arr.append(my_tr.GetEntries())
        # print "Energy: "  + str(E_arr[i])
        # print "Normalization: "  + str(norm_arr[i])

    #Create and fit histograms in larger file
    f = TFile.Open(file, 'read')
    my_tr = TTree()
    my_tr = f.Get("gamma_pi+pi-p_nofit_Tree")
    h_DeltaPhi_list = []
    h_DeltaTheta_list = []
    h_EGammaPostCuts_list = []
    for i in range(0, len(E_arr)):
        h_DeltaPhi_curr = TH1F()
        my_tr.Draw("DeltaPhi>>h_DeltaPhi_curr(1000,-180.,180.)", "")
        h_DeltaPhi_curr = gPad.GetPrimitive("h_DeltaPhi_curr")
        h_DeltaPhi_curr.SetName("h_DeltaPhi_" + str(E_arr[i]))
        h_DeltaPhi_list.append(h_DeltaPhi_curr)
        h_DeltaTheta_curr = TH1F()
        my_tr.Draw("DeltaTheta>>h_DeltaTheta_curr(1000,-25.,10.)", "")
        h_DeltaTheta_curr = gPad.GetPrimitive("h_DeltaTheta_curr")
        h_DeltaTheta_curr.SetName("h_DeltaTheta_" + str(E_arr[i]))
        h_DeltaTheta_list.append(h_DeltaTheta_curr)

        h_EGammaPostCuts_curr = TH1F()
        E_cut_str = "abs(ThrownE-" + str(E_arr[i]) + ")<0.0001"
        print "Cut string: " + E_cut_str
        my_tr.Draw("FoundE>>h_EGammaPostCuts_curr(1000,0,4)",
                   "abs(DeltaPhi)<2.&&abs(DeltaTheta)<0.5&&" + E_cut_str)
        h_EGammaPostCuts_curr = gPad.GetPrimitive("h_EGammaPostCuts_curr")
        h_EGammaPostCuts_curr.SetName("h_EGammaPostCuts_" + str(E_arr[i]))
        h_EGammaPostCuts_list.append(h_EGammaPostCuts_curr)

        my_gaus_fit = TF1("my_gaus_fit", "gausn")
        my_gaus_fit.SetParLimits(0, 0, 30000)
        my_gaus_fit.SetParameter(1, E_arr[i])
        my_gaus_fit.SetParLimits(1, E_arr[i] - 0.5, E_arr[i] + 0.1)
        my_gaus_fit.SetParLimits(2, 0.02, 0.5)
        my_gaus_fit.SetNpx(1000)
        h_EGammaPostCuts_curr.Fit(my_gaus_fit, "Q", "", E_arr[i] - 0.3,
                                  E_arr[i] + 0.2)
        c1.SaveAs(".plots/ReactionFilterFit_pipip_" + str(E_arr[i]) + ".png")
        effic = my_gaus_fit.GetParameter(
            0) / h_EGammaPostCuts_curr.GetBinWidth(0) / norm_arr[i]
        effic_err = my_gaus_fit.GetParError(
            0) / h_EGammaPostCuts_curr.GetBinWidth(0) / norm_arr[i]

        if (E_arr[i] < 0.2):
            effic = h_EGammaPostCuts_curr.GetEntries() / norm_arr[i]
            effic_err = sqrt(h_EGammaPostCuts_curr.GetEntries()) / norm_arr[i]
        if (h_EGammaPostCuts_curr.GetEntries() < 100.):
            effic = -1
            effic_err = 0

        print "File Entries: " + str(my_tr.GetEntries())
        print "Histogram Entries: " + str(h_EGammaPostCuts_curr.GetEntries())
        print "Gaussian yield: " + str(
            my_gaus_fit.GetParameter(0) / h_EGammaPostCuts_curr.GetBinWidth(0))
        print "Normalization: " + str(norm_arr[i])
        print "Efficiency: " + str(effic)

        effic_arr.append(effic)
        effic_arr_err.append(effic_err)

    gr_gauscore_effic = TGraphErrors(len(E_arr), E_arr, effic_arr, E_arr_err,
                                     effic_arr_err)
    gr_gauscore_effic.SetName("gr_gauscore_effic")

    f_out = TFile(f_out_str, "RECREATE")
    gr_gauscore_effic.Write()
    for i in range(0, len(E_arr)):
        h_DeltaPhi_list[i].Write()
    for i in range(0, len(E_arr)):
        h_DeltaTheta_list[i].Write()
    for i in range(0, len(E_arr)):
        h_EGammaPostCuts_list[i].Write()
    f_out.Close()

    # curr_E_val = 0.1
    # curr_E_val = 2.0
    # while curr_E_val <= 3.:
    # while curr_E_val <= 2.2:
    # print "Curr E thing " + str(curr_E_val)

    # normfile = REL_DIR+"tree_pi+pi-p_nofit_"+str(curr_E_val)+".root"
    # my_tr2 = TTree()
    # my_tr2 =  f_wogamma_list[i].Get("pi+pi-p_nofit_Tree")
    # NormalizationFactor = my_tr2.GetEntries()

    # curr_E_val+=0.05

    return
Esempio n. 13
0
def FitHistsGetEffic(hist_fname, fit_out_fname, MIN_E, MAX_E, E_STEP,
                     scale_factor):

    E_arr = array('d', [])
    E_arr_err = array('d', [])
    effic_arr = array('d', [])
    effic_arr_err = array('d', [])
    norm_arr = array('d', [])

    curr_E_val = MIN_E
    while curr_E_val <= MAX_E + 0.0001:
        E_arr.append(curr_E_val)
        if (NORMALIZE_EXTERNAL): E_arr_err.append(0)
        if (not NORMALIZE_EXTERNAL): E_arr_err.append(E_STEP / 2.)
        curr_E_val += E_STEP

    c1 = TCanvas("c1", "c1", 1600, 900)
    c1.SetGridx(1)
    c1.SetGridy(1)

    #Get normalization from external files assuming matching directory scheme for embedded photon gun data
    if (NORMALIZE_EXTERNAL):
        #Get normalization
        curr_E_val = MIN_E
        while curr_E_val <= MAX_E + 0.0001:

            print "Getting normalization for energy value: " + str(curr_E_val)

            thisfile_string = NORM_BASE_DIR + str(
                curr_E_val) + "/" + NORM_FILENAME

            if (not os.path.isfile(thisfile_string)):
                print "file not found, normalization will be 0: " + thisfile_string
                norm_arr.append(0.)
                curr_E_val += E_STEP
                continue

            normfile = TFile.Open(thisfile_string, 'read')

            my_tr = TTree()
            my_tr.Print()
            my_tr = normfile.Get(NORM_TREENAME)
            norm_arr.append(my_tr.GetEntries())
            curr_E_val += E_STEP
        #Done getting normalization

    # Get normalization from same root file
    f_in = TFile.Open(hist_fname)  #Open rootfile
    if (not NORMALIZE_EXTERNAL):
        for i in range(0, len(E_arr)):
            E_cut_str = str(E_arr[i] -
                            E_STEP / 2.) + "<ThrownE&&ThrownE<" + str(
                                E_arr[i] +
                                E_STEP / 2.) + "&&ThrownTheta<9&&ThrownTheta>4"
            h_curr = f_in.Get("h_EThrown_" + E_cut_str)
            norm_arr.append(h_curr.GetEntries() / scale_factor)

    #Fit stuff
    for i in range(0, len(E_arr)):
        h_curr = TH1F()
        E_cut_str = str(E_arr[i] - E_STEP / 2.) + "<ThrownE&&ThrownE<" + str(
            E_arr[i] + E_STEP / 2.) + "&&ThrownTheta<9&&ThrownTheta>4"
        if (NORMALIZE_EXTERNAL):
            h_curr = f_in.Get("h_EGammaPostCuts_" + str(E_arr[i]))
        if (not NORMALIZE_EXTERNAL):
            h_curr = f_in.Get("h_EGammaPostCuts_" + E_cut_str)
        curr_E_val = E_arr[i]

        norm_count = norm_arr[i]
        if (norm_count < MIN_EVENTS):
            effic_arr.append(-1)
            effic_arr_err.append(0)
            continue

        my_gaus_fit = TF1("my_gaus_fit", "gausn", 0.001, 3.)
        my_gaus_fit.SetParLimits(0, 0, 10000)
        my_gaus_fit.SetParLimits(1, curr_E_val - 0.2, curr_E_val + 0.1)
        my_gaus_fit.SetParLimits(2, 0.005, 0.4)
        my_gaus_fit.SetNpx(1000)
        h_curr.GetXaxis().SetRangeUser(curr_E_val - 1.2, curr_E_val + 0.5)
        h_curr.Fit(my_gaus_fit, "Q", "", curr_E_val - E_BELOWTHROWN_TOFIT,
                   curr_E_val + E_ABOVETHROWN_TOFIT)
        h_curr.Fit(my_gaus_fit, "QL", "", curr_E_val - E_BELOWTHROWN_TOFIT,
                   curr_E_val + E_ABOVETHROWN_TOFIT)

        if (POLY_ORDER >= 1):
            gaus_fit_amplitude = my_gaus_fit.GetParameter(0)
            gaus_fit_mean = my_gaus_fit.GetParameter(1)
            gaus_fit_sigma = my_gaus_fit.GetParameter(2)

            gaus_plus_poly_fit = TF1("gaus_plus_poly_fit",
                                     "gausn+pol" + str(POLY_ORDER) + "(3)",
                                     0.001, 3.)
            gaus_plus_poly_fit.SetParameter(0, gaus_fit_amplitude)
            gaus_plus_poly_fit.SetParameter(1, gaus_fit_mean)
            gaus_plus_poly_fit.SetParameter(2, gaus_fit_sigma)
            gaus_plus_poly_fit.SetParLimits(0, 0, 10000)
            gaus_plus_poly_fit.SetParLimits(1, curr_E_val - 0.2,
                                            curr_E_val + 0.1)
            gaus_plus_poly_fit.SetParLimits(2, 0.005, 0.4)

            h_curr.Fit(gaus_plus_poly_fit, "Q", "",
                       curr_E_val - E_BELOWTHROWN_TOFIT,
                       curr_E_val + E_ABOVETHROWN_TOFIT)
            h_curr.Fit(gaus_plus_poly_fit, "QL", "",
                       curr_E_val - E_BELOWTHROWN_TOFIT,
                       curr_E_val + E_ABOVETHROWN_TOFIT)

        c1.SaveAs(".plots/FitE_" + str(curr_E_val) + ".png")
        effic_arr.append(
            (my_gaus_fit.GetParameter(0) / h_curr.GetBinWidth(0)) / norm_count)
        effic_arr_err.append(
            my_gaus_fit.GetParError(0) / h_curr.GetBinWidth(0) / norm_count)

    gr_gauscore_effic = TGraphErrors(len(E_arr), E_arr, effic_arr, E_arr_err,
                                     effic_arr_err)
    gr_gauscore_effic.SetMarkerStyle(15)
    gr_gauscore_effic.SetMarkerSize(1.2)
    gr_gauscore_effic.SetMarkerColor(kBlue)
    gr_gauscore_effic.SetName("gr_gauscore_effic")

    f_out = TFile(fit_out_fname, "RECREATE")
    f_out.cd()
    gr_gauscore_effic.Write()
    f_out.Close()
Esempio n. 14
0
def MakeHistsToFit(file, MIN_E, MAX_E, E_STEP, f_out_str):

    E_arr = array('d', [])

    c1 = TCanvas("c1", "c1", 1600, 900)
    c1.SetGridx(1)
    c1.SetGridy(1)

    norm_arr = array('d', [])
    curr_E_val = MIN_E
    while curr_E_val <= MAX_E + 0.0001:
        E_arr.append(curr_E_val)
        curr_E_val += E_STEP

    #Create and fit histograms in larger file
    f = TFile.Open(file, 'read')
    my_tr = TTree()
    my_tr = f.Get(WITH_GAMMA_TREENAME)
    h_DeltaPhi_list = []  #List of deltaPhi histograms to save
    h_DeltaTheta_list = []  #List of deltaTheta histograms to save
    h_EGammaPreCuts_list = []  #List of E_gamma histograms to save
    h_EGammaPostCuts_list = []  #List of E_gamma histograms to save
    h_EThrown_list = []  #List of E_gamma histograms to save

    num_tree_entries = float(my_tr.GetEntries())
    num_branch_entries = my_tr.Draw("ThrownE", "")
    norm_scale_factor = num_branch_entries / num_tree_entries

    # print "Num tree entries: " + str(num_tree_entries)
    # print "Num branch entries: " + str(num_branch_entries)

    print "Need to scale by: " + str(norm_scale_factor)

    for i in range(0, len(E_arr)):

        E_cut_str = ""
        if (NORMALIZE_EXTERNAL):
            E_cut_str = "abs(ThrownE-" + str(E_arr[i]) + ")<0.0001"
        if (not NORMALIZE_EXTERNAL):
            E_cut_str = str(E_arr[i] -
                            E_STEP / 2.) + "<ThrownE&&ThrownE<" + str(
                                E_arr[i] +
                                E_STEP / 2.) + "&&ThrownTheta<9&&ThrownTheta>4"
        print "Cut string: " + E_cut_str

        #Make DeltaPhi and DeltaTheta histograms
        h_DeltaPhi_curr = TH1F()
        my_tr.Draw("DeltaPhi>>h_DeltaPhi_curr(1000,-180.,180.)", E_cut_str)
        h_DeltaPhi_curr = gPad.GetPrimitive("h_DeltaPhi_curr")
        if (NORMALIZE_EXTERNAL):
            h_DeltaPhi_curr.SetNameTitle("h_DeltaPhi_" + str(E_arr[i]),
                                         "h_DeltaPhi_" + str(E_arr[i]))
        if (not NORMALIZE_EXTERNAL):
            h_DeltaPhi_curr.SetNameTitle("h_DeltaPhi_" + E_cut_str,
                                         "h_DeltaPhi_" + E_cut_str)
        h_DeltaPhi_list.append(h_DeltaPhi_curr)
        print "h_DeltaPhi_curr hist entries:" + str(
            h_DeltaPhi_curr.GetEntries())

        h_DeltaTheta_curr = TH1F()
        my_tr.Draw("DeltaTheta>>h_DeltaTheta_curr(1000,-25.,10.)", E_cut_str)
        h_DeltaTheta_curr = gPad.GetPrimitive("h_DeltaTheta_curr")
        if (NORMALIZE_EXTERNAL):
            h_DeltaTheta_curr.SetNameTitle("h_DeltaTheta_" + str(E_arr[i]),
                                           "h_DeltaTheta_" + str(E_arr[i]))
        if (not NORMALIZE_EXTERNAL):
            h_DeltaTheta_curr.SetNameTitle("h_DeltaTheta_" + E_cut_str,
                                           "h_DeltaTheta_" + E_cut_str)
        h_DeltaTheta_list.append(h_DeltaTheta_curr)
        print "h_DeltaTheta_curr hist entries:" + str(
            h_DeltaTheta_curr.GetEntries())

        h_EGammaPreCuts_curr = TH1F()
        my_tr.Draw(
            "FoundE>>h_EGammaPreCuts_curr(1000,0," + str(MAX_E + 1) + ")",
            E_cut_str)
        h_EGammaPreCuts_curr = gPad.GetPrimitive("h_EGammaPreCuts_curr")
        if (NORMALIZE_EXTERNAL):
            h_EGammaPreCuts_curr.SetNameTitle(
                "h_EGammaPreCuts_" + str(E_arr[i]),
                "h_EGammaPreCuts_" + str(E_arr[i]))
        if (not NORMALIZE_EXTERNAL):
            h_EGammaPreCuts_curr.SetNameTitle("h_EGammaPreCuts_" + E_cut_str,
                                              "h_EGammaPreCuts_" + E_cut_str)
        h_EGammaPreCuts_list.append(h_EGammaPreCuts_curr)
        print "h_EGammaPreCuts_curr hist entries:" + str(
            h_EGammaPreCuts_curr.GetEntries())

        h_EGammaPostCuts_curr = TH1F()
        my_tr.Draw(
            "FoundE>>h_EGammaPostCuts_curr(1000,0," + str(MAX_E + 1) + ")",
            "abs(DeltaPhi)<" + DELTA_PHI_CUT + "&&abs(DeltaTheta)<" +
            DELTA_THETA_CUT + "&&" + E_cut_str)
        h_EGammaPostCuts_curr = gPad.GetPrimitive("h_EGammaPostCuts_curr")
        if (NORMALIZE_EXTERNAL):
            h_EGammaPostCuts_curr.SetNameTitle(
                "h_EGammaPostCuts_" + str(E_arr[i]),
                "h_EGammaPostCuts_" + str(E_arr[i]))
        if (not NORMALIZE_EXTERNAL):
            h_EGammaPostCuts_curr.SetNameTitle("h_EGammaPostCuts_" + E_cut_str,
                                               "h_EGammaPostCuts_" + E_cut_str)
        h_EGammaPostCuts_curr.SetTitle("E_{#gamma} thrown = " + str(E_arr[i]))
        h_EGammaPostCuts_curr.GetXaxis().SetTitle("E_{#gamma}")
        h_EGammaPostCuts_curr.GetYaxis().SetTitle("Yield")
        h_EGammaPostCuts_list.append(h_EGammaPostCuts_curr)
        print "h_EGammaPostCuts_curr hist entries:" + str(
            h_EGammaPostCuts_curr.GetEntries())

        h_EThrown_curr = TH1F()
        # my_tr.Draw("ThrownE>>h_EThrown_curr(1000,0,10",E_cut_str)
        my_tr.Draw("MCWeight>>h_EThrown_curr(1000,-10,10", E_cut_str)
        h_EThrown_curr = gPad.GetPrimitive("h_EThrown_curr")
        if (NORMALIZE_EXTERNAL):
            h_EThrown_curr.SetNameTitle("h_EThrown_" + str(E_arr[i]),
                                        "h_EThrown_" + str(E_arr[i]))
        if (not NORMALIZE_EXTERNAL):
            h_EThrown_curr.SetNameTitle("h_EThrown_" + E_cut_str,
                                        "h_EThrown_" + E_cut_str)
        h_EThrown_curr.SetTitle("E_{#gamma} thrown = " + str(E_arr[i]))
        h_EThrown_curr.GetXaxis().SetTitle("E_{#gamma}")
        h_EThrown_curr.GetYaxis().SetTitle("Yield")
        h_EThrown_list.append(h_EThrown_curr)
        print "h_EThrown_curr hist entries:" + str(h_EThrown_curr.GetEntries())

    #Save to file
    f_out = TFile(f_out_str, "RECREATE")
    # gr_gauscore_effic.Write()

    for i in range(0, len(E_arr)):
        h_EGammaPostCuts_list[i].Write()
    for i in range(0, len(E_arr)):
        h_DeltaPhi_list[i].Write()
    for i in range(0, len(E_arr)):
        h_DeltaTheta_list[i].Write()
    for i in range(0, len(E_arr)):
        h_EGammaPreCuts_list[i].Write()
    for i in range(0, len(E_arr)):
        h_EThrown_list[i].Write()
    f_out.Close()

    return norm_scale_factor
Esempio n. 15
0
def jetdisplay():
	outputfile = "zjj"
	displayfile = TFile(outputfile+".root","RECREATE")

	energies = [30,50,70,90, 150, 250]
	inputfiles = ["jetscan_leakage_029/jetscan_"+str(e)+".root" for e in energies]
	
	#for geant4.10.5
	inputfiles = ["resultsgeant4.10.5/jetscan/jetscan"+str(e)+".root" for e in energies]
	#end of geant4.10.5

	#for geant4.10.5 with X0 or B
	inputfiles = ["resultsgeant4.10.5/jetscan_leakage_X0/jetscan/jetscan"+str(e)+".root" for e in energies]
	#end of geant4.10.5 with X0 or B

	#for CaloLoop
	#inputfiles = ["/home/software/Calo/CaloLoop/CaloJet/resultsgeant4.10.5/jetscan_leakage_B/jetscan/jetscan"+str(e)+".root" for e in energies]
	#end CaloLoop

	for counter, inputfile in enumerate(inputfiles):
		inputfile = TFile(inputfile)
		print "Analyzing: "+str(inputfile)+" \n"
		tree = TTree()
		inputfile.GetObject("MyTree", tree)	

		#graphEjet1 = TH1F("energyjet1", "energyjet1", 100, 0., 200.)
		#graphEjet2 = TH1F("energyjet2", "energyjet2", 100, 0., 200.)
		
		#graphEcherjet1 = TH1F("energycher1", "energycherjet", 100, 0., 200.)
		#graph3 = TH1F("energyscinjet", "energyscinjet", 100, 0., 200.)
		
		#graphmass = TH1F("mass_jet", "mass_jet", 100, 0., 200.)
		graphtest = TH1F("test"+str(energies[counter]), "test"+str(energies[counter]), 80, -40., 40.)
		graphenergy = TH1F("energy"+str(energies[counter]), "energy"+str(energies[counter]), 200, 0., 300.)
		graphenergytruth = TH1F("energytruth"+str(energies[counter]), "energytruth"+str(energies[counter]), 200, 0., 300.) 
		graphjs = TH1F("energyjs"+str(energies[counter]), "energyjs"+str(energies[counter]), 200, 0., 300.) 
		graphjc = TH1F("energyjc"+str(energies[counter]), "energyjc"+str(energies[counter]), 200, 0., 300.) 
			
		graph_emcomp02 = TH1F("emcomp02_"+str(energies[counter]), "emcomp02"+str(energies[counter]), 80, -40, 40)
		graph_emcomp04 = TH1F("emcomp04_"+str(energies[counter]), "emcomp04"+str(energies[counter]), 80, -40, 40)
		graph_emcomp06 = TH1F("emcomp06_"+str(energies[counter]), "emcomp06"+str(energies[counter]), 80, -40, 40)
		graph_emcomp08 = TH1F("emcomp08_"+str(energies[counter]), "emcomp08"+str(energies[counter]), 80, -40, 40)
		graph_emcomp1 = TH1F("emcomp1_"+str(energies[counter]), "emcomp1"+str(energies[counter]), 80, -40, 40)
		
		#loop over events
		for Event in range(tree.GetEntries()):		

			tree.GetEntry(Event)	
			#print "Event "+str(Event)
			nmuon = tree.nmuon
			nneu = tree.nneu
			mjjr = tree.mjjr
			mjjt = tree.mjjt
			edep = tree.edep
			muene_che = tree.muene_che
			muene_sci = tree.muene_sci
			emcomp1 = tree.emcomp1
			emcomp2 = tree.emcomp2
			eleak = tree.eleak
			eleakn = tree.eleakn
			   
			j1t_E = tree.j1t_E
			j1t_m = tree.j1t_m
			j1t_theta = tree.j1t_theta
			j1t_pt = tree.j1t_pt
			j1t_eta = tree.j1t_eta
			j1t_phi = tree.j1t_phi
			j2t_E = tree.j2t_E
			j2t_m = tree.j2t_m
			j2t_theta = tree.j2t_theta
			j2t_pt = tree.j2t_pt
			j2t_eta = tree.j2t_eta
			j2t_phi = tree.j2t_phi	

			j1r_E = tree.j1r_E
			j1r_m = tree.j1r_m
			j1r_theta = tree.j1r_theta
			j1r_pt = tree.j1r_pt
			j1r_eta = tree.j1r_eta
			j1r_phi = tree.j1r_phi
			j2r_E = tree.j2r_E
			j2r_m = tree.j2r_m
			j2r_theta = tree.j2r_theta
			j2r_pt = tree.j2r_pt
			j2r_eta = tree.j2r_eta
			j2r_phi = tree.j2r_phi	

			j1s_E = tree.j1s_E
			j1s_m = tree.j1s_m
			j1s_theta = tree.j1s_theta
			j1s_pt = tree.j1s_pt
			j1s_eta = tree.j1s_eta
			j1s_phi = tree.j1s_phi
			j2s_E = tree.j2s_E
			j2s_m = tree.j2s_m
			j2s_theta = tree.j2s_theta
			j2s_pt = tree.j2s_pt
			j2s_eta = tree.j2s_eta
			j2s_phi = tree.j2s_phi	

			j1c_E = tree.j1c_E
			j1c_m = tree.j1c_m
			j1c_theta = tree.j1c_theta
			j1c_pt = tree.j1c_pt
			j1c_eta = tree.j1c_eta
			j1c_phi = tree.j1c_phi
			j2c_E = tree.j2c_E
			j2c_m = tree.j2c_m
			j2c_theta = tree.j2c_theta
			j2c_pt = tree.j2c_pt
			j2c_eta = tree.j2c_eta
			j2c_phi = tree.j2c_phi	

			cut1 =  nmuon==0 and nneu==0
			cut2 =  abs(j1t_eta)<2.0 and abs(j2t_eta)<2.0
			cut3 = eleak<3000.
			cut3 = True	
			cut4 = j1t_E+j2t_E>0.999*energies[counter]
			cut4= True
			cut5 = abs(j1t_E-j2t_E)<5.
			cut5 = abs(j1t_phi-j2t_phi)>0.1
			if cut1 and cut2 and cut3 and cut4 and cut5:
				deltaj1 = 0.04406*j1r_E+0.1158
				deltaj2 = 0.04406*j2r_E+0.1158
				deltaj1 = 0.03401*j1r_E+0.9152
				deltaj2 = 0.03401*j2r_E+0.9152
				deltaj1 = 0.04911*j1r_E+0.5723
				deltaj2 = 0.04911*j2r_E+0.5723
				graphtest.Fill(j1r_E+deltaj1-j1t_E)
				graphtest.Fill(j2r_E+deltaj2-j2t_E)
				#deltaj1_2 = 0.0098*(j1r_E+deltaj1)+0.186
				#deltaj2_2 = 0.0098*(j2r_E*deltaj2)+0.186
				'''
				if (emcomp1+emcomp2)<0.2*90.:
					graph_emcomp02.Fill(j1r_E+deltaj1-j1t_E)
					graph_emcomp02.Fill(j2r_E+deltaj2-j2t_E)
				if 0.2*90.<emcomp1+emcomp2<0.4*90.:
					graph_emcomp04.Fill(j1r_E+deltaj1-j1t_E)
					graph_emcomp04.Fill(j2r_E+deltaj2-j2t_E)
				if 0.4*90.<emcomp1+emcomp2<0.6*90.:
					graph_emcomp06.Fill(j1r_E+deltaj1-j1t_E)
					graph_emcomp06.Fill(j2r_E+deltaj2-j2t_E)				
				if 0.6*90.<emcomp1+emcomp2<0.8*90.:
					graph_emcomp08.Fill(j1r_E+deltaj1-j1t_E)
					graph_emcomp08.Fill(j2r_E+deltaj2-j2t_E)				
				if 0.8*90.<emcomp1+emcomp2<90.:
					graph_emcomp1.Fill(j1r_E+deltaj1-j1t_E)
					graph_emcomp1.Fill(j2r_E+deltaj2-j2t_E)				
				'''
				graphenergy.Fill(j1r_E+deltaj1)
				graphenergy.Fill(j2r_E+deltaj2)
				graphenergytruth.Fill(j1t_E)
				graphenergytruth.Fill(j2t_E)
				graphjs.Fill(j2s_E)
				graphjs.Fill(j1s_E)
				graphjc.Fill(j2c_E)
				graphjc.Fill(j1c_E)

		displayfile.cd()
		graphtest.Write()
		graphenergy.Write()
		graphenergytruth.Write()
		graphjs.Write()
		graphjc.Write()
Esempio n. 16
0
def energylinearity():
    outputfile = "EMLinearityEnergyRes"
    displayfile = TFile(outputfile + ".root", "RECREATE")

    MeanEnergyScin = array('d')
    MeanEnergyCher = array('d')
    MeanEnergy = array('d')
    resolutionscin = array('d')
    resolutioncher = array('d')
    resolution = array('d')
    towers = array('d')
    ##inputfiles = sorted(glob.glob(datapath+"*"), key=os.path.getmtime) #get files from tower 1 to 75 ordered by creation time
    inputfiles = [
        "/home/software/Calo/results/newresults/barrel2/Barrel_" + str(i) +
        ".root" for i in range(1, 76)
    ]
    #inputfiles = ["/home/software/Calo/results/NewTowerScan4/Barrel_"+str(i)+".root" for i in range(1,76)]
    #inputfiles = ["/home/lorenzo/Desktop/Calo/results/NewTowerScan4/Barrel_"+str(i)+".root" for i in range(1,76)]

    for counter, inputfile in enumerate(inputfiles):
        inputfile = TFile(inputfile)
        print "Analyzing: " + str(inputfile) + " \n"
        tree = TTree()
        inputfile.GetObject("B4", tree)

        ScinEnergyHist = TH1F("scinenergy_",
                              str(counter + 1) + "_scin", 200, 0., 100.)
        CherEnergyHist = TH1F("cherenergy_",
                              str(counter + 1) + "_cher", 200, 0., 100.)
        RecEnergyHist = TH1F("RecEnergy_",
                             str(counter + 1) + "_Energy", 200, 0., 100.)
        Energytot = 0.0

        energy = 40.0
        sqrtenergy = 1 / (40.0**0.5)
        towers.append(counter + 1.)

        #loop over events
        for Event in range(int(tree.GetEntries())):

            tree.GetEntry(Event)

            #Set values of the tree
            PrimaryParticleName = tree.PrimaryParticleName  # MC truth: primary particle Geant4 name
            PrimaryParticleEnergy = tree.PrimaryParticleEnergy
            EnergyTot = tree.EnergyTot  # Total energy deposited in calorimeter
            Energyem = tree.Energyem  # Energy deposited by the em component
            EnergyScin = tree.EnergyScin  # Energy deposited in Scin fibers (not Birk corrected)
            EnergyCher = tree.EnergyCher  # Energy deposited in Cher fibers (not Birk corrected)
            NofCherenkovDetected = tree.NofCherenkovDetected  # Total Cher p.e. detected
            BarrelR_VectorSignals = tree.VectorSignalsR  # Vector of energy deposited in Scin fibers (Birk corrected)
            BarrelL_VectorSignals = tree.VectorSignalsL  # Vector of energy deposited in Scin fibers (Birk corrected)
            BarrelR_VectorSignalsCher = tree.VectorSignalsCherR  # Vector of Cher p.e. detected in Cher fibers
            BarrelL_VectorSignalsCher = tree.VectorSignalsCherL
            VectorR = tree.VectorR
            VectorL = tree.VectorL

            #apply calibrations
            Calib_BarrelL_VectorSignals = calibration2.calibscin(
                BarrelL_VectorSignals)
            Calib_BarrelR_VectorSignals = calibration2.calibscin(
                BarrelR_VectorSignals)
            Calib_BarrelL_VectorSignalsCher = calibration2.calibcher(
                BarrelL_VectorSignalsCher)
            Calib_BarrelR_VectorSignalsCher = calibration2.calibcher(
                BarrelR_VectorSignalsCher)
            #end of calibrations

            energyscin = sum(Calib_BarrelR_VectorSignals) + sum(
                Calib_BarrelL_VectorSignals)
            energycher = sum(Calib_BarrelR_VectorSignalsCher) + sum(
                Calib_BarrelL_VectorSignalsCher)

            ScinEnergyHist.Fill(energyscin)
            CherEnergyHist.Fill(energycher)

            #sigmascin = 0.15*(energyscin**0.5)+0.012*energyscin #old value
            #sigmacher = 0.18*(energycher**0.5)+0.0045*energycher #old value
            sigmascin = 0.177 * (energyscin**0.5) + 0.006 * energyscin
            sigmacher = 0.194 * (energycher**0.5) + 0.001 * energycher

            RecEnergyHist.Fill(
                (energyscin / (sigmascin**2) + energycher /
                 (sigmacher**2)) / (1 / sigmascin**2 + 1 / sigmacher**2))

            #RecEnergyHist.Fill((energyscin+energycher)/2)

            Energytot += (sum(VectorL) + sum(VectorR)) / 1000

        Energytot = Energytot / int(tree.GetEntries())
        print Energytot, ScinEnergyHist.GetMean(), CherEnergyHist.GetMean()
        displayfile.cd()
        gStyle.SetOptStat(111)
        ScinEnergyHist.Fit("gaus")
        CherEnergyHist.Fit("gaus")
        RecEnergyHist.Fit("gaus")
        RecEnergyHist.Write()
        ScinEnergyHist.Write()
        CherEnergyHist.Write()
        #MeanEnergyScin.append(ScinEnergyHist.GetFunction("gaus").GetParameter(1)/Energytot)
        MeanEnergyScin.append(ScinEnergyHist.GetMean() / energy)
        #MeanEnergyCher.append(CherEnergyHist.GetFunction("gaus").GetParameter(1)/Energytot)
        MeanEnergyCher.append(CherEnergyHist.GetMean() / energy)
        MeanEnergy.append(
            RecEnergyHist.GetFunction("gaus").GetParameter(1) / energy)
        resolution.append(
            RecEnergyHist.GetFunction("gaus").GetParameter(2) /
            RecEnergyHist.GetFunction("gaus").GetParameter(1))
        resolutionscin.append(
            ScinEnergyHist.GetFunction("gaus").GetParameter(2) /
            ScinEnergyHist.GetFunction("gaus").GetParameter(1))
        resolutioncher.append(
            CherEnergyHist.GetFunction("gaus").GetParameter(2) /
            CherEnergyHist.GetFunction("gaus").GetParameter(1))

    print MeanEnergyScin, MeanEnergyCher
    LinearityGraph = TGraph(len(towers), towers, MeanEnergy)
    LinearityGraph.SetName("LinearityGraph")
    LinearityGraph.Write()
    LinearityGraphScin = TGraph(len(towers), towers, MeanEnergyScin)
    LinearityGraphCher = TGraph(len(towers), towers, MeanEnergyCher)
    LinearityGraphCher.SetName("LinearityGraphCher")
    LinearityGraphCher.Write()
    LinearityGraphScin.SetName("LinearityGraphScin")
    LinearityGraphScin.Write()
    ResolutionGraphScin = TGraph(len(towers), towers, resolutionscin)
    ResolutionGraphCher = TGraph(len(towers), towers, resolutioncher)
    ResolutionGraphScin.SetName("ResolutionGraphScin")
    ResolutionGraphScin.Write()
    ResolutionGraphCher.SetName("ResolutionGraphCher")
    ResolutionGraphCher.Write()
    ResolutionGraph = TGraph(len(towers), towers, resolution)
    ResolutionGraph.SetName("ResolutionGraph")
    ResolutionGraph.Write()
    x2 = array('d', (0., 90., 90., 0.))
    y2 = array('d', (0.024, 0.024, 0.034, 0.034))
    Fillgraph2 = TGraph(4, x2, y2)
    Fillgraph2.SetName("ban")
    Fillgraph2.Write()
    linefill1 = TF1("1", str(1.0), 0., 90.)
    linefill1.Write()

    EMResolutions = TMultiGraph()
    EMResolutions.Add(ResolutionGraphScin)
    EMResolutions.Add(ResolutionGraphCher)
    EMResolutions.Add(ResolutionGraph)
    EMResolutions.SetName("EMResolutions")
    EMResolutions.Write()

    Linearities = TMultiGraph()
    Linearities.Add(LinearityGraph)
    Linearities.Add(LinearityGraphScin)
    Linearities.Add(LinearityGraphCher)
    Linearities.SetName("Linearities")
    Linearities.Write()
Esempio n. 17
0
def main(argv):
    # gROOT.ProcessLine("gErrorIgnoreLevel = 3001;") # suppress ROOT error messages
    startT = time.clock()
    print "Started:", time.strftime('%X %x %Z')
    intMode, batMode, rangeMode, fileMode, dsNum, subNum, runNum = False, False, False, False, -1, -1, -1
    for i, opt in enumerate(argv):
        if opt == "-i":
            intMode = True
            print "Interactive mode selected. Use \"p\" for previous and \"q\" to exit."
        if opt == "-r":
            rangeMode = True
            dsNum, subNum = int(argv[i + 1]), int(argv[i + 2])
            print "Scanning DS-%d range %d" % (dsNum, subNum)
        if opt == "-f":
            fileMode = True
            dsNum, runNum = int(argv[i + 1]), int(argv[i + 2])
        if opt == "-b":
            batMode = True
            import matplotlib
            if os.environ.get('DISPLAY', '') == '':
                print('No display found. Using non-interactive Agg backend')
                matplotlib.use('Agg')
            print "Batch mode selected.  A new file will be created."
    import matplotlib.pyplot as plt

    # Set file I/O and cuts
    inFile = "./data/waveSkimDS4_test.root"
    outFile = "./data/waveletSkimDS4_test.root"
    if (rangeMode):
        inFile = "~/project/v2-waveskim/waveSkimDS%d_%d.root" % (dsNum, subNum)
        outFile = "~/project/v2-processwfs/waveletSkimDS%d_%d.root" % (dsNum,
                                                                       subNum)
    if (fileMode):
        # inFile = "~/project/cal-wave-skim/waveSkimDS%d_run%d.root" % (dsNum,runNum)
        # outFile = "~/project/cal-wavelet-skim/waveletSkimDS%d_run%d.root" % (dsNum,runNum)
        inFile = "./waveSkimDS%d_run%d.root" % (dsNum, runNum)
        outFile = "./waveletSkimDS%d_run%d.root" % (dsNum, runNum)

    inputFile = TFile(inFile)
    waveTree = inputFile.Get("skimTree")
    print "Found", waveTree.GetEntries(), "input entries."

    theCut = inputFile.Get("theCut").GetTitle()
    # theCut = "trapENFCal > 0.8 && gain==0 && mHClean==1 && isGood && !muVeto && !isLNFill1 && !isLNFill2 && P!=0 && D!=0 && trapETailMin<0"
    # theCut += " && Entry$ < 2000"
    # theCut += " && trapENFCal > 20"
    print "Using cut:\n", theCut, "\n"

    print "Attempting entrylist draw ..."
    waveTree.Draw(">>elist", theCut, "GOFF entrylist")
    elist = gDirectory.Get("elist")
    waveTree.SetEntryList(elist)
    nList = elist.GetN()
    print "Found", nList, "entries passing cuts."

    # In batch mode ONLY, create an output file+tree & append new branches
    outputFile = TFile()
    outTree = TTree()
    if (batMode == True):
        outputFile = TFile(outFile, "RECREATE")
        print "Attempting tree copy to", outFile
        outTree = waveTree.CopyTree("")
        outTree.Write()
        print "Wrote", outTree.GetEntries(), "entries."
        cutUsed = TNamed("cutUsedHere", theCut)
        cutUsed.Write()

    waveS0 = std.vector("double")()
    waveS1 = std.vector("double")()
    waveS2 = std.vector("double")()
    waveS3 = std.vector("double")()
    waveS4 = std.vector("double")()
    waveS5 = std.vector("double")()
    wpar4 = std.vector("double")()
    waveEnt = std.vector("double")()
    butterMax = std.vector("double")()
    butterTime = std.vector("double")()
    tOffset = std.vector("double")()
    lastZeroTime = std.vector("double")()
    pol0 = std.vector("double")()
    pol1 = std.vector("double")()
    pol2 = std.vector("double")()
    pol3 = std.vector("double")()
    exp0 = std.vector("double")()
    exp1 = std.vector("double")()
    rt10 = std.vector("double")()
    rt20 = std.vector("double")()
    rt50 = std.vector("double")()
    rt90 = std.vector("double")()

    bWaveS0 = outTree.Branch("waveS0", waveS0)
    bWaveS1 = outTree.Branch("waveS1", waveS1)
    bWaveS2 = outTree.Branch("waveS2", waveS2)
    bWaveS3 = outTree.Branch("waveS3", waveS3)
    bWaveS4 = outTree.Branch("waveS4", waveS4)
    bWaveS5 = outTree.Branch("waveS5", waveS5)
    bWPar4 = outTree.Branch("wpar4", wpar4)
    bWaveEnt = outTree.Branch("waveEnt", waveEnt)
    bButterMax = outTree.Branch("butterMax", butterMax)
    bButterTime = outTree.Branch("butterTime", butterTime)
    bTOffset = outTree.Branch("tOffset", tOffset)
    bLastZeroTime = outTree.Branch("lastZeroTime", lastZeroTime)
    bPol0 = outTree.Branch("pol0", pol0)
    bPol1 = outTree.Branch("pol1", pol1)
    bPol2 = outTree.Branch("pol2", pol2)
    bPol3 = outTree.Branch("pol3", pol3)
    bExp0 = outTree.Branch("exp0", exp0)
    bExp1 = outTree.Branch("exp1", exp1)
    bRt10 = outTree.Branch("rt10", rt10)
    bRt20 = outTree.Branch("rt20", rt20)
    bRt50 = outTree.Branch("rt50", rt50)
    bRt90 = outTree.Branch("rt90", rt90)

    # Make a figure
    fig = plt.figure(figsize=(7, 7), facecolor='w')
    p1 = plt.subplot(211)
    p2 = plt.subplot(212)
    if not batMode: plt.show(block=False)

    # Begin loop over events
    iList = -1
    while True:
        iList += 1
        if intMode == True and iList != 0:
            value = raw_input()
            if value == 'q': break  # quit
            if value == 'p': iList -= 2  # go to previous
            if (value.isdigit()):
                iList = int(value)  # go to entry number
        elif intMode == False and batMode == False:
            plt.pause(0.001)  # rapid-draw mode
        if iList >= nList: break  # bail out, goose!

        entry = waveTree.GetEntryNumber(iList)
        waveTree.LoadTree(entry)
        waveTree.GetEntry(entry)
        nChans = waveTree.channel.size()
        waveS1.assign(nChans, -999)
        waveS0.assign(nChans, -999)
        waveS1.assign(nChans, -999)
        waveS2.assign(nChans, -999)
        waveS3.assign(nChans, -999)
        waveS4.assign(nChans, -999)
        waveS5.assign(nChans, -999)
        wpar4.assign(nChans, -999)
        waveEnt.assign(nChans, -999)
        butterMax.assign(nChans, -999)
        butterTime.assign(nChans, -999)
        tOffset.assign(nChans, -999)
        lastZeroTime.assign(nChans, -999)
        pol0.assign(nChans, -999)
        pol1.assign(nChans, -999)
        pol2.assign(nChans, -999)
        pol3.assign(nChans, -999)
        exp0.assign(nChans, -999)
        exp1.assign(nChans, -999)
        rt10.assign(nChans, -999)
        rt20.assign(nChans, -999)
        rt50.assign(nChans, -999)
        rt90.assign(nChans, -999)

        # Loop over hits passing cuts
        numPass = waveTree.Draw("channel", theCut, "GOFF", 1, iList)
        chans = waveTree.GetV1()
        chanList = list(set(int(chans[n]) for n in xrange(numPass)))
        hitList = (iH for iH in xrange(nChans)
                   if waveTree.channel.at(iH) in chanList
                   )  # a 'generator expression'
        for iH in hitList:
            run = waveTree.run
            iEvent = waveTree.iEvent
            chan = waveTree.channel.at(iH)
            energy = waveTree.trapENFCal.at(iH)
            wf = waveTree.MGTWaveforms.at(iH)
            startTime = waveTree.triggerTrapt0.at(iH)
            blrwfFMR50 = waveTree.blrwfFMR50.at(iH)

            if wf.GetID() != chan:
                print "Warning!!  Vector matching failed!  iList %d  run %d  iEvent %d" % (
                    iList, run, iEvent)
                break

            signal = wl.processWaveform(wf, opt='full')
            waveBLSub = signal.GetWaveBLSub()
            waveFilt = signal.GetWaveFilt()
            waveTS = signal.GetTS()
            waveletYOrig, waveletYTrans = signal.GetWavelet()

            # waveFTX, waveFTY, waveFTPow = signal.GetFourier()
            # waveTrap = signal.GetTrapezoid()
            # waveTrapF = signal.GetFiltTrapezoid()
            # waveTrapX = np.linspace(0, len(waveTrap), num=len(waveTrap))
            # _,waveletFilt = wl.waveletTransform(waveFilt,level=3)  # testing other levels on the filtered WF

            # Wavelet cut parameters
            waveS0[iH] = np.sum(waveletYTrans[0:1, 1:-1])
            waveS1[iH] = np.sum(waveletYTrans[0:1, 1:33])
            waveS2[iH] = np.sum(waveletYTrans[0:1, 33:65])
            waveS3[iH] = np.sum(waveletYTrans[0:1, 65:97])
            waveS4[iH] = np.sum(waveletYTrans[0:1, 97:-1])
            waveS5[iH] = np.sum(waveletYTrans[2:-1, 1:-1])
            wpar4[iH] = np.amax(waveletYTrans[0:1, 1:-1])

            # Waveform entropy parameters
            d1 = 2. * np.multiply(
                waveletYTrans[0:1, 1:65],
                np.log(waveletYTrans[0:1, 1:65] / waveS0[iH] / 2.0))
            d2 = np.multiply(waveletYTrans[0:1, 65:-1],
                             np.log(waveletYTrans[0:1, 65:-1] / waveS0[iH]))
            waveEnt[iH] = np.abs(np.sum(d1)) + np.abs(np.sum(d2))

            # Smoothed derivative params
            waveFiltDeriv = wl.wfDerivative(waveFilt)
            butterMax[iH] = np.amax(waveFiltDeriv)
            butterTime[iH] = np.argmax(
                waveFiltDeriv[100:]) * 10 + signal.GetOffset() + 1000
            tOffset[iH] = signal.GetOffset()
            # print "max %.3f  ts %.0f ns  offset %.0f ns" % (butterMax[iH], butterTime[iH], tOffset[iH])

            # Make a super denoised waveform
            wp = pywt.WaveletPacket(data=waveBLSub,
                                    wavelet='haar',
                                    mode='symmetric',
                                    maxlevel=3)
            new_wp = pywt.WaveletPacket(data=None,
                                        wavelet='haar',
                                        mode='symmetric')
            new_wp['aaa'] = wp['aaa'].data
            superDenoisedWF = new_wp.reconstruct(update=False)
            mgtSDWF = wl.MGTWFFromNpArray(superDenoisedWF)

            # Try to get start time by finding the super denoised last zero crossing
            lastZero = 0
            zeros = np.asarray(np.where(superDenoisedWF < 0.1))
            if (zeros.size > 0): lastZero = zeros[0, -1]
            lastZeroTime[iH] = waveTS[lastZero]

            # Time point calculator
            timePointCalc = MGWFTimePointCalculator()
            timePointCalc.AddPoint(.1)
            timePointCalc.AddPoint(.2)
            timePointCalc.AddPoint(.5)
            timePointCalc.AddPoint(.9)
            timePointCalc.FindTimePoints(mgtSDWF)
            rt10[iH] = timePointCalc.GetFromStartRiseTime(0) * 10
            rt20[iH] = timePointCalc.GetFromStartRiseTime(1) * 10
            rt50[iH] = timePointCalc.GetFromStartRiseTime(2) * 10
            rt90[iH] = timePointCalc.GetFromStartRiseTime(3) * 10
            # print "lastZero %.2f  startTime %.2f  10 %.2f  20 %.2f  50 %.2f  90 %.2f" % (lastZeroTime[iH], startTime,rt10, rt20, rt50, rt90)

            # Fit tail slope (2 methods).  Guard against fit fails
            idxMax = np.where(waveTS > rt90[iH])  # returns an array/tuple
            idxMax = idxMax[0][0]  # "cast" to int
            tail, tailTS = waveBLSub[idxMax:], waveTS[idxMax:]
            try:
                popt1, _ = curve_fit(wl.tailModelPol, tailTS, tail)
                pol0[iH], pol1[iH], pol2[iH], pol3[iH] = popt1[0], popt1[
                    1], popt1[2], popt1[3]
            except:
                pass
            try:
                popt2, _ = curve_fit(wl.tailModelExp,
                                     tailTS,
                                     tail,
                                     p0=[energy, 72000])
                exp0[iH], exp1[iH] = popt2[0], popt2[1]
            except:
                # print "curve_fit tailModelExp failed, run %i  event %i  channel %i" % (run, iList, chan)
                pass

            # Make a plot
            if not batMode:
                print "i %d  run %d  iEvent(i) %d  iH(j+1) %d/%d  chan %d  energy %.2f  bt %.2f" % (
                    iList, run, iEvent, iH + 1, nChans, chan, energy,
                    butterTime[iH])

                # Waveform plot
                p1.cla()
                p1.plot(waveTS, waveBLSub, color='blue')
                p1.plot(waveTS, superDenoisedWF, color='red')
                p1.plot(tailTS, wl.tailModelPol(tailTS, *popt1), color='cyan')
                p1.plot(tailTS,
                        wl.tailModelExp(tailTS, *popt2),
                        color='orange')
                p1.set_xlabel("Time (ns)")
                p1.set_ylabel("ADC")
                p1.set_title(
                    "Run %d  Ch %d  Energy %.2f  \n S5 %.2f  (S3-S2)/E %.2f  (S3-S4)/E %.2f"
                    % (run, chan, energy, waveS5[iH],
                       ((waveS3[iH] - waveS2[iH]) / energy),
                       ((waveS3[iH] - waveS4[iH]) / energy)))

                # Wavelet plot
                p2.cla()
                p2.imshow(waveletYTrans,
                          interpolation='nearest',
                          aspect="auto",
                          origin="lower",
                          extent=[0, 1, 0, len(waveletYTrans)],
                          cmap='jet')

                plt.tight_layout()
                plt.subplots_adjust(hspace=.2, top=0.92)
                plt.pause(0.00001)

        # End loop over hits
        if (batMode == True):
            bWaveS0.Fill()
            bWaveS1.Fill()
            bWaveS2.Fill()
            bWaveS3.Fill()
            bWaveS4.Fill()
            bWaveS5.Fill()
            bWPar4.Fill()
            bWaveEnt.Fill()
            bButterMax.Fill()
            bButterTime.Fill()
            bTOffset.Fill()
            bLastZeroTime.Fill()
            bPol0.Fill()
            bPol1.Fill()
            bPol2.Fill()
            bPol3.Fill()
            bExp0.Fill()
            bExp1.Fill()
            bRt10.Fill()
            bRt20.Fill()
            bRt50.Fill()
            bRt90.Fill()
            if iList % 5000 == 0:
                outTree.Write("", TObject.kOverwrite)
                print "%d / %d entries saved (%.2f %% done)." % (
                    iList, nList, 100 * (float(iList) / nList))

    # End loop over events
    if (batMode == True):
        outTree.Write("", TObject.kOverwrite)
        print "Wrote", outTree.GetBranch(
            "channel").GetEntries(), "entries in the copied tree,"
        print "and wrote", bWaveS0.GetEntries(), "entries in the new branches."

    stopT = time.clock()
    print "Process time (min): ", (stopT - startT) / 60
Esempio n. 18
0
if line or noline:
    gr = TGraph(tree.GetSelectedRows(),tree.GetV2(),tree.GetV1())
    gr.SetTitle(modulename +" " + " Channel "+ str(chan_n) +" " + gainvalue + titsuff)
    gr.SetMarkerStyle(20)
    gr.SetMarkerSize(1.3)
    gr.SetMarkerColor(2)
    gr.SetLineColor(2)
    gr.GetXaxis().SetNoExponent(kTRUE)
    if one_run: gr.GetXaxis().SetTitle("Lumi")
    else: gr.GetXaxis().SetTitle("Runs")
    if noline: gr.Draw("ap")
    else: gr.Draw("apl")

elif opt2d:
    nentries = tree.GetEntries()
    hhh.SetName(modulename)
    hhh.SetTitle(partname +" " + gainvalue + titsuff)
    hhh.GetYaxis().SetTitle("Channels")
    hhh.GetXaxis().SetTitle("Modules")

    if multi:
        Matrix = [[1 for y in range(48)] for x in range(64)] 
        for i in xrange(nentries):
            tree.GetEntry(i)
            if vals.size()>0:
                x=int(module_n[0])
                y=int(channel_n[0])
                Matrix[x][y] *= vals[0]
        sc = Matrix[modmax-1][chanmax] if norm and Matrix[modmax-1][chanmax]>0 else 1
        for x in xrange(modmin,modmax+1):
Esempio n. 19
0
class TrigNtupleHandler:

    def __init__(self):

        self.fileName = 'lumi.root'
        self.treeName = 'lumiData'
        self.file = None
        self.tree = None
        self.updatemode = False

        # Flag showing whether BCID data is initialized
        self.bcidData = False

        # Flag showing whether L1 trigger counts are initialized
        self.l1TrigData = True
        
    def open(self, update=True):
        print('NtupleHandler.open() called')

        if os.path.exists(self.fileName) and update:
            print('Opening %s for updating' % self.fileName)
            self.updatemode = True
            self.file = TFile(self.fileName, 'update')
            self.tree = self.file.Get(self.treeName)
            
        else:
            print('Creating %s for writing' % self.fileName)
            self.updatemode = False
            self.file = TFile(self.fileName, 'create')
            self.tree = TTree(self.treeName, self.treeName)
            

    def close(self):
        print('ScanNtupleHandler.close() called')
        
        self.tree.Write('', TObject.kOverwrite)
        self.file.Close()
        
    def init(self):
        print('ScanNtupleHandler.init() called')

        self.initLBData()
        self.initBCIDData()

    def save(self):
        self.tree.Fill()

    def readLastEntry(self):
        entries = self.tree.GetEntries()
        self.tree.GetEntry(entries-1)
        
    # Fill information from LumiLBData object
    def fillLumi(self, lumi):

        # Erase any old data
        self.clear()

        # Time in COOL format (ns)
        self.lbData.coolStartTime = lumi.startTime.timerunlb()
        self.lbData.coolEndTime = lumi.endTime.timerunlb()
        # Time in seconds
        self.lbData.startTime = lumi.startTime.timerunlb()/1.E9
        self.lbData.endTime = lumi.endTime.timerunlb()/1.E9
        # LB duration in seconds
        self.lbData.lbTime = (lumi.endTime.timerunlb() - lumi.startTime.timerunlb())/1.E9
        
        self.lbData.run = lumi.runLB.run
        self.lbData.lb = lumi.runLB.lb

        # Need to fill these elsewhere
        # self.lbData.fill = 0
        # self.lbData.eBeam = 0.
        # self.lbData.stable = False
        # self.lbData.ready = False
        # self.lbData.physics = False        

        # if lumi.onlEvtsPerBX > 0.:
        #    self.lbData.muToLumi = lumi.onlInstLumi/lumi.onlEvtsPerBX
        
        self.lbData.onlInstLum = lumi.onlInstLumi
        self.lbData.onlInstLumAll = lumi.onlInstLumiAll
        self.lbData.onlEvtsPerBX = lumi.onlEvtsPerBX

        self.lbData.onlPrefChan = lumi.onlPrefChan
        self.lbData.onlValid = lumi.onlValid
        self.lbData.olcValid = lumi.olcValid
        
        self.lbData.nColl = lumi.bcid.nbcol()
        self.lbData.nBeam1 = lumi.bcid.nb1()
        self.lbData.nBeam2 = lumi.bcid.nb2()
        self.lbData.qBeam1Col = lumi.IBeam1
        self.lbData.qBeam2Col = lumi.IBeam2
        self.lbData.qBeam1All = lumi.IBeam1All
        self.lbData.qBeam2All = lumi.IBeam2All

        self.lbData.specLumi = lumi.specLumi
        self.lbData.geomLumi = lumi.geomLumi
        self.lbData.maxEvtsPerBX = lumi.maxEvtsPerBX

        self.lbData.l1LiveFrac = lumi.l1Livefrac

        # Get this from the veto folders
        # self.lbData.avgLiveFrac = -1.
        # self.lbData.lumiWtLiveFrac = -1.

        self.lbData.matched = lumi.matched

        if not self.bcidData: return
        
        # And fill the per-BCID values
        for (bcid, caliLumi) in lumi.bcid.caliLumi.iteritems():
            self.lumiDel[int(bcid)] = caliLumi

        for (bcid, q) in lumi.bcid.b1Curr.iteritems():
            self.qBeam1[int(bcid)] = q

        for (bcid, q) in lumi.bcid.b2Curr.iteritems():
            self.qBeam2[int(bcid)] = q

        i=0
        for bcid in sorted(list(lumi.bcid.b1BCID)):
            self.b1BCID[i] = bcid
            i += 1

        i=0
        for bcid in sorted(list(lumi.bcid.b2BCID)):
            self.b2BCID[i] = bcid
            i += 1

        i=0
        for bcid in sorted(list(lumi.bcid.colBCID)):
            self.colBCID[i] = bcid
            i += 1

        # Still need live fraction

    # Pass TriggerL1Data object for lumi block filled by TriggerHandler
    # Also need mapping of channel names to channel values
    def fillL1Trig(self, trigData, trigChan):
        for (trig, chan) in trigChan.iteritems():
            self.l1TBP[chan] = trigData.TBP[trig]
            self.l1TAP[chan] = trigData.TAP[trig]
            self.l1TAV[chan] = trigData.TAV[trig]

    def defineBranch(self, name, type):
        self.tree.Branch(name, AddressOf(self.lbData, name), name+'/'+type)

    def loadBranch(self, name):
        branch = self.tree.GetBranch(name)
        branch.SetAddress(AddressOf(self.lbData, name))
        
    def initLBData(self):

        # Define the main lumiblock data
        # Time is in ns
        
#            ULong64_t startTime;\
#            ULong64_t endTime;\

        LBDataStructStr = "struct LBDataStruct {\
            ULong64_t coolStartTime;\
            ULong64_t coolEndTime;\
            Double_t startTime;\
            Double_t endTime;\
            Float_t lbTime;\
            UInt_t fill;\
            UInt_t run;\
            UInt_t lb;\
            Float_t eBeam;\
            Bool_t stable;\
            Bool_t ready;\
            Bool_t physics;\
            Bool_t larVeto;\
            \
            UInt_t onlValid;\
            UInt_t olcValid;\
            UInt_t onlPrefChan;\
            Float_t muToLumi;\
            Float_t onlInstLum;\
            Float_t onlInstLumAll;\
            Float_t onlEvtsPerBX;\
            \
            UInt_t nColl;\
            UInt_t nBeam1;\
            UInt_t nBeam2;\
            Float_t qBeam1Col;\
            Float_t qBeam2Col;\
            Float_t qBeam1All;\
            Float_t qBeam2All;\
            \
            Float_t specLumi;\
            Float_t geomLumi;\
            Float_t maxEvtsPerBX;\
            \
            Float_t l1LiveFrac;\
            Float_t avgLiveFrac;\
            Float_t lumiWtLiveFrac;\
            \
            UInt_t matched;\
        };"
        
        # Replace sizes if needed
        gROOT.ProcessLine(LBDataStructStr)
        from ROOT import LBDataStruct
        self.lbData = LBDataStruct()

        self.varList = []

        self.varList.append(('startTime', 'D'))
        self.varList.append(('endTime', 'D'))
        self.varList.append(('coolStartTime', 'l'))
        self.varList.append(('coolEndTime', 'l'))
        self.varList.append(('lbTime', 'F'))
        
        self.varList.append(('fill', 'i'))
        self.varList.append(('run', 'i'))
        self.varList.append(('lb', 'i'))
        self.varList.append(('eBeam', 'F'))

        # Boolean status flags
        self.varList.append(('stable', 'O'))
        self.varList.append(('ready', 'O'))
        self.varList.append(('physics', 'O'))
        self.varList.append(('larVeto', 'O'))
        
        # Luminosity information
        self.varList.append(('onlPrefChan', 'i'))
        self.varList.append(('muToLumi', 'F'))
        self.varList.append(('onlInstLum', 'F'))
        self.varList.append(('onlInstLumAll', 'F'))
        self.varList.append(('onlEvtsPerBX', 'F'))
        self.varList.append(('onlValid', 'i'))  # From LBLESTONL & 0x3FF
        self.varList.append(('olcValid', 'i'))  # From LUMINOSITY
        
        # Total bunch information
        self.varList.append(('nColl', 'i'))
        self.varList.append(('nBeam1', 'i'))
        self.varList.append(('nBeam2', 'i'))
        self.varList.append(('qBeam1Col', 'F')) # Total charge colliding
        self.varList.append(('qBeam2Col', 'F'))
        self.varList.append(('qBeam1All', 'F')) # Total charge in all BCIDs
        self.varList.append(('qBeam2All', 'F'))

        self.varList.append(('specLumi', 'F'))
        self.varList.append(('geomLumi', 'F'))
        self.varList.append(('maxEvtsPerBX', 'F'))

        # Livetime information
        self.varList.append(('l1LiveFrac', 'F'))
        self.varList.append(('avgLiveFrac', 'F'))
        self.varList.append(('lumiWtLiveFrac', 'F')) # lumi-weighted per-BCID livefraction

        # Where the lumi information came from
        self.varList.append(('matched', 'i'))

        for (var, type) in self.varList:
            if self.updatemode:
                self.loadBranch(var)

            else:
                self.defineBranch(var, type)
            
    def initBCIDData(self):

        self.bcidData = True
        
        # Delivered luminosity
        self.lumiDel = array.array('f', (0.,)*3564)
        self.qBeam1  = array.array('f', (0.,)*3564)         # Charge per BCID
        self.qBeam2  = array.array('f', (0.,)*3564)
        self.liveFrac = array.array('f', (0.,)*3564)        # Deadtime

        if self.updatemode:
            self.tree.GetBranch('lumiDel').SetAddress(self.lumiDel)
            self.tree.GetBranch('qBeam1').SetAddress(self.qBeam1)
            self.tree.GetBranch('qBeam2').SetAddress(self.qBeam2)
            self.tree.GetBranch('liveFrac').SetAddress(self.liveFrac)
            
            
        else:
            self.tree.Branch('lumiDel',  self.lumiDel,  'lumiDel[3564]/F')
            self.tree.Branch('qBeam1',   self.qBeam1,   'qBeam1[3564]/F')
            self.tree.Branch('qBeam2',   self.qBeam2,   'qBeam2[3564]/F')
            self.tree.Branch('liveFrac', self.liveFrac, 'liveFrac[3564]/F') # Per-BCID livetime from lumi counters
            
        # BCID arrays (unsigned shorts)
        self.b1BCID = array.array('H', (0,)*3564)
        self.b2BCID = array.array('H', (0,)*3564)
        self.colBCID = array.array('H', (0,)*3564)

        if self.updatemode:
            self.tree.GetBranch('b1BCID').SetAddress(self.b1BCID)
            self.tree.GetBranch('b2BCID').SetAddress(self.b2BCID)
            self.tree.GetBranch('colBCID').SetAddress(self.colBCID)
        else:
            self.tree.Branch('b1BCID', self.b1BCID, 'b1BCID[nBeam1]/s') # Unsigned short
            self.tree.Branch('b2BCID', self.b2BCID, 'b2BCID[nBeam2]/s') # Unsigned short
            self.tree.Branch('colBCID', self.colBCID, 'colBCID[nColl]/s') # Unsigned short

    def initL1TrigData(self):

        self.l1TrigData = True
        
        # Counts by channel ID
        self.l1TBP = array.array('I', (0,)*256)
        self.l1TAP = array.array('I', (0,)*256)
        self.l1TAV = array.array('I', (0,)*256)

        if self.updatemode:
            self.tree.GetBranch('l1TBP').SetAddress(self.l1TBP)
            self.tree.GetBranch('l1TAP').SetAddress(self.l1TAP)
            self.tree.GetBranch('l1TAV').SetAddress(self.l1TAV)
            
        else:
            self.tree.Branch('l1TBP',  self.l1TBP, 'l1TBP[256]/i')
            self.tree.Branch('l1TAP',  self.l1TAP, 'l1TAP[256]/i')
            self.tree.Branch('l1TAV',  self.l1TAV, 'l1TAV[256]/i')
            

    # Set all ntuple variables to default values
    def clear(self):

        self.lbData.startTime = 0
        self.lbData.endTime = 0
        self.lbData.lbTime = 0.
        self.lbData.fill = 0
        self.lbData.run = 0
        self.lbData.lb = 0
        self.lbData.eBeam = 0.

        self.lbData.stable = False
        self.lbData.ready = False
        self.lbData.physics = False        
        self.lbData.larVeto = False
        
        self.lbData.onlPrefChan = 0
        self.lbData.muToLumi = 0.
        self.lbData.onlInstLum = -1.
        self.lbData.onlInstLumAll = -1.
        self.lbData.onlEvtsPerBX = -1.
        self.lbData.onlValid = 0xFFFFFF
        self.lbData.olcValid = 0xFFFFFF
        
        self.lbData.nColl = 0
        self.lbData.nBeam1 = 0
        self.lbData.nBeam2 = 0
        self.lbData.qBeam1Col = -1.
        self.lbData.qBeam2Col = -1.
        self.lbData.qBeam1All = -1.
        self.lbData.qBeam2All = -1.

        self.lbData.specLumi = -1.
        self.lbData.geomLumi = -1.
        self.lbData.maxEvtsPerBX = -1.

        self.lbData.l1LiveFrac = -1.
        self.lbData.avgLiveFrac = -1.
        self.lbData.lumiWtLiveFrac = -1.

        self.lbData.matched = 0

        if self.bcidData:
                    
            # Per-BCID arrays
            for i in range(3564):
                self.lumiDel[i] = 0.
                self.qBeam1[i] = 0.
                self.qBeam2[i] = 0.
                self.liveFrac[i] = 0.
                self.b1BCID[i] = 0
                self.b2BCID[i] = 0
                self.colBCID[i] = 0

        if self.l1TrigData:

            # L1 trigger counts
            for i in range(256):
                self.l1TBP[i] = 0
                self.l1TAP[i] = 0
                self.l1TAV[i] = 0
Esempio n. 20
0
class CompareSpring15:
    def __init__(self):
        self.ref = ""
        self.test = ""

        self.cref = None
        self.ctest = None

        self.lref = []
        self.ltest = []

        self.ref_missing = []
        self.test_missing = []

        self.bad_events = []
        self.bad_leptons = []
        self.bad_met = []
        self.bad_mvamet = []
        self.bad_jets = []

        self.good_events = []

    def load(self, ref, test):
        self.ref = ref
        self.test = test

        #self.cref=TChain(ref.split(":")[1])
        #self.ctest=TChain(test.split(":")[1])

        #self.cref.Add(ref.split(":")[0])
        #self.ctest.Add(test.split(":")[0])

        self.fref = TFile(ref.split(":")[0])
        self.ftest = TFile(test.split(":")[0])

        self.cref = TTree()
        self.fref.GetObject(ref.split(":")[1], self.cref)

        self.ctest = TTree()
        self.ftest.GetObject(test.split(":")[1], self.ctest)

        self.lref = load_entries(self.cref)
        self.ltest = load_entries(self.ctest)

    def Compare(self):
        print self.cref.GetEntries(), self.ctest.GetEntries()

        self.ref_min_pt_1 = 9999999999.
        self.ref_min_pt_2 = 9999999999.

        self.test_min_pt_1 = 9999999999.
        self.test_min_pt_2 = 9999999999.

        iref = 0
        itest = 0

        self.cref.LoadBaskets(2000000000)
        self.ctest.LoadBaskets(2000000000)

        while (iref < len(self.lref)):
            ref_id = self.lref[iref][1]

            while (itest < len(self.ltest)):
                test_id = self.ltest[itest][1]

                if (test_id < ref_id):
                    self.ref_missing.append(self.ltest[itest])
                elif (test_id > ref_id):
                    self.test_missing.append(self.lref[iref])
                    break
                else:
                    break
                itest += 1

            if (test_id != ref_id):
                iref += 1
                continue

            if (self.cref.pt_1 < self.ref_min_pt_1):
                self.ref_min_pt_1 = self.cref.pt_1
            if (self.cref.pt_2 < self.ref_min_pt_2):
                self.ref_min_pt_2 = self.cref.pt_2

            if (self.ctest.pt_1 < self.test_min_pt_1):
                self.test_min_pt_1 = self.ctest.pt_1
            if (self.ctest.pt_2 < self.test_min_pt_2):
                self.test_min_pt_2 = self.ctest.pt_2

            isGood = True

            self.cref.GetEntry(self.lref[iref][0])
            self.ctest.GetEntry(self.ltest[itest][0])

            # compare leptons
            if (not areEqual(self.cref.pt_1, self.ctest.pt_1)
                    or not areEqual(self.cref.iso_1, self.ctest.iso_1)
                    or not areEqual(self.cref.pt_2, self.ctest.pt_2)
                    or not areEqual(self.cref.iso_2, self.ctest.iso_2)):

                isGood = False

                print "Event", self.lref[iref][1]
                print "    lep1: pt=",self.cref.pt_1, self.ctest.pt_1,\
                        ", eta=",self.cref.eta_1,self.ctest.eta_1,\
                        ", phi=",self.cref.phi_1,self.ctest.phi_1,\
                        ", iso=",self.cref.iso_1,self.ctest.iso_1,\
                        ", q=",self.cref.q_1,self.ctest.q_1
                print "    lep2: pt=",self.cref.pt_2, self.ctest.pt_2,\
                        ", eta=",self.cref.eta_2,self.ctest.eta_2,\
                        ", phi=",self.cref.phi_2,self.ctest.phi_2,\
                        ", iso=",self.cref.iso_2,self.ctest.iso_2,\
                        ", q=",self.cref.q_2,self.ctest.q_2

                refDR = deltaR(self.cref.eta_1, self.cref.phi_1,
                               self.cref.eta_2, self.cref.phi_2)
                testDR = deltaR(self.ctest.eta_1, self.ctest.phi_1,
                                self.ctest.eta_2, self.ctest.phi_2)
                print "    deltaR(lep1, lep2)=", refDR, testDR

                self.bad_leptons.append((self.lref[iref], self.ltest[itest]))

            # compare met
            if (not areEqual(self.cref.met, self.ctest.met)):

                isGood = False

                print "Event", self.lref[iref][1]
                print "    met: |met|=", self.cref.met, self.ctest.met

                self.bad_met.append((self.lref[iref], self.ltest[itest]))

            iref += 1
            itest += 1

    def Print(self):

        print len(self.ref_missing), "Events missing in ref tree"
        print len(self.test_missing), "Events missing in test tree"

        print "Events missing in test tree:"
        print[x[1] for x in self.test_missing]

        print
        print "Events missing in ref tree:"
        print[x[1] for x in self.ref_missing]

        print
        print len(self.bad_leptons), "Events with bad leptons"
        print[x[0][1] for x in self.bad_leptons]

        print
        print len(self.bad_met), "Events with bad pfmet"
        print[x[0][1] for x in self.bad_met]

        print self.ref_min_pt_1, self.test_min_pt_1
        print self.ref_min_pt_2, self.test_min_pt_2
def makeRooDataSet(type, infile_name, outfile_name, tree_name, nevents):
    """ Make RooDataSets from TTrees"""

    inputfile = TFile.Open(infile_name, "READ")
    print "Importing tree"
    tree = TTree()
    inputfile.GetObject(tree_name, tree)  #get the tree from the data file

    #define variables for the RooDataSet
    m_mumu = RooRealVar("m_mumu", "m_mumu", 0.0, 4.0)
    y_mumu = RooRealVar("y_mumu", "y_mumu", 0.0, 2.0)
    pt_mumu = RooRealVar("pt_mumu", "pt_mumu", 0.0, 260.0)
    eta_gamma = RooRealVar("eta_gamma", "eta_gamma", -3.5, 3.5)
    pt_gamma = RooRealVar("pt_gamma", "pt_gamma", 0.0, 100.0)
    m_gamma = RooRealVar("m_gamma", "m_gamma", -0.1, 0.1)

    m_chi_rf1S = RooRealVar("m_chi_rf1S", "m_chi_rf1S", 0.0, 7.0)
    m_chi_rf2S = RooRealVar("m_chi_rf2S", "m_chi_rf2S", -1.0, 1.0)
    Qvalue = RooRealVar("Qvalue", "Q", -15., 15.)

    ctpv = RooRealVar("ctpv", "ctpv", -1.0, 3.5)
    ctpv_error = RooRealVar("ctpv_err", "ctpv_err", -1.0, 1.0)
    pi0_abs_mass = RooRealVar("pi0_abs_mass", "pi0_abs_mass", 0.0, 2.2)
    psi1S_nsigma = RooRealVar("psi1S_nsigma", "psi1S_nsigma", 0.0, 1.0)
    psi2S_nsigma = RooRealVar("psi2S_nsigma", "psi2S_nsigma", 0.0, 1.0)
    psi3S_nsigma = RooRealVar("psi3S_nsigma", "psi3S_nsigma", 0.0, 1.0)
    rho_conv = RooRealVar("rho_conv", "rho_conv", 0.0, 70.0)
    dz = RooRealVar("dz", "dz", -1.0, 1.0)

    probFit1S = RooRealVar('probFit1S', 'probFit1S', 0, 1)
    probFit2S = RooRealVar('probFit2S', 'probFit2S', 0, 1)
    probFit3S = RooRealVar('probFit3S', 'probFit3S', 0, 1)

    dataArgSet = RooArgSet(m_mumu, y_mumu, pt_mumu, eta_gamma, pt_gamma,
                           m_gamma, m_chi_rf1S)

    dataArgSet.add(m_chi_rf2S)
    dataArgSet.add(Qvalue)
    dataArgSet.add(ctpv)
    dataArgSet.add(ctpv_error)
    dataArgSet.add(pi0_abs_mass)
    dataArgSet.add(psi1S_nsigma)
    dataArgSet.add(psi2S_nsigma)
    dataArgSet.add(rho_conv)
    dataArgSet.add(dz)
    dataArgSet.add(probFit1S)
    dataArgSet.add(probFit2S)
    dataArgSet.add(probFit3S)

    print "Creating DataSet"
    dataSet = RooDataSet("chicds", "Chic RooDataSet", dataArgSet)

    entries = tree.GetEntries()
    print entries

    if nevents is not 0:
        entries = nevents

    for ientry in range(0, entries):
        tree.GetEntry(ientry)

        # unfort ntuples are slightly different for chic and chib

        if applyscale:

            if usekinfit:
                spatial = tree.rf1S_photon_p4.Vect()
                spatial *= (1 / escale)
                corr_photon_p4 = TLorentzVector()
                #corr_photon_p4.SetVectM(spatial,tree.rf1S_photon_p4.M())
                corr_photon_p4.SetVectM(spatial, 0)
                corr_chi_p4 = tree.rf1S_dimuon_p4 + corr_photon_p4

            else:

                spatial = tree.photon_p4.Vect()
                spatial *= (1 / escale)
                corr_photon_p4 = TLorentzVector()
                corr_photon_p4.SetVectM(spatial, tree.photon_p4.M())
                corr_chi_p4 = tree.dimuon_p4 + corr_photon_p4

        else:
            corr_chi_p4 = tree.chi_p4

        if type == 'chic':

            m_mumu.setVal(tree.dimuon_p4.M())
            y_mumu.setVal(tree.dimuon_p4.Rapidity())
            pt_mumu.setVal(tree.dimuon_p4.Pt())
            eta_gamma.setVal(tree.photon_p4.Eta())
            pt_gamma.setVal(tree.photon_p4.Pt())
            m_gamma.setVal(tree.photon_p4.M())
            m_chi_rf1S.setVal(tree.rf1S_chi_p4.M())
            m_chi_rf1S.setVal(tree.rf2S_chi_p4.M())

            if usekinfit: Qvalue.setVal(corr_chi_p4.M())
            else: Qvalue.setVal((corr_chi_p4).M() - tree.dimuon_p4.M())
            print 'corr value ', corr_chi_p4.M()

            #Qvalue.setVal((tree.chi_p4).M()**2 - tree.dimuon_p4.M()**2)
            psi1S_nsigma.setVal(tree.psi1S_nsigma)
            psi2S_nsigma.setVal(tree.psi2S_nsigma)
            psi3S_nsigma.setVal(0)

        elif type == 'chib':

            m_mumu.setVal(tree.dimuon_p4.M())
            y_mumu.setVal(tree.dimuon_p4.Rapidity())
            pt_mumu.setVal(tree.dimuon_p4.Pt())
            eta_gamma.setVal(tree.photon_p4.Eta())
            pt_gamma.setVal(tree.photon_p4.Pt())
            m_chi_rf1S.setVal(tree.rf1S_chi_p4.M())
            m_chi_rf2S.setVal(tree.rf2S_chi_p4.M())

            if usekinfit: Qvalue.setVal(corr_chi_p4.M())
            else: Qvalue.setVal(corr_chi_p4.M() - tree.dimuon_p4.M())

            psi1S_nsigma.setVal(tree.Y1S_nsigma)
            psi2S_nsigma.setVal(tree.Y2S_nsigma)
            psi3S_nsigma.setVal(tree.Y3S_nsigma)

            probFit1S.setVal(tree.probFit1S)
            probFit2S.setVal(tree.probFit2S)
            probFit3S.setVal(tree.probFit3S)

        ctpv.setVal(tree.ctpv)
        ctpv_error.setVal(tree.ctpv_error)
        pi0_abs_mass.setVal(tree.pi0_abs_mass)

        rho_conv.setVal(tree.conv_vertex)
        dz.setVal(tree.dz)

        if selectchi1:
            if (tree.chic_pdgId == 20443): dataSet.add(dataArgSet)
        else:
            dataSet.add(dataArgSet)

    outfile = TFile(outfile_name, 'recreate')
    dataSet.Write()
Esempio n. 22
0
def jetdisplay():
    outputfile = "hznb"
    displayfile = TFile(outputfile + ".root", "RECREATE")

    inputfiles = ["hznb_noleakage_029/hznb.root"]

    #for geant4.10.5
    inputfiles = ["resultsgeant4.10.5/jetscan_leakage/hznb/hznb.root"]
    #end of geant4.10.5

    #for geant4.10.5 FTFPBERT
    inputfiles = ["results_FTFPBERT/noBnoX0/2j/hznb.root"]
    #end geant4.10.5 FTFPBERT

    for counter, inputfile in enumerate(inputfiles):
        inputfile = TFile(inputfile)
        print "Analyzing: " + str(inputfile) + " \n"
        tree = TTree()
        inputfile.GetObject("MyTree", tree)

        #graphEjet1 = TH1F("energyjet1", "energyjet1", 100, 0., 200.)
        #graphEjet2 = TH1F("energyjet2", "energyjet2", 100, 0., 200.)

        #graphEcherjet1 = TH1F("energycher1", "energycherjet", 100, 0., 200.)
        #graph3 = TH1F("energyscinjet", "energyscinjet", 100, 0., 200.)

        #graphmass = TH1F("mass_jet", "mass_jet", 100, 0., 200.)
        graphtest = TH1F("test", "test", 80, -40., 40.)
        graphenergy = TH1F("energy", "energy", 100, 60., 160.)
        graphenergytruth = TH1F("energytruth", "energytruth", 100, 60., 160.)
        graphjs = TH1F("energyjs", "energyjs", 200, 0., 100.)
        graphjc = TH1F("energyjc", "energyjc", 200, 0., 100.)

        #loop over events
        for Event in range(tree.GetEntries()):

            tree.GetEntry(Event)
            #print "Event "+str(Event)
            nmuon = tree.nmuon
            nneu = tree.nneu
            mjjr = tree.mjjr
            mjjt = tree.mjjt
            edep = tree.edep
            muene_che = tree.muene_che
            muene_sci = tree.muene_sci
            emcomp1 = tree.emcomp1
            emcomp2 = tree.emcomp2
            eleak = tree.eleak
            eleakn = tree.eleakn

            j1t_E = tree.j1t_E
            j1t_m = tree.j1t_m
            j1t_theta = tree.j1t_theta
            j1t_pt = tree.j1t_pt
            j1t_eta = tree.j1t_eta
            j1t_phi = tree.j1t_phi
            j2t_E = tree.j2t_E
            j2t_m = tree.j2t_m
            j2t_theta = tree.j2t_theta
            j2t_pt = tree.j2t_pt
            j2t_eta = tree.j2t_eta
            j2t_phi = tree.j2t_phi

            j1r_E = tree.j1r_E
            j1r_m = tree.j1r_m
            j1r_theta = tree.j1r_theta
            j1r_pt = tree.j1r_pt
            j1r_eta = tree.j1r_eta
            j1r_phi = tree.j1r_phi
            j2r_E = tree.j2r_E
            j2r_m = tree.j2r_m
            j2r_theta = tree.j2r_theta
            j2r_pt = tree.j2r_pt
            j2r_eta = tree.j2r_eta
            j2r_phi = tree.j2r_phi

            #deltaj1 = 0.04406*j1r_E+0.1158
            #deltaj1 = 0.04135*j1r_E+0.08789
            deltaj1 = 0.07113 * j1r_E + 0.5201
            j1 = TLorentzVector()
            j1.SetPtEtaPhiE(j1r_pt + deltaj1 * np.sin(j1r_theta), j1r_eta,
                            j1r_phi, j1r_E + deltaj1)
            #deltaj2 = 0.04406*j2r_E+0.1158
            #deltaj2 = 0.04135*j2r_E+0.08789
            deltaj2 = 0.07113 * j2r_E + 0.5201
            j2 = TLorentzVector()
            j2.SetPtEtaPhiE(j2r_pt + deltaj2 * np.sin(j2r_theta), j2r_eta,
                            j2r_phi, j2r_E + deltaj2)
            newmass = (j1 + j2).M()

            j1s_E = tree.j1s_E
            j1s_m = tree.j1s_m
            j1s_theta = tree.j1s_theta
            j1s_pt = tree.j1s_pt
            j1s_eta = tree.j1s_eta
            j1s_phi = tree.j1s_phi
            j2s_E = tree.j2s_E
            j2s_m = tree.j2s_m
            j2s_theta = tree.j2s_theta
            j2s_pt = tree.j2s_pt
            j2s_eta = tree.j2s_eta
            j2s_phi = tree.j2s_phi

            j1c_E = tree.j1c_E
            j1c_m = tree.j1c_m
            j1c_theta = tree.j1c_theta
            j1c_pt = tree.j1c_pt
            j1c_eta = tree.j1c_eta
            j1c_phi = tree.j1c_phi
            j2c_E = tree.j2c_E
            j2c_m = tree.j2c_m
            j2c_theta = tree.j2c_theta
            j2c_pt = tree.j2c_pt
            j2c_eta = tree.j2c_eta
            j2c_phi = tree.j2c_phi

            cut1 = nmuon == 0 and nneu == 2
            cut2 = abs(j1t_eta) < 2.0 and abs(j2t_eta) < 2.0
            cut3 = eleak < 1000.
            #cut4 = j1t_E+j2t_E>85.0
            cut4 = True
            #cut5 = edep>100
            cut5 = True
            if cut1 and cut2 and cut3 and cut4 and cut5:
                graphtest.Fill(j1r_E - j1t_E)
                graphtest.Fill(j2r_E - j2t_E)
                graphenergy.Fill(newmass)
                #graphenergy.Fill(j2r_E+deltaj2)
                #graphenergytruth.Fill(j1t_E)
                #graphenergytruth.Fill(j2t_E+j1t_E)
                graphenergytruth.Fill(mjjt)
                graphjs.Fill(j2s_E)
                graphjs.Fill(j1s_E)
                graphjc.Fill(j2c_E)
                graphjc.Fill(j1c_E)

        displayfile.cd()
        #graphtest.Write()
        scale = 1. / graphenergy.Integral()
        graphenergy.Scale(scale)
        graphenergy.Write()
        graphenergytruth.Write()
Esempio n. 23
0
                for strO in attr_out:
                    tempAttrStrO = 'pfc' + str(ii + 1) + '_' + strO
                    tempAttrStrI = forLola.attrNameDic[strO] + '_' + str(ii +
                                                                         1)
                    setattr(Jets1, tempAttrStrO, jet1[tempAttrStrI][i])
            #!!!!!!!!!!!!!!!!!!!!deal with the empty value situation!!!!!!!!!!!!!!!!!!!!!!
            newTree.Fill()

        #showTimeLeft(ii=i,mode='e',startTime=start,numOfJobs=numOfEntriesToScan_local)

    #set up parallel pool
    pool = mp.Pool(processes=80)
    pool.map(FillTrees, xrange(0, numOfEntriesToScan_local))

    print '\nproduced skimmed file', newFile.GetName(
    ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight()
    newFile.cd()
    newFile.Write()
    newFile.Close()
#-----------------------------------------------------------------------------------------------------------

#_____________________________________________old lines:
"""
    for j in xrange(num_of_jets):
        if 'QCD' in name:
            #oldTree.SetBranchAddress( 'Jets' , AddressOf(Jet_old_dict[j+1], 'pt') )
            #oldTree.SetBranchAddress( 'MatchedCHSJet' + str(j+1) , AddressOf(Jet_old_dict[j+1], 'pt') )
        elif 'ctauS' in name:
            #oldTree.SetBranchAddress( 'MatchedCHSJet' + str(j+1) , AddressOf(Jet_old_dict[j+1], 'pt') )
            #oldTree.SetBranchAddress( 'Jets' , AddressOf(Jet_old_dict[j+1], 'pt') )
"""
Esempio n. 24
0
def skim(name):

    oldFile = TFile(name, "READ")
    oldTree = oldFile.Get("ntuple/tree")
    oldTree.SetBranchAddress("Lepton1", AddressOf(Lepton1, "pt"))
    oldTree.SetBranchAddress("Lepton2", AddressOf(Lepton2, "pt"))
    oldTree.SetBranchAddress("FatJet1", AddressOf(FatJet1, "pt"))
    oldTree.SetBranchAddress("V", AddressOf(V, "pt"))
    oldTree.SetBranchAddress("X", AddressOf(X, "pt"))

    print 'skimming file', oldFile.GetName(), '\tevents =', oldTree.GetEntries(
    ), '\tweight =', oldTree.GetWeight()

    newFile = TFile("Skim/" + name, "RECREATE")
    newFile.cd()
    newTree = TTree("alpha", "alpha")

    EventNumberBranch = newTree.Branch('EventNumber', EventNumber,
                                       'EventNumber/F')
    EventWeightBranch = newTree.Branch('EventWeight', EventWeight,
                                       'EventWeight/F')
    RunNumberBranch = newTree.Branch('RunNumber', RunNumber, 'RunNumber/F')
    LumiNumberBranch = newTree.Branch('LumiNumber', LumiNumber, 'LumiNumber/F')
    isZtoEEBranch = newTree.Branch('isZtoEE', isZtoEE, 'isZtoEE/O')
    isZtoMMBranch = newTree.Branch('isZtoMM', isZtoMM, 'isZtoMM/O')
    isMCBranch = newTree.Branch('isMC', isMC, 'isMC/O')
    FatJet1_ptBranch = newTree.Branch('FatJet1_pt', FatJet1_pt, 'FatJet1_pt/F')
    FatJet1_softdropPuppiMassBranch = newTree.Branch(
        'FatJet1_softdropPuppiMass', FatJet1_softdropPuppiMass,
        'FatJet1_softdropPuppiMass/F')
    FatJet1_softdropPuppiMassCorrBranch = newTree.Branch(
        'FatJet1_softdropPuppiMassCorr', FatJet1_softdropPuppiMassCorr,
        'FatJet1_softdropPuppiMassCorr/F')
    FatJet1_puppiTau21Branch = newTree.Branch('FatJet1_puppiTau21',
                                              FatJet1_puppiTau21,
                                              'FatJet1_puppiTau21/F')
    FatJet1_ddtTau21Branch = newTree.Branch('FatJet1_ddtTau21',
                                            FatJet1_ddtTau21,
                                            'FatJet1_ddtTau21/F')
    V_massBranch = newTree.Branch('V_mass', V_mass, 'V_mass/F')
    V_ptBranch = newTree.Branch('V_pt', V_pt, 'V_pt/F')
    X_massBranch = newTree.Branch('X_mass', X_mass, 'X_mass/F')

    theweight = oldTree.GetWeight()

    for event in range(0, oldTree.GetEntries() - 1):
        oldTree.GetEntry(event)

        # Alpha selections

        # Channel
        if not oldTree.isZtoMM and not oldTree.isZtoEE: continue

        # Trigger
        if not oldTree.isMC:
            if oldTree.isZtoMM:
                #if not ( oldTree.HLT_TkMu50_v or oldTree.HLT_Mu50_v ): continue
                if not (oldTree.HLT_Mu45_eta2p1_v): continue
            elif oldTree.isZtoEE:
                if not (oldTree.HLT_Ele105_CaloIdVT_GsfTrkIdT_v
                        or oldTree.HLT_Ele115_CaloIdVT_GsfTrkIdT_v):
                    continue
            else:
                continue
        # Leptons
        if oldTree.isZtoMM and not (
            ((Lepton1.isHighPt and Lepton2.isHighPt) or
             (Lepton1.isTrackerHighPt and Lepton2.isHighPt) or
             (Lepton1.isHighPt and Lepton2.isTrackerHighPt))
                and Lepton1.pt > 55 and Lepton2.pt > 20
                and abs(Lepton1.eta) < 2.1 and abs(Lepton2.eta) < 2.1
                and not (Lepton1.pt > 500 and abs(Lepton1.eta) > 1.2)
                and not (Lepton2.pt > 500 and abs(Lepton2.eta) > 1.2)
                and Lepton1.trkIso < 0.1 and Lepton2.trkIso < 0.1):
            continue

        if oldTree.isZtoEE and not (Lepton1.pt > 135 and Lepton2.pt > 35
                                    and Lepton1.isLoose and Lepton2.isLoose):
            continue

        # Boost and Z
        if not (V.pt > 170 and FatJet1.pt > 170 and V.mass > 70
                and V.mass < 110):
            continue
        # Grooming
        if not (FatJet1.softdropPuppiMassCorr > 30): continue

        # Copy relevant variables
        EventNumber[0] = oldTree.EventNumber
        EventWeight[0] = oldTree.EventWeight * theweight
        RunNumber[0] = oldTree.RunNumber
        LumiNumber[0] = oldTree.LumiNumber
        isZtoEE[0] = oldTree.isZtoEE
        isZtoMM[0] = oldTree.isZtoMM
        isMC[0] = oldTree.isMC
        FatJet1_pt[0] = FatJet1.pt
        FatJet1_softdropPuppiMass[0] = FatJet1.softdropPuppiMass
        FatJet1_softdropPuppiMassCorr[0] = FatJet1.softdropPuppiMassCorr
        FatJet1_puppiTau21[0] = FatJet1.puppiTau21
        FatJet1_ddtTau21[0] = FatJet1.ddtTau21
        V_mass[0] = V.mass
        V_pt[0] = V.pt
        X_mass[0] = X.mass

        newTree.Fill()

    print 'produced skimmed file', newFile.GetName(
    ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight()

    newFile.cd()
    newTree.Write()
    newFile.Close()
    oldFile.Close()
Esempio n. 25
0
def makeCutFiles():

    from ROOT import TFile, TTree, MGTWaveform

    enrExc, natExc, enrRates, natRates = getOutliers(True)
    # enrExc, natExc: [:,0]=dsNum, [:,1]=cpd, [:,2]=bkgIdx
    # enrRates, natRates: [:,0]=rate1, [:,1]=rate2, [:,2]=expo, [:,3]=bkgIdx, [:,4]=cpd, [:,5]=ds

    cutType = "fr"
    outType = "frc"

    for ds in [0, 1, 2, 3, 4, "5A", "5B", "5C"]:
        # for ds in ["5B","5C"]:

        dsNum = int(ds[0]) if isinstance(ds, str) else ds
        nBkg = bkg.dsMap()[dsNum]
        bLo, bHi = 0, nBkg
        if ds == "5A": bLo, bHi = 0, 79
        if ds == "5B": bLo, bHi = 80, 112
        if ds == "5C": bLo, bHi = 113, 121
        runRanges = bkg.getRanges(dsNum)
        chList = det.getGoodChanList(dsNum)

        # clear out any files from a previous attempt
        fList = [
            "%s/bkg/cut/%s/%s_ds%d_%d_*.root" %
            (dsi.dataDir, outType, outType, dsNum, bIdx)
            for bIdx in range(bLo, bHi + 1)
        ]
        for f in fList:
            fTmp = glob.glob(f)
            for f in fTmp:
                os.remove(f)

        # build skip list
        dsTmp = ds
        if ds == "5A": dsTmp = 50
        if ds == "5B": dsTmp = 51
        if ds == "5C": dsTmp = 52
        iE = np.where(enrExc[:, 0] == dsTmp)
        iN = np.where(natExc[:, 0] == dsTmp)
        skipList = np.vstack((enrExc[iE], natExc[iN]))
        print("DS-%s, skipList:" % ds)
        print(skipList)

        for bIdx in range(bLo, bHi + 1):
            print("DS-%s  bIdx %d" % (ds, bIdx))

            # load the cut files
            for ch in sorted(chList):

                cpd = det.getChanCPD(dsNum, ch)

                if len(
                        np.where(
                            (skipList
                             == (dsTmp, int(cpd), bIdx)).all(axis=1))[0]) > 0:
                    # print("skipping det %s in bkgIdx %d" % (cpd, bIdx))
                    continue

                # skip nonexistent files.  other parts of chsel should tell us why these aren't here.
                fName = "%s/bkg/cut/%s/%s_ds%d_%d_ch%d.root" % (
                    dsi.dataDir, cutType, cutType, dsNum, bIdx, ch)
                if not os.path.isfile(fName):
                    # print("no file for det %s in bkgIdx %d" % (cpd, bIdx))
                    continue

                tf = TFile(fName)
                tt = tf.Get("skimTree")
                nEvt = tt.GetEntries()

                # TODO: zombie file check (need to know livetime)

                outName = "%s/bkg/cut/%s/%s_ds%d_%d_ch%d.root" % (
                    dsi.dataDir, outType, outType, dsNum, bIdx, ch)
                outFile = TFile(outName, "RECREATE")
                outTree = TTree()
                outTree = tt.CopyTree("")
                # print("Wrote %d entries." % outTree.GetEntries())

                if nEvt != outTree.GetEntries():
                    print(
                        "ERROR, number of entries don't match: input %d  output %d"
                        % (nEvt, outTree.GetEntries()))

                outTree.Write()
                outFile.Close()
                tf.Close()
def REGENERATE_TREE_WITH_CUT(filename, outputpath=''):

    FileNameList = read_file_name(filename)
    #    print(FileNameList)
    BranchListAll = get_branch_list_all(FileNameList[2])
    BranchListEachTree = get_branch_list_each_tree(FileNameList[2])
    #    print("@#!@#!@#",BranchListEachTree)
    #    print("@#!@#!@#",BranchListAll)

    DicNumpyArray_branch = {}
    for numpyarray in BranchListAll:
        a = numpy.array([0], 'd')
        DicNumpyArray_branch[numpyarray] = a
    DicNumpyArray_branch = collections.OrderedDict(
        sorted(DicNumpyArray_branch.items())
    )  #  !!!the input times are ordered!!!
    #    print(DicNumpyArray_branch)

    DicNumpyArray_branch_w = {}
    for numpyarray_w in BranchListAll:
        a_w = numpy.array([0], 'd')
        DicNumpyArray_branch_w[numpyarray_w] = a_w
    DicNumpyArray_branch_w = collections.OrderedDict(
        sorted(DicNumpyArray_branch_w.items()))
    #    print(DicNumpyArray_branch_w)

    #    print(DicNumpyArray_branch.keys())
    #    print(DicNumpyArray_branch.values())

    WCuts = WhetherAddCut(BranchListEachTree)
    if (WCuts == None):
        print("\n")
        print(
            "*********************************************************************************************"
        )
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print("\n")
        #        for i in range(len(BranchListEachTree)):
        #            print("compenets below :")
        #            print("The Tree name is : ",BranchListEachTree.keys()[i])
        #            for j in range(len(BranchListEachTree.values()[i])):
        #                print("        it contains : ", BranchListEachTree.values()[i][j])
        #            print("\n")
        print("!!!! Please check input CUT !!!!! ")
        print("     !!!! No Output File !!!!    ")
        print(
            "*********************************************************************************************"
        )
    else:
        print("\n")
        print("...Cut added and regenerating tree root file...")
        gBenchmark.Start("Regerating tree root")

        f = TFile(FileNameList[2], "READ")
        dirlist = f.GetListOfKeys()
        ITER = dirlist.MakeIterator()
        key = ITER.Next()

        if (outputpath == ''):
            outfileName = FileNameList[3] + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)
        elif (outputpath[0] == "/"):
            outfileName = outputpath + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)
        elif (outputpath[0] == "~"):
            outfileName = outputpath.replace(
                "~", os.environ['HOME']) + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)
        else:
            outfileName = os.getcwd(
            ) + "/" + outputpath + "/" + FileNameList[0] + "_cut.root"
            outfileName = outfileName.replace("//", "/")
#            print("!@#!@!@#!@ ",outfileName)

        outfile = TFile(outfileName, "RECREATE")

        ijk = 0
        while key:
            tree = key.ReadObj()
            tree_f = TTree(tree.GetName() + "_f", tree.GetName() + "_f")
            ENTRY = tree.GetEntries()
            #print(ENTRY)
            for i in range(len(DicNumpyArray_branch)):
                if (list(DicNumpyArray_branch.keys())[i]
                        in BranchListEachTree[tree.GetName()]):
                    tree.SetBranchAddress(
                        list(DicNumpyArray_branch.keys())[i],
                        list(DicNumpyArray_branch.values())[i])
                    tree_f.Branch(
                        list(DicNumpyArray_branch_w.keys())[i],
                        list(DicNumpyArray_branch_w.values())[i],
                        list(DicNumpyArray_branch_w.keys())[i] + "/D")
                else:
                    continue

            print("for tree", tree.GetName())
            #            tt = 0
            for j in range(ENTRY):
                tree.GetEntry(j)
                if (j % 5000 == 0):
                    print("now looping", j, "th Events, total of ", ENTRY,
                          "events")
                for k in range(len(DicNumpyArray_branch)):
                    if (list(DicNumpyArray_branch.keys())[k]
                            in BranchListEachTree[tree.GetName()]
                        ):  ### FIXED MAYBE not correct....
                        pass
                    else:
                        continue
                    list(DicNumpyArray_branch_w.values())[k][0] = list(
                        DicNumpyArray_branch.values())[k][0]


#                    if(j==0):
#                        print(k,DicNumpyArray_branch_w.keys()[k])
#                        print(DicNumpyArray_branch_w.keys()[k])

#                tt = raw_input("press 'Y' to proceed, 'enter' to quit!")  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME

                if (True  #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME

                        #                   & (DicNumpyArray_branch.values()[0][0] > 0)          # [i][0]  means "i+1"th branch of each tree, [0] don't change   #FIXME#FIXME#FIXME#FIXME
                        #&  (DicNumpyArray_branch.values()[1][0] > 0)        #FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME#FIXME
                    ):
                    ijk = ijk + 1
                    tree_f.Fill()
                else:
                    continue
            print(" Event left after filtering : ", ijk, "!!!!!!!!!")
            print("\n")
            ijk = 0
            if (tree_f.GetEntries() == 0):
                print("!!!!!!! ", tree_f.GetName(),
                      " is Empty, would not be written !!!!!")
            else:
                tree_f.Write()

            key = ITER.Next()

        print("")
        print("////////////////////////////////////////////////")
        print("outputfile : ")
        print(outfileName)
        print("////////////////////////////////////////////////")
        print("")

        outfile.Close()
        f.Close()
        print(
            "*********************************************************************************************"
        )
        gBenchmark.Show("Regerating tree root")
        print(
            "*********************************************************************************************"
        )

        return outfileName
Esempio n. 27
0
def skim(name):

    oldFile = TFile(name, "READ")
    oldTree = oldFile.Get("ntuple/tree")
    oldTree.SetBranchAddress("Lepton1", AddressOf(Lepton1, "pt"))
    oldTree.SetBranchAddress("Lepton2", AddressOf(Lepton2, "pt"))
    oldTree.SetBranchAddress("FatJet1", AddressOf(FatJet1, "pt"))
    oldTree.SetBranchAddress("V", AddressOf(V, "pt"))
    oldTree.SetBranchAddress("X", AddressOf(X, "pt"))
    oldTree.SetBranchAddress("MEt", AddressOf(MEt, "pt"))

    print 'skimming file', oldFile.GetName(), '\tevents =', oldTree.GetEntries(
    ), '\tweight =', oldTree.GetWeight()

    newFile = TFile("Skim/" + name, "RECREATE")
    newFile.cd()
    newTree = TTree("alpha", "alpha")

    EventNumberBranch = newTree.Branch('EventNumber', EventNumber,
                                       'EventNumber/F')
    EventWeightBranch = newTree.Branch('EventWeight', EventWeight,
                                       'EventWeight/F')
    RunNumberBranch = newTree.Branch('RunNumber', RunNumber, 'RunNumber/F')
    LumiNumberBranch = newTree.Branch('LumiNumber', LumiNumber, 'LumiNumber/F')
    isZtoEEBranch = newTree.Branch('isZtoEE', isZtoEE, 'isZtoEE/O')
    isZtoMMBranch = newTree.Branch('isZtoMM', isZtoMM, 'isZtoMM/O')
    isZtoNNBranch = newTree.Branch('isZtoNN', isZtoNN, 'isZtoNN/O')
    isMCBranch = newTree.Branch('isMC', isMC, 'isMC/O')
    FatJet1_ptBranch = newTree.Branch('FatJet1_pt', FatJet1_pt, 'FatJet1_pt/F')
    FatJet1_softdropPuppiMassBranch = newTree.Branch(
        'FatJet1_softdropPuppiMass', FatJet1_softdropPuppiMass,
        'FatJet1_softdropPuppiMass/F')
    FatJet1_softdropPuppiMassCorrBranch = newTree.Branch(
        'FatJet1_softdropPuppiMassCorr', FatJet1_softdropPuppiMassCorr,
        'FatJet1_softdropPuppiMassCorr/F')
    FatJet1_softdropPuppiMassCorrNotSmearedBranch = newTree.Branch(
        'FatJet1_softdropPuppiMassCorrNotSmeared',
        FatJet1_softdropPuppiMassCorrNotSmeared,
        'FatJet1_softdropPuppiMassCorrNotSmeared/F')
    FatJet1_puppiTau21Branch = newTree.Branch('FatJet1_puppiTau21',
                                              FatJet1_puppiTau21,
                                              'FatJet1_puppiTau21/F')
    FatJet1_ddtTau21Branch = newTree.Branch('FatJet1_ddtTau21',
                                            FatJet1_ddtTau21,
                                            'FatJet1_ddtTau21/F')
    FatJet1_CSVBranch = newTree.Branch('FatJet1_CSV', FatJet1_CSV,
                                       'FatJet1_CSV/F')
    FatJet1_CSVRBranch = newTree.Branch('FatJet1_CSVR', FatJet1_CSVR,
                                        'FatJet1_CSVR/F')
    FatJet1_CSV1Branch = newTree.Branch('FatJet1_CSV1', FatJet1_CSV1,
                                        'FatJet1_CSV1/F')
    FatJet1_CSV2Branch = newTree.Branch('FatJet1_CSV2', FatJet1_CSV2,
                                        'FatJet1_CSV2/F')
    FatJet1_CSVR1Branch = newTree.Branch('FatJet1_CSVR1', FatJet1_CSVR1,
                                         'FatJet1_CSVR1/F')
    FatJet1_CSVR2Branch = newTree.Branch('FatJet1_CSVR2', FatJet1_CSVR2,
                                         'FatJet1_CSVR2/F')
    V_massBranch = newTree.Branch('V_mass', V_mass, 'V_mass/F')
    V_ptBranch = newTree.Branch('V_pt', V_pt, 'V_pt/F')
    MEt_ptBranch = newTree.Branch('MEt_pt', MEt_pt, 'MEt_pt/F')
    X_massBranch = newTree.Branch('X_mass', X_mass, 'X_mass/F')
    X_tmassBranch = newTree.Branch('X_tmass', X_tmass, 'X_tmass/F')

    theweight = oldTree.GetWeight()

    for event in range(0, oldTree.GetEntries() - 1):
        oldTree.GetEntry(event)

        # Alpha selections

        # Channel
        if not oldTree.isZtoMM and not oldTree.isZtoEE and not oldTree.isZtoNN:
            continue

        # Trigger
        if not oldTree.isMC:
            if oldTree.isZtoMM:
                #if not ( oldTree.HLT_TkMu50_v or oldTree.HLT_Mu50_v ): continue
                if not (oldTree.HLT_Mu45_eta2p1_v): continue
            elif oldTree.isZtoEE:
                if not (oldTree.HLT_Ele105_CaloIdVT_GsfTrkIdT_v
                        or oldTree.HLT_Ele115_CaloIdVT_GsfTrkIdT_v):
                    continue
            elif oldTree.isZtoNN:
                if not (oldTree.HLT_PFMETNoMu90_PFMHTNoMu90_IDTight_v
                        or oldTree.HLT_PFMETNoMu110_PFMHTNoMu110_IDTight_v
                        or oldTree.HLT_PFMETNoMu120_PFMHTNoMu120_IDTight_v
                        or oldTree.HLT_PFMET170_NoiseCleaned_v
                        or oldTree.HLT_PFMET170_JetIdCleaned_v
                        or oldTree.HLT_PFMET170_HBHECleaned_v):
                    continue
            else:
                continue
        # Leptons
        if oldTree.isZtoMM and not (
            ((Lepton1.isHighPt and Lepton2.isHighPt) or
             (Lepton1.isTrackerHighPt and Lepton2.isHighPt) or
             (Lepton1.isHighPt and Lepton2.isTrackerHighPt))
                and Lepton1.pt > 55 and Lepton2.pt > 20
                and abs(Lepton1.eta) < 2.1 and abs(Lepton2.eta) < 2.1
                and not (Lepton1.pt > 500 and abs(Lepton1.eta) > 1.2)
                and not (Lepton2.pt > 500 and abs(Lepton2.eta) > 1.2)
                and Lepton1.trkIso < 0.1 and Lepton2.trkIso < 0.1):
            continue

        if oldTree.isZtoEE and not (Lepton1.pt > 135 and Lepton2.pt > 35
                                    and Lepton1.isLoose and Lepton2.isLoose):
            continue
        # No Leptons
        if oldTree.isZtoNN and not (
                MEt.pt > 200 and oldTree.nLooseMuons == 0
                and oldTree.nVetoElectrons == 0 and oldTree.nPhotons == 0
                and oldTree.nTaus == 0 and oldTree.MinJetMetDPhi > 0.5
                and oldTree.Flag_EcalDeadCellTriggerPrimitiveFilter
                and oldTree.Flag_HBHENoiseFilter
                and oldTree.Flag_HBHENoiseIsoFilter
                and oldTree.Flag_globalTightHalo2016Filter
                and oldTree.Flag_goodVertices and oldTree.Flag_BadPFMuon
                and oldTree.Flag_BadChCand):
            continue
        if not oldTree.isMC:
            if oldTree.isZtoNN and not oldTree.Flag_eeBadScFilter: continue
        # Boost and Z
        if (oldTree.isZtoEE
                or oldTree.isZtoMM) and not (V.pt > 170 and FatJet1.pt > 170
                                             and V.mass > 70 and V.mass < 110):
            continue
        # Boost and Cleaning for Z invisible
        if oldTree.isZtoNN and not (FatJet1.pt > 200 and FatJet1.isTight
                                    and FatJet1.nhf < 0.8 and FatJet1.chf > 0.2
                                    and oldTree.MaxJetBTag < 0.5426
                                    and X.dPhi > 2):
            continue
        # Grooming
        if not (FatJet1.softdropPuppiMassCorr > 30): continue

        # Copy relevant variables
        EventNumber[0] = oldTree.EventNumber
        EventWeight[0] = oldTree.EventWeight * theweight
        RunNumber[0] = oldTree.RunNumber
        LumiNumber[0] = oldTree.LumiNumber
        isZtoEE[0] = oldTree.isZtoEE
        isZtoMM[0] = oldTree.isZtoMM
        isZtoNN[0] = oldTree.isZtoNN
        isMC[0] = oldTree.isMC
        FatJet1_pt[0] = FatJet1.pt
        FatJet1_softdropPuppiMass[0] = FatJet1.softdropPuppiMass
        FatJet1_softdropPuppiMassCorr[0] = FatJet1.softdropPuppiMassCorr
        FatJet1_softdropPuppiMassCorrNotSmeared[
            0] = FatJet1.softdropPuppiMassCorrNotSmeared
        FatJet1_puppiTau21[0] = FatJet1.puppiTau21
        FatJet1_ddtTau21[0] = FatJet1.ddtTau21
        FatJet1_CSV[0] = FatJet1.CSV
        FatJet1_CSVR[0] = FatJet1.CSVR
        FatJet1_CSV1[0] = FatJet1.CSV1
        FatJet1_CSV2[0] = FatJet1.CSV2
        FatJet1_CSVR1[0] = FatJet1.CSVR1
        FatJet1_CSVR2[0] = FatJet1.CSVR2
        V_mass[0] = V.mass
        V_pt[0] = V.pt
        MEt_pt[0] = MEt.pt
        X_mass[0] = X.mass
        X_tmass[0] = X.tmass

        newTree.Fill()

    print 'produced skimmed file', newFile.GetName(
    ), '\tevents =', newTree.GetEntries(), '\tweight =', newTree.GetWeight()

    newFile.cd()
    newTree.Write()
    newFile.Close()
    oldFile.Close()
Esempio n. 28
0
File: lat3.py Progetto: gothman5/LAT
def ApplyChannelCuts(dsNum, cutType, dType):
    """ ./lat3.py -cut [dsNum] [cutType]
    This runs over whole datasets.
    cutTypes:
        fs, rn, wf, fs+rn, fs+wf, rn+wf, fs+rn+wf

    dTypes:
        bkg, cal
    """
    # load the database
    calDB = db.TinyDB('../calDB.json')
    pars = db.Query()

    # setup a loop over modules and dataset ranges
    gROOT.ProcessLine("gErrorIgnoreLevel = 3001;")
    cInfo = ds.CalInfo()
    nMods = [1]
    if dsNum == 4: nMods = [2]
    if dsNum == 5: nMods = [1,2]
    # Dummy string for file writing -- adds nothing to the directories if background
    dString = ""
    if dType == "cal": dString = "cal"

    for modNum in nMods:
        # Changed so range of idx are set here to take advantage of module number
        nRanges = []
        if dType == "bkg":
            nRanges = [0, ds.dsMap[dsNum]]
            # if dsNum==5: nRanges[0] = 80 # exclude DS-5A
        elif dType == "cal":
            nRanges = [0, len(cInfo.master['ds%d_m%d'%(dsNum, modNum)])-1]
        else:
            print "cal or bkg not set, returning"
            return 0

        # Loop over bkgIdx, even though for calibration runs this will represent calIdx
        for bkgIdx in range(nRanges[0], nRanges[1]+1):
            # load the chains and find the right calIdx's.
            skimTree = TChain("skimTree")

            # build the file list
            fRegex = ""
            fList = []
            if dType == "bkg":
                fRegex = "/global/homes/w/wisecg/project/bg-lat/latSkimDS%d_%d_*.root" % (dsNum, bkgIdx)
                fList = glob.glob(fRegex)
                skimTree.Add(fRegex)
            elif dType == "cal":
                calList = cInfo.GetCalList("ds%d_m%d" % (dsNum, modNum), bkgIdx, runLimit=10)
                for i in calList:
                    fList += glob.glob("/global/homes/w/wisecg/project/cal-lat/latSkimDS%d_run%d_*.root"%(dsNum,i))
                    skimTree.Add("/global/homes/w/wisecg/project/cal-lat/latSkimDS%d_run%d_*.root" % (dsNum, i))
            file0 = fList[0]
            print "DS-%d subset %d, Mod-%d.  N_files: %d" % (dsNum, bkgIdx, modNum, len(fList))

            # Print some basic info about files
            f = TFile(file0)
            firstRun, lastRun, calIdxLo, calIdxHi = 0,0,0,0
            theCut = f.Get("theCut").GetTitle()
            if dType == "bkg":
                skimTree.GetEntry(0)
                firstRun = skimTree.run
                skimTree.GetEntry(skimTree.GetEntries()-1)
                lastRun = skimTree.run
                calIdxLo = cInfo.GetCalIdx("ds%d_m%d" % (dsNum, modNum), firstRun)
                calIdxHi = cInfo.GetCalIdx("ds%d_m%d" % (dsNum, modNum), lastRun)
            elif dType == "cal":
                # All the idx are the same for calibration!
                calIdxLo = calIdxHi = bkgIdx
                firstRun, lastRun = calList[0], calList[-1]

            print "    Entries %d  firstRun %d  lastRun %d  calIdxLo %d  calIdxHi %d" % (skimTree.GetEntries(),firstRun,lastRun,calIdxLo,calIdxHi)

            # build the channel list  (remove 692 and 1232 from DS5 for now.)
            chList = ds.GetGoodChanList(dsNum)
            if dsNum==5 and modNum==1:
                chList = [ch for ch in chList if ch < 1000 and ch!=692]
            if dsNum==5 and modNum==2:
                chList = [ch for ch in chList if ch > 1000 and ch!=1232]

            # -- create a dict of cuts for each channel, covering each calIdx. --
            cutDict = {}
            for calIdx in range(calIdxLo, calIdxHi+1):

                runCovMin = cInfo.master["ds%d_m%d" % (dsNum, modNum)][calIdx][1]
                runCovMax = cInfo.master["ds%d_m%d" % (dsNum, modNum)][calIdx][2]
                runCut = "run>=%d && run<=%d" % (runCovMin, runCovMax)

                fsD = ds.getDBRecord("fitSlo_ds%d_idx%d_m%d_Peak" % (dsNum, calIdx, modNum), False, calDB, pars)
                rnSD = ds.getDBRecord("riseNoise_ds%d_idx%d_m%d_SoftPlus" % (dsNum, calIdx, modNum), False, calDB, pars)
                rnCD = ds.getDBRecord("riseNoise_ds%d_idx%d_m%d_Continuum" % (dsNum, calIdx, modNum), False, calDB, pars)
                wfD = ds.getDBRecord("wfstd_ds%d_idx%d_mod%d" % (dsNum, calIdx, modNum), False, calDB, pars)

                for ch in chList:

                    fsCut, rnCut, wfCut, chanCut = None, None, None, None

                    # print ch,":",fsD[ch][2]

                    # fitSlo: check the 90% value is positive
                    if fsD[ch][2] > 0:
                        fsCut = "fitSlo<%.2f" % fsD[ch][2]

                    # riseNoise: check the softplus curvature is positive
                    if rnSD[ch][3] > 0:
                        rnCut = "riseNoise<(%.3f+%.5f*TMath::Log(1+TMath::Exp((trapENFCal-(%.3f))/%.3f)))" % (max(rnSD[ch][0],rnCD[ch][4]), rnSD[ch][1], rnSD[ch][2], rnSD[ch][3])

                    # wfStd: check if ralph says this is ok to use
                    if wfD!=0 and ch in wfD.keys() and wfD[ch][0]==u'y':
                        wfCut = "abs(wfstd - sqrt((%.4e + %.4e*trapENFCal + %.4e*trapENFCal**2 + %.2e*pow(trapENFCal,3) + %.2e*pow(trapENFCal,4))**2 + %.4f)) < (%.2f+%.3f*trapENFCal)" % (wfD[ch][3], wfD[ch][4], wfD[ch][5], wfD[ch][6], wfD[ch][7], wfD[ch][8], wfD[ch][9], wfD[ch][10])

                    # set the combination channel cut
                    if cutType == "fs" and fsCut!=None:
                        chanCut = "(%s && %s)" % (runCut, fsCut)

                    if cutType == "rn" and rnCut!=None:
                        chanCut = "(%s && %s)" % (runCut, rnCut)

                    if cutType == "wf" and wfCut!=None:
                        chanCut = "(%s && %s)" % (runCut, wfCut)

                    if cutType == "fs+rn" and fsCut!=None and rnCut!=None:
                        chanCut = "(%s && %s && %s)" % (runCut, fsCut, rnCut)

                    if cutType == "fs+wf" and fsCut!=None and wfCut!=None:
                        chanCut = "(%s && %s && %s)" % (runCut, fsCut, wfCut)

                    if cutType == "rn+wf" and rnCut!=None and wfCut!=None:
                        chanCut = "(%s && %s && %s)" % (runCut, rnCut, wfCut)

                    if cutType == "fs+rn+wf" and fsCut!=None and rnCut!=None and wfCut!=None:
                        chanCut = "(%s && %s && %s && %s)" % (runCut, fsCut, rnCut, wfCut)

                    # create dict entry for this channel or append to existing, taking care of parentheses and OR's.
                    if ch in cutDict.keys() and chanCut!=None:
                        cutDict[ch] += " || %s" % chanCut
                    elif ch not in cutDict.keys() and chanCut!=None:
                        cutDict[ch] = "(%s" % chanCut

            # close the parens for each channel entry
            for key in cutDict:
                cutDict[key] += ")"

            # -- finally, loop over each channel we have an entry for, get its cut, and create an output file. --
            for ch in cutDict:
                # TODO: threshold cut (or at least save the value for each bkgIdx)

                chanCut = theCut + "&& gain==0 && channel==%d" % ch

                if cutType == "fs":
                    outFile = "~/project/cuts/%sfs/%sfitSlo-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                if cutType == "rn":
                    outFile = "~/project/cuts/%srn/%sriseNoise-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& %s" % cutDict[ch]

                if cutType == "wf":
                    outFile = "~/project/cuts/%swf/%swfstd-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& %s" % cutDict[ch]

                if cutType == "fs+rn":
                    outFile = "~/project/cuts/%sfs_rn/%sfs_rn-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                if cutType == "fs+wf":
                    outFile = "~/project/cuts/%sfs_wf/%sfs_wf-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                if cutType == "rn+wf":
                    outFile = "~/project/cuts/%srn_wf/%srn_wf-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& %s" % cutDict[ch]

                if cutType == "fs+rn+wf":
                    outFile = "~/project/cuts/%sfs_rn_wf/%sfs_rn_wf-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                print "    Writing to:",outFile
                print "    Cut used:",chanCut,"\n"
                outFile = TFile(outFile,"RECREATE")
                outTree = TTree()
                outTree = skimTree.CopyTree(chanCut)
                outTree.Write()
                cutUsed = TNamed("chanCut",chanCut)
                cutUsed.Write()
                print "Wrote",outTree.GetEntries(),"entries."
                outFile.Close()
Esempio n. 29
0
            if tob != -1:
                cells = createCellLists(tob)
                
                tob_event = event_from_tob(my_tree, tob)
                run3_et[0] = tob_event.reco_et
            else:
                run3_et[0] = -1
            
            true_pt[0] = truePts[tob_num] / 1000.
            true_eta[0] = trueEta[tob_num]
            reco_pt[0] = recoPts[tob_num] / 1000. if recoPts != -1 else -1
            reco_eta[0] = recoEta[tob_num]

            t_out.Fill()

            if t_out.GetEntries() % 1000 == 0:
                print 'Entries filled: ',t_out.GetEntries()
            
            continue

        # For background, fill only highest-Et in event and no eta cut because we care about overall rate
        else:
            tob_event = event_from_tob(my_tree, tob)

            # Only consider those that pass Run3 seed cut
            if not isCentralTowerSeed(tob_event):
                continue

            if tob_event.reco_et < 12:
                continue
Esempio n. 30
0
def main(argv):
    """ Interactive-fit or 'rapid'-fit waveforms that pass a given TCut.
        BUG: Doesn't always work with a TChain.  Add input files together
        with hadd and use a single TFile.
    """
    scanSpeed = 0.2
    iList = -1
    opt1 = ""
    intMode, batMode = False, False
    if (len(argv) >= 1): opt1 = argv[0]
    if "-i" in (opt1):
        intMode = True
        print "Interactive mode selected."
    if "-b" in (opt1):
        batMode = True
        print "Batch mode selected.  A new file will be created."

    # Load template waveform (created by gen-template.py)
    npzfile = np.load("./data/genTemplateWF.npz")
    temp, tempTS, tempE, tempST = npzfile['arr_0'] + 1, npzfile[
        'arr_1'], npzfile['arr_2'], npzfile['arr_3'] * 10

    # Set cuts
    # theCut = inputFile.Get("cutUsedHere").GetTitle()
    # DS3 "big DC" cut - PRELIMINARY
    theCut = "trapENFCal > 0.8 && gain==0 && mHClean==1 && isGood && !muVeto && !wfDCBits && !isLNFill1 && !isLNFill2 && trapETailMin < 0 && channel!=596 && channel!=676 && channel!=676 && channel!=612 && channel!=1104 && channel!=1200 && channel!=1334 && channel!=1336 && tOffset < 10 && waveS5/TMath::Power(trapENFCal,1/4) < 1200 && (waveS3-waveS2)/trapENFCal > 100 && (waveS3-waveS2)/trapENFCal < 300 && !(channel==692 && (run==16974 || run==16975 || run==16976 || run==16977 || run==16978 || run==16979)) && butterTime < 11000"
    # theCut += " && trapENFCal > 1.5 && trapENFCal < 2.1"
    # theCut += " && trapENFCal < 20 && trapENFCal > 2 && run > 17480"
    # theCut += " && kvorrT/trapENFCal > 2.2 && trapENFCal > 2 && trapENFCal < 10"

    # Set file I/O and create entry lists
    startT = time.clock()
    inFile = "~/project/wavelet-skim/waveletSkimDS3_1.root"
    # inFile = "~/project/wavelet-skim/hadd/waveletSkimDS3.root"
    outFile = "~/project/fit-skim/fitSkimDS3_1.root"
    inputFile = TFile(inFile)
    waveTree = inputFile.Get("skimTree")
    print "Found", waveTree.GetEntries(
    ), "input entries.  Using cut:\n", theCut, "\n"
    waveTree.Draw(">>elist", theCut, "entrylist")
    elist = gDirectory.Get("elist")
    waveTree.SetEntryList(elist)
    nList = elist.GetN()
    print "Found", nList, "entries passing cuts."
    stopT = time.clock()
    print "Data loading time (s): ", (stopT - startT)

    # In batch mode ONLY, create an output file+tree & append new branches
    outputFile = TFile()
    outTree = TTree()
    if batMode:
        outputFile = TFile(outFile, "RECREATE")
        print "Attempting tree copy to", outFile
        outTree = waveTree.CopyTree("")
        outTree.Write()
        print "Wrote", outTree.GetEntries(), "entries."
        cutUsed = TNamed("cutUsedHere", theCut)
        cutUsed.Write()
    fitStart = std.vector("double")()
    fitE = std.vector("double")()
    fitSlo = std.vector("double")()
    fitStartSD = std.vector("double")()
    fitESD = std.vector("double")()
    fitSloSD = std.vector("double")()
    fitChi2NDF = std.vector("double")()
    fitLnLike = std.vector("double")()
    tExp1 = std.vector("double")()
    tExp2 = std.vector("double")()
    tPol0 = std.vector("double")()
    tPol1 = std.vector("double")()
    tPol2 = std.vector("double")()
    tPol3 = std.vector("double")()
    baseAvg = std.vector("double")()
    baseNoise = std.vector("double")()
    bFitStart = outTree.Branch("fitStart", fitStart)
    bFitE = outTree.Branch("fitE", fitE)
    bFitSlo = outTree.Branch("fitSlo", fitSlo)
    bFitStart_sd = outTree.Branch("fitStartSD", fitStartSD)
    bFitE_sd = outTree.Branch("fitESD", fitESD)
    bFitSlo_sd = outTree.Branch("fitSloSD", fitSloSD)
    bFitChi2NDF = outTree.Branch("fitChi2NDF", fitChi2NDF)
    bFitLnLike = outTree.Branch("fitLnLike", fitLnLike)
    bTExp1 = outTree.Branch("tExp1", tExp1)
    bTExp2 = outTree.Branch("tExp2", tExp2)
    bTPol0 = outTree.Branch("tPol0", tPol0)
    bTPol1 = outTree.Branch("tPol1", tPol1)
    bTPol2 = outTree.Branch("tPol2", tPol2)
    bTPol3 = outTree.Branch("tPol3", tPol3)
    bBaseAvg = outTree.Branch("baseAvg", baseAvg)
    bBaseNoise = outTree.Branch("baseNoise", baseNoise)

    # Make a figure
    # with PdfPages('multipage_pdf.pdf') as pdf:
    fig = plt.figure(figsize=(11, 7), facecolor='w')
    p1 = plt.subplot2grid((6, 7), (0, 0), colspan=4, rowspan=2)  # original
    p2 = plt.subplot2grid((6, 7), (2, 0), colspan=4, rowspan=3)  # rising edge
    p3 = plt.subplot2grid((6, 7), (5, 0), colspan=4, rowspan=1)  # residual
    p4 = plt.subplot2grid((6, 7), (0, 4), colspan=3, rowspan=2)  # trace 1
    p5 = plt.subplot2grid((6, 7), (2, 4), colspan=3, rowspan=2,
                          sharex=p4)  # trace 2
    p6 = plt.subplot2grid((6, 7), (4, 4), colspan=3, rowspan=2,
                          sharex=p4)  # trace 3
    if not batMode: plt.show(block=False)

    # Setup multiprocessing
    manager = Manager()
    returnDict = manager.dict()

    # Loop over events
    while (True):
        saveMe = False
        iList += 1
        if intMode == True and iList != 0:
            value = raw_input()
            if value == 'q': break
            if value == 's': saveMe = True
            if value == 'p': iList -= 2  # previous
            if (value.isdigit()): iList = int(value)  # go to entry
        if iList >= elist.GetN(): break

        entry = waveTree.GetEntryNumber(iList)
        waveTree.LoadTree(entry)
        waveTree.GetEntry(entry)
        nChans = waveTree.channel.size()
        numPass = waveTree.Draw("channel", theCut, "GOFF", 1, iList)
        chans = waveTree.GetV1()
        chanList = list(set(int(chans[n]) for n in xrange(numPass)))

        fitStart.assign(nChans, -9999)
        fitE.assign(nChans, -9999)
        fitSlo.assign(nChans, -9999)
        fitStartSD.assign(nChans, -9999)
        fitESD.assign(nChans, -9999)
        fitSloSD.assign(nChans, -9999)
        fitChi2NDF.assign(nChans, -9999)
        fitLnLike.assign(nChans, -9999)
        tExp1.assign(nChans, -9999)
        tExp2.assign(nChans, -9999)
        tPol0.assign(nChans, -9999)
        tPol1.assign(nChans, -9999)
        tPol2.assign(nChans, -9999)
        tPol3.assign(nChans, -9999)
        baseAvg.assign(nChans, -9999)
        baseNoise.assign(nChans, -9999)

        # Loop over hits passing cuts
        hitList = (iH for iH in xrange(nChans)
                   if waveTree.channel.at(iH) in chanList
                   )  # a 'generator expression'
        for iH in hitList:

            # Load waveform for this hit
            run = waveTree.run
            chan = waveTree.channel.at(iH)
            dataE = waveTree.trapENFCal.at(iH)
            dataST = waveTree.butterTime.at(iH)  # replace with blrwfFMR50?
            toe = waveTree.kvorrT.at(iH) / dataE
            print "%d / %d  Run %d  nCh %d  chan %d  trapENF %.1f  t/e %.1f" % (
                iList, nList, run, nChans, chan, dataE, toe)
            signal = wl.processWaveform(waveTree.MGTWaveforms.at(iH),
                                        opt='full')
            waveBLSub = signal.GetWaveBLSub()
            waveFilt = signal.GetWaveFilt()
            waveTS = signal.GetTS()
            dataBaseline, dataNoise = signal.GetBaseNoise()

            # Denoise the data waveform (take only lowest-frequency components)
            wp = pywt.WaveletPacket(data=waveBLSub,
                                    wavelet='haar',
                                    mode='symmetric',
                                    maxlevel=3)
            new_wp = pywt.WaveletPacket(data=None,
                                        wavelet='haar',
                                        mode='symmetric')
            new_wp['aaa'] = wp['aaa'].data
            waveDenoised = new_wp.reconstruct(update=False)

            # Window the fit around rising edge - start time calculator method
            loWin, hiWin = dataST - 1000, dataST + 4000  # ns
            if loWin < waveTS[0] or hiWin > waveTS[-1]:
                print "Window out of range!  dataST: %.1f  loWin %.1f  hiWin %.1f" % (
                    dataST, loWin, hiWin)
            idx = np.where((waveTS >= loWin) & (waveTS <= hiWin))
            data = waveBLSub[idx]
            # data = waveDenoised[idx]
            dataTS = waveTS[idx]

            # Pack into lists
            rawList = [waveBLSub, waveTS, dataE, dataST]
            dataList = [data, dataTS, dataE, dataST, loWin, hiWin]
            tempList = [temp, tempTS, tempE, tempST]

            # Optionally save something to a file
            if saveMe:
                print "Saved entry", iList, iH
                np.savez("./data/tailSlopeInputs.npz", rawList, tempList)

            # Recreate the guess and the guess's rising edge
            guessFull, guessFullTS = wm.MakeModel(dataList,
                                                  tempList,
                                                  [dataST, dataE, 1.],
                                                  opt="full")
            guess, guessTS = wm.MakeModel(dataList,
                                          tempList, [dataST, dataE, 1.],
                                          opt="!fancy")

            # Make an "almost complete" guess - no MCMC
            # st, en, slo = dataST-100, dataE, 5
            # InterpFn = interpolate.interp1d(tempTS, temp, kind="linear", copy="False", assume_sorted="True")
            # model, modelTS = wm.MakeModel(dataList, tempList, [st,en,slo], fn=InterpFn)

            # Fit with MCMC and get best-fit parameters
            numSteps, burnIn = 3000, 1800  # default: 10000, 5000.  fast: 3000, 1800  long test: 20000,10000
            wfModel = wm.TemplateModel(dataList, dataNoise, tempList)
            p = Process(target=RunMCMC,
                        args=(wfModel, numSteps, burnIn, returnDict))
            p.start()
            p.join()
            startTimeTr = returnDict["startTimeTr"]
            energyTr = returnDict["energyTr"]
            slownessTr = returnDict["slownessTr"]
            st = np.median(startTimeTr[burnIn:])
            en = np.median(energyTr[burnIn:])
            slo = np.median(slownessTr[burnIn:])
            InterpFn = interpolate.interp1d(tempTS,
                                            temp,
                                            kind="linear",
                                            copy="False",
                                            assume_sorted="True")
            model, modelTS = wm.MakeModel(dataList,
                                          tempList, [st, en, slo],
                                          fn=InterpFn)

            # Save some extra parameters for the ROOT output
            # Calculate residual, Chi2/NDF, likelihood, etc.
            st_std = np.std(startTimeTr[burnIn:])
            en_std = np.std(energyTr[burnIn:])
            slo_std = np.std(slownessTr[burnIn:])
            residual = model - data
            frac = (np.power(data - model, 2)) / np.abs(model)
            chi2NDF = np.sum(frac) / len(model)
            inv_sigma2 = 1.0 / (dataNoise**2)
            lnLike = -0.5 * (np.sum((data - model)**2 * inv_sigma2 -
                                    np.log(inv_sigma2)))

            # ** Do a separate & simple fit of the tail slope **
            # TODO: Add this to process-waveforms.py
            idxMax = np.where(
                guessFull == guessFull.max())  # returns an array/tuple
            idxMax = idxMax[0][0]  # "cast" to int
            tail, tailTS = waveDenoised[idxMax:], waveTS[idxMax:]
            popt, _ = curve_fit(wl.tailModelPol, tailTS, tail)  # poly fit
            pol0, pol1, pol2, pol3 = popt[0], popt[1], popt[2], popt[3]
            a, b = dataE, 72000
            popt2, _ = curve_fit(wl.tailModelExp, tailTS, tail,
                                 p0=[a, b])  # expo fit
            e1, e2 = popt2[0], popt2[1]

            # Assign values to output vectors and fill branches
            fitStart[iH], fitStartSD[iH] = st, st_std
            fitE[iH], fitESD[iH] = en, en_std
            fitSlo[iH], fitSloSD[iH] = slo, slo_std
            fitChi2NDF[iH] = chi2NDF
            fitLnLike[iH] = lnLike
            tExp1[iH], tExp2[iH] = e1, e2
            tPol0[iH], tPol1[iH], tPol2[iH], tPol3[iH] = pol0, pol1, pol2, pol3
            baseAvg[iH] = dataBaseline
            baseNoise[iH] = dataNoise
            if batMode:
                bFitStart.Fill()
                bFitE.Fill()
                bFitSlo.Fill()
                bFitStart_sd.Fill()
                bFitE_sd.Fill()
                bFitSlo_sd.Fill()
                bFitChi2NDF.Fill()
                bFitLnLike.Fill()
                bTExp1.Fill()
                bTExp2.Fill()
                bTPol0.Fill()
                bTPol1.Fill()
                bTPol2.Fill()
                bTPol3.Fill()
                bBaseAvg.Fill()
                bBaseNoise.Fill()
                if iList % 5000 == 0:
                    outTree.Write("", TObject.kOverwrite)
                    print "%d / %d entries saved (%.2f %% done)." % (
                        iList, nList, 100 * (float(iList) / nList))

            # If not in batch mode, fill the figure
            if batMode: continue
            p1.cla()
            p1.set_ylabel("ADC")
            p1.set_title(
                "Run %d  Channel %d  Entry %d\ntrapENFCal %.1f  T/E %.1f  ST %.1f"
                % (run, chan, iList, dataE, toe, dataST))
            p1.plot(waveTS, waveBLSub, color='blue')
            p1.plot(waveTS, waveDenoised, color='red', alpha=0.8)
            p1.plot(guessFullTS, guessFull, color='orange', linewidth=2)
            p1.axvline(x=dataST, color='green', linewidth=2)
            p1.axvline(x=loWin, color='black')
            p1.axvline(x=hiWin, color='black')
            p1.plot(tailTS,
                    wl.tailModelPol(tailTS, *popt),
                    color='cyan',
                    linewidth=1)  # tail poly fit
            p1.plot(tailTS,
                    wl.tailModelExp(tailTS, *popt2),
                    color='cyan',
                    linewidth=1)  # tail expo fit

            p2.cla()
            p2.plot(dataTS, data, color='blue', label='Data')
            p2.plot(guessTS, guess, color='orange', label='Guess')
            # special: plot the values of the trace after burn-in
            # to see how the model is covering the "money-zone"/rising edge after it's converged.
            # for i in range(burnIn,numSteps):
            # st_tr, en_tr, slo_tr = M.trace('startTime')[i], M.trace('energy')[i], M.trace('slowness')[i]
            # trace, traceTS = wm.MakeModel(dataList, tempList, [st_tr,en_tr,slo_tr], fn=InterpFn)
            # p2.plot(traceTS, trace, color='red',alpha=0.1,linewidth=2)
            p2.plot(modelTS,
                    model,
                    color='red',
                    linewidth=3,
                    alpha=0.7,
                    label='Best Fit')
            p2.legend(loc=4)

            p3.cla()
            p3.set_xlabel("Time [ns]", x=0.95, ha='right')
            p3.set_ylabel("Residual [ADC]")
            p3.plot(modelTS, residual, color='red')
            p3.axhline(y=0, color='blue', alpha=0.3)
            p3.axhline(y=dataNoise, color='blue', alpha=0.3)
            p3.axhline(y=-1.0 * dataNoise, color='blue', alpha=0.3)

            p4.cla()
            minST = tempST - tempTS[-1] + hiWin
            maxST = tempST - tempTS[0] + loWin
            p4.set_title(
                "startTime %.1f  Energy %.2f\nSlow %.1f  chi2/ndf %.1f  Min %d  Max %d"
                % (st, en, slo, chi2NDF, minST, maxST))
            p4.plot(startTimeTr[:])
            p4.set_ylabel('startTime')
            p4.axvline(x=burnIn, color='red', alpha=0.5)

            p5.cla()
            p5.plot(energyTr[:])
            p5.set_ylabel('energy')
            p5.axvline(x=burnIn, color='red', alpha=0.5)

            p6.cla()
            p6.plot(slownessTr[:])
            p6.set_ylabel('slowness')
            p6.axvline(x=burnIn, color='red', alpha=0.5)

            plt.tight_layout()
            plt.subplots_adjust(hspace=0.35)
            plt.pause(scanSpeed)
            # pdf.savefig()

    # End loop over events
    if batMode:
        outTree.Write("", TObject.kOverwrite)
        print "Wrote", outTree.GetBranch(
            "channel").GetEntries(), "entries in the copied tree,"
        print "and wrote", bFitStart.GetEntries(
        ), "entries in the new branches."