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

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