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

Last change on this file since 1521 was 1489, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 6.5 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 kernel function to calculate the distance use:
37// MMultiDimDistCalc::SetUseKernelMethod();
38// * To use only the n next neighbors for your calculation use:
39// MMultiDimDistCalc::SetUseNumRows(n);
40// * To use all reference events set the number to 0 <default>
41//
42////////////////////////////////////////////////////////////////////////////
43#include "MMultiDimDistCalc.h"
44
45#include <fstream.h>
46
47#include "MHMatrix.h" // must be before MLogManip.h
48
49#include "MLog.h"
50#include "MLogManip.h"
51
52#include "MParList.h"
53#include "MDataChain.h"
54#include "MDataArray.h"
55
56#include "MHadroness.h"
57
58ClassImp(MMultiDimDistCalc);
59
60static const TString gsDefName = "MMultiDimDistCalc";
61static const TString gsDefTitle = "Composite Probabilities Loop 1/2";
62// --------------------------------------------------------------------------
63//
64// Setup histograms and the number of distances which are used for
65// avaraging in CalcDist
66//
67MMultiDimDistCalc::MMultiDimDistCalc(const char *name, const char *title)
68 : fNum(0), fUseKernel(kFALSE), fData(NULL)
69{
70 //
71 // set the name and title of this object
72 //
73 fName = name ? name : gsDefName.Data();
74 fTitle = title ? title : gsDefTitle.Data();
75
76 /*
77 fData = new TList;
78 fData->SetOwner();
79 */
80}
81
82// --------------------------------------------------------------------------
83//
84// Delete the data chains
85//
86MMultiDimDistCalc::~MMultiDimDistCalc()
87{
88 // delete fData;
89}
90
91// --------------------------------------------------------------------------
92//
93// Needs:
94// - MatrixGammas [MHMatrix]
95// - MatrixHadrons {MHMatrix]
96// - MHadroness
97// - all data containers used to build the matrixes
98//
99// The matrix object can be filles using MFillH. And must be of the same
100// number of columns (with the same meaning).
101//
102Bool_t MMultiDimDistCalc::PreProcess(MParList *plist)
103{
104 fMGammas = (MHMatrix*)plist->FindObject("MatrixGammas", "MHMatrix");
105 if (!fMGammas)
106 {
107 *fLog << err << dbginf << "MatrixGammas [MHMatrix] not found... aborting." << endl;
108 return kFALSE;
109 }
110
111 fMHadrons = (MHMatrix*)plist->FindObject("MatrixHadrons", "MHMatrix");
112 if (!fMHadrons)
113 {
114 *fLog << err << dbginf << "MatrixHadrons [MHMatrix] not found... aborting." << endl;
115 return kFALSE;
116 }
117
118 if (fMGammas->GetM().GetNcols() != fMHadrons->GetM().GetNcols())
119 {
120 *fLog << err << dbginf << "Error matrices have different numbers of columns... aborting." << endl;
121 return kFALSE;
122 }
123
124 /*
125 TIter Next(fMGammas->GetRules());
126 TObject *data=NULL;
127 while ((data=Next()))
128 {
129 MDataChain *chain = new MDataChain(data->GetName());
130 if (!chain->PreProcess(plist))
131 {
132 delete chain;
133 return kFALSE;
134 }
135 fData->Add(chain);
136 }
137 */
138 fData = fMGammas->GetColumns();
139 if (!fData)
140 {
141 *fLog << err << dbginf << "Error matrix doesn't contain columns... aborting." << endl;
142 return kFALSE;
143 }
144
145 if (!fData->PreProcess(plist))
146 {
147 *fLog << err << dbginf << "PreProcessing of the MDataArray failed for the columns failed... aborting." << endl;
148 return kFALSE;
149 }
150
151 fHadroness = (MHadroness*)plist->FindCreateObj("MHadroness");
152 if (!fHadroness)
153 return kFALSE;
154
155 return kTRUE;
156}
157
158// --------------------------------------------------------------------------
159//
160// Evaluates the avarage over the fNum shortest distances in a
161// multidimensional space to a matrix (set of vectors) describing
162// gammas and hadrons.
163// The hadroness of the event is defines as the avarage distance to the
164// set of gammas (dg) divided by the avarage distance to the set of
165// hadrons (dh). Because this value is in teh range [0, inf] it is
166// transformed into [0,1] by:
167// H = exp(-dh/dg);
168//
169Bool_t MMultiDimDistCalc::Process()
170{
171 const Double_t ncols = fMGammas->GetM().GetNcols();
172
173 TVector event(ncols);
174
175 for (int i=0; i<fData->GetNumEntries(); i++)
176 event(i) = (*fData)(i);
177
178 /*
179 Int_t n=0;
180 TIter Next(fData);
181 MData *data = NULL;
182 while ((data=(MData*)Next()))
183 event(n++) = data->GetValue();
184 */
185
186 Double_t numg = fNum;
187 Double_t numh = fNum;
188 if (fNum==0)
189 {
190 numg = fMGammas->GetM().GetNrows();
191 numh = fMHadrons->GetM().GetNrows();
192 }
193 if (fUseKernel)
194 {
195 numg = -numg;
196 numh = -numh;
197 }
198
199 Double_t dg = fMGammas->CalcDist(event, numg);
200 Double_t dh = fMHadrons->CalcDist(event, numh);
201
202 //fHadroness->SetHadroness(dg/(dg+dh));
203 fHadroness->SetHadroness(exp(-dh/dg));
204
205 return kTRUE;
206}
207
208void MMultiDimDistCalc::StreamPrimitive(ofstream &out) const
209{
210 out << " MMultiDimDist " << GetUniqueName();
211
212 if (fName!=gsDefName || fTitle!=gsDefTitle)
213 {
214 out << "(\"" << fName << "\"";
215 if (fTitle!=gsDefTitle)
216 out << ", \"" << fTitle << "\")";
217 }
218 out << ";" << endl;
219
220 if (fNum!=0)
221 out << " " << GetUniqueName() << ".SetUseNumRows(" << fNum << ");" << endl;
222 if (fUseKernel)
223 out << " " << GetUniqueName() << ".SetUseKernelMethod();" << endl;
224}
Note: See TracBrowser for help on using the repository browser.