source: trunk/MagicSoft/Mars/manalysis/MParameterCalc.cc@ 8785

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