source: trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc@ 8246

Last change on this file since 8246 was 8075, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 6.6 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, 5/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Rudy Bock, 5/2002 <mailto:rkb@mppmu.mpg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2002
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MMultiDimDistCalc
29//
30// Calculated a multidimensional distance. It calculates the distance to
31// all vectors in a given matrix describing Gammas and another one
32// describing Hadrons (non gammas). The shortest distances are avaraged.
33// How many distances are used for avaraging can be specified in the
34// constructor.
35//
36// * If you want to use the nearest neighbor function for calculation use:
37// MMultiDimDistCalc::SetUseKernelMethod(kFALSE);
38// * If you want to use the kernel function for calculation use:
39// MMultiDimDistCalc::SetUseKernelMethod(kTRUE); <default>
40// * To use only the n next neighbors for your calculation use:
41// MMultiDimDistCalc::SetUseNumRows(n);
42// * To use all reference events set the number to 0 <default>
43//
44////////////////////////////////////////////////////////////////////////////
45#include "MMultiDimDistCalc.h"
46
47#include <fstream>
48
49#include <TVector.h>
50
51#include "MHMatrix.h" // must be before MLogManip.h
52
53#include "MLog.h"
54#include "MLogManip.h"
55
56#include "MParList.h"
57//#include "MDataPhrase.h"
58#include "MDataArray.h"
59
60#include "MParameters.h"
61
62ClassImp(MMultiDimDistCalc);
63
64using namespace std;
65
66static const TString gsDefName = "MMultiDimDistCalc";
67static const TString gsDefTitle = "Calculate Hadronness with Nearest Neighbor/Kernel Method";
68
69// --------------------------------------------------------------------------
70//
71// Setup histograms and the number of distances which are used for
72// avaraging in CalcDist
73//
74MMultiDimDistCalc::MMultiDimDistCalc(const char *name, const char *title)
75 : fNum(0), fUseKernel(kTRUE), fHadronnessName("MHadronness"), fData(NULL)
76{
77 //
78 // set the name and title of this object
79 //
80 fName = name ? name : gsDefName.Data();
81 fTitle = title ? title : gsDefTitle.Data();
82
83 /*
84 fData = new TList;
85 fData->SetOwner();
86 */
87}
88
89// --------------------------------------------------------------------------
90//
91// Needs:
92// - MatrixGammas [MHMatrix]
93// - MatrixHadrons {MHMatrix]
94// - MHadroness
95// - all data containers used to build the matrixes
96//
97// The matrix object can be filles using MFillH. And must be of the same
98// number of columns (with the same meaning).
99//
100Int_t MMultiDimDistCalc::PreProcess(MParList *plist)
101{
102 fMGammas = (MHMatrix*)plist->FindObject("MatrixGammas", "MHMatrix");
103 if (!fMGammas)
104 {
105 *fLog << err << dbginf << "MatrixGammas [MHMatrix] not found... aborting." << endl;
106 return kFALSE;
107 }
108
109 fMHadrons = (MHMatrix*)plist->FindObject("MatrixHadrons", "MHMatrix");
110 if (!fMHadrons)
111 {
112 *fLog << err << dbginf << "MatrixHadrons [MHMatrix] not found... aborting." << endl;
113 return kFALSE;
114 }
115
116 if (fMGammas->GetM().GetNcols() != fMHadrons->GetM().GetNcols())
117 {
118 *fLog << err << dbginf << "Error matrices have different numbers of columns... aborting." << endl;
119 return kFALSE;
120 }
121
122 /*
123 TIter Next(fMGammas->GetRules());
124 TObject *data=NULL;
125 while ((data=Next()))
126 {
127 MDataPhrase *chain = new MDataPhrase(data->GetName());
128 if (!chain->PreProcess(plist))
129 {
130 delete chain;
131 return kFALSE;
132 }
133 fData->Add(chain);
134 }
135 */
136 fData = fMGammas->GetColumns();
137 if (!fData)
138 {
139 *fLog << err << dbginf << "Error matrix doesn't contain columns... aborting." << endl;
140 return kFALSE;
141 }
142
143 if (!fData->PreProcess(plist))
144 {
145 *fLog << err << dbginf << "PreProcessing of the MDataArray failed for the columns failed... aborting." << endl;
146 return kFALSE;
147 }
148
149 fHadronness = (MParameterD*)plist->FindCreateObj("MParameterD", fHadronnessName);
150 if (!fHadronness)
151 return kFALSE;
152
153 return kTRUE;
154}
155
156// --------------------------------------------------------------------------
157//
158// Evaluates the avarage over the fNum shortest distances in a
159// multidimensional space to a matrix (set of vectors) describing
160// gammas and hadrons.
161// The hadroness of the event is defines as the avarage distance to the
162// set of gammas (dg) divided by the avarage distance to the set of
163// hadrons (dh). Because this value is in teh range [0, inf] it is
164// transformed into [0,1] by:
165// H = exp(-dh/dg);
166//
167Int_t MMultiDimDistCalc::Process()
168{
169 // first copy the data from the data array to a vector event
170 TVector event;
171 *fData >> event;
172
173 Int_t numg = fNum;
174 Int_t numh = fNum;
175 if (fNum==0)
176 {
177 numg = fMGammas->GetM().GetNrows();
178 numh = fMHadrons->GetM().GetNrows();
179 }
180 if (fUseKernel)
181 {
182 numg = -numg;
183 numh = -numh;
184 }
185
186 const Double_t dg = fMGammas->CalcDist(event, numg);
187 const Double_t dh = fMHadrons->CalcDist(event, numh);
188
189 if (dg<0 || dh<0)
190 {
191 *fLog << err << "MHMatrix::CalcDist failed (dg=" << dg << ", dh=" << dh << ")... aborting" << endl;
192 return kERROR;
193 }
194
195 fHadronness->SetVal(dg==0 ? 0 : exp(-dh/dg));
196 fHadronness->SetReadyToSave();
197
198 return kTRUE;
199}
200
201void MMultiDimDistCalc::StreamPrimitive(ostream &out) const
202{
203 out << " MMultiDimDist " << GetUniqueName();
204
205 if (fName!=gsDefName || fTitle!=gsDefTitle)
206 {
207 out << "(\"" << fName << "\"";
208 if (fTitle!=gsDefTitle)
209 out << ", \"" << fTitle << "\")";
210 }
211 out << ";" << endl;
212
213 if (fHadronnessName!="MHadronness")
214 out << " " << GetUniqueName() << ".SetHadronnessName(\"" << fHadronnessName << "\");" << endl;
215 if (fNum!=0)
216 out << " " << GetUniqueName() << ".SetUseNumRows(" << fNum << ");" << endl;
217 if (fUseKernel)
218 out << " " << GetUniqueName() << ".SetUseKernelMethod();" << endl;
219}
220
Note: See TracBrowser for help on using the repository browser.