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

Last change on this file since 7443 was 7150, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 5.4 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-2005
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 MDataChain.
36//
37// Output:
38// MEnergyEst
39//
40/////////////////////////////////////////////////////////////////////////////
41#include "MParameterCalc.h"
42
43#include "MParList.h"
44
45#include "MDataChain.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 MDataChain(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 MDataChain with rule.
80// Returns if fData->IsValid()
81//
82Bool_t MParameterCalc::SetRule(const char *rule)
83{
84 delete fData;
85 fData = new MDataChain(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: " << 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
144Int_t MParameterCalc::PostProcess()
145{
146 if (GetNumExecutions()==0)
147 return kTRUE;
148
149 *fLog << inf << endl;
150 *fLog << GetDescriptor() << " execution statistics:" << endl;
151 PrintSkipped(fCounter[0], "Skipped due to NaN-result (Not a Number)");
152 PrintSkipped(fCounter[1], "Skipped due to inf-result (infinite)");
153 *fLog << endl;
154
155 return kTRUE;
156}
157
158// --------------------------------------------------------------------------
159//
160// Print the rule used for energy estimation
161//
162void MParameterCalc::Print(Option_t *o) const
163{
164 *fLog << all << GetDescriptor() << ":";
165 if (!fData)
166 *fLog << " <n/a>" << endl;
167 else
168 *fLog << endl << fData->GetRule() << endl;
169}
170
171// --------------------------------------------------------------------------
172//
173// Check for corresponding entries in resource file and setup filters.
174// Avoid trailing 0's!
175//
176// Example:
177// test.C:
178// MParameterCalc est("MyEstimator");
179//
180// test.rc:
181// MyEstimator.Rule: {0} + {1}
182// MyEstimator.0: log10(MHillas.fSize)
183// MyEstimator.1: 5.5
184//
185// For more details see MDataChain::ReadEnv
186//
187Int_t MParameterCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
188{
189 MDataChain *f = new MDataChain;
190 f->SetName(fName);
191
192 const Bool_t rc = f->ReadEnv(env, prefix, print);
193 if (rc!=kTRUE)
194 {
195 delete f;
196 return rc;
197 }
198
199 delete fData;
200 fData = f;
201
202 if (!fData->IsValid())
203 {
204 *fLog << err << "MParameterCalc::ReadEnv - ERROR: Inavlid rule from resource file." << endl;
205 return kERROR;
206 }
207
208 return kTRUE;
209}
Note: See TracBrowser for help on using the repository browser.