source: trunk/MagicSoft/Mars/macros/train/trainenergy.C@ 9492

Last change on this file since 9492 was 8729, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 6.2 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2007
21!
22!
23\* ======================================================================== *//////////////////////////////////////////////////////////////////////////////
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// trainenergy.C
28// =============
29//
30// Trains a random forest for energy estimation.
31//
32// The additional code below shows how the MagicCuts can be used in
33// training and testing. There is also code which allows to change the
34// slope of the input spectrum. If a min- or max-break energy is given
35// the new slope is only applied in this region. Further possibilities
36// for additional cuts are shown.
37//
38// SequencesOn: Monte Carlo Sequences used for training
39// SequencesOff: Monte Carlo Sequences used for testing
40//
41// Use:
42// opt.AddPreCut to use cut for test and training
43// opt.AddTestCut to use cut for test only
44// opt.AddTrainCut to use cut for train only
45// opt.AddPreTask a task executed before filling the matrix/evaluating the RF
46// opt.AddPostTask a task executed before training, after evaluating the RF
47//
48/////////////////////////////////////////////////////////////////////////////
49void trainenergy()
50{
51 MDataSet set("mcsponde/mcdataset-for-training.txt");
52 set.SetNumAnalysis(1); // Necessary
53
54 // alternatively use:
55 //MDataSet set("mcctesttrain.txt", "/magic/montacrlo/sequences", "/magic/montecarlo/star");
56
57 MJTrainEnergy opt;
58 //opt.SetDebug();
59
60 // These are control parameters how to train the random forest (use with care!)
61 //opt.SetNumTrees(100);
62 //opt.SetNdSize(5);
63 //opt.SetNumTry(0);
64
65 // ------- Parameters to train Random Forest --------
66 // The following parameters are the best parameters
67 opt.AddParameter("MHillas.fSize");
68 opt.AddParameter("MHillasSrc.fDist");
69 opt.AddParameter("MPointingPos.fZd");
70 opt.AddParameter("MNewImagePar.fLeakage1");
71 opt.AddParameter("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)");
72 // The influence of these parameters is unclear
73 //opt.AddParameter("MHillasExt.fM3Long*sign(MHillasSrc.fCosDeltaAlpha)");
74 //opt.AddParameter("MNewImagePar.fConcCOG");
75 //opt.AddParameter("MHillas.GetArea");
76 //opt.AddParameter("MNewImagePar.fConc1");
77 //opt.AddParameter("MNewImagePar.fConc");
78 //opt.AddParameter("MNewImagePar.fUsedArea");
79 //opt.AddParameter("MNewImagePar.fCoreArea");
80 //opt.AddParameter("MNewImagePar.fLeakage2");
81
82 // Setup how to train the RF. This one gives best results so far
83 opt.SetTrainFunc("log(MMcEvt.fEnergy)/log(MHillas.fSize)", "MHillas.fSize^x");
84 // opt.SetTrainLog(); // Train in log-energy
85 // opt.SetTrainLin(); // Train just in energy
86
87 // -------------------- Run ----------------------------
88
89 MStatusDisplay *d = new MStatusDisplay;
90 opt.SetDisplay(d);
91
92 // -------------------- Magic-Cuts ----------------------
93 // It is recommended to test with your cuts, but train with all events
94
95 MFMagicCuts cuts;
96 cuts.SetHadronnessCut(MFMagicCuts::kArea);
97 cuts.SetThetaCut(MFMagicCuts::kOn);
98
99 TArrayD arr(13);
100 arr[0] = 1.15136;
101 arr[1] = 0.215;
102 arr[2] = 0.215468;
103 arr[3] = 5.63973;
104 arr[4] = 0.0836169;
105 arr[5] = -0.07;
106 arr[6] = 7.2;
107 arr[7] = 0.5;
108 arr[8] = 0.0681437;
109 arr[9] = 2.62932;
110 arr[10] = 1.51279;
111 arr[11] = 0.0507821;
112
113 cuts.SetVariables(arr);
114
115 // opt.AddPreCut(&cuts); // add cut for training and testing
116 // opt.AddTrainCut(&cuts); // add cut for training only
117 opt.AddTestCut(&cuts); // add cut for testing only
118
119 /*
120 // ---------------------- Histogram --------------------
121 MHn hist("MyHist", "Energy Residual (lg E_{est} - lg E_{mc})");
122
123 hist.AddHist("MNewImagePar.fConcCOG", "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)");
124 hist.InitName("ResConc;Conc;EnergyResidual");
125 hist.InitTitle(";C;\\Delta lg E;");
126 hist.SetDrawOption("colz profx");
127
128 MBinning bins(50, 0, 1);
129 hist.SetBinnings(&bins);
130
131 MFillH fill(&hist, "", "FillMyHist");
132 //fill.SetWeight(); // Enable weights to be used to fill this histogram
133 opt.AddTestTask(&fill);
134
135 // -------------------- Other cuts ----------------------
136 opt.AddPreCut("MHillasSrc.fDist*MGeomCam.fConvMm2Deg<1.0");
137 opt.AddPreCut("MHillas.fSize>200");
138
139 // -------------------- Energy Slope --------------------
140 // Note, that weight normally doesn't improve anything here.
141 // This is a way to throw away events to a different slope
142 MFEnergySlope slope(-4.0); // New slope for mc spectrum
143 opt.AddPreCut(&slope); // throw away events to change slope
144
145 // This is a way to weight the events to a different spectrum
146 MMcSpectrumWeight weight;
147 weight.SetFormula("pow(X/300, -2.31-0.26*log10(X/300))");
148 opt.SetWeights(&weight);
149
150 // ------------------ Zd distribution -------------------
151 TFile file("ganymed00001111.root");
152
153 MStatusArray arr;
154 if (arr.Read()<=0)
155 return;
156 TH1D *vstime = (TH1D*)arr.FindObjectInCanvas("Theta", "TH1D", "OnTime");
157 if (!vstime)
158 return -1;
159
160 MMcSpectrumWeight weight;
161 weight.SetWeightsZd(vstime);
162 opt.AddPreTask(&weight);
163
164 // ------------------------------------------------------
165 */
166
167 // To allow overwrite of existing files
168 // opt.SetOverwrite();
169
170 opt.Train("rf-energy.root", set, 30000);
171}
Note: See TracBrowser for help on using the repository browser.