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 07/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MChisqEval
|
---|
28 | //
|
---|
29 | // Evaluates a chisq from one or two MParameterD calculated in an eventloop.
|
---|
30 | //
|
---|
31 | /////////////////////////////////////////////////////////////////////////////
|
---|
32 | #include "MChisqEval.h"
|
---|
33 |
|
---|
34 | #include <fstream>
|
---|
35 |
|
---|
36 | #include "MLog.h"
|
---|
37 | #include "MLogManip.h"
|
---|
38 |
|
---|
39 | #include "MDataPhrase.h"
|
---|
40 | #include "MParameters.h" // MParameterD
|
---|
41 |
|
---|
42 | #include "MParList.h"
|
---|
43 |
|
---|
44 | ClassImp(MChisqEval);
|
---|
45 |
|
---|
46 | using namespace std;
|
---|
47 |
|
---|
48 | const TString MChisqEval::gsDefName = "MChisqEval";
|
---|
49 | const TString MChisqEval::gsDefTitle = "Evaluate a chisq";
|
---|
50 |
|
---|
51 | MChisqEval::MChisqEval(const char *name, const char *title)
|
---|
52 | : fData0(NULL), fData1(NULL), fWeight(NULL), fNameResult("MinimizationValue")
|
---|
53 | {
|
---|
54 | fName = name ? name : gsDefName.Data();
|
---|
55 | fTitle = title ? title : gsDefTitle.Data();
|
---|
56 | }
|
---|
57 |
|
---|
58 | MChisqEval::MChisqEval(MData *y1, const char *name, const char *title)
|
---|
59 | : fData0(NULL), fData1(NULL), fWeight(NULL), fNameResult("MinimizationValue")
|
---|
60 | {
|
---|
61 | fName = name ? name : gsDefName.Data();
|
---|
62 | fTitle = title ? title : gsDefTitle.Data();
|
---|
63 | SetY1(y1);
|
---|
64 | }
|
---|
65 |
|
---|
66 | MChisqEval::MChisqEval(MData *y1, MData *y2, const char *name, const char *title)
|
---|
67 | : fData0(NULL), fData1(NULL), fWeight(NULL), fNameResult("MinimizationValue")
|
---|
68 | {
|
---|
69 | fName = name ? name : gsDefName.Data();
|
---|
70 | fTitle = title ? title : gsDefTitle.Data();
|
---|
71 | SetY1(y1);
|
---|
72 | SetY2(y2);
|
---|
73 | }
|
---|
74 |
|
---|
75 | MChisqEval::~MChisqEval()
|
---|
76 | {
|
---|
77 | if (fData0 && (fData0->TestBit(kCanDelete) || TestBit(kIsOwner)))
|
---|
78 | delete fData0;
|
---|
79 |
|
---|
80 | if (fData1 && (fData1->TestBit(kCanDelete) || TestBit(kIsOwner)))
|
---|
81 | delete fData1;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void MChisqEval::SetY1(MData *data)
|
---|
85 | {
|
---|
86 | // Set MC value
|
---|
87 | if (fData0 && (fData0->TestBit(kCanDelete) || TestBit(kIsOwner)))
|
---|
88 | delete fData0;
|
---|
89 | fData0 = data;
|
---|
90 | fData0->SetBit(kCanDelete);
|
---|
91 | AddToBranchList(fData0->GetDataMember());
|
---|
92 | }
|
---|
93 |
|
---|
94 | void MChisqEval::SetY2(MData *data)
|
---|
95 | {
|
---|
96 | // Set measured/estimated value
|
---|
97 | if (fData1 && (fData1->TestBit(kCanDelete) || TestBit(kIsOwner)))
|
---|
98 | delete fData1;
|
---|
99 | fData1 = data;
|
---|
100 | fData1->SetBit(kCanDelete);
|
---|
101 | AddToBranchList(fData1->GetDataMember());
|
---|
102 | }
|
---|
103 |
|
---|
104 | void MChisqEval::SetY1(const TString data)
|
---|
105 | {
|
---|
106 | SetY1(new MDataPhrase(data));
|
---|
107 | }
|
---|
108 |
|
---|
109 | void MChisqEval::SetY2(const TString data)
|
---|
110 | {
|
---|
111 | SetY2(new MDataPhrase(data));
|
---|
112 | }
|
---|
113 |
|
---|
114 | Int_t MChisqEval::PreProcess(MParList *plist)
|
---|
115 | {
|
---|
116 | fChisq = 0;
|
---|
117 | fSumW = 0;
|
---|
118 |
|
---|
119 | if (!fData0)
|
---|
120 | return kFALSE;
|
---|
121 |
|
---|
122 | if (!fData0->PreProcess(plist))
|
---|
123 | return kFALSE;
|
---|
124 |
|
---|
125 | if (fData1)
|
---|
126 | if (!fData1->PreProcess(plist))
|
---|
127 | return kFALSE;
|
---|
128 |
|
---|
129 | if (!fNameWeight.IsNull())
|
---|
130 | {
|
---|
131 | fWeight = (MParameterD*)plist->FindObject(fNameWeight, "MParameterD");
|
---|
132 | if (!fWeight)
|
---|
133 | return kFALSE;
|
---|
134 | }
|
---|
135 |
|
---|
136 | fResult = (MParameterD*)plist->FindCreateObj("MParameterD", fNameResult);
|
---|
137 | if (!fResult)
|
---|
138 | return kFALSE;
|
---|
139 |
|
---|
140 | return kTRUE;
|
---|
141 | }
|
---|
142 |
|
---|
143 | Int_t MChisqEval::Process()
|
---|
144 | {
|
---|
145 | const Double_t y1 = fData0->GetValue();
|
---|
146 | const Double_t y2 = fData1 ? fData1->GetValue() : 0;
|
---|
147 |
|
---|
148 | const Double_t dy = y2-y1;
|
---|
149 | const Double_t er = fData1 ? y1*y2 : 1;
|
---|
150 |
|
---|
151 | const Double_t w = fWeight ? fWeight->GetVal() : 1;
|
---|
152 |
|
---|
153 | fChisq += w*dy*dy/er;
|
---|
154 | fSumW += w;
|
---|
155 |
|
---|
156 | return kTRUE;
|
---|
157 | }
|
---|
158 |
|
---|
159 | Int_t MChisqEval::PostProcess()
|
---|
160 | {
|
---|
161 | if (GetNumExecutions()>0)
|
---|
162 | fChisq /= fSumW;
|
---|
163 |
|
---|
164 | fResult->SetVal(fChisq);
|
---|
165 |
|
---|
166 | *fLog << inf << GetDescriptor() << ": Result=" << fChisq << endl;
|
---|
167 |
|
---|
168 | return kTRUE;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void MChisqEval::StreamPrimitive(ostream &out) const
|
---|
172 | {
|
---|
173 | out << " MChisqEval " << GetUniqueName() << ";";
|
---|
174 | if (fData0)
|
---|
175 | out << " " << GetUniqueName() << ".SetY1(\"" << fData0->GetRule() << "\");" << endl;
|
---|
176 | if (fData1)
|
---|
177 | out << " " << GetUniqueName() << ".SetY1(\"" << fData1->GetRule() << "\");" << endl;
|
---|
178 | }
|
---|
179 |
|
---|