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

Last change on this file since 6910 was 5832, checked in by tbretz, 20 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 <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 "MDataChain.h"
58#include "MDataArray.h"
59
60#include "MHadronness.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// Delete the data chains
92//
93MMultiDimDistCalc::~MMultiDimDistCalc()
94{
95 // delete fData;
96}
97
98// --------------------------------------------------------------------------
99//
100// Needs:
101// - MatrixGammas [MHMatrix]
102// - MatrixHadrons {MHMatrix]
103// - MHadroness
104// - all data containers used to build the matrixes
105//
106// The matrix object can be filles using MFillH. And must be of the same
107// number of columns (with the same meaning).
108//
109Int_t MMultiDimDistCalc::PreProcess(MParList *plist)
110{
111 fMGammas = (MHMatrix*)plist->FindObject("MatrixGammas", "MHMatrix");
112 if (!fMGammas)
113 {
114 *fLog << err << dbginf << "MatrixGammas [MHMatrix] not found... aborting." << endl;
115 return kFALSE;
116 }
117
118 fMHadrons = (MHMatrix*)plist->FindObject("MatrixHadrons", "MHMatrix");
119 if (!fMHadrons)
120 {
121 *fLog << err << dbginf << "MatrixHadrons [MHMatrix] not found... aborting." << endl;
122 return kFALSE;
123 }
124
125 if (fMGammas->GetM().GetNcols() != fMHadrons->GetM().GetNcols())
126 {
127 *fLog << err << dbginf << "Error matrices have different numbers of columns... aborting." << endl;
128 return kFALSE;
129 }
130
131 /*
132 TIter Next(fMGammas->GetRules());
133 TObject *data=NULL;
134 while ((data=Next()))
135 {
136 MDataChain *chain = new MDataChain(data->GetName());
137 if (!chain->PreProcess(plist))
138 {
139 delete chain;
140 return kFALSE;
141 }
142 fData->Add(chain);
143 }
144 */
145 fData = fMGammas->GetColumns();
146 if (!fData)
147 {
148 *fLog << err << dbginf << "Error matrix doesn't contain columns... aborting." << endl;
149 return kFALSE;
150 }
151
152 if (!fData->PreProcess(plist))
153 {
154 *fLog << err << dbginf << "PreProcessing of the MDataArray failed for the columns failed... aborting." << endl;
155 return kFALSE;
156 }
157
158 fHadronness = (MHadronness*)plist->FindCreateObj("MHadronness", fHadronnessName);
159 if (!fHadronness)
160 return kFALSE;
161
162 return kTRUE;
163}
164
165// --------------------------------------------------------------------------
166//
167// Evaluates the avarage over the fNum shortest distances in a
168// multidimensional space to a matrix (set of vectors) describing
169// gammas and hadrons.
170// The hadroness of the event is defines as the avarage distance to the
171// set of gammas (dg) divided by the avarage distance to the set of
172// hadrons (dh). Because this value is in teh range [0, inf] it is
173// transformed into [0,1] by:
174// H = exp(-dh/dg);
175//
176Int_t MMultiDimDistCalc::Process()
177{
178 // first copy the data from the data array to a vector event
179 TVector event;
180 *fData >> event;
181
182 Int_t numg = fNum;
183 Int_t numh = fNum;
184 if (fNum==0)
185 {
186 numg = fMGammas->GetM().GetNrows();
187 numh = fMHadrons->GetM().GetNrows();
188 }
189 if (fUseKernel)
190 {
191 numg = -numg;
192 numh = -numh;
193 }
194
195 const Double_t dg = fMGammas->CalcDist(event, numg);
196 const Double_t dh = fMHadrons->CalcDist(event, numh);
197
198 if (dg<0 || dh<0)
199 {
200 *fLog << err << "MHMatrix::CalcDist failed (dg=" << dg << ", dh=" << dh << ")... aborting" << endl;
201 return kERROR;
202 }
203
204 fHadronness->SetHadronness(dg==0 ? 0 : exp(-dh/dg));
205 fHadronness->SetReadyToSave();
206
207 return kTRUE;
208}
209
210void MMultiDimDistCalc::StreamPrimitive(ofstream &out) const
211{
212 out << " MMultiDimDist " << GetUniqueName();
213
214 if (fName!=gsDefName || fTitle!=gsDefTitle)
215 {
216 out << "(\"" << fName << "\"";
217 if (fTitle!=gsDefTitle)
218 out << ", \"" << fTitle << "\")";
219 }
220 out << ";" << endl;
221
222 if (fHadronnessName!="MHadronness")
223 out << " " << GetUniqueName() << ".SetHadronnessName(\"" << fHadronnessName << "\");" << endl;
224 if (fNum!=0)
225 out << " " << GetUniqueName() << ".SetUseNumRows(" << fNum << ");" << endl;
226 if (fUseKernel)
227 out << " " << GetUniqueName() << ".SetUseKernelMethod();" << endl;
228}
Note: See TracBrowser for help on using the repository browser.