| 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 <TArrayF.h> | 
|---|
| 63 |  | 
|---|
| 64 | #include <TLine.h> | 
|---|
| 65 | #include <TEllipse.h> | 
|---|
| 66 | #include <TVector2.h> | 
|---|
| 67 |  | 
|---|
| 68 | #include "MGeomPix.h" | 
|---|
| 69 | #include "MGeomCam.h" | 
|---|
| 70 |  | 
|---|
| 71 | #include "MSignalPix.h" | 
|---|
| 72 | #include "MSignalCam.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 | //  return the direction of the main axis (normalized). The main axis | 
|---|
| 121 | //  always has the direction  -->  x>0 | 
|---|
| 122 | // | 
|---|
| 123 | TVector2 MHillas::GetNormAxis() const | 
|---|
| 124 | { | 
|---|
| 125 | return TVector2(fCosDelta, fSinDelta); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | // -------------------------------------------------------------------------- | 
|---|
| 129 | // | 
|---|
| 130 | //  Analytical estimation of length of border line: | 
|---|
| 131 | //    U = pi*(a+b - sqrt(a*b)) | 
|---|
| 132 | // | 
|---|
| 133 | //  GetBorderLine() [mm] | 
|---|
| 134 | // | 
|---|
| 135 | Double_t MHillas::GetBorderLine() const | 
|---|
| 136 | { | 
|---|
| 137 | const Double_t a = fWidth+fLength; | 
|---|
| 138 | const Double_t s = fWidth*fLength; | 
|---|
| 139 |  | 
|---|
| 140 | return TMath::Pi()*(1.5*a - TMath::Sqrt(s)); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | // -------------------------------------------------------------------------- | 
|---|
| 144 | // | 
|---|
| 145 | //  Analytical estimation of length of border line: | 
|---|
| 146 | //    A = pi*a*b | 
|---|
| 147 | // | 
|---|
| 148 | //  GetArea() [mm^2] | 
|---|
| 149 | // | 
|---|
| 150 | Double_t MHillas::GetArea() const | 
|---|
| 151 | { | 
|---|
| 152 | return TMath::Pi()*fWidth*fLength; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | // -------------------------------------------------------------------------- | 
|---|
| 156 | // | 
|---|
| 157 | // Print the hillas Parameters to *fLog | 
|---|
| 158 | // | 
|---|
| 159 | void MHillas::Print(Option_t *) const | 
|---|
| 160 | { | 
|---|
| 161 | const Double_t atg = atan2(fMeanX, fMeanY)*kRad2Deg; | 
|---|
| 162 |  | 
|---|
| 163 | *fLog << all; | 
|---|
| 164 | *fLog << GetDescriptor() << endl; | 
|---|
| 165 | *fLog << " - Length         [mm]  = " << fLength << endl; | 
|---|
| 166 | *fLog << " - Width          [mm]  = " << fWidth  << endl; | 
|---|
| 167 | *fLog << " - Delta          [deg] = " << fDelta*kRad2Deg << endl; | 
|---|
| 168 | *fLog << " - Size           [phe] = " << fSize   << endl; | 
|---|
| 169 | *fLog << " - Meanx          [mm]  = " << fMeanX  << endl; | 
|---|
| 170 | *fLog << " - Meany          [mm]  = " << fMeanY  << endl; | 
|---|
| 171 | *fLog << " - atg(y/x)       [deg] = " << atg     << endl; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | // -------------------------------------------------------------------------- | 
|---|
| 175 | // | 
|---|
| 176 | // Print the hillas Parameters to *fLog, depending on the geometry in | 
|---|
| 177 | // units of deg | 
|---|
| 178 | // | 
|---|
| 179 | void MHillas::Print(const MGeomCam &geom) const | 
|---|
| 180 | { | 
|---|
| 181 | const Double_t atg = atan2(fMeanX, fMeanY)*kRad2Deg; | 
|---|
| 182 |  | 
|---|
| 183 | *fLog << all; | 
|---|
| 184 | *fLog << GetDescriptor() << endl; | 
|---|
| 185 | *fLog << " - Length         [deg] = " << fLength*geom.GetConvMm2Deg() << endl; | 
|---|
| 186 | *fLog << " - Width          [deg] = " << fWidth *geom.GetConvMm2Deg() << endl; | 
|---|
| 187 | *fLog << " - Delta          [deg] = " << fDelta*kRad2Deg << endl; | 
|---|
| 188 | *fLog << " - Size           [phe] = " << fSize << endl; | 
|---|
| 189 | *fLog << " - Meanx          [deg] = " << fMeanX *geom.GetConvMm2Deg() << endl; | 
|---|
| 190 | *fLog << " - Meany          [deg] = " << fMeanY *geom.GetConvMm2Deg() << endl; | 
|---|
| 191 | *fLog << " - atg(y/x)       [deg] = " << atg     << endl; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | // -------------------------------------------------------------------------- | 
|---|
| 195 | // | 
|---|
| 196 | // Paint the ellipse corresponding to the parameters | 
|---|
| 197 | // | 
|---|
| 198 | void MHillas::Paint(Option_t *opt) | 
|---|
| 199 | { | 
|---|
| 200 | static const Float_t sOffsetW=40.0; | 
|---|
| 201 | static const Float_t sOffsetL=266.0; | 
|---|
| 202 |  | 
|---|
| 203 | if (fLength<0 || fWidth<0) | 
|---|
| 204 | return; | 
|---|
| 205 |  | 
|---|
| 206 | const Float_t l = fLength+sOffsetL; | 
|---|
| 207 | const Float_t w = fWidth +sOffsetW; | 
|---|
| 208 |  | 
|---|
| 209 | TLine line; | 
|---|
| 210 | line.SetLineWidth(1); | 
|---|
| 211 | line.SetLineColor(kRed); | 
|---|
| 212 |  | 
|---|
| 213 | // Length line | 
|---|
| 214 | line.PaintLine(-l*fCosDelta+fMeanX, -l*fSinDelta+fMeanY, | 
|---|
| 215 | +l*fCosDelta+fMeanX, +l*fSinDelta+fMeanY); | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 | // Width line | 
|---|
| 219 | line.PaintLine(+w*fSinDelta+fMeanX, -w*fCosDelta+fMeanY, | 
|---|
| 220 | -w*fSinDelta+fMeanX, +w*fCosDelta+fMeanY); | 
|---|
| 221 |  | 
|---|
| 222 | // Rough estimate for disp | 
|---|
| 223 | const Double_t p = 362*(1-fWidth/fLength); | 
|---|
| 224 |  | 
|---|
| 225 | // Disp line | 
|---|
| 226 | line.PaintLine( p*fCosDelta+20*fSinDelta+fMeanX,  p*fSinDelta-20*fCosDelta+fMeanY, | 
|---|
| 227 | p*fCosDelta-20*fSinDelta+fMeanX,  p*fSinDelta+20*fCosDelta+fMeanY); | 
|---|
| 228 | line.PaintLine(-p*fCosDelta+20*fSinDelta+fMeanX, -p*fSinDelta-20*fCosDelta+fMeanY, | 
|---|
| 229 | -p*fCosDelta-20*fSinDelta+fMeanX, -p*fSinDelta+20*fCosDelta+fMeanY); | 
|---|
| 230 |  | 
|---|
| 231 | TEllipse e(fMeanX, fMeanY, fLength, fWidth, 0, 360, fDelta*kRad2Deg+180); | 
|---|
| 232 | e.SetLineWidth(2); | 
|---|
| 233 | e.SetLineColor(kGreen); | 
|---|
| 234 | e.Paint(); | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | // -------------------------------------------------------------------------- | 
|---|
| 238 | // | 
|---|
| 239 | // Calculate the image parameters from a Cherenkov photon event | 
|---|
| 240 | // assuming Cher.photons/pixel and pixel coordinates are given | 
|---|
| 241 | // In case you don't call Calc from within an eventloop make sure, that | 
|---|
| 242 | // you call the Reset member function before. | 
|---|
| 243 | // Returns: | 
|---|
| 244 | //   0  no error | 
|---|
| 245 | //   1  number of pixels in event == 0 (special MC events) | 
|---|
| 246 | //   2  size==0 | 
|---|
| 247 | //   3  number of used pixel < 3 | 
|---|
| 248 | //   4  CorrXY == 0 | 
|---|
| 249 | // | 
|---|
| 250 | Int_t MHillas::Calc(const MGeomCam &geom, const MSignalCam &evt, Int_t island) | 
|---|
| 251 | { | 
|---|
| 252 | const UInt_t numpix  = evt.GetNumPixels(); | 
|---|
| 253 |  | 
|---|
| 254 | // | 
|---|
| 255 | // sanity check 1 (special MC events) | 
|---|
| 256 | // | 
|---|
| 257 | if (numpix==0) | 
|---|
| 258 | return 1; | 
|---|
| 259 |  | 
|---|
| 260 | // | 
|---|
| 261 | // calculate mean value of pixel coordinates and fSize | 
|---|
| 262 | // ----------------------------------------------------- | 
|---|
| 263 | // | 
|---|
| 264 | // Because this are only simple sums of roughly 600 values | 
|---|
| 265 | // with an accuracy less than three digits there should not | 
|---|
| 266 | // be any need to use double precision (Double_t) for the | 
|---|
| 267 | // calculation of fMeanX, fMeanY and fSize. | 
|---|
| 268 | // | 
|---|
| 269 | fMeanX = 0; | 
|---|
| 270 | fMeanY = 0; | 
|---|
| 271 | fSize  = 0; | 
|---|
| 272 |  | 
|---|
| 273 | UInt_t numused = 0; | 
|---|
| 274 |  | 
|---|
| 275 | for (UInt_t i=0; i<numpix; i++) | 
|---|
| 276 | { | 
|---|
| 277 | MSignalPix &pix = evt[i]; | 
|---|
| 278 | if (!pix.IsPixelUsed()) | 
|---|
| 279 | continue; | 
|---|
| 280 |  | 
|---|
| 281 | if (island>=0 && pix.GetIdxIsland()!=island) | 
|---|
| 282 | continue; | 
|---|
| 283 |  | 
|---|
| 284 | const MGeomPix &gpix = geom[i]; | 
|---|
| 285 |  | 
|---|
| 286 | const Float_t nphot = pix.GetNumPhotons(); | 
|---|
| 287 |  | 
|---|
| 288 | fSize  += nphot;                             // [counter] | 
|---|
| 289 | fMeanX += nphot * gpix.GetX();               // [mm] | 
|---|
| 290 | fMeanY += nphot * gpix.GetY();               // [mm] | 
|---|
| 291 |  | 
|---|
| 292 | numused++; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | // | 
|---|
| 296 | // sanity check 2 | 
|---|
| 297 | // | 
|---|
| 298 | if (fSize==0) | 
|---|
| 299 | return 2; | 
|---|
| 300 |  | 
|---|
| 301 | fMeanX /= fSize;                                 // [mm] | 
|---|
| 302 | fMeanY /= fSize;                                 // [mm] | 
|---|
| 303 |  | 
|---|
| 304 | // | 
|---|
| 305 | // sanity check 3 | 
|---|
| 306 | // | 
|---|
| 307 | if (numused<3) | 
|---|
| 308 | return 3; | 
|---|
| 309 |  | 
|---|
| 310 | // | 
|---|
| 311 | // calculate 2nd moments | 
|---|
| 312 | // --------------------- | 
|---|
| 313 | // | 
|---|
| 314 | // To make sure that the more complicated sum is evaluated as | 
|---|
| 315 | // accurate as possible (because it is needed for more | 
|---|
| 316 | // complicated calculations (see below) we calculate it using | 
|---|
| 317 | // double prcision. | 
|---|
| 318 | // | 
|---|
| 319 | Double_t corrxx=0;                               // [m^2] | 
|---|
| 320 | Double_t corrxy=0;                               // [m^2] | 
|---|
| 321 | Double_t corryy=0;                               // [m^2] | 
|---|
| 322 |  | 
|---|
| 323 | for (UInt_t i=0; i<numpix; i++) | 
|---|
| 324 | { | 
|---|
| 325 | const MSignalPix &pix = evt[i]; | 
|---|
| 326 | if (!pix.IsPixelUsed()) | 
|---|
| 327 | continue; | 
|---|
| 328 |  | 
|---|
| 329 | if (island>=0 && pix.GetIdxIsland()!=island) | 
|---|
| 330 | continue; | 
|---|
| 331 |  | 
|---|
| 332 | const MGeomPix &gpix = geom[i]; | 
|---|
| 333 |  | 
|---|
| 334 | const Float_t dx = gpix.GetX() - fMeanX;     // [mm] | 
|---|
| 335 | const Float_t dy = gpix.GetY() - fMeanY;     // [mm] | 
|---|
| 336 |  | 
|---|
| 337 | const Float_t nphot = pix.GetNumPhotons();   // [#phot] | 
|---|
| 338 |  | 
|---|
| 339 | corrxx += nphot * dx*dx;                     // [mm^2] | 
|---|
| 340 | corrxy += nphot * dx*dy;                     // [mm^2] | 
|---|
| 341 | corryy += nphot * dy*dy;                     // [mm^2] | 
|---|
| 342 | } | 
|---|
| 343 |  | 
|---|
| 344 | // | 
|---|
| 345 | // calculate the basic Hillas parameters: orientation and size of axes | 
|---|
| 346 | // ------------------------------------------------------------------- | 
|---|
| 347 | // | 
|---|
| 348 | // fDelta is the angle between the major axis of the ellipse and | 
|---|
| 349 | //  the x axis. It is independent of the position of the ellipse | 
|---|
| 350 | //  in the camera it has values between -pi/2 and pi/2 degrees | 
|---|
| 351 | // | 
|---|
| 352 | // Rem: I tested replacing sqrt() by hypot() but they exactly | 
|---|
| 353 | //      consume the same amount of time | 
|---|
| 354 | // | 
|---|
| 355 | const Double_t d0    = corryy - corrxx; | 
|---|
| 356 | const Double_t d1    = corrxy*2; | 
|---|
| 357 | const Double_t d2    = TMath::Sqrt(d0*d0 + d1*d1) + d0; // [0 | 
|---|
| 358 |  | 
|---|
| 359 | const Double_t tand  = d2==0 ? 0 : d2 / d1; // Force 0 also if d1==0 | 
|---|
| 360 | const Double_t tand2 = tand*tand; | 
|---|
| 361 |  | 
|---|
| 362 | const Double_t s2    = tand2+1; | 
|---|
| 363 | const Double_t s     = TMath::Sqrt(s2); | 
|---|
| 364 |  | 
|---|
| 365 | // Set default for the case in which the image is symmetric on the y-axis | 
|---|
| 366 | fDelta    = TMath::Pi()/2; | 
|---|
| 367 | fCosDelta = 0; | 
|---|
| 368 | fSinDelta = 1; | 
|---|
| 369 |  | 
|---|
| 370 | Double_t axis1 = corryy; | 
|---|
| 371 | Double_t axis2 = corrxx; | 
|---|
| 372 |  | 
|---|
| 373 | // This are all cases in which the image is not symmetric on the y-axis | 
|---|
| 374 | if (d1!=0 || d2==0) | 
|---|
| 375 | { | 
|---|
| 376 | fDelta    = TMath::ATan(tand); | 
|---|
| 377 |  | 
|---|
| 378 | fCosDelta = 1.0 /s;   // need these in derived classes | 
|---|
| 379 | fSinDelta = tand/s;   // like MHillasExt | 
|---|
| 380 |  | 
|---|
| 381 | axis1 = (tand2*corryy + d2 + corrxx)/s2; | 
|---|
| 382 | axis2 = (tand2*corrxx - d2 + corryy)/s2; | 
|---|
| 383 | } | 
|---|
| 384 |  | 
|---|
| 385 | // | 
|---|
| 386 | // fLength^2 is the second moment along the major axis of the distribution | 
|---|
| 387 | // fWidth^2  is the second moment along the minor axis of the distribution | 
|---|
| 388 | // | 
|---|
| 389 | // From the algorithm we get: fWidth <= fLength is always true | 
|---|
| 390 | // | 
|---|
| 391 | // very small numbers can get negative by rounding | 
|---|
| 392 | // | 
|---|
| 393 | fLength = axis1<0 ? 0 : TMath::Sqrt(axis1/fSize);  // [mm] | 
|---|
| 394 | fWidth  = axis2<0 ? 0 : TMath::Sqrt(axis2/fSize);  // [mm] | 
|---|
| 395 |  | 
|---|
| 396 | SetReadyToSave(); | 
|---|
| 397 |  | 
|---|
| 398 | return 0; | 
|---|
| 399 | } | 
|---|
| 400 |  | 
|---|
| 401 | // -------------------------------------------------------------------------- | 
|---|
| 402 | // | 
|---|
| 403 | // This function is ment for special usage, please never try to set | 
|---|
| 404 | // values via this function | 
|---|
| 405 | // | 
|---|
| 406 | void MHillas::Set(const TArrayF &arr) | 
|---|
| 407 | { | 
|---|
| 408 | if (arr.GetSize() != 6) | 
|---|
| 409 | return; | 
|---|
| 410 |  | 
|---|
| 411 | fLength = arr.At(0);  // [mm]        major axis of ellipse | 
|---|
| 412 | fWidth  = arr.At(1);  // [mm]        minor axis of ellipse | 
|---|
| 413 | fDelta  = arr.At(2);  // [rad]       angle of major axis with x-axis | 
|---|
| 414 | fSize   = arr.At(3);  // [#CerPhot]  sum of content of all pixels (number of Cherenkov photons) | 
|---|
| 415 | fMeanX  = arr.At(4);  // [mm]        x-coordinate of center of ellipse | 
|---|
| 416 | fMeanY  = arr.At(5);  // [mm]        y-coordinate of center of ellipse | 
|---|
| 417 | } | 
|---|