source: branches/AddingGoogleTestEnvironment/macros/RFEnergyEst.C@ 18096

Last change on this file since 18096 was 6669, checked in by hengsteb, 20 years ago
*** empty log message ***
File size: 8.6 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 Hengstebeck, 02/2005 <mailto:hengsteb@physik.hu-berlin.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// This macro shows you how to use the new class MRFEnergyEst, which is
28// an implementation of energy estimation with RF. It contains following
29// functions:
30//
31// - CreateMatrices() : Create matrix for training (and matrix for test)
32// - RFEnergyEstTrain(): Training of RFs
33// - ReadForests() : Read and print out energy bins which have been used
34// in training (depence on statistics)
35//
36// - RFEnergyEstTest() : Fast testing using a matrix with test data
37// - RFEnergyEstLoop() : Application of RF energy est in eventloop
38//
39////////////////////////////////////////////////////////////////////////////
40
41
42//****************************************************************************
43// main user settings
44//****************************************************************************
45
46//----------------------------------------------------------------------------
47// settings for RF training
48const Int_t ntrees = 50; // 50-100 is usually sufficient
49const Int_t numtrials = 3; // should be <= sqrt(no. of used var)
50const Int_t nodesize = 1; // best results with 1
51
52// settings for energy grid
53const Int_t ebins = 30;
54const Float_t e_low = log10(10); // lower limit of log10(energy[GeV])
55const Float_t e_up = log10(30000); // upper limit
56
57//----------------------------------------------------------------------------
58// data settings
59TString path="/emc/commich/Mars/mcdata/oldout/";
60
61//TString nameTrain = "19990101_10001_I_MCGamTrainLZA_E_10_5";
62//TString nameTest = "19990101_10002_I_MCGamTestLZA_E_10_5";
63TString nameTrain = "19990101_10003_I_MCGamTrainHZA_E_10_5";
64TString nameTest = "19990101_10004_I_MCGamTestHZA_E_10_5" ;
65
66TString info=""; // put here additional info e.g. about cuts used in
67 // CreateMatrices function
68
69//----------------------------------------------------------------------------
70// generate filenames
71TString fileNameTrain = path + nameTrain + ".root";
72TString fileNameTest = path + nameTest + ".root";
73
74TString fileMatrixTrain = nameTrain + "_Matrix" + info + ".root";
75TString fileMatrixTest = nameTest + "_Matrix" + info + ".root";
76
77TString fileForest = "EForests" + nameTrain + info + ".root";
78
79
80//****************************************************************************
81// Create matrices for training (and test)
82//****************************************************************************
83void CreateMatrices()
84{
85 MGeomCamMagic gcam;
86 const Double_t mm2deg=gcam.GetConvMm2Deg();//180./17000./3.14159265358979312;
87
88 TString filename[2] = {fileNameTrain,fileNameTest};
89 TString filematrix[2] = {fileMatrixTrain,fileMatrixTest};
90
91 for(int i=0;i<2;i++)
92 {
93 if(filename[i].IsNull() || filematrix[i].IsNull())
94 continue;
95
96 MParList plist;
97 MTaskList tlist;
98 plist.AddToList(&tlist);
99
100 MReadTree read("Events", filename[i]);
101 read.DisableAutoScheme();
102
103 MHMatrix matrix("MatrixGammas");
104 // setting rules (MC energy must be last column!!!)
105 matrix.AddColumn("log10(MHillas.fSize)");
106 matrix.AddColumn("MHillasSrc.fDist");
107 matrix.AddColumn("MHillas.fWidth");
108 matrix.AddColumn("MHillas.fLength");
109 matrix.AddColumn("log10(MHillas.fSize/(MHillas.fLength*MHillas.fWidth))");
110 matrix.AddColumn("MNewImagePar.fConc");
111 matrix.AddColumn("MNewImagePar.fLeakage1");
112 matrix.AddColumn("MPointingPos.fZd");
113 matrix.AddColumn("MMcEvt.fEnergy");
114
115 plist.AddToList(&matrix);
116 MFillH fillmat("MatrixGammas");
117
118 // pre-cuts on data,
119 // take care that this is inverted logic (MContinue task!!)
120 MContinue sizecut("MHillas.fSize<60.");
121 MContinue leakcut("MNewImagePar.fLeakage1>0.1");
122 MContinue distcutlo(Form("MHillasSrc.fDist*%f<0.3",mm2deg));
123 MContinue distcutup(Form("MHillasSrc.fDist*%f>1.1",mm2deg));
124 MContinue hcut("MHadronness.fHadronness>0.3");
125
126 tlist.AddToList(&read);
127
128 // put cuts into tlist
129 tlist.AddToList(&sizecut);
130 tlist.AddToList(&leakcut);
131 tlist.AddToList(&distcutlo);
132 tlist.AddToList(&distcutup);
133 //tlist.AddToList(&hcut);
134
135 tlist.AddToList(&fillmat);
136
137 MEvtLoop evtloop;
138 evtloop.SetParList(&plist);
139
140 if (!evtloop.Eventloop()) return;
141 tlist.PrintStatistics();
142
143 TFile file(filematrix[i],"recreate","");
144 matrix.Write();
145 file.Close();
146 }
147
148 return;
149}
150
151//****************************************************************************
152// Training of RFs
153//****************************************************************************
154void RFEnergyEstTrain()
155{
156 // initializations
157 TArrayD egrid(ebins+1);
158
159 for(Int_t i=0;i<=ebins;i++)
160 egrid[i]=e_low+i*(e_up-e_low)/float(ebins);
161
162 MHMatrix matrix;
163 TFile *file=new TFile(fileMatrixTrain);
164 matrix.Read("MatrixGammas");
165
166 // output info about used rules
167 cout<<endl<<"Rules for energy estimation:"<<endl;
168 for(Int_t i=0;i<matrix.GetM().GetNcols();i++)
169 {
170 MDataArray *rules = matrix.GetColumns();
171 MData &data=(*rules)[i];
172
173 cout<<" "<<i+1<<") "<<data.GetRule()<<endl;
174 }
175
176 // setup RF for energy estimation
177 MRFEnergyEst rf;
178 rf.SetMatrixTrain(&matrix);
179 rf.SetFile(fileForest);
180 rf.SetLogEnergyGrid(egrid);
181
182 rf.SetNumTrees(ntrees); // number of trees
183 rf.SetNumTry(numtrials); // number of trials in random split selection
184 rf.SetNdSize(nodesize); // limit for nodesize
185
186 rf.Train();
187
188 return;
189}
190
191//****************************************************************************
192// Check which energy bins have been used
193//****************************************************************************
194void ReadForests()
195{
196 TFile fileRF(fileForest,"read");
197 TList *list=(TList*)fileRF.GetListOfKeys();
198 const Int_t n=list->GetSize()-1;// subtract 1 since 1 key belongs to MDataArray
199
200 MRanForest forest;
201 for(Int_t i=0;i<n;i++)
202 {
203 forest.Read(Form("%d",i));
204 MRanForest *curforest=(MRanForest*)forest.Clone();
205 const char *energy=list->FindObject(Form("%d",i))->GetTitle();
206 const char *bin =list->FindObject(Form("%d",i))->GetName();
207
208 if(i<10) cout<<"Bin "<<bin<<": log10(Energy[GeV]) = "<<energy<<endl;
209 else cout<<"Bin "<< bin<<": log10(Energy[GeV]) = "<<energy<<endl;
210 }
211 fileRF.Close();
212
213 return;
214}
215
216//****************************************************************************
217// Fast Testing with matrix
218//****************************************************************************
219void RFEnergyEstTest()
220{
221 MHMatrix matrix;
222 TFile *file=new TFile(fileMatrixTest);
223 matrix.Read("MatrixGammas");
224
225 MRFEnergyEst rf;
226 rf.SetMatrixTest(&matrix);
227 rf.SetFile(fileForest);
228
229 rf.SetBit(MParContainer::kEnableGraphicalOutput,1);
230 rf.Test();
231
232 return;
233}
234
235//****************************************************************************
236// Apply RF energy estimation in eventloop
237//****************************************************************************
238void RFEnergyEstLoop()
239{
240 MParList plist;
241
242 MTaskList tlist;
243 plist.AddToList(&tlist);
244 MReadTree read("Events",fileNameTest);
245 read.DisableAutoScheme();
246
247 MRFEnergyEst rf;
248 rf.SetFile(fileForest);
249
250 MWriteRootFile write("EnergyEstTest.root");
251 write.AddContainer("MMcEvt", "Events");
252 write.AddContainer("MEnergyEst", "Events");
253
254 tlist.AddToList(&read);
255 tlist.AddToList(&rf);
256 tlist.AddToList(&write);
257
258 MEvtLoop evtloop;
259 evtloop.SetParList(&plist);
260
261 if (!evtloop.Eventloop()) return;
262 tlist.PrintStatistics();
263
264 return;
265}
Note: See TracBrowser for help on using the repository browser.