Exemplo n.º 1
0
# MAIN PROGRAM

runTraining = True
nBackgroundEvents = 55000
nSignalEvents = 55000

# Selections
cutBackground = "isSignal==0"
cutSignal = "isSignal==1"

# Input file and TTree
inputFile = TFile("TMVA_tree.root","read")
tree = inputFile.Get("TMVA_tree")

# Assemble data arrays
X_train = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,0,nBackgroundEvents,"TRAINING SAMPLE (background only)")
W_train = buildArraysFromROOT(tree,susyWeightsNtup,cutBackground,0,nBackgroundEvents,"TRAINING SAMPLE WEIGHTS").reshape(X_train.shape[0])
X_test = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,nBackgroundEvents,nBackgroundEvents,"TESTING SAMPLE - background")
X_signal = buildArraysFromROOT(tree,susyFeaturesNtup,cutSignal,0,nSignalEvents,"TESTING SAMPLE - signal")

# Feature scaling
min_max_scaler = preprocessing.MinMaxScaler()
X_train = min_max_scaler.fit_transform(X_train)
X_test = min_max_scaler.transform(X_test)
X_signal = min_max_scaler.transform(X_signal)

# Calculate feature means
means = np.mean(X_train,0)

# Reconstruction error
rec_errors_same = reconstructionError(X_train,means)
    hyperparameterSets.append([10,0,0.01,0,20,0])
    hyperparameterSets.append([10,0,0.01,0,100,0])
    hyperparameterSets.append([10,0,0.01,0,200,0])
    hyperparameterSets.append([10,0,0.01,0,500,0])

# Input file and TTree
inputFile = TFile("TMVA_tree.root","read")
tree = inputFile.Get("TMVA_tree")

# Selections
cutBackground = "isSignal==0"
cutSignal = "isSignal==1"

# Assemble data arrays
# Training
X_train_background = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,0,nBackgroundEvents,"TRAINING SAMPLE (background)")
X_train_signal = buildArraysFromROOT(tree,susyFeaturesNtup,cutSignal,0,nSignalEvents,"TRAINING SAMPLE (signal)")
X_train = np.concatenate((X_train_background,X_train_signal),0)
# Testing
X_test_background = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,nBackgroundEvents,nBackgroundEvents+nSignalEvents,"TESTING SAMPLE (background)")
X_test_signal = buildArraysFromROOT(tree,susyFeaturesNtup,cutSignal,nBackgroundEvents,nBackgroundEvents+nSignalEvents,"TESTING SAMPLE (signal)")
X_test = np.concatenate((X_test_background,X_test_signal),0)
# Target/ground truth
Y_background = np.zeros(nBackgroundEvents).reshape(nBackgroundEvents,1)
Y_signal = np.ones(nSignalEvents).reshape(nSignalEvents,1)
Y = np.concatenate((Y_background,Y_signal),0)

# Feature scaling - has to be done separately for the autoencoder and the classifier
# to avoid contaminating the autoencoder training sample with information about the signal
# Autoencoder...
scaler_autoencoder = preprocessing.MinMaxScaler()
Exemplo n.º 3
0
newTree = newFile.Get("DQEWSTree")

# Automatic numbers of event 
#tcut = TCut(cutSignal)
#tree.Draw(">>eventList",tcut)
#eventList = TEventList()
#eventList = gDirectory.Get("eventList")
#nReferenceEvents = referenceTree.GetEntries()/2
#nNewEvents = newTree.GetEntries()/2
nReferenceEvents = 100000
nNewEvents = 100000


# Build data arrays
# Reference
X_train_ref = buildArraysFromROOT(referenceTree,trackingFeatures,"",0,nReferenceEvents,"TRAINING SAMPLE (reference)")
X_test_ref = buildArraysFromROOT(referenceTree,trackingFeatures,"",nReferenceEvents,nReferenceEvents,"TESTING SAMPLE (reference)")

# New data
X_train_new = buildArraysFromROOT(newTree,trackingFeatures,"",0,nNewEvents,"TRAINING SAMPLE (new)")
X_test_new = buildArraysFromROOT(newTree,trackingFeatures,"",nNewEvents,nNewEvents,"TESTING SAMPLE (new)")

# Combining signal & background
X_train = np.concatenate((X_train_ref,X_train_new),0)
X_test = np.concatenate((X_test_ref,X_test_new),0)
Y_ref = np.zeros(nReferenceEvents)
Y_new = np.ones(nNewEvents)
Y = np.concatenate((Y_ref,Y_new),0)

# Feature scaling
min_max_scaler = preprocessing.MinMaxScaler()
runTraining = True
nBackgroundEvents = 50000
nSignalEvents = 50000

# Selections
cutBackground = "isSignal==0"
cutSignal = "isSignal==1"

# Input file and TTree
inputFile = TFile("TMVA_tree.root", "read")
tree = inputFile.Get("TMVA_tree")

# Build data arrays
X_train_bg = buildArraysFromROOT(tree, susyFeaturesNtup, cutBackground, 0,
                                 nBackgroundEvents,
                                 "TRAINING SAMPLE (background)")
W_bg = buildArraysFromROOT(
    tree, susyWeightsNtup, cutBackground, 0, nBackgroundEvents,
    "EVENT WEIGHTS (background)").reshape(X_train_bg.shape[0])
X_train_sig = buildArraysFromROOT(tree, susyFeaturesNtup, cutSignal, 0,
                                  nSignalEvents, "TRAINING SAMPLE (signal)")
W_sig = buildArraysFromROOT(tree, susyWeightsNtup, cutSignal, 0, nSignalEvents,
                            "EVENT WEIGHTS (signal)").reshape(
                                X_train_sig.shape[0])
X_train = np.concatenate((X_train_bg, X_train_sig), 0)
W = np.concatenate((W_bg, W_sig), 0)

X_test_bg = buildArraysFromROOT(tree, susyFeaturesNtup, cutBackground,
                                nBackgroundEvents, nBackgroundEvents,
                                "TESTING SAMPLE (background)")
from CommonTools import reconstructionError,reconstructionErrorByFeature,buildArraysFromROOT,makeMetrics,areaUnderROC
from featuresLists import susyFeaturesNtup, susyWeightsNtup

###############################################
# MAIN PROGRAM

runTraining = False

# Input file and TTree
inputFile = TFile("TMVA_tree.root","read")
tree = inputFile.Get("TMVA_tree")

# Full background samples
nAllBackgroundEvents = 100000
cutBackground = "isSignal==0"
X_train_bg_all = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,0,nAllBackgroundEvents,"TRAINING SAMPLE (background)")
X_test_bg_all = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,nAllBackgroundEvents,nAllBackgroundEvents,"TESTING SAMPLE (background)")

# Feature scaling
min_max_scaler = preprocessing.MinMaxScaler()
X_train_bg_all = min_max_scaler.fit_transform(X_train_bg_all)
X_test_bg_all = min_max_scaler.transform(X_test_bg_all)

cutList = ["isSignal==1 && massSplit<199",
           "isSignal==1 && massSplit>199 && massSplit<299",
           "isSignal==1 && massSplit>299 && massSplit<399",
           "isSignal==1 && massSplit>399 && massSplit<499",
           "isSignal==1 && massSplit>499 && massSplit<599",
           "isSignal==1 && massSplit>599 && massSplit<699"]

# Set up the axes for plotting
# MAIN PROGRAM

runTraining = True
nBackgroundEvents = 50000
nSignalEvents = 50000

# Selections
cutBackground = "isSignal==0"
cutSignal = "isSignal==1"

# Input file and TTree
inputFile = TFile("TMVA_tree.root","read")
tree = inputFile.Get("TMVA_tree")

# Build data arrays
X_train_bg = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,0,nBackgroundEvents,"TRAINING SAMPLE (background)")
W_bg = buildArraysFromROOT(tree,susyWeightsNtup,cutBackground,0,nBackgroundEvents,"EVENT WEIGHTS (background)").reshape(X_train_bg.shape[0])
X_train_sig = buildArraysFromROOT(tree,susyFeaturesNtup,cutSignal,0,nSignalEvents,"TRAINING SAMPLE (signal)")
W_sig = buildArraysFromROOT(tree,susyWeightsNtup,cutSignal,0,nSignalEvents,"EVENT WEIGHTS (signal)").reshape(X_train_sig.shape[0])
X_train = np.concatenate((X_train_bg,X_train_sig),0)
W = np.concatenate((W_bg,W_sig),0)

X_test_bg = buildArraysFromROOT(tree,susyFeaturesNtup,cutBackground,nBackgroundEvents,nBackgroundEvents,"TESTING SAMPLE (background)")
X_test_sig = buildArraysFromROOT(tree,susyFeaturesNtup,cutSignal,nSignalEvents,nSignalEvents,"TESTING SAMPLE (signal)")
X_test = np.concatenate((X_test_bg,X_test_sig),0)

Y_bg = np.zeros(nBackgroundEvents).reshape(nBackgroundEvents,1)
Y_sig = np.ones(nSignalEvents).reshape(nSignalEvents,1)
Y = np.concatenate((Y_bg,Y_sig),0)

# Feature scaling
Exemplo n.º 7
0
newTree = newFile.Get("DQEWSTree")

# Automatic numbers of event
#tcut = TCut(cutSignal)
#tree.Draw(">>eventList",tcut)
#eventList = TEventList()
#eventList = gDirectory.Get("eventList")
#nReferenceEvents = referenceTree.GetEntries()/2
#nNewEvents = newTree.GetEntries()/2
nReferenceEvents = 100000
nNewEvents = 100000

# Build data arrays
# Reference
X_train_ref = buildArraysFromROOT(referenceTree, trackingFeatures, "", 0,
                                  nReferenceEvents,
                                  "TRAINING SAMPLE (reference)")
X_test_ref = buildArraysFromROOT(referenceTree, trackingFeatures, "",
                                 nReferenceEvents, nReferenceEvents,
                                 "TESTING SAMPLE (reference)")

# New data
X_train_new = buildArraysFromROOT(newTree, trackingFeatures, "", 0, nNewEvents,
                                  "TRAINING SAMPLE (new)")
X_test_new = buildArraysFromROOT(newTree, trackingFeatures, "", nNewEvents,
                                 nNewEvents, "TESTING SAMPLE (new)")

# Combining signal & background
X_train = np.concatenate((X_train_ref, X_train_new), 0)
X_test = np.concatenate((X_test_ref, X_test_new), 0)
Y_ref = np.zeros(nReferenceEvents)
Exemplo n.º 8
0
referenceFile = TFile("304008_lb0775-lb0784.root","read")
referenceTree = referenceFile.Get("DQEWSTree")
newFile = TFile("296942.root","read")
newTree = newFile.Get("DQEWSTree")

# Automatic numbers of event
#tcut = TCut(cutSignal)
#tree.Draw(">>eventList",tcut)
#eventList = TEventList()
#eventList = gDirectory.Get("eventList")
#nNewEvents = eventList.GetN()/2
nNewEvents = 100000
nReferenceEvents = nNewEvents

# Assemble data arrays
X_train = buildArraysFromROOT(referenceTree,trackingFeatures,"",0,nReferenceEvents,"TRAINING SAMPLE (reference only)")
X_test = buildArraysFromROOT(referenceTree,trackingFeatures,"",nReferenceEvents,nReferenceEvents,"TESTING SAMPLE - reference")
X_new = buildArraysFromROOT(newTree,trackingFeatures,"",0,nNewEvents,"TESTING SAMPLE - new")

# Feature scaling
min_max_scaler = preprocessing.MinMaxScaler()
X_train = min_max_scaler.fit_transform(X_train)
X_test = min_max_scaler.transform(X_test)
X_new = min_max_scaler.transform(X_new)

# Set target equal to input - autoencoder 
Y_train = X_train
print X_train.shape

# NEURAL NETWORK TRAINING AND TESTING
# Set up neural network