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 "MHadronness.h"
|
---|
57 |
|
---|
58 | ClassImp(MMultiDimDistCalc);
|
---|
59 |
|
---|
60 | static const TString gsDefName = "MMultiDimDistCalc";
|
---|
61 | static 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 | //
|
---|
67 | MMultiDimDistCalc::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 | //
|
---|
86 | MMultiDimDistCalc::~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 | //
|
---|
102 | Bool_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 | fHadronness = (MHadronness*)plist->FindCreateObj("MHadronness");
|
---|
152 | if (!fHadronness)
|
---|
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 | //
|
---|
169 | Bool_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 | if (dg<0 || dh<0)
|
---|
203 | {
|
---|
204 | *fLog << err << "MHMatrix::CalcDist failed (dg=" << dg << ", dh=" << dh << ")... aborting" << endl;
|
---|
205 | return kFALSE;
|
---|
206 | }
|
---|
207 |
|
---|
208 | //fHadronness->SetHadronness(dg/(dg+dh));
|
---|
209 | fHadronness->SetHadronness(exp(-dh/dg));
|
---|
210 |
|
---|
211 | return kTRUE;
|
---|
212 | }
|
---|
213 |
|
---|
214 | void MMultiDimDistCalc::StreamPrimitive(ofstream &out) const
|
---|
215 | {
|
---|
216 | out << " MMultiDimDist " << GetUniqueName();
|
---|
217 |
|
---|
218 | if (fName!=gsDefName || fTitle!=gsDefTitle)
|
---|
219 | {
|
---|
220 | out << "(\"" << fName << "\"";
|
---|
221 | if (fTitle!=gsDefTitle)
|
---|
222 | out << ", \"" << fTitle << "\")";
|
---|
223 | }
|
---|
224 | out << ";" << endl;
|
---|
225 |
|
---|
226 | if (fNum!=0)
|
---|
227 | out << " " << GetUniqueName() << ".SetUseNumRows(" << fNum << ");" << endl;
|
---|
228 | if (fUseKernel)
|
---|
229 | out << " " << GetUniqueName() << ".SetUseKernelMethod();" << endl;
|
---|
230 | }
|
---|