source: trunk/Mars/manalysis/MParameterCalc.cc@ 13881

Last change on this file since 13881 was 8910, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.7 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 6/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2007
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MParameterCalc
28//
29// Task to calculate a parameter stored ina MParametrD by a rule, eg:
30//
31// MParameterCalc est;
32// est.SetRule("0.5 + (1.1*MHillas.fLength) + (2.2*MHillasSrc.fDist) + (3.3*MHillas.fSize) +"
33// "(4.4*MHillas.fSize*MHillas.fLength) + (5.5*MHillasSrc.fDist*MHillas.fLength)");
34//
35// For description of rules, see MDataPhrase.
36//
37// Output:
38// fNameParameter [MParameterD] <default=MParameterD>
39//
40/////////////////////////////////////////////////////////////////////////////
41#include "MParameterCalc.h"
42
43#include <TMath.h>
44
45#include "MParList.h"
46
47#include "MDataPhrase.h"
48#include "MParameters.h"
49
50#include "MLog.h"
51#include "MLogManip.h"
52
53ClassImp(MParameterCalc);
54
55using namespace std;
56
57// --------------------------------------------------------------------------
58//
59// Default constructor. Initialize fData with default rule "MMcEvt.fEnergy"
60//
61MParameterCalc::MParameterCalc(const char *def, const char *name, const char *title)
62 : fData(0), fParameter(0), fNameParameter("MParameterD")
63{
64 fName = name ? name : "MParameterCalc";
65 fTitle = title ? title : "Task to calculate a MParameterD";
66
67 fData = new MDataPhrase(def);
68}
69
70// --------------------------------------------------------------------------
71//
72// delete fData
73//
74MParameterCalc::~MParameterCalc()
75{
76 delete fData;
77}
78
79// --------------------------------------------------------------------------
80//
81// Delete fData. Initialize a new MDataPhrase with rule.
82// Returns if fData->IsValid()
83//
84Bool_t MParameterCalc::SetRule(const char *rule)
85{
86 delete fData;
87 fData = new MDataPhrase(rule);
88
89 return fData->IsValid();
90}
91
92// --------------------------------------------------------------------------
93//
94// Forwards SetVariables to fData to allow optimizations.
95//
96void MParameterCalc::SetVariables(const TArrayD &arr)
97{
98 fData->SetVariables(arr);
99}
100
101// --------------------------------------------------------------------------
102//
103// FindCreate "MEnergyEst"
104// PreProcess fData.
105//
106Int_t MParameterCalc::PreProcess(MParList *plist)
107{
108 memset(fCounter, 0, sizeof(ULong_t)*2);
109
110 fParameter = (MParameterD*)plist->FindCreateObj("MParameterD", fNameParameter);
111 if (!fParameter)
112 return kFALSE;
113
114 *fLog << inf << "Rule for " << fNameParameter << ": " << fData->GetRule() << endl;
115
116 if (!fData->PreProcess(plist))
117 return kFALSE;
118
119 return kTRUE;
120}
121
122// --------------------------------------------------------------------------
123//
124// Get value from fData and set it to fEnergy. SetReadyToSave for fEnergy.
125// Return kCONTINUE if value is NaN (Not a Number)
126//
127Int_t MParameterCalc::Process()
128{
129 const Double_t val = fData->GetValue();
130 if (TMath::IsNaN(val))
131 {
132 fCounter[0]++;
133 return kCONTINUE;
134 }
135 if (!TMath::Finite(val))
136 {
137 fCounter[1]++;
138 return kCONTINUE;
139 }
140
141 fParameter->SetVal(val);
142 fParameter->SetReadyToSave();
143 return kTRUE;
144}
145
146// --------------------------------------------------------------------------
147//
148// Print some execution statistics
149//
150Int_t MParameterCalc::PostProcess()
151{
152 if (GetNumExecutions()==0)
153 return kTRUE;
154
155 *fLog << inf << endl;
156 *fLog << GetDescriptor() << " execution statistics:" << endl;
157 PrintSkipped(fCounter[0], "Skipped due to NaN-result (Not a Number)");
158 PrintSkipped(fCounter[1], "Skipped due to inf-result (infinite)");
159 *fLog << endl;
160
161 return kTRUE;
162}
163
164// --------------------------------------------------------------------------
165//
166// Print the rule used for energy estimation
167//
168void MParameterCalc::Print(Option_t *o) const
169{
170 *fLog << all << GetDescriptor() << ":";
171 if (!fData)
172 *fLog << " <n/a>" << endl;
173 else
174 *fLog << endl << fData->GetRule() << endl;
175}
176
177// --------------------------------------------------------------------------
178//
179// Check for corresponding entries in resource file and setup filters.
180// Avoid trailing 0's!
181//
182// Example:
183// test.C:
184// MParameterCalc est("MyEstimator");
185//
186// test.rc:
187// MyEstimator.Rule: {0} + {1}
188// MyEstimator.NameOutput: MParameterD
189// MyEstimator.0: log10(MHillas.fSize)
190// MyEstimator.1: 5.5
191//
192// For more details see MDataPhrase::ReadEnv
193//
194Int_t MParameterCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
195{
196 MDataPhrase *f = new MDataPhrase;
197 f->SetName(fName);
198
199 const Bool_t rc = f->ReadEnv(env, prefix, print);
200 if (rc!=kTRUE)
201 {
202 delete f;
203 return rc;
204 }
205
206 delete fData;
207 fData = f;
208
209 if (!f->HasValidRule())
210 {
211 *fLog << err << "MParameterCalc::ReadEnv - ERROR: Inavlid rule from resource file." << endl;
212 return kERROR;
213 }
214
215 if (IsEnvDefined(env, prefix, "NameOutput", print))
216 fNameParameter = GetEnvValue(env, prefix, "NameOutput", fNameParameter);
217
218 return kTRUE;
219}
Note: See TracBrowser for help on using the repository browser.