source: trunk/Mars/mjoptim/MJOptimizeBase.cc@ 19857

Last change on this file since 19857 was 9874, checked in by tbretz, 14 years ago
MJOptimizeBase now prints a warning if TRandom is used.
File size: 6.3 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, 2005-2010
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJOptimizeBase
28//
29// Base class for classes training a random forest
30//
31// The order when reading a file is:
32// 1) Execution of PreTasks
33// 2) Execution of PreCuts, TestCuts, TrainCuts
34// 3) Selector (calculated from number of requested events)
35// 4) Splitter (if sample is split automatically in test/train)
36// 5) PostTasks (set by user)
37//
38// The split into Pre- and PostTasks is done for speed reason. So, if
39// you calculate a vlue which is not needed for your PreCuts, you can
40// calculate it afterwards, which will speed up execution.
41//
42// void AddPreTask(MTask *t)
43// Add a task which is executed just after reading the events from the file
44// this can be any kind of task or even a task list. You can use dedicated
45// tasks performing some analysis, but also cuts (e.g. MContinue) or a
46// general task to evaluate new parameters (e.g. MParameterCalc)
47//
48// void AddPreTask(const char *rule, const char *name="MWeight")
49// This is a short cut to AddPreTask(MTask*) which will add a
50// MParameterCalc to the list evaluating the given rule and storing the
51// result in a MParameterD with the given name. For the default "MWeight",
52// for example, the value can later be accessed by MWeight.fVal.
53//
54// The same functions are available as
55// void AddPostTask(MTask *t)
56// void AddPostTask(const char *rule, const char *name="MWeight")
57//
58// It is advicable to use these function if the task is not needed for your
59// event selection. The event selection (by number and by PreCuts) is done
60// in between PreTasks and PostTasks, i.e. a task which is not needed for the
61// event selection but needed later is only executed as often as necessary.
62//
63// The event selection (apart from the number of target events) can be setup
64// by the user. Therefore two functions are available
65// void AddPreCut(const char *rule)
66// void AddPreCut(MFilter *f)
67// Both add cuts which are executed after the PreTasks but before the
68// PostTasks. They are executed in both training and testing. To execute cuts
69// only in training or testing use
70// void AddTrainCut(const char *rule)
71// void AddTrainCut(MFilter *f)
72// or
73// void AddTestCut(const char *rule)
74// void AddTestCut(MFilter *f)
75//
76// To weight the events of your train- and test sample the following functions
77// can be used
78// void SetWeights(const char *rule)
79// void SetWeights(MTask *t)
80// This either adds a MParameterCalc to the list of post tasks or a MTask,
81// which is supposed to fill a MParameterD named "MWeight".
82//
83// Last but not least a function if available to add additional tasks to the
84// end of the task list for testing. This can for example be used to add
85// the filling and display of an addition histogram (e.g. add a MFillH)
86// or to do some calculation and write out the result. Basically everything
87// can be done in this way.
88// void AddTestTask(MTask *t)
89// void AddTestTask(const char *rule, const char *name="MWeight")
90//
91//
92// REMARK: Note that the actual behavior might still vary a bit
93// depeding of the implementation in the derived class.
94//
95/////////////////////////////////////////////////////////////////////////////
96#include "MJOptimizeBase.h"
97
98#include <TFile.h>
99#include <TRandom3.h>
100
101#include "MLog.h"
102#include "MLogManip.h"
103
104#include "MFDataPhrase.h"
105#include "MParameterCalc.h"
106
107#include "MStatusDisplay.h"
108
109ClassImp(MJOptimizeBase);
110
111using namespace std;
112
113//------------------------------------------------------------------------
114//
115MJOptimizeBase::MJOptimizeBase() : fDebug(-1), fEnableWeights(kFALSE)
116{
117 if (gRandom && gRandom->IsA()==TRandom::Class())
118 *fLog << warn << "WARNING - gRandom is a TRandom instance. It is highly adviced to use another random generator!" << endl;
119}
120
121//------------------------------------------------------------------------
122//
123// Add a cut which is used to fill the matrix, eg "MMcEvt.fPartId<1.5"
124// (The rule is applied, not inverted: The matrix is filled with
125// the events fullfilling the condition)
126//
127void MJOptimizeBase::AddCut(TList &l, const char *rule)
128{
129 MFilter *f = new MFDataPhrase(rule);
130 f->SetBit(kCanDelete); //FIXME!!!! Why does not any other list delete it???
131 Add(l, f);
132}
133
134//------------------------------------------------------------------------
135//
136// Add an additional parameter (MParameterCalc), eg "0.5", "MWeight"
137// The default container name is "MWeight"
138//
139void MJOptimizeBase::AddPar(TList &l, const char *rule, const char *pname)
140{
141 TString tname(pname);
142 tname += "Calc";
143
144 MParameterCalc *par = new MParameterCalc(rule, tname);
145 par->SetNameParameter(pname);
146// par->SetBit(kCanDelete); //FIXME!!!! MTaskList is deleting it
147 Add(l, par);
148}
149
150//------------------------------------------------------------------------
151//
152// Add a task/cut which is used to fill the matrix. If kCanDelete is set
153// MJOptimize takes the ownership.
154//
155void MJOptimizeBase::Add(TList &l, MTask *f)
156{
157 l.Add(f);
158}
159
160//------------------------------------------------------------------------
161//
162// Add a parameter to the list of parameters. The index in the list is
163// returned.
164//
165// Int_t idx = AddParameter("log10(MHillas.fSize)");
166//
167// Indices are starting with 0.
168//
169Int_t MJOptimizeBase::AddParameter(const char *rule)
170{
171 fRules.Add(new TNamed(rule, ""));
172 return fRules.GetSize()-1;
173}
Note: See TracBrowser for help on using the repository browser.