| 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 "MDataChain.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) : fData0(NULL), fData1(NULL), fNameResult("MinimizationValue") | 
|---|
| 52 | { | 
|---|
| 53 | fName  = name  ? name  : gsDefName.Data(); | 
|---|
| 54 | fTitle = title ? title : gsDefTitle.Data(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | MChisqEval::MChisqEval(MData *y1, const char *name, const char *title) : fData0(NULL), fData1(NULL), fNameResult("MinimizationValue") | 
|---|
| 58 | { | 
|---|
| 59 | fName  = name  ? name  : gsDefName.Data(); | 
|---|
| 60 | fTitle = title ? title : gsDefTitle.Data(); | 
|---|
| 61 | SetY1(y1); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | MChisqEval::MChisqEval(MData *y1, MData *y2, const char *name, const char *title) : fData0(NULL), fData1(NULL), fNameResult("MinimizationValue") | 
|---|
| 65 | { | 
|---|
| 66 | fName  = name  ? name  : gsDefName.Data(); | 
|---|
| 67 | fTitle = title ? title : gsDefTitle.Data(); | 
|---|
| 68 | SetY1(y1); | 
|---|
| 69 | SetY2(y2); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | MChisqEval::~MChisqEval() | 
|---|
| 73 | { | 
|---|
| 74 | if (fData0 && (fData0->TestBit(kCanDelete) || TestBit(kIsOwner))) | 
|---|
| 75 | delete fData0; | 
|---|
| 76 |  | 
|---|
| 77 | if (fData1 && (fData1->TestBit(kCanDelete) || TestBit(kIsOwner))) | 
|---|
| 78 | delete fData1; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | void MChisqEval::SetY1(MData *data) | 
|---|
| 82 | { | 
|---|
| 83 | // Set MC value | 
|---|
| 84 | if (fData0 && (fData0->TestBit(kCanDelete) || TestBit(kIsOwner))) | 
|---|
| 85 | delete fData0; | 
|---|
| 86 | fData0 = data; | 
|---|
| 87 | fData0->SetBit(kCanDelete); | 
|---|
| 88 | AddToBranchList(fData0->GetDataMember()); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void MChisqEval::SetY2(MData *data) | 
|---|
| 92 | { | 
|---|
| 93 | // Set measured/estimated value | 
|---|
| 94 | if (fData1 && (fData1->TestBit(kCanDelete) || TestBit(kIsOwner))) | 
|---|
| 95 | delete fData1; | 
|---|
| 96 | fData1 = data; | 
|---|
| 97 | fData1->SetBit(kCanDelete); | 
|---|
| 98 | AddToBranchList(fData1->GetDataMember()); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void MChisqEval::SetY1(const TString data) | 
|---|
| 102 | { | 
|---|
| 103 | SetY1(new MDataChain(data)); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void MChisqEval::SetY2(const TString data) | 
|---|
| 107 | { | 
|---|
| 108 | SetY2(new MDataChain(data)); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | Int_t MChisqEval::PreProcess(MParList *plist) | 
|---|
| 112 | { | 
|---|
| 113 | fChisq = 0; | 
|---|
| 114 |  | 
|---|
| 115 | if (!fData0) | 
|---|
| 116 | return kFALSE; | 
|---|
| 117 |  | 
|---|
| 118 | if (!fData0->PreProcess(plist)) | 
|---|
| 119 | return kFALSE; | 
|---|
| 120 |  | 
|---|
| 121 | if (fData1) | 
|---|
| 122 | if (!fData1->PreProcess(plist)) | 
|---|
| 123 | return kFALSE; | 
|---|
| 124 |  | 
|---|
| 125 | fResult = (MParameterD*)plist->FindCreateObj("MParameterD", fNameResult); | 
|---|
| 126 | if (!fResult) | 
|---|
| 127 | return kFALSE; | 
|---|
| 128 |  | 
|---|
| 129 | return kTRUE; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | Int_t MChisqEval::Process() | 
|---|
| 133 | { | 
|---|
| 134 | const Double_t y1 = fData0->GetValue(); | 
|---|
| 135 | const Double_t y2 = fData1 ? fData1->GetValue() : 0; | 
|---|
| 136 |  | 
|---|
| 137 | const Double_t dy  = y2-y1; | 
|---|
| 138 | const Double_t err = fData1 ? y1*y2 : 1; | 
|---|
| 139 |  | 
|---|
| 140 | fChisq += dy*dy/err; | 
|---|
| 141 | return kTRUE; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | Int_t MChisqEval::PostProcess() | 
|---|
| 145 | { | 
|---|
| 146 | if (GetNumExecutions()>0) | 
|---|
| 147 | fChisq /= GetNumExecutions(); | 
|---|
| 148 |  | 
|---|
| 149 | fResult->SetVal(fChisq); | 
|---|
| 150 |  | 
|---|
| 151 | *fLog << inf << GetDescriptor() << ": Result=" << fChisq << endl; | 
|---|
| 152 |  | 
|---|
| 153 | return kTRUE; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | void MChisqEval::StreamPrimitive(ostream &out) const | 
|---|
| 157 | { | 
|---|
| 158 | out << "   MChisqEval " << GetUniqueName() << ";"; | 
|---|
| 159 | if (fData0) | 
|---|
| 160 | out << "   " << GetUniqueName() << ".SetY1(\"" << fData0->GetRule() << "\");" << endl; | 
|---|
| 161 | if (fData1) | 
|---|
| 162 | out << "   " << GetUniqueName() << ".SetY1(\"" << fData1->GetRule() << "\");" << endl; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|