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

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