| 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    12/2000 <mailto:tbretz@uni-sw.gwdg.de> | 
|---|
| 19 | !   Author(s): Harald Kornmayer 1/2001 | 
|---|
| 20 | !   Author(s): Rudolf Bock     10/2001 <mailto:Rudolf.Bock@cern.ch> | 
|---|
| 21 | !   Author(s): Wolfgang Wittek  6/2002 <mailto:wittek@mppmu.mpg.de> | 
|---|
| 22 | ! | 
|---|
| 23 | !   Copyright: MAGIC Software Development, 2000-2003 | 
|---|
| 24 | ! | 
|---|
| 25 | ! | 
|---|
| 26 | \* ======================================================================== */ | 
|---|
| 27 |  | 
|---|
| 28 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 29 | // | 
|---|
| 30 | // MHillas | 
|---|
| 31 | // | 
|---|
| 32 | // Storage Container for image parameters | 
|---|
| 33 | // | 
|---|
| 34 | //    basic image parameters | 
|---|
| 35 | // | 
|---|
| 36 | // Version 1: | 
|---|
| 37 | // ---------- | 
|---|
| 38 | // fLength   [mm]       major axis of ellipse | 
|---|
| 39 | // fWidth    [mm]       minor axis | 
|---|
| 40 | // fDelta    [rad]      angle of major axis with x-axis | 
|---|
| 41 | //                      by definition the major axis is pointing into | 
|---|
| 42 | //                      the hemisphere x>0, thus -pi/2 < delta < pi/2 | 
|---|
| 43 | // fSize     [#CerPhot] total sum of pixels | 
|---|
| 44 | // fMeanX    [mm]       x of center of ellipse | 
|---|
| 45 | // fMeanY    [mm]       y of center of ellipse | 
|---|
| 46 | // | 
|---|
| 47 | // Version 2: | 
|---|
| 48 | // ---------- | 
|---|
| 49 | // fNumCorePixels  number of pixels called core | 
|---|
| 50 | // fNumUsedPixels  number of pixels which survived the cleaning | 
|---|
| 51 | // | 
|---|
| 52 | // Version 3: | 
|---|
| 53 | // ---------- | 
|---|
| 54 | // fNumCorePixels  moved to MNewImagePar | 
|---|
| 55 | // fNumUsedPixels  moved to MNewImagePar | 
|---|
| 56 | // fCosDelta       added | 
|---|
| 57 | // fSinDelte       added | 
|---|
| 58 | // | 
|---|
| 59 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 60 | #include "MHillas.h" | 
|---|
| 61 |  | 
|---|
| 62 | #include <fstream> | 
|---|
| 63 |  | 
|---|
| 64 | #include <TArrayF.h> | 
|---|
| 65 | #include <TEllipse.h> | 
|---|
| 66 | #include <TVector2.h> | 
|---|
| 67 |  | 
|---|
| 68 | #include "MGeomPix.h" | 
|---|
| 69 | #include "MGeomCam.h" | 
|---|
| 70 |  | 
|---|
| 71 | #include "MCerPhotPix.h" | 
|---|
| 72 | #include "MCerPhotEvt.h" | 
|---|
| 73 |  | 
|---|
| 74 | #include "MLog.h" | 
|---|
| 75 | #include "MLogManip.h" | 
|---|
| 76 |  | 
|---|
| 77 | ClassImp(MHillas); | 
|---|
| 78 |  | 
|---|
| 79 | using namespace std; | 
|---|
| 80 |  | 
|---|
| 81 | // -------------------------------------------------------------------------- | 
|---|
| 82 | // | 
|---|
| 83 | // Default constructor. | 
|---|
| 84 | // | 
|---|
| 85 | MHillas::MHillas(const char *name, const char *title) | 
|---|
| 86 | { | 
|---|
| 87 | fName  = name  ? name  : "MHillas"; | 
|---|
| 88 | fTitle = title ? title : "Storage container for image parameters of one event"; | 
|---|
| 89 |  | 
|---|
| 90 | Reset(); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | // -------------------------------------------------------------------------- | 
|---|
| 94 | // | 
|---|
| 95 | // Initializes the values with defaults. For the default values see the | 
|---|
| 96 | // source code. | 
|---|
| 97 | // | 
|---|
| 98 | void MHillas::Reset() | 
|---|
| 99 | { | 
|---|
| 100 | fLength = -1; | 
|---|
| 101 | fWidth  = -1; | 
|---|
| 102 | fDelta  =  0; | 
|---|
| 103 |  | 
|---|
| 104 | fSize   = -1; | 
|---|
| 105 | fMeanX  =  0; | 
|---|
| 106 | fMeanY  =  0; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | // -------------------------------------------------------------------------- | 
|---|
| 110 | // | 
|---|
| 111 | //  return the mean position as TVector2(meanx, meany) | 
|---|
| 112 | // | 
|---|
| 113 | TVector2 MHillas::GetMean() const | 
|---|
| 114 | { | 
|---|
| 115 | return TVector2(fMeanX, fMeanY); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | // -------------------------------------------------------------------------- | 
|---|
| 119 | // | 
|---|
| 120 | // Print the hillas Parameters to *fLog | 
|---|
| 121 | // | 
|---|
| 122 | void MHillas::Print(Option_t *) const | 
|---|
| 123 | { | 
|---|
| 124 | Double_t atg = atan2(fMeanY, fMeanX)*kRad2Deg; | 
|---|
| 125 |  | 
|---|
| 126 | if (atg<0) | 
|---|
| 127 | atg += 180; | 
|---|
| 128 |  | 
|---|
| 129 | *fLog << all; | 
|---|
| 130 | *fLog << "Basic Image Parameters (" << GetName() << ")" << endl; | 
|---|
| 131 | *fLog << " - Length         [mm]  = " << fLength << endl; | 
|---|
| 132 | *fLog << " - Width          [mm]  = " << fWidth  << endl; | 
|---|
| 133 | *fLog << " - Delta          [deg] = " << fDelta*kRad2Deg << endl; | 
|---|
| 134 | *fLog << " - Size           [1]   = " << fSize   << " #CherPhot"   << endl; | 
|---|
| 135 | *fLog << " - Meanx          [mm]  = " << fMeanX  << endl; | 
|---|
| 136 | *fLog << " - Meany          [mm]  = " << fMeanY  << endl; | 
|---|
| 137 | *fLog << " - atg(y/x)       [deg] = " << atg     << endl; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | // -------------------------------------------------------------------------- | 
|---|
| 141 | // | 
|---|
| 142 | // Print the hillas Parameters to *fLog, depending on the geometry in | 
|---|
| 143 | // units of deg | 
|---|
| 144 | // | 
|---|
| 145 | void MHillas::Print(const MGeomCam &geom) const | 
|---|
| 146 | { | 
|---|
| 147 | Double_t atg = atan2(fMeanY, fMeanX)*kRad2Deg; | 
|---|
| 148 |  | 
|---|
| 149 | if (atg<0) | 
|---|
| 150 | atg += 180; | 
|---|
| 151 |  | 
|---|
| 152 | *fLog << all; | 
|---|
| 153 | *fLog << "Basic Image Parameters (" << GetName() << ")" << endl; | 
|---|
| 154 | *fLog << " - Length         [deg] = " << fLength*geom.GetConvMm2Deg() << endl; | 
|---|
| 155 | *fLog << " - Width          [deg] = " << fWidth *geom.GetConvMm2Deg() << endl; | 
|---|
| 156 | *fLog << " - Delta          [deg] = " << fDelta*kRad2Deg << endl; | 
|---|
| 157 | *fLog << " - Size           [1]   = " << fSize   << " #CherPhot"   << endl; | 
|---|
| 158 | *fLog << " - Meanx          [deg] = " << fMeanX *geom.GetConvMm2Deg() << endl; | 
|---|
| 159 | *fLog << " - Meany          [deg] = " << fMeanY *geom.GetConvMm2Deg() << endl; | 
|---|
| 160 | *fLog << " - atg(y/x)       [deg] = " << atg     << endl; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | // -------------------------------------------------------------------------- | 
|---|
| 164 | // | 
|---|
| 165 | // Paint the ellipse corresponding to the parameters | 
|---|
| 166 | // | 
|---|
| 167 | void MHillas::Paint(Option_t *opt) | 
|---|
| 168 | { | 
|---|
| 169 | if (fLength<0 || fWidth<0) | 
|---|
| 170 | return; | 
|---|
| 171 |  | 
|---|
| 172 | TEllipse e(fMeanX, fMeanY, fLength, fWidth, 0, 360, fDelta*kRad2Deg+180); | 
|---|
| 173 | e.SetLineWidth(2); | 
|---|
| 174 | e.Paint(); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | // -------------------------------------------------------------------------- | 
|---|
| 178 | // | 
|---|
| 179 | // Calculate the image parameters from a Cherenkov photon event | 
|---|
| 180 | // assuming Cher.photons/pixel and pixel coordinates are given | 
|---|
| 181 | // In case you don't call Calc from within an eventloop make sure, that | 
|---|
| 182 | // you call the Reset member function before. | 
|---|
| 183 | // Returns: | 
|---|
| 184 | //   0  no error | 
|---|
| 185 | //   1  number of pixels < 3 | 
|---|
| 186 | //   2  size==0 | 
|---|
| 187 | //   3  number of used pixel < 3 | 
|---|
| 188 | //   4  CorrXY == 0 | 
|---|
| 189 | // | 
|---|
| 190 | Int_t MHillas::Calc(const MGeomCam &geom, const MCerPhotEvt &evt) | 
|---|
| 191 | { | 
|---|
| 192 | // | 
|---|
| 193 | // sanity check 1 | 
|---|
| 194 | // | 
|---|
| 195 | if (evt.GetNumPixels()<3) | 
|---|
| 196 | return 1; | 
|---|
| 197 |  | 
|---|
| 198 | // | 
|---|
| 199 | // calculate mean value of pixel coordinates and fSize | 
|---|
| 200 | // ----------------------------------------------------- | 
|---|
| 201 | // | 
|---|
| 202 | // Because this are only simple sums of roughly 600 values | 
|---|
| 203 | // with an accuracy less than three digits there should not | 
|---|
| 204 | // be any need to use double precision (Double_t) for the | 
|---|
| 205 | // calculation of fMeanX, fMeanY and fSize. | 
|---|
| 206 | // | 
|---|
| 207 | fMeanX = 0; | 
|---|
| 208 | fMeanY = 0; | 
|---|
| 209 | fSize  = 0; | 
|---|
| 210 |  | 
|---|
| 211 | MCerPhotPix *pix = 0; | 
|---|
| 212 |  | 
|---|
| 213 | TIter Next(evt); | 
|---|
| 214 | UInt_t numused = 0; | 
|---|
| 215 | while ((pix=(MCerPhotPix*)Next())) | 
|---|
| 216 | { | 
|---|
| 217 | const MGeomPix &gpix = geom[pix->GetPixId()]; | 
|---|
| 218 |  | 
|---|
| 219 | const Float_t nphot = pix->GetNumPhotons(); | 
|---|
| 220 |  | 
|---|
| 221 | fSize  += nphot;                             // [counter] | 
|---|
| 222 | fMeanX += nphot * gpix.GetX();               // [mm] | 
|---|
| 223 | fMeanY += nphot * gpix.GetY();               // [mm] | 
|---|
| 224 |  | 
|---|
| 225 | numused++; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | // | 
|---|
| 229 | // sanity check 2 | 
|---|
| 230 | // | 
|---|
| 231 | if (fSize==0) | 
|---|
| 232 | return 2; | 
|---|
| 233 |  | 
|---|
| 234 | fMeanX /= fSize;                                 // [mm] | 
|---|
| 235 | fMeanY /= fSize;                                 // [mm] | 
|---|
| 236 |  | 
|---|
| 237 | // | 
|---|
| 238 | // sanity check 3 | 
|---|
| 239 | // | 
|---|
| 240 | if (numused<3) | 
|---|
| 241 | return 3; | 
|---|
| 242 |  | 
|---|
| 243 | // | 
|---|
| 244 | // calculate 2nd moments | 
|---|
| 245 | // --------------------- | 
|---|
| 246 | // | 
|---|
| 247 | // To make sure that the more complicated sum is evaluated as | 
|---|
| 248 | // accurate as possible (because it is needed for more | 
|---|
| 249 | // complicated calculations (see below) we calculate it using | 
|---|
| 250 | // double prcision. | 
|---|
| 251 | // | 
|---|
| 252 | Double_t corrxx=0;                               // [m^2] | 
|---|
| 253 | Double_t corrxy=0;                               // [m^2] | 
|---|
| 254 | Double_t corryy=0;                               // [m^2] | 
|---|
| 255 |  | 
|---|
| 256 | Next.Reset(); | 
|---|
| 257 | while ((pix=(MCerPhotPix*)Next())) | 
|---|
| 258 | { | 
|---|
| 259 | const MGeomPix &gpix = geom[pix->GetPixId()]; | 
|---|
| 260 |  | 
|---|
| 261 | const Float_t dx = gpix.GetX() - fMeanX;     // [mm] | 
|---|
| 262 | const Float_t dy = gpix.GetY() - fMeanY;     // [mm] | 
|---|
| 263 |  | 
|---|
| 264 | const Float_t nphot = pix->GetNumPhotons();  // [#phot] | 
|---|
| 265 |  | 
|---|
| 266 | corrxx += nphot * dx*dx;                     // [mm^2] | 
|---|
| 267 | corrxy += nphot * dx*dy;                     // [mm^2] | 
|---|
| 268 | corryy += nphot * dy*dy;                     // [mm^2] | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | // | 
|---|
| 272 | // If corrxy=0 (which should happen not very often, because fSize>0) | 
|---|
| 273 | // we cannot calculate Length and Width. | 
|---|
| 274 | // In reallity it is almost impossible to have a distribution of | 
|---|
| 275 | // cerenkov photons in the used pixels which is exactly symmetric | 
|---|
| 276 | // along one of the axis. | 
|---|
| 277 | // It seems to be less than 0.1% of all events. | 
|---|
| 278 | // | 
|---|
| 279 | if (corrxy==0) | 
|---|
| 280 | return 4; | 
|---|
| 281 |  | 
|---|
| 282 | // | 
|---|
| 283 | // calculate the basic Hillas parameters: orientation and size of axes | 
|---|
| 284 | // ------------------------------------------------------------------- | 
|---|
| 285 | // | 
|---|
| 286 | // fDelta is the angle between the major axis of the ellipse and | 
|---|
| 287 | //  the x axis. It is independent of the position of the ellipse | 
|---|
| 288 | //  in the camera it has values between -pi/2 and pi/2 degrees | 
|---|
| 289 | // | 
|---|
| 290 | // Rem: I tested replacing sqrt() by hypot() but they exactly | 
|---|
| 291 | //      consume the same amount of time | 
|---|
| 292 | // | 
|---|
| 293 | const Double_t d0    = corryy - corrxx; | 
|---|
| 294 | const Double_t d1    = corrxy*2; | 
|---|
| 295 | const Double_t d2    = d0 + TMath::Sqrt(d0*d0 + d1*d1); | 
|---|
| 296 | const Double_t tand  = d2 / d1; | 
|---|
| 297 | const Double_t tand2 = tand*tand; | 
|---|
| 298 |  | 
|---|
| 299 | fDelta = TMath::ATan(tand); | 
|---|
| 300 |  | 
|---|
| 301 | const Double_t s2 = tand2+1; | 
|---|
| 302 | const Double_t s  = TMath::Sqrt(s2); | 
|---|
| 303 |  | 
|---|
| 304 | fCosDelta =  1.0/s;   // need these in derived classes | 
|---|
| 305 | fSinDelta = tand/s;   // like MHillasExt | 
|---|
| 306 |  | 
|---|
| 307 | const Double_t axis1 = (tand2*corryy + d2 + corrxx)/s2/fSize; | 
|---|
| 308 | const Double_t axis2 = (tand2*corrxx - d2 + corryy)/s2/fSize; | 
|---|
| 309 |  | 
|---|
| 310 | // | 
|---|
| 311 | // fLength^2 is the second moment along the major axis of the ellipse | 
|---|
| 312 | // fWidth^2  is the second moment along the minor axis of the ellipse | 
|---|
| 313 | // | 
|---|
| 314 | // From the algorithm we get: fWidth <= fLength is always true | 
|---|
| 315 | // | 
|---|
| 316 | // very small numbers can get negative by rounding | 
|---|
| 317 | // | 
|---|
| 318 | fLength = axis1<0 ? 0 : TMath::Sqrt(axis1);  // [mm] | 
|---|
| 319 | fWidth  = axis2<0 ? 0 : TMath::Sqrt(axis2);  // [mm] | 
|---|
| 320 |  | 
|---|
| 321 | SetReadyToSave(); | 
|---|
| 322 |  | 
|---|
| 323 | return 0; | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | // -------------------------------------------------------------------------- | 
|---|
| 327 | // | 
|---|
| 328 | // This function is ment for special usage, please never try to set | 
|---|
| 329 | // values via this function | 
|---|
| 330 | // | 
|---|
| 331 | void MHillas::Set(const TArrayF &arr) | 
|---|
| 332 | { | 
|---|
| 333 | if (arr.GetSize() != 6) | 
|---|
| 334 | return; | 
|---|
| 335 |  | 
|---|
| 336 | fLength = arr.At(0);  // [mm]        major axis of ellipse | 
|---|
| 337 | fWidth  = arr.At(1);  // [mm]        minor axis of ellipse | 
|---|
| 338 | fDelta  = arr.At(2);  // [rad]       angle of major axis with x-axis | 
|---|
| 339 | fSize   = arr.At(3);  // [#CerPhot]  sum of content of all pixels (number of Cherenkov photons) | 
|---|
| 340 | fMeanX  = arr.At(4);  // [mm]        x-coordinate of center of ellipse | 
|---|
| 341 | fMeanY  = arr.At(5);  // [mm]        y-coordinate of center of ellipse | 
|---|
| 342 | } | 
|---|