| 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 |
|
|---|
| 60 | ClassImp(MMultiDimDistCalc);
|
|---|
| 61 |
|
|---|
| 62 | static const TString gsDefName = "MMultiDimDistCalc";
|
|---|
| 63 | static 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 | //
|
|---|
| 70 | MMultiDimDistCalc::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 | //
|
|---|
| 89 | MMultiDimDistCalc::~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 | //
|
|---|
| 105 | Bool_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 | //
|
|---|
| 172 | Bool_t MMultiDimDistCalc::Process()
|
|---|
| 173 | {
|
|---|
| 174 | const Double_t ncols = fMGammas->GetM().GetNcols();
|
|---|
| 175 |
|
|---|
| 176 | TVector event(ncols);
|
|---|
| 177 |
|
|---|
| 178 | for (int i=0; i<fData->GetNumEntries(); i++)
|
|---|
| 179 | event(i) = (*fData)(i);
|
|---|
| 180 |
|
|---|
| 181 | /*
|
|---|
| 182 | Int_t n=0;
|
|---|
| 183 | TIter Next(fData);
|
|---|
| 184 | MData *data = NULL;
|
|---|
| 185 | while ((data=(MData*)Next()))
|
|---|
| 186 | event(n++) = data->GetValue();
|
|---|
| 187 | */
|
|---|
| 188 |
|
|---|
| 189 | Double_t numg = fNum;
|
|---|
| 190 | Double_t numh = fNum;
|
|---|
| 191 | if (fNum==0)
|
|---|
| 192 | {
|
|---|
| 193 | numg = fMGammas->GetM().GetNrows();
|
|---|
| 194 | numh = fMHadrons->GetM().GetNrows();
|
|---|
| 195 | }
|
|---|
| 196 | if (fUseKernel)
|
|---|
| 197 | {
|
|---|
| 198 | numg = -numg;
|
|---|
| 199 | numh = -numh;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | const Double_t dg = fMGammas->CalcDist(event, numg);
|
|---|
| 203 | const Double_t dh = fMHadrons->CalcDist(event, numh);
|
|---|
| 204 |
|
|---|
| 205 | if (dg<0 || dh<0)
|
|---|
| 206 | {
|
|---|
| 207 | *fLog << err << "MHMatrix::CalcDist failed (dg=" << dg << ", dh=" << dh << ")... aborting" << endl;
|
|---|
| 208 | return kERROR;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | fHadronness->SetHadronness(dg==0 ? 0 : exp(-dh/dg));
|
|---|
| 212 | fHadronness->SetReadyToSave();
|
|---|
| 213 |
|
|---|
| 214 | return kTRUE;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | void MMultiDimDistCalc::StreamPrimitive(ofstream &out) const
|
|---|
| 218 | {
|
|---|
| 219 | out << " MMultiDimDist " << GetUniqueName();
|
|---|
| 220 |
|
|---|
| 221 | if (fName!=gsDefName || fTitle!=gsDefTitle)
|
|---|
| 222 | {
|
|---|
| 223 | out << "(\"" << fName << "\"";
|
|---|
| 224 | if (fTitle!=gsDefTitle)
|
|---|
| 225 | out << ", \"" << fTitle << "\")";
|
|---|
| 226 | }
|
|---|
| 227 | out << ";" << endl;
|
|---|
| 228 |
|
|---|
| 229 | if (fHadronnessName!="MHadronness")
|
|---|
| 230 | out << " " << GetUniqueName() << ".SetHadronnessName(\"" << fHadronnessName << "\");" << endl;
|
|---|
| 231 | if (fNum!=0)
|
|---|
| 232 | out << " " << GetUniqueName() << ".SetUseNumRows(" << fNum << ");" << endl;
|
|---|
| 233 | if (fUseKernel)
|
|---|
| 234 | out << " " << GetUniqueName() << ".SetUseKernelMethod();" << endl;
|
|---|
| 235 | }
|
|---|