def draw_flux(self, bin_width=5, cut='', rel_time=True, show=True, prnt=True, save=True): cut = TCut( 'beam_current < 10000 && rate[{0}] < 1e9 && rate[{1}] < 1e9 && rate[{0}] && rate[{1}]' .format(*self.Run.TriggerPlanes + 1)) + TCut(cut) if self.has_branch('rate'): flux1, flux2, t = self.get_tree_vec( var=[self.get_flux_var(p) for p in [1, 2]] + [self.get_t_var()], cut=cut) flux = mean([flux1, flux2], axis=0)[1:] / 1000 else: t, flux = self.Run.Time / 1000, full(self.Run.NEvents - 1, self.get_flux().n) p = self.Draw.profile(t[1:], flux, self.Bins.get_raw_time(bin_width=bin_width), 'Flux Profile', draw_opt='hist', **Draw.mode(2), show=show) format_histo(p, x_tit='Time [hh:mm]', y_tit='Flux [kHz/cm^{2}]', markersize=1, t_ax_off=self.StartTime if rel_time else 0, stats=0, y_range=[0, p.GetMaximum() * 1.2]) self.Draw.save_plots('FluxProfile', prnt=prnt, show=show, save=save) return p
def plot_inpix(inputfilename=None, histofilename="EfficiencyHistos.root", basecut="nTelTracks == 1", matchcut="hasHit == 0", usize=0.0, vsize=0.0, ubins=10, vbins=10): if inputfilename == None: return None total_cut = TCut(basecut) pass_cut = TCut(basecut) + TCut(matchcut) rawfile = gROOT.FindObject(inputfilename) if rawfile: rawfile.Close() rawfile = TFile(inputfilename, 'READ') histofile = gROOT.FindObject(histofilename) if histofile: histofile.Close() histofile = TFile( histofilename, 'RECREATE', 'Efficiency histos created from input file ' + inputfilename) # Get access to tracks tree = rawfile.Get("Track") h_track_total = TH2F("h_track_total", "", ubins, -usize, +usize, vbins, -vsize, vsize) tree.Draw(" v_fit:u_fit >> +h_track_total", total_cut, "goff") h_track_total.GetXaxis().SetTitle("u_fit [mm]") h_track_total.GetYaxis().SetTitle("v_fit [mm]") h_track_total.GetZaxis().SetTitle("tracks") h_track_total.SetStats(0) h_track_pass = TH2F("h_track_pass", "", ubins, -usize, usize, vbins, -vsize, +usize) tree.Draw(" v_fit:u_fit >> +h_track_pass", pass_cut, "goff") h_track_pass.GetXaxis().SetTitle("u_fit [mm]") h_track_pass.GetYaxis().SetTitle("v_fit [mm]") h_track_pass.GetZaxis().SetTitle("tracks") h_track_pass.SetStats(0) h_efficiency = h_track_pass h_efficiency.SetName("h_efficiency") h_efficiency.Divide(h_track_total) h_efficiency.SetTitle("") h_efficiency.SetXTitle("u_fit [mm]") h_efficiency.SetYTitle("v_fit [mm]") h_efficiency.SetZTitle("efficiency") h_efficiency.SetStats(0) # write the tree into the output file and close the file histofile.Write() histofile.Close() rawfile.Close()
def chi2_range_sweep(): ## Checks if the chi2 sweep is complete, or if it needs to change directions because it overshot def check_range(window, percentage): if (percentage <= 0.95 + window) and (percentage >= 0.95 - window): direction = 0 # Sweep completed elif percentage > 0.95 + window: direction = -1 # Decrease chi_val elif percentage < 0.95 + window: direction = 1 # Increase chi_val return direction chi_vals = [0, 0] # One range for xtal[0], and one for xtal[1] print "Beginning chi2 sweep..." for i in range(len(chi_vals)): h_chi = TH1F("h_chi", "", 1000, 0, 1000) basic_cut = "n_tracks==1 && " basic_cut += "fabs(fitResult[0].y())<10 && fabs(fitResult[0].x())<10 && ".format( self.xtal[i]) basic_cut += "fit_time[{0}]>0 && fit_time[{1}]>0".format( self.xtal[0], self.xtal[1]) tree.Draw("fit_chi2[{}]>>h_chi".format(self.xtal[i]), TCut(basic_cut)) tot_events = h_chi.GetEntries() stepsize = 64 # By how much chi_val is changed each time chi_val = 0 # A guess to start percent_plus_minus = 0.005 # What is the acceptable range of percent's around 0.95, IE 0.01->0.94-0.96 direction = 1 # Start by increasing chi_val lastdirection = direction # Memory for checking if the direction has changed while direction != 0: # Keep searching until 'direction'==0 chi_val += ( direction * stepsize ) # Adjust the chi_val in the right direction, by the current stepsize chi_cut = "fit_chi2[{0}]>1./{1} && fit_chi2[{0}]<{1} && {2}".format( self.xtal[i], chi_val, basic_cut) tree.Draw("fit_chi2[{}]>>h_chi".format(self.xtal[i]), TCut(chi_cut)) percent_accept = 1. * h_chi.GetEntries() / tot_events lastdirection = direction direction = check_range( percent_plus_minus, percent_accept ) # Update direction. +1, -1, or 0 if complete if direction != lastdirection: stepsize /= 2. # Reduce step size if we overshoot it and changed direction chi_vals[i] = chi_val print "Range found for {}:\n\t{:.4f} - {} with {:.2f}% acceptance".format( self.xtal[i], 1. / chi_val, chi_val, percent_accept * 100.) return [[1. / chi_vals[0], chi_vals[0]], [1. / chi_vals[1], chi_vals[1]]]
def main(m1, m2): f1 = TFile("histograms/mc.root") h1 = TH1D("h1", "h1", 40, -0.04, 0.04) h2 = TH1D("h2", "h2", 40, -0.04, 0.04) ntuple = gROOT.FindObject('Tree') cut1 = TCut("abs(m3pimc - " + str(m1) + ") < 0.05") ntuple.Draw("m3pimc-m3pi >> h1", cut1) cut2 = TCut("abs(m3pimc - " + str(m2) + ") < 0.05") ntuple.Draw("m3pimc-m3pi >> h2", cut2) h2.Scale(h1.GetEntries() / h2.GetEntries()) h2.SetLineColor(2) h1.Draw() h2.Draw("same") input()
def Evaluate(outdir): sys.stdout = open(outdir + '/tmva.log', 'w') # Output file output = TFile(outdir + '/tmva.root', 'RECREATE') # Create instance of TMVA factory (see TMVA/macros/TMVAClassification.C for more factory options) # All TMVA output can be suppressed by removing the "!" (not) in # front of the "Silent" argument in the option string factory = TMVA.Factory("TMVARuleFit", output, "!V:!Silent:Color" ) # Set the variables use for the analysis input = open(outdir + '/inputvars.txt') for variable in input.readlines(): factory.AddVariable(variable[:-1], 'F') # Set the weight directory TMVA.gConfig().GetIONames().fWeightFileDir = outdir + "/weights" # Limit the creation of correlation plots TMVA.gConfig().GetVariablePlotting().fMaxNumOfAllowedVariablesForScatterPlots = 20 # Set the input file with signal and background events factory.SetInputTrees( outdir + '/signals.txt', outdir + '/backgrounds.txt' ) cutsig = TCut('') cutbkg = TCut('') factory.PrepareTrainingAndTestTree( cutsig, cutbkg, "SplitMode=Random:NormMode=NumEvents:!V" ) factory.BookMethod( TMVA.Types.kRuleFit, "RuleFit", "H:!V:RuleFitModule=RFTMVA:Model=ModRuleLinear:MinImp=0.00001:RuleMinDist=0.001:NTrees=20:fEventsMin=0.01:fEventsMax=0.5:GDTau=-1.:GDTauPrec=0.01:GDStep=0.01:GDNSteps=10000:GDErrScale=1.02" ) # Train MVAs factory.TrainAllMethods() # Test MVAs factory.TestAllMethods() # Evaluate MVAs factory.EvaluateAllMethods() # Save the output. output.Close()
def ORtwo(cut1,cut2): """OR of two TCuts in PyROOT""" if cut1.GetTitle() == "": return cut2 if cut2.GetTitle() == "": return cut1 return TCut("(%s) || (%s)"%(cut1.GetTitle(),cut2.GetTitle()))
def ANDtwo(cut1,cut2): """AND of two TCuts in PyROOT""" if cut1.GetTitle() == "": return cut2 if cut2.GetTitle() == "": return cut1 return TCut("(%s) && (%s)"%(cut1.GetTitle(),cut2.GetTitle()))
def main(): # Arguments: assert len(sys.argv) > 1 f_in = sys.argv[1] with open("cuts.yaml") as f: known_cuts = yaml.load(f) cut_key = "pre" if len(sys.argv) > 2: if sys.argv[2] in known_cuts: cut_key = sys.argv[2] tcut = TCut(cut_key, known_cuts[cut_key]) cut = tcut.GetTitle() print "[..] Applying the following cut to {}:\n{}\n".format(f_in, cut) f_out = f_in.replace(".root", "_{}.root".format(cut_key)) # Apply the cut: rf = root.rfile(f_in) tts = rf.get_ttrees() tf_out = TFile(f_out, "RECREATE") for tt_name, tt in tts.items(): print "[..] Cutting {}.".format(tt.GetName()) tt_out = tt.CopyTree(cut) tf_out.WriteTObject(tt_out) tf_out.Close() return True
def draw_occupancy(self, plane, name=None, cluster=True, tel_coods=False, cut='', **dkw): cut_string = self.Cut(cut) + TCut( '' if cluster else 'plane == {}'.format(plane)) self.Tree.SetEstimate( sum(self.get_tree_vec('n_hits_tot', cut, dtype='u1'))) x, y = self.get_tree_vec(var=self.get_hit_vars(plane, cluster, tel_coods), cut=cut_string) bins = Bins.get_native_global() if tel_coods else Bins.get_pixel() tit = f'{"Cluster" if cluster else "Hit"} Occupancy {f"ROC {plane}" if name is None else name}' return self.Draw.histo_2d( x, y, bins, **prep_kw( dkw, title=tit, x_tit='x [mm]' if tel_coods else 'Column', y_tit='y [mm]' if tel_coods else 'Row', y_off=1.2, file_name=f'{"Cluster" if cluster else "Hit"}Map{plane}'))
def makeTrigger(which="OR"): cutValues = getCutValues() cuts = {} if which == "OR": cuts["trigger"] = TCut( "triggerFired_175 > 0.5 || triggerFired_165HE10 > 0.5") return combineCuts(cuts)
def import_ED(reco_path_,var_,hid_,xbins_,xmin_,xmax_): #print 'reco_path = ',reco_path_ print 'var_ = ',var_ # input files in directory. # draw all onto same histogram to combine stats # I would like a plot for each one... #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_FullyLeptonic') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_SemiLeptonic') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_All_Events') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_Dipho_PS') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_AtleastOneElec') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_AtleastOneMuon') ch = TChain('HHWWggCandidateDumper/trees/_13TeV_Dipho_PS') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_DiphoPSandTwoElec') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_DiphoPSandgteTwoElec') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_AtleastOneMuon') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_1a3_1') #ch = TChain('HHWWggCandidateDumper/trees/_13TeV_1a3_1') ch.Add(reco_path_) hname1 = hid_ h1 = TH1F(hname1, hid_, xbins_, xmin_, xmax_) ch.Draw(var_+'>>'+hname1,TCut('')) print 'h1.GetEntries() = ',h1.GetEntries() # Tells you if the histogram was actually filled return h1
def draw_trigger_phase_trend(self, dut=False, bin_width=None, cut=None, show=True): values, t = self.get_tree_vec( var=[ 'trigger_phase[{}]'.format(1 if dut else 0), self.get_t_var() ], cut=self.Cut.generate_custom( exclude=['trigger_phase']) if cut is None else TCut(cut)) p = self.Draw.profile( t, values, self.Bins.get_time(bin_width, cut), '{} Trigger Phase vs Time'.format('DUT' if dut else 'TEL'), show=show, lm=.16, stats=set_entries()) format_histo(p, x_tit='Time [hh:mm]', y_tit='Trigger Phase', y_off=1.8, fill_color=Draw.FillColor, t_ax_off=self.StartTime)
def generate_timing(self, n_sigma=3): t_correction, fit = self.calc_timing_range() if fit is None: return TCut('') corrected_time = '{peak} - {t_corr}'.format(peak=self.Ana.Timing.get_peak_var(corr=True), t_corr=t_correction) string = 'TMath::Abs({cor_t} - {mp}) / {sigma} < {n_sigma}'.format(cor_t=corrected_time, mp=fit.GetParameter(1), sigma=fit.GetParameter(2), n_sigma=n_sigma) description = '{:1.1f}ns < peak timing < {:.1f}ns'.format(fit.GetParameter(1) - fit.GetParameter(2), fit.GetParameter(1) + fit.GetParameter(2)) return CutString('timing', string, description)
def draw_rf_phase(self, cut=None, show=True): x = self.get_tree_vec(var='rf_phase', cut=self.Cut(cut) + TCut('rf_chi2 < 100')) self.Draw.distribution(x, make_bins(-30, 30, n=500), 'RF Phase', x_tit='RF Phase [ns]', show=show)
def get_raw_cut(self, cut=None, nsigma=3): f = self.draw_peaks(show=False, fine_corr=False, prnt=False, save=False).GetListOfFunctions()[1] return TCut( 'RawTiming', '({} - {}) / {} < {}'.format( self.Ana.PeakName, f.GetParameter(1), f.GetParameter(2), nsigma)) + self.TimingCut(cut)
def draw_bc_vs_rate(self, cut='', show=True): cut = TCut( 'beam_current < 10000 && rate[{0}] < 1e9 && rate[{1}] < 1e9 && rate[{0}] && rate[{1}]' .format(*self.Run.TriggerPlanes + 1)) + TCut(cut) flux1, flux2, bc = self.get_tree_vec( var=[self.get_flux_var(p) for p in self.Run.TriggerPlanes] + ['beam_current'], cut=cut) h = self.Draw.histo_2d( bc, mean([flux1, flux2], axis=0)[1:] / 1000, title='Correlation between Beam Current and Flux', show=show) format_histo(h, x_tit='Beam Current [mA]', y_tit='Flux [kHz/cm^{2}]', y_off=1.3, stats=0)
def draw_beam_current(self, bin_width=30, cut='', rel_t=True, prof=True, show=True, save=True): if not self.has_branch('beam_current'): return warning('Branch "beam_current" does not exist!') values, t = self.get_tree_vec(var=['beam_current', self.get_t_var()], cut=TCut('beam_current < 2500') + TCut(cut)) if prof: h = self.Draw.profile(t, values, self.Bins.get_raw_time(bin_width), 'Beam Current [mA]', w=1.5, h=.75, lm=.08, draw_opt='hist', fill_color=Draw.FillColor) else: h = self.Draw.graph(concatenate([t, [t[-1]]]), concatenate([values, [0]]), w=1.5, h=.75, title='Beam Current [mA]', lm=.08, draw_opt='afp', fill_color=Draw.FillColor) format_histo(h, x_tit='Time [hh:mm]', y_tit='Beam Current [mA]', markersize=.4, t_ax_off=self.StartTime if rel_t else 0, x_range=None if prof else [h.GetX()[0], h.GetX()[t.size]]) format_statbox(h, all_stat=True) self.Draw.save_plots('BeamCurrent{}'.format(h.ClassName()[1]), show=show, save=save) return h
def draw_inter_dia(self, show=True): x = self.get_tree_vec( var='rise_time[{}] - rise_time[{}]'.format(*self.Run.Channels), cut=self.Cut() + TCut('rise_time[{}]'.format(self.Channel))) h = self.Draw.distribution(x, make_bins(-10, 10, .05), 'Peak Width', x_tit='Rise Time Difference [ns]', show=show) format_histo(h, x_range=ax_range(5, 5, .1, .3, h))
def plot(inputfilename = None, histofilename = "OccupancyHistos.root", ucells=0,vcells=0): if inputfilename == None: return None rawfile = gROOT.FindObject( inputfilename ) if rawfile: rawfile.Close() rawfile = TFile( inputfilename, 'READ' ) histofile = gROOT.FindObject( histofilename ) if histofile: histofile.Close() histofile = TFile( histofilename, 'RECREATE', 'Occupancy histos created from input file ' + inputfilename ) # Get access to hits tree = rawfile.Get("Hit") # Only count hits not matched to a track basecut = TCut("hasTrack == -1") # Number of events events = rawfile.Get("Event").GetEntries() # Compute occupancy for v cells h_occupancy_v = TH1F("h_occupancy_v","",vcells,0,vcells) tree.Draw("cellV_hit >> +h_occupancy_v", basecut , "goff") h_occupancy_v.Scale(1.0/(ucells*events ) ) h_occupancy_v.SetTitle("") h_occupancy_v.GetXaxis().SetTitle("cellV [cellID]") h_occupancy_v.GetYaxis().SetTitle("occupancy") # Compute occupancy for u cells h_occupancy_u = TH1F("h_occupancy_u","",ucells,0,ucells) tree.Draw("cellU_hit >> +h_occupancy_u", basecut , "goff") h_occupancy_u.Scale(1.0/(vcells*events ) ) h_occupancy_u.SetTitle("") h_occupancy_u.GetXaxis().SetTitle("cellU [cellID]") h_occupancy_u.GetYaxis().SetTitle("occupancy") # Compute occupancy for u:v cells h_occupancy = TH2F("h_occupancy","",ucells,0,ucells,vcells,0,vcells) tree.Draw("cellV_hit:cellU_hit >> +h_occupancy", basecut , "goff") h_occupancy.Scale(1.0/( 1.0*events ) ) h_occupancy.SetTitle("") h_occupancy.GetXaxis().SetTitle("cellU [cellID]") h_occupancy.GetYaxis().SetTitle("cellV [cellID]") h_occupancy.GetYaxis().SetTitle("occupancy") # write the tree into the output file and close the file histofile.Write() histofile.Close() rawfile.Close()
def buildArraysFromROOT(tree, allowedFeatures, cut, skipEvents, maxEvents, name): dataContainer = {} featureNames = [] eventCounter = -1 gROOT.Reset() # Get branch names for item in tree.GetListOfBranches(): featureName = item.GetName() if featureName in allowedFeatures: featureNames.append(featureName) dataContainer[featureName] = [] # Build the event list tcut = TCut(cut) tree.Draw(">>eventList", tcut) eventList = TEventList() eventList = gDirectory.Get("eventList") nSelectedEvents = eventList.GetN() # Event loop for i in range(0, nSelectedEvents): if (i < skipEvents): continue if (i % 100 == 0): sys.stdout.write("Reading %s: %d%% \r" % (tree.GetName(), 100 * i / (maxEvents + skipEvents))) sys.stdout.flush() if i >= (maxEvents + skipEvents): break selectedEvNum = eventList.GetEntry(i) tree.GetEntry(selectedEvNum) for feature in featureNames: dataContainer[feature].append(tree.__getattr__(feature)) sys.stdout.write("\n") # Make the numpy arrays outputArray = np.array([]) for feature in allowedFeatures: column = dataContainer[feature] feature_vector = np.asarray(column) feature_vector = feature_vector.reshape(feature_vector.size, 1) if outputArray.shape[0] == 0: outputArray = feature_vector else: outputArray = np.concatenate((outputArray, feature_vector), axis=1) imp = Imputer(missing_values=-999, strategy='mean', axis=0) imp.fit(outputArray) outputArray = imp.transform(outputArray) print name print "Events: ", outputArray.shape[0] print "Features: ", outputArray.shape[1] return outputArray
def runJob(): TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() output = TFile.Open('TMVA_SSSF.root', 'RECREATE') factory = TMVA.Factory( 'TMVAClassification', output, '!V:!Silent:Color:DrawProgressBar:AnalysisType=Classification') # factory = TMVA.Factory('TMVAClassification', output, '!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Classification') dataloader = TMVA.DataLoader('datasetSSSF04Feb') for br in config.mvaVariables: dataloader.AddVariable(br) for sampleName, sample in config.samples.items(): if config.structure[sampleName]['isData'] == 1: continue print sampleName sample['tree'] = TChain("Events") for f in sample['name']: sample['tree'].Add(f) if config.structure[sampleName]['isSignal'] == 1: dataloader.AddSignalTree(sample['tree'], 1.0) else: dataloader.AddBackgroundTree(sample['tree'], 1.0) # output_dim += 1 dataloader.PrepareTrainingAndTestTree( TCut(config.cut), 'SplitMode=Random:NormMode=NumEvents:!V') # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDTG4C2", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=200:MaxDepth=2" ); factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4C1", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=100:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4C05", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=50:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4500", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=500:MaxDepth=2" ) factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDTG4750", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=750:MaxDepth=2" ) # Run training, test and evaluation factory.TrainAllMethods() factory.TestAllMethods() factory.EvaluateAllMethods() output.Close()
def makeHiggsWindow(sideband=False, windowEdges=[100.0, 110.0]): #print "makeHiggsWindow got sideband =", sideband, "and windowEdges =", windowEdges cutValues = getCutValues() cuts = {} #window = "higgsWindow" #if sideband: # if windowEdges == [100.0,110.0]: # window = "sidebandWindow" # elif windowEdges == [50.0,70.0]: # window = "sideband5070Window" # elif windowEdges == [80.0,100.0]: # window = "sideband80100Window" # elif windowEdges == [30.0,99999.9]: # window = "preselectionWindow" cuts["higgsWindowLow"] = TCut("higgsPuppi_softdropJetCorrMass>%f" % windowEdges[0]) cuts["higgsWindowHi"] = TCut("higgsPuppi_softdropJetCorrMass<%f" % windowEdges[1]) #print "will return combineCuts(cuts)=", combineCuts(cuts) return combineCuts(cuts)
def draw_threshold(self, corr=False, channel=None, show=True): ch = choose(channel, self.Channel) x = self.get_tree_vec( var='signal_peak_time[{}]{}'.format(ch, '+rf_phase' if corr else ''), cut=self.Cut() + TCut('t_thresh[{}] > 10'.format(ch))) return self.Draw.distribution(x, make_bins(0, 500, .1 if corr else .2), 'Time over Threshold', x_tit='ToT [ns]', show=show, x_range=ax_range(x, 0, .1, .3, thresh=3))
def draw_inflexion_time(self, corr=False, channel=None, show=True): x = self.get_tree_vec( var='rise_time[{}]{}'.format(choose(channel, self.Channel), '+rf_phase' if corr else ''), cut=self.Cut() + TCut('rise_time[{}]'.format(choose(channel, self.Channel)))) h = self.Draw.distribution(x, make_bins(0, 20, .05), 'Inflexion Time', x_tit='Rise Time [ns]', show=show) format_histo(h, x_range=ax_range(5, h.GetMaximum() * .01, .1, .3, h))
def runJob(): TMVA.Tools.Instance() TMVA.PyMethodBase.PyInitialize() dataloader = TMVA.DataLoader('dataset_8Feb') output = TFile.Open('TMVA16.root', 'RECREATE') factory = TMVA.Factory( 'TMVAClassification', output, '!V:!Silent:Color:DrawProgressBar:AnalysisType=Classification') for br in config.mvaVariables: dataloader.AddVariable(br) for sampleName, sample in config.samples.items(): if config.structure[sampleName]['isData'] == 1: continue sample['tree'] = TChain("Events") for f in sample['name']: sample['tree'].Add(f) if config.structure[sampleName]['isSignal'] == 1: dataloader.AddSignalTree(sample['tree'], 1.0) else: dataloader.AddBackgroundTree(sample['tree'], 1.0) # output_dim += 1 dataloader.PrepareTrainingAndTestTree( TCut(config.cut), 'SplitMode=Random::SplitSeed=10:NormMode=EqualNumEvents') factory.BookMethod( dataloader, TMVA.Types.kBDT, "BDT", "!H:!V:NTrees=500:MinNodeSize=0.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.1:SeparationType=GiniIndex:nCuts=500" ) # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDT8","!H:!V:NTrees=1500:MinNodeSize=0.5%:MaxDepth=1:BoostType=AdaBoost:AdaBoostBeta=0.75:SeparationType=GiniIndex:nCuts=1000" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDT2", "!H:!V:NTrees=1200:MinNodeSize=0.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.1:SeparationType=GiniIndex:nCuts=800" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDT3", "!H:!V:NTrees=800:MinNodeSize=0.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.2:SeparationType=GiniIndex:nCuts=500" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDT4", "!H:!V:NTrees=700:MinNodeSize=0.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=500" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDTG4D3", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=500:MaxDepth=3" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDTG4C3", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=300:MaxDepth=2" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDTG4SK01", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.01:UseBaggedBoost:GradBaggingFraction=0.5:nCuts=500:MaxDepth=2" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDTG4F07" , "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.05:UseBaggedBoost:GradBaggingFraction=0.7:nCuts=500:MaxDepth=2" ); # factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDTG4SK01F07", "!H:!V:NTrees=500:MinNodeSize=1.5%:BoostType=Grad:Shrinkage=0.01:UseBaggedBoost:GradBaggingFraction=0.7:nCuts=500:MaxDepth=2" ); # Run training, test and evaluation factory.TrainAllMethods() factory.TestAllMethods() factory.EvaluateAllMethods() output.Close()
def _dataLoader(self, sigTreeNames, bkgTreeNames): self._data_loader = TMVA.DataLoader(self._options['factory']['name']) for value in self._variables.values(): self._data_loader.AddVariable(value['name'], value['type']) #---- for sigTreeName in sigTreeNames: self._data_loader.AddSignalTree( self._trees[sigTreeName + "_Train"], 1.0, "train") self._data_loader.AddSignalTree(self._trees[sigTreeName + "_Test"], 1.0, "test") for bkgTreeName in bkgTreeNames: self._data_loader.AddBackgroundTree( self._trees[bkgTreeName + "_Train"], 1.0, "train") self._data_loader.AddBackgroundTree( self._trees[bkgTreeName + "_Test"], 1.0, "test") self._data_loader.SetSignalWeightExpression( self._options['factory']['weight']) self._data_loader.SetBackgroundWeightExpression( self._options['factory']['weight']) #---- self._data_loader.PrepareTrainingAndTestTree( TCut(self._cuts['sig']), TCut(self._cuts['bkg']), self._options['prepareTrees'])
def quants_in_range(nbins_quant, lb, ub): aeff_tmp = TH1F( 'aeff', "", 100, lb, ub) # Creates a temporary histogram to find the quantiles tree.Draw(self.Aeff + '>>aeff', TCut(cut)) probs = array( 'd', [x / (nbins_quant - 1.) for x in range(0, nbins_quant)]) # Quantile proportions array quantiles = array('d', [0 for x in range(0, nbins_quant) ]) # Bin edges, initialized as all 0's aeff_tmp.GetQuantiles( nbins_quant, quantiles, probs) # Overwrites 'quantiles' with bin edges positions return quantiles
def draw_inter_dia_corr(self, show=True): x, y = self.get_tree_vec( var=['rise_time[{}]'.format(ch) for ch in self.Run.Channels], cut=self.Cut() + TCut('rise_time[{}]'.format(self.Channel))) h = self.Draw.histo_2d(x, y, make_bins(0, 20, .05) * 2, 'Inflextion Times of Diamond Signals', x_tit='Inflexion Time1 [ns]', y_tit='Inflexion Time2 [ns]', show=show) format_histo( h, **{ n: v for n, v in zip(['x_range', 'y_range'], ax_range(5, 5, .2, .2, h)) })
def get_inner_efficiency(roc=1, q=.2): i_pl = 2 if roc == 1 else 1 cut = TCut(f'n_clusters[0] == 1 & n_clusters[3] == 1 & n_clusters[{i_pl}] == 1') # select only events with exactly one cluster in the other three planes n_tracks = t.GetEntries(cut.GetTitle()) info(f'Found {n_tracks} tracks') cut += f'n_clusters[{roc}] > 0' xfits, chi_x, yfits, chi_y = fit_tracks(roc, cut) z_ = get_z_positions() (x, y), n = z.get_tree_vec(get_track_vars(roc=roc), cut=cut), z.get_tree_vec(f'n_clusters[{roc}]', cut, dtype='i2') chi_cut = (chi_x < quantile(chi_x, q)) & (chi_y < quantile(chi_y, q)) dx, dy = array([polyval(xfits.repeat(n, axis=1), z_[roc]) - x, polyval(yfits.repeat(n, axis=1), z_[roc]) - y]) * 10000 # to um rcut = get_res_cut(dx, n, chi_cut, 2 * z.Plane.PX * 1000) & get_res_cut(dy, n, chi_cut, 2 * z.Plane.PY * 1000) # n_good = round(mean([get_res_cut(dx, n, chi_cut), get_res_cut(dy, n, chi_cut)])) n_max = count_nonzero(chi_cut) # TODO fix wrong number of events n_good = rcut[rcut].size eff = calc_eff(n_good, n_max) info('Efficiency for plane {}: ({:1.1f}+{:1.1f}-{:1.1f})%'.format(roc, *eff)) return eff
def draw_rf_vs_peak(self, show=True): x, y = self.get_tree_vec( var=['signal_peak_time[{}]'.format(self.Channel), 'rf_phase'], cut=self.Cut() + TCut('rf_chi2 < 100')) h = self.Draw.histo_2d(x, y, make_bins(0, 500, .25) + make_bins(-12, 12, .1), 'RF Phase vs. Peak Timings', x_tit='Signal Peak Timing [ns]', y_tit='RF Phase [ns]', stats=0, show=show) format_histo( h, **{ n: v for n, v in zip(['x_range', 'y_range'], ax_range(5, 5, .2, .2, h)) })