Logo ROOT   6.07/09
Reference Guide
mlpHiggs.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_mlp
3 /// \notebook
4 /// Example of a Multi Layer Perceptron
5 /// For a LEP search for invisible Higgs boson, a neural network
6 /// was used to separate the signal from the background passing
7 /// some selection cuts. Here is a simplified version of this network,
8 /// taking into account only WW events.
9 ///
10 /// \macro_image
11 /// \macro_output
12 /// \macro_code
13 ///
14 /// \author Christophe Delaere
15 
16 void mlpHiggs(Int_t ntrain=100) {
17  const char *fname = "mlpHiggs.root";
18  TFile *input = 0;
19  if (!gSystem->AccessPathName(fname)) {
20  input = TFile::Open(fname);
21  } else {
22  printf("accessing %s file from http://root.cern.ch/files\n",fname);
23  input = TFile::Open(Form("http://root.cern.ch/files/%s",fname));
24  }
25  if (!input) return;
26 
27  TTree *sig_filtered = (TTree *) input->Get("sig_filtered");
28  TTree *bg_filtered = (TTree *) input->Get("bg_filtered");
29  TTree *simu = new TTree("MonteCarlo", "Filtered Monte Carlo Events");
30  Float_t ptsumf, qelep, nch, msumf, minvis, acopl, acolin;
31  Int_t type;
32  sig_filtered->SetBranchAddress("ptsumf", &ptsumf);
33  sig_filtered->SetBranchAddress("qelep", &qelep);
34  sig_filtered->SetBranchAddress("nch", &nch);
35  sig_filtered->SetBranchAddress("msumf", &msumf);
36  sig_filtered->SetBranchAddress("minvis", &minvis);
37  sig_filtered->SetBranchAddress("acopl", &acopl);
38  sig_filtered->SetBranchAddress("acolin", &acolin);
39  bg_filtered->SetBranchAddress("ptsumf", &ptsumf);
40  bg_filtered->SetBranchAddress("qelep", &qelep);
41  bg_filtered->SetBranchAddress("nch", &nch);
42  bg_filtered->SetBranchAddress("msumf", &msumf);
43  bg_filtered->SetBranchAddress("minvis", &minvis);
44  bg_filtered->SetBranchAddress("acopl", &acopl);
45  bg_filtered->SetBranchAddress("acolin", &acolin);
46  simu->Branch("ptsumf", &ptsumf, "ptsumf/F");
47  simu->Branch("qelep", &qelep, "qelep/F");
48  simu->Branch("nch", &nch, "nch/F");
49  simu->Branch("msumf", &msumf, "msumf/F");
50  simu->Branch("minvis", &minvis, "minvis/F");
51  simu->Branch("acopl", &acopl, "acopl/F");
52  simu->Branch("acolin", &acolin, "acolin/F");
53  simu->Branch("type", &type, "type/I");
54  type = 1;
55  Int_t i;
56  for (i = 0; i < sig_filtered->GetEntries(); i++) {
57  sig_filtered->GetEntry(i);
58  simu->Fill();
59  }
60  type = 0;
61  for (i = 0; i < bg_filtered->GetEntries(); i++) {
62  bg_filtered->GetEntry(i);
63  simu->Fill();
64  }
65  // Build and train the NN ptsumf is used as a weight since we are primarily
66  // interested by high pt events.
67  // The datasets used here are the same as the default ones.
69  new TMultiLayerPerceptron("@msumf,@ptsumf,@acolin:5:3:type",
70  "ptsumf",simu,"Entry$%2","(Entry$+1)%2");
71  mlp->Train(ntrain, "text,graph,update=10");
72  mlp->Export("test","python");
73  // Use TMLPAnalyzer to see what it looks for
74  TCanvas* mlpa_canvas = new TCanvas("mlpa_canvas","Network analysis");
75  mlpa_canvas->Divide(2,2);
76  TMLPAnalyzer ana(mlp);
77  // Initialisation
78  ana.GatherInformations();
79  // output to the console
80  ana.CheckNetwork();
81  mlpa_canvas->cd(1);
82  // shows how each variable influences the network
83  ana.DrawDInputs();
84  mlpa_canvas->cd(2);
85  // shows the network structure
86  mlp->Draw();
87  mlpa_canvas->cd(3);
88  // draws the resulting network
89  ana.DrawNetwork(0,"type==1","type==0");
90  mlpa_canvas->cd(4);
91  // Use the NN to plot the results for each sample
92  // This will give approx. the same result as DrawNetwork.
93  // All entries are used, while DrawNetwork focuses on
94  // the test sample. Also the xaxis range is manually set.
95  TH1F *bg = new TH1F("bgh", "NN output", 50, -.5, 1.5);
96  TH1F *sig = new TH1F("sigh", "NN output", 50, -.5, 1.5);
97  bg->SetDirectory(0);
98  sig->SetDirectory(0);
99  Double_t params[3];
100  for (i = 0; i < bg_filtered->GetEntries(); i++) {
101  bg_filtered->GetEntry(i);
102  params[0] = msumf;
103  params[1] = ptsumf;
104  params[2] = acolin;
105  bg->Fill(mlp->Evaluate(0, params));
106  }
107  for (i = 0; i < sig_filtered->GetEntries(); i++) {
108  sig_filtered->GetEntry(i);
109  params[0] = msumf;
110  params[1] = ptsumf;
111  params[2] = acolin;
112  sig->Fill(mlp->Evaluate(0,params));
113  }
114  bg->SetLineColor(kBlue);
115  bg->SetFillStyle(3008); bg->SetFillColor(kBlue);
116  sig->SetLineColor(kRed);
117  sig->SetFillStyle(3003); sig->SetFillColor(kRed);
118  bg->SetStats(0);
119  sig->SetStats(0);
120  bg->Draw();
121  sig->Draw("same");
122  TLegend *legend = new TLegend(.75, .80, .95, .95);
123  legend->AddEntry(bg, "Background (WW)");
124  legend->AddEntry(sig, "Signal (Higgs)");
125  legend->Draw();
126  mlpa_canvas->cd(0);
127  delete input;
128 }
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3127
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:27
float Float_t
Definition: RtypesCore.h:53
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8008
Definition: Rtypes.h:61
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5210
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:44
TLegend * legend
Definition: pirndm.C:35
void Export(Option_t *filename="NNfunction", Option_t *language="C++") const
Exports the NN as a function for any non-ROOT-dependant code Supported languages are: only C++ ...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3871
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7719
Double_t Evaluate(Int_t index, Double_t *params) const
Returns the Neural Net for a given set of input parameters #parameters must equal #input neurons...
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2853
void Train(Int_t nEpoch, Option_t *option="text", Double_t minE=0)
Train the network.
virtual void Draw(Option_t *option="")
Draws the network structure.
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
char * Form(const char *fmt,...)
The Canvas class.
Definition: TCanvas.h:41
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
int type
Definition: TGX11.cxx:120
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1651
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1089
virtual Long64_t GetEntries() const
Definition: TTree.h:392
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Definition: Rtypes.h:61
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8058