ROOT logo
//////////////////////////////////////////////////////////////////////////////
//
// $Id: $
//
//*-- Author  : RICH team member
//*-- Revised : Martin Jurkovic <martin.jurkovic@ph.tum.de> 2010
//
//_HADES_CLASS_DESCRIPTION
//////////////////////////////////////////////////////////////////////////////
//
//  HRichFrame
//
//
//////////////////////////////////////////////////////////////////////////////


#include "hparamlist.h"
#include "hrichframe.h"
#include "hrichframecorner.h"

#include <iostream>
#include <cstdlib>

using namespace std;

ClassImp(HRichFrame)


HRichFrame::HRichFrame()
   : TObject()
{
   clear();
}


HRichFrame::~HRichFrame()
{
   clear();
}

void
HRichFrame::clear()
{
   fCornerNr = 0;
   fFrameArr.Delete();
}

Bool_t
HRichFrame::getParams(HParamList* l)
{
   HRichFrameCorner *pFrameCorner = NULL;

   if (!l) return kFALSE;
   if (!l->fill("fFrameCorners", &fFrameCorners)) return kFALSE;

// We have 2 items (X,Y,flag) per frame corner
   for (Int_t i = 0; i < fFrameCorners.GetSize() / 2; ++i) {
      pFrameCorner = new HRichFrameCorner;
      pFrameCorner->setX(fFrameCorners[2*i]);
      pFrameCorner->setY(fFrameCorners[2*i+1]);
      pFrameCorner->setCornerNr(i);
      addCorner(pFrameCorner);
   }

   return calculateFlagArea();
}

void
HRichFrame::printParams()
{
   if (fFrameArr.GetSize() < getCornerNr() || getCornerNr() != 7) {
      Error("printParams", "Inconsistency in number of frame corners (%d < %d)", fFrameArr.GetSize(), getCornerNr());
      return;
   }
   cout << "HRichFrame" << endl;
   cout << "==========================================" << endl;
   cout << "fFrameCorners        " << endl;
   for (Int_t i = 0; i < getCornerNr(); ++i) {
      cout << getCorner(i)->getX() << " "
           << getCorner(i)->getY() << " " << endl;
   }
   cout << endl;
}

void
HRichFrame::putParams(HParamList* l)
{
   if (!l) return;
   l->add("fFrameCorners", fFrameCorners);
}

HRichFrameCorner*
HRichFrame::getCorner(Int_t n)
{
   if ((n < 0) || (n >= getCornerNr())) {
      return NULL;
   }

   return static_cast<HRichFrameCorner*>(fFrameArr.At(n));
}


Int_t
HRichFrame::setCorner(HRichFrameCorner* pCorner, Int_t n)
{
   if ((n < 0) || (n >= getCornerNr())) {
      return 0;
   }

   delete fFrameArr.At(n);
   fFrameArr.AddAt(pCorner, n);

   return 1;
}

Int_t
HRichFrame::addCorner(HRichFrameCorner* pCorner)
{
   fFrameArr.Add(pCorner);
   return fCornerNr++;
}

Int_t
HRichFrame::isOut(Float_t x, Float_t y)
{

   Int_t nCornerNr = getCornerNr();

   if (nCornerNr <= 2) {
      Error("isOut", "Frame must have at least 3 corners!");
      return -1;
   }

#ifdef RICHDIGI_DEBUG3
   cout << "RICHDIGI: HRichFrame _isOut_ is now calculated \n";
#endif


   Float_t X0    = 0.;
   Float_t X1    = 0.;
   Float_t Y0    = 0.;
   Float_t Y1    = 0.;
   Float_t a     = 0.;
   Float_t b     = 0.;
   Int_t   nFlag = 0;

   for (Int_t i = 0; i < nCornerNr; i++) {
      getCorner(i)->getXY(&X0, &Y0);
      nFlag = getCorner(i)->getFlagArea();
      if (i + 1 < nCornerNr) {
      	getCorner(i + 1)->getXY(&X1, &Y1);
      } else {
      	getCorner(0)->getXY(&X1, &Y1);
      }

      if (X0 == X1) {
         if (nFlag == 0 && x > X0) return 1;
         else if (nFlag == 1 && x < X0) return 1;
      } else if (Y0 == Y1) {
         if (nFlag == 0 && y > Y0) return 1;
         else if (nFlag == 1 && y < Y0) return 1;
      } else {
         a = (Y1 - Y0) / (X1 - X0);
         b = (X1 * Y0 - X0 * Y1) / (X1 - X0);
         if (nFlag == 0 && y > (a * x + b)) return 1;
         else if (nFlag == 1 && y < (a * x + b)) return 1;
      }
   }

   return 0;
}

Bool_t
HRichFrame::calculateFlagArea()
{
// calculation of the flag area

  Int_t fArea0;
  Int_t fArea1;
  Int_t nrcor1;
  Int_t nrcor2;
  Float_t a;
  Float_t b;
  Float_t xcor1;
  Float_t xcor2;
  Float_t ycor1;
  Float_t ycor2;

   for (Int_t i = 0; i < getCornerNr(); i++) {
      getCorner(i)->getXY(&xcor1, &ycor1);
      nrcor1 = getCorner(i)->getCornerNr();
      if (i + 1 < getCornerNr()) {
         getCorner(i + 1)->getXY(&xcor2, &ycor2);
         nrcor2 = getCorner(i + 1)->getCornerNr();
      } else {
         getCorner(0)->getXY(&xcor2, &ycor2);
         nrcor2 = getCorner(0)->getCornerNr();
      }

      fArea0 = 0;
      fArea1 = 0;
      if (xcor1 == xcor2) {
         for (Int_t j = 0; j < getCornerNr(); j++) {
            if (getCorner(j)->getCornerNr() != nrcor1 &&
                getCorner(j)->getCornerNr() != nrcor2) {
               if (getCorner(j)->getX() > xcor1) fArea1++;
               if (getCorner(j)->getX() < xcor1) fArea0++;
            }
         }
         if (fArea1 + 2 == getCornerNr()) {
            getCorner(i)->setFlagArea(1);
         } else if (fArea0 + 2 == getCornerNr()) {
            getCorner(i)->setFlagArea(0);
         } else {
            Error("calculateFlagArea", "Inconsistency in frame corners coordinates.");
            return kFALSE;
         }

      } else if (ycor1 == ycor2) {
         for (Int_t j = 0; j < getCornerNr(); j++) {
            if (getCorner(j)->getCornerNr() != nrcor1 &&
                getCorner(j)->getCornerNr() != nrcor2) {
               if (getCorner(j)->getY() > ycor1) fArea1++;
               if (getCorner(j)->getY() < ycor1) fArea0++;
            }
         }
         if (fArea1 + 2 == getCornerNr()) {
            getCorner(i)->setFlagArea(1);
         } else if (fArea0 + 2 == getCornerNr()) {
            getCorner(i)->setFlagArea(0);
         } else {
            Error("calculateFlagArea", "Inconsistency in frame corners coordinates.");
            return kFALSE;
         }

      } else {
         a = (ycor2 - ycor1) / (xcor2 - xcor1);
         b = (xcor2 * ycor1 - xcor1 * ycor2) / (xcor2 - xcor1);
         for (Int_t j = 0; j < getCornerNr(); j++) {
            if (getCorner(j)->getCornerNr() != nrcor1 &&
                getCorner(j)->getCornerNr() != nrcor2) {
               if (getCorner(j)->getY() > a * getCorner(j)->getX() + b) fArea1++;
               if (getCorner(j)->getY() < a * getCorner(j)->getX() + b) fArea0++;
            }
         }
         if (fArea1 + 2 == getCornerNr()) {
            getCorner(i)->setFlagArea(1);
         } else if (fArea0 + 2 == getCornerNr()) {
            getCorner(i)->setFlagArea(0);
         } else {
            Error("calculateFlagArea", "Inconsistency in frame corners coordinates.");
            return kFALSE;
         }
      }
   }

   return kTRUE;
}
 hrichframe.cc:1
 hrichframe.cc:2
 hrichframe.cc:3
 hrichframe.cc:4
 hrichframe.cc:5
 hrichframe.cc:6
 hrichframe.cc:7
 hrichframe.cc:8
 hrichframe.cc:9
 hrichframe.cc:10
 hrichframe.cc:11
 hrichframe.cc:12
 hrichframe.cc:13
 hrichframe.cc:14
 hrichframe.cc:15
 hrichframe.cc:16
 hrichframe.cc:17
 hrichframe.cc:18
 hrichframe.cc:19
 hrichframe.cc:20
 hrichframe.cc:21
 hrichframe.cc:22
 hrichframe.cc:23
 hrichframe.cc:24
 hrichframe.cc:25
 hrichframe.cc:26
 hrichframe.cc:27
 hrichframe.cc:28
 hrichframe.cc:29
 hrichframe.cc:30
 hrichframe.cc:31
 hrichframe.cc:32
 hrichframe.cc:33
 hrichframe.cc:34
 hrichframe.cc:35
 hrichframe.cc:36
 hrichframe.cc:37
 hrichframe.cc:38
 hrichframe.cc:39
 hrichframe.cc:40
 hrichframe.cc:41
 hrichframe.cc:42
 hrichframe.cc:43
 hrichframe.cc:44
 hrichframe.cc:45
 hrichframe.cc:46
 hrichframe.cc:47
 hrichframe.cc:48
 hrichframe.cc:49
 hrichframe.cc:50
 hrichframe.cc:51
 hrichframe.cc:52
 hrichframe.cc:53
 hrichframe.cc:54
 hrichframe.cc:55
 hrichframe.cc:56
 hrichframe.cc:57
 hrichframe.cc:58
 hrichframe.cc:59
 hrichframe.cc:60
 hrichframe.cc:61
 hrichframe.cc:62
 hrichframe.cc:63
 hrichframe.cc:64
 hrichframe.cc:65
 hrichframe.cc:66
 hrichframe.cc:67
 hrichframe.cc:68
 hrichframe.cc:69
 hrichframe.cc:70
 hrichframe.cc:71
 hrichframe.cc:72
 hrichframe.cc:73
 hrichframe.cc:74
 hrichframe.cc:75
 hrichframe.cc:76
 hrichframe.cc:77
 hrichframe.cc:78
 hrichframe.cc:79
 hrichframe.cc:80
 hrichframe.cc:81
 hrichframe.cc:82
 hrichframe.cc:83
 hrichframe.cc:84
 hrichframe.cc:85
 hrichframe.cc:86
 hrichframe.cc:87
 hrichframe.cc:88
 hrichframe.cc:89
 hrichframe.cc:90
 hrichframe.cc:91
 hrichframe.cc:92
 hrichframe.cc:93
 hrichframe.cc:94
 hrichframe.cc:95
 hrichframe.cc:96
 hrichframe.cc:97
 hrichframe.cc:98
 hrichframe.cc:99
 hrichframe.cc:100
 hrichframe.cc:101
 hrichframe.cc:102
 hrichframe.cc:103
 hrichframe.cc:104
 hrichframe.cc:105
 hrichframe.cc:106
 hrichframe.cc:107
 hrichframe.cc:108
 hrichframe.cc:109
 hrichframe.cc:110
 hrichframe.cc:111
 hrichframe.cc:112
 hrichframe.cc:113
 hrichframe.cc:114
 hrichframe.cc:115
 hrichframe.cc:116
 hrichframe.cc:117
 hrichframe.cc:118
 hrichframe.cc:119
 hrichframe.cc:120
 hrichframe.cc:121
 hrichframe.cc:122
 hrichframe.cc:123
 hrichframe.cc:124
 hrichframe.cc:125
 hrichframe.cc:126
 hrichframe.cc:127
 hrichframe.cc:128
 hrichframe.cc:129
 hrichframe.cc:130
 hrichframe.cc:131
 hrichframe.cc:132
 hrichframe.cc:133
 hrichframe.cc:134
 hrichframe.cc:135
 hrichframe.cc:136
 hrichframe.cc:137
 hrichframe.cc:138
 hrichframe.cc:139
 hrichframe.cc:140
 hrichframe.cc:141
 hrichframe.cc:142
 hrichframe.cc:143
 hrichframe.cc:144
 hrichframe.cc:145
 hrichframe.cc:146
 hrichframe.cc:147
 hrichframe.cc:148
 hrichframe.cc:149
 hrichframe.cc:150
 hrichframe.cc:151
 hrichframe.cc:152
 hrichframe.cc:153
 hrichframe.cc:154
 hrichframe.cc:155
 hrichframe.cc:156
 hrichframe.cc:157
 hrichframe.cc:158
 hrichframe.cc:159
 hrichframe.cc:160
 hrichframe.cc:161
 hrichframe.cc:162
 hrichframe.cc:163
 hrichframe.cc:164
 hrichframe.cc:165
 hrichframe.cc:166
 hrichframe.cc:167
 hrichframe.cc:168
 hrichframe.cc:169
 hrichframe.cc:170
 hrichframe.cc:171
 hrichframe.cc:172
 hrichframe.cc:173
 hrichframe.cc:174
 hrichframe.cc:175
 hrichframe.cc:176
 hrichframe.cc:177
 hrichframe.cc:178
 hrichframe.cc:179
 hrichframe.cc:180
 hrichframe.cc:181
 hrichframe.cc:182
 hrichframe.cc:183
 hrichframe.cc:184
 hrichframe.cc:185
 hrichframe.cc:186
 hrichframe.cc:187
 hrichframe.cc:188
 hrichframe.cc:189
 hrichframe.cc:190
 hrichframe.cc:191
 hrichframe.cc:192
 hrichframe.cc:193
 hrichframe.cc:194
 hrichframe.cc:195
 hrichframe.cc:196
 hrichframe.cc:197
 hrichframe.cc:198
 hrichframe.cc:199
 hrichframe.cc:200
 hrichframe.cc:201
 hrichframe.cc:202
 hrichframe.cc:203
 hrichframe.cc:204
 hrichframe.cc:205
 hrichframe.cc:206
 hrichframe.cc:207
 hrichframe.cc:208
 hrichframe.cc:209
 hrichframe.cc:210
 hrichframe.cc:211
 hrichframe.cc:212
 hrichframe.cc:213
 hrichframe.cc:214
 hrichframe.cc:215
 hrichframe.cc:216
 hrichframe.cc:217
 hrichframe.cc:218
 hrichframe.cc:219
 hrichframe.cc:220
 hrichframe.cc:221
 hrichframe.cc:222
 hrichframe.cc:223
 hrichframe.cc:224
 hrichframe.cc:225
 hrichframe.cc:226
 hrichframe.cc:227
 hrichframe.cc:228
 hrichframe.cc:229
 hrichframe.cc:230
 hrichframe.cc:231
 hrichframe.cc:232
 hrichframe.cc:233
 hrichframe.cc:234
 hrichframe.cc:235
 hrichframe.cc:236
 hrichframe.cc:237
 hrichframe.cc:238
 hrichframe.cc:239
 hrichframe.cc:240
 hrichframe.cc:241
 hrichframe.cc:242
 hrichframe.cc:243
 hrichframe.cc:244
 hrichframe.cc:245
 hrichframe.cc:246
 hrichframe.cc:247
 hrichframe.cc:248
 hrichframe.cc:249
 hrichframe.cc:250
 hrichframe.cc:251
 hrichframe.cc:252
 hrichframe.cc:253
 hrichframe.cc:254
 hrichframe.cc:255
 hrichframe.cc:256
 hrichframe.cc:257
 hrichframe.cc:258