// @(#)root/eve:$Id$
// Author: Matevz Tadel 2007

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TEveCaloData
#define ROOT_TEveCaloData

#include <vector>
#include "TEveElement.h"

class TGLSelectRecord;

class TH2F;
class TAxis;
class THStack;

class TEveCaloData: public TEveElement,
                    public TNamed
{
public:
   struct SliceInfo_t
   {
      TString  fName;      // Name of the slice, eg. ECAL, HCAL.
      Float_t  fThreshold; // Only display towers with higher energy.
      Color_t  fColor;     // Color used to draw this longitudinal slice.
      Color_t  fTransparency; // Transparency used to draw this longitudinal slice.
      
      SliceInfo_t(): fName(""), fThreshold(0), fColor(kRed), fTransparency(0) {}

      virtual ~SliceInfo_t() {}

      void Setup(const char* name, Float_t threshold, Color_t col, Char_t transp = 101)
      {
         fName      = name;
         fThreshold = threshold;
         fColor     = col;
         if (transp <= 100) fTransparency = transp;
      };

      ClassDef(SliceInfo_t, 0); // Slice info for histogram stack.
   };

   typedef std::vector<SliceInfo_t>           vSliceInfo_t;
   typedef std::vector<SliceInfo_t>::iterator vSliceInfo_i;

   /**************************************************************************/

   struct CellId_t
   {
      // Cell ID inner structure.

      Int_t fTower;
      Int_t fSlice;

      Float_t fFraction;

      CellId_t(Int_t t, Int_t s, Float_t f=1.0f) : fTower(t), fSlice(s), fFraction(f) {}

      bool operator<(const CellId_t& o) const
      { return (fTower == o.fTower) ? fSlice < o.fSlice : fTower < o.fTower; }
   };

   struct CellGeom_t
   {
      // Cell geometry inner structure.

      Float_t fPhiMin;
      Float_t fPhiMax;
      Float_t fEtaMin;
      Float_t fEtaMax;

      Float_t fThetaMin; // cached
      Float_t fThetaMax; // cached

      CellGeom_t(): fPhiMin(0), fPhiMax(0), fEtaMin(0), fEtaMax(0), fThetaMin(0), fThetaMax(0) {}
      CellGeom_t(Float_t etaMin, Float_t etaMax, Float_t phiMin, Float_t phiMax) {Configure(etaMin, etaMax, phiMin, phiMax);}
      virtual ~CellGeom_t() {}

      void Configure(Float_t etaMin, Float_t etaMax, Float_t phiMin, Float_t phiMax);

      Float_t EtaMin()   const { return fEtaMin; }
      Float_t EtaMax()   const { return fEtaMax; }
      Float_t Eta()      const { return (fEtaMin+fEtaMax)*0.5f; }
      Float_t EtaDelta() const { return fEtaMax-fEtaMin; }

      Float_t PhiMin()   const { return fPhiMin; }
      Float_t PhiMax()   const { return fPhiMax; }
      Float_t Phi()      const { return (fPhiMin+fPhiMax)*0.5f; }
      Float_t PhiDelta() const { return fPhiMax-fPhiMin; }

      Float_t ThetaMin() const { return fThetaMin; }
      Float_t ThetaMax() const { return fThetaMax; }
      Float_t Theta() const { return (fThetaMax+fThetaMin)*0.5f; }
      Float_t ThetaDelta() const { return fThetaMax-fThetaMin; }

      virtual void  Dump() const;
   };

   struct CellData_t : public CellGeom_t
   {
      // Cell data inner structure.

      Float_t fValue;

      CellData_t() : CellGeom_t(), fValue(0) {}
      virtual ~CellData_t() {}

      Float_t Value(Bool_t) const;
      virtual void Dump() const;
   };


   struct RebinData_t
   {
      Int_t fNSlices;

      std::vector<Float_t> fSliceData;
      std::vector<Int_t>   fBinData;

      Float_t* GetSliceVals(Int_t bin);

      void Clear()
      {
         fSliceData.clear();
         fBinData.clear();
      }
   };

   /**************************************************************************/

   typedef std::vector<CellId_t>               vCellId_t;
   typedef std::vector<CellId_t>::iterator     vCellId_i;

   typedef std::vector<CellGeom_t>             vCellGeom_t;
   typedef std::vector<CellGeom_t>::iterator   vCellGeom_i;
   typedef std::vector<CellGeom_t>::const_iterator   vCellGeom_ci;

private:
   TEveCaloData(const TEveCaloData&);            // Not implemented
   TEveCaloData& operator=(const TEveCaloData&); // Not implemented

protected:
   vSliceInfo_t fSliceInfos;

   TAxis*       fEtaAxis;
   TAxis*       fPhiAxis;

   Bool_t       fWrapTwoPi;

   Float_t      fMaxValEt; // cached
   Float_t      fMaxValE;  // cached

   Float_t      fEps;

   vCellId_t    fCellsSelected;
   vCellId_t    fCellsHighlighted;

public:
   TEveCaloData(const char* n="TEveCalData", const char* t="");
   virtual ~TEveCaloData() {}

   virtual void UnSelected();
   virtual void UnHighlighted();

   virtual TString GetHighlightTooltip();

   virtual void    FillImpliedSelectedSet(Set_t& impSelSet);

   virtual void    GetCellList(Float_t etaMin, Float_t etaMax,
                               Float_t phi,    Float_t phiRng,
                               vCellId_t &out) const = 0;

   vCellId_t&      GetCellsSelected()    { return fCellsSelected; }
   vCellId_t&      GetCellsHighlighted() { return fCellsHighlighted; }
   void            PrintCellsSelected();
   void  ProcessSelection(vCellId_t& sel_cells, TGLSelectRecord& rec);

   virtual void    Rebin(TAxis *ax, TAxis *ay, vCellId_t &in, Bool_t et, RebinData_t &out) const = 0;


   virtual void    GetCellData(const CellId_t &id, CellData_t& data) const = 0;

   virtual void    InvalidateUsersCellIdCache();
   virtual void    DataChanged();
   virtual void    CellSelectionChanged();

   Int_t           GetNSlices()    const { return fSliceInfos.size(); }
   SliceInfo_t&    RefSliceInfo(Int_t s) { return fSliceInfos[s]; }
   void            SetSliceThreshold(Int_t slice, Float_t threshold);
   Float_t         GetSliceThreshold(Int_t slice) const;
   void            SetSliceColor(Int_t slice, Color_t col);
   Color_t         GetSliceColor(Int_t slice) const;
   void            SetSliceTransparency(Int_t slice, Char_t t);
   Char_t          GetSliceTransparency(Int_t slice) const;

   virtual void    GetEtaLimits(Double_t &min, Double_t &max) const = 0;

   virtual void    GetPhiLimits(Double_t &min, Double_t &max) const = 0;

   virtual Float_t GetMaxVal(Bool_t et) const { return et ? fMaxValEt : fMaxValE; }
   Bool_t  Empty() const { return fMaxValEt < 1e-5; }

   virtual TAxis*  GetEtaBins()    const { return fEtaAxis; }
   virtual void    SetEtaBins(TAxis* ax) { fEtaAxis=ax; }

   virtual TAxis*  GetPhiBins()    const { return fPhiAxis; }
   virtual void    SetPhiBins(TAxis* ax) { fPhiAxis=ax; }

   virtual Float_t GetEps()      const { return fEps; }
   virtual void    SetEps(Float_t eps) { fEps=eps; }

   Bool_t   GetWrapTwoPi() const { return fWrapTwoPi; }
   void     SetWrapTwoPi(Bool_t w) { fWrapTwoPi=w; }

   static  Float_t EtaToTheta(Float_t eta);


   ClassDef(TEveCaloData, 0); // Manages calorimeter event data.
};

/**************************************************************************/
/**************************************************************************/

class TEveCaloDataVec: public TEveCaloData
{

private:
   TEveCaloDataVec(const TEveCaloDataVec&);            // Not implemented
   TEveCaloDataVec& operator=(const TEveCaloDataVec&); // Not implemented

protected:
   typedef std::vector<Float_t>               vFloat_t;
   typedef std::vector<Float_t>::iterator     vFloat_i;

   typedef std::vector<vFloat_t>              vvFloat_t;
   typedef std::vector<vFloat_t>::iterator    vvFloat_i;

   vvFloat_t   fSliceVec;
   vCellGeom_t fGeomVec;

   Int_t       fTower; // current tower

   Float_t     fEtaMin;
   Float_t     fEtaMax;

   Float_t     fPhiMin;
   Float_t     fPhiMax;

public:
   TEveCaloDataVec(Int_t nslices);
   virtual ~TEveCaloDataVec();
  
   Int_t AddSlice();
   Int_t AddTower(Float_t etaMin, Float_t etaMax, Float_t phiMin, Float_t phiMax);
   void  FillSlice(Int_t slice, Float_t value);
   void  FillSlice(Int_t slice, Int_t tower, Float_t value);

   Int_t GetNCells() { return fGeomVec.size(); }
   std::vector<Float_t>&  GetSliceVals(Int_t slice) { return fSliceVec[slice]; }
   std::vector<TEveCaloData::CellGeom_t>& GetCellGeom() { return fGeomVec; } 

   virtual void GetCellList(Float_t etaMin, Float_t etaMax,
                            Float_t phi,    Float_t phiRng,
                            vCellId_t &out) const;

   virtual void Rebin(TAxis *ax, TAxis *ay, vCellId_t &in, Bool_t et, RebinData_t &out) const;

   virtual void GetCellData(const TEveCaloData::CellId_t &id, TEveCaloData::CellData_t& data) const;
   virtual void GetEtaLimits(Double_t &min, Double_t &max) const { min=fEtaMin, max=fEtaMax;}
   virtual void GetPhiLimits(Double_t &min, Double_t &max) const { min=fPhiMin; max=fPhiMax;}

  
   virtual void  DataChanged();
   void          SetAxisFromBins(Double_t epsX=0.001, Double_t epsY=0.001);

   ClassDef(TEveCaloDataVec, 0); // Manages calorimeter event data.
};

/**************************************************************************/
/**************************************************************************/

class TEveCaloDataHist: public TEveCaloData
{
private:
   TEveCaloDataHist(const TEveCaloDataHist&);            // Not implemented
   TEveCaloDataHist& operator=(const TEveCaloDataHist&); // Not implemented

protected:
   THStack*    fHStack;

public:
   TEveCaloDataHist();
   virtual ~TEveCaloDataHist();

   virtual void GetCellList( Float_t etaMin, Float_t etaMax,
                             Float_t phi, Float_t phiRng, vCellId_t &out) const;

   virtual void Rebin(TAxis *ax, TAxis *ay, vCellId_t &in, Bool_t et, RebinData_t &out) const;

   virtual void GetCellData(const TEveCaloData::CellId_t &id, TEveCaloData::CellData_t& data) const;

   virtual void GetEtaLimits(Double_t &min, Double_t &max) const;
   virtual void GetPhiLimits(Double_t &min, Double_t &max) const;


   virtual void DataChanged();

   THStack* GetStack() { return fHStack; }

   TH2F*    GetHist(Int_t slice) const;
   
   Int_t   AddHistogram(TH2F* hist);

   ClassDef(TEveCaloDataHist, 0); // Manages calorimeter TH2F event data.
};

#endif

 TEveCaloData.h:1
 TEveCaloData.h:2
 TEveCaloData.h:3
 TEveCaloData.h:4
 TEveCaloData.h:5
 TEveCaloData.h:6
 TEveCaloData.h:7
 TEveCaloData.h:8
 TEveCaloData.h:9
 TEveCaloData.h:10
 TEveCaloData.h:11
 TEveCaloData.h:12
 TEveCaloData.h:13
 TEveCaloData.h:14
 TEveCaloData.h:15
 TEveCaloData.h:16
 TEveCaloData.h:17
 TEveCaloData.h:18
 TEveCaloData.h:19
 TEveCaloData.h:20
 TEveCaloData.h:21
 TEveCaloData.h:22
 TEveCaloData.h:23
 TEveCaloData.h:24
 TEveCaloData.h:25
 TEveCaloData.h:26
 TEveCaloData.h:27
 TEveCaloData.h:28
 TEveCaloData.h:29
 TEveCaloData.h:30
 TEveCaloData.h:31
 TEveCaloData.h:32
 TEveCaloData.h:33
 TEveCaloData.h:34
 TEveCaloData.h:35
 TEveCaloData.h:36
 TEveCaloData.h:37
 TEveCaloData.h:38
 TEveCaloData.h:39
 TEveCaloData.h:40
 TEveCaloData.h:41
 TEveCaloData.h:42
 TEveCaloData.h:43
 TEveCaloData.h:44
 TEveCaloData.h:45
 TEveCaloData.h:46
 TEveCaloData.h:47
 TEveCaloData.h:48
 TEveCaloData.h:49
 TEveCaloData.h:50
 TEveCaloData.h:51
 TEveCaloData.h:52
 TEveCaloData.h:53
 TEveCaloData.h:54
 TEveCaloData.h:55
 TEveCaloData.h:56
 TEveCaloData.h:57
 TEveCaloData.h:58
 TEveCaloData.h:59
 TEveCaloData.h:60
 TEveCaloData.h:61
 TEveCaloData.h:62
 TEveCaloData.h:63
 TEveCaloData.h:64
 TEveCaloData.h:65
 TEveCaloData.h:66
 TEveCaloData.h:67
 TEveCaloData.h:68
 TEveCaloData.h:69
 TEveCaloData.h:70
 TEveCaloData.h:71
 TEveCaloData.h:72
 TEveCaloData.h:73
 TEveCaloData.h:74
 TEveCaloData.h:75
 TEveCaloData.h:76
 TEveCaloData.h:77
 TEveCaloData.h:78
 TEveCaloData.h:79
 TEveCaloData.h:80
 TEveCaloData.h:81
 TEveCaloData.h:82
 TEveCaloData.h:83
 TEveCaloData.h:84
 TEveCaloData.h:85
 TEveCaloData.h:86
 TEveCaloData.h:87
 TEveCaloData.h:88
 TEveCaloData.h:89
 TEveCaloData.h:90
 TEveCaloData.h:91
 TEveCaloData.h:92
 TEveCaloData.h:93
 TEveCaloData.h:94
 TEveCaloData.h:95
 TEveCaloData.h:96
 TEveCaloData.h:97
 TEveCaloData.h:98
 TEveCaloData.h:99
 TEveCaloData.h:100
 TEveCaloData.h:101
 TEveCaloData.h:102
 TEveCaloData.h:103
 TEveCaloData.h:104
 TEveCaloData.h:105
 TEveCaloData.h:106
 TEveCaloData.h:107
 TEveCaloData.h:108
 TEveCaloData.h:109
 TEveCaloData.h:110
 TEveCaloData.h:111
 TEveCaloData.h:112
 TEveCaloData.h:113
 TEveCaloData.h:114
 TEveCaloData.h:115
 TEveCaloData.h:116
 TEveCaloData.h:117
 TEveCaloData.h:118
 TEveCaloData.h:119
 TEveCaloData.h:120
 TEveCaloData.h:121
 TEveCaloData.h:122
 TEveCaloData.h:123
 TEveCaloData.h:124
 TEveCaloData.h:125
 TEveCaloData.h:126
 TEveCaloData.h:127
 TEveCaloData.h:128
 TEveCaloData.h:129
 TEveCaloData.h:130
 TEveCaloData.h:131
 TEveCaloData.h:132
 TEveCaloData.h:133
 TEveCaloData.h:134
 TEveCaloData.h:135
 TEveCaloData.h:136
 TEveCaloData.h:137
 TEveCaloData.h:138
 TEveCaloData.h:139
 TEveCaloData.h:140
 TEveCaloData.h:141
 TEveCaloData.h:142
 TEveCaloData.h:143
 TEveCaloData.h:144
 TEveCaloData.h:145
 TEveCaloData.h:146
 TEveCaloData.h:147
 TEveCaloData.h:148
 TEveCaloData.h:149
 TEveCaloData.h:150
 TEveCaloData.h:151
 TEveCaloData.h:152
 TEveCaloData.h:153
 TEveCaloData.h:154
 TEveCaloData.h:155
 TEveCaloData.h:156
 TEveCaloData.h:157
 TEveCaloData.h:158
 TEveCaloData.h:159
 TEveCaloData.h:160
 TEveCaloData.h:161
 TEveCaloData.h:162
 TEveCaloData.h:163
 TEveCaloData.h:164
 TEveCaloData.h:165
 TEveCaloData.h:166
 TEveCaloData.h:167
 TEveCaloData.h:168
 TEveCaloData.h:169
 TEveCaloData.h:170
 TEveCaloData.h:171
 TEveCaloData.h:172
 TEveCaloData.h:173
 TEveCaloData.h:174
 TEveCaloData.h:175
 TEveCaloData.h:176
 TEveCaloData.h:177
 TEveCaloData.h:178
 TEveCaloData.h:179
 TEveCaloData.h:180
 TEveCaloData.h:181
 TEveCaloData.h:182
 TEveCaloData.h:183
 TEveCaloData.h:184
 TEveCaloData.h:185
 TEveCaloData.h:186
 TEveCaloData.h:187
 TEveCaloData.h:188
 TEveCaloData.h:189
 TEveCaloData.h:190
 TEveCaloData.h:191
 TEveCaloData.h:192
 TEveCaloData.h:193
 TEveCaloData.h:194
 TEveCaloData.h:195
 TEveCaloData.h:196
 TEveCaloData.h:197
 TEveCaloData.h:198
 TEveCaloData.h:199
 TEveCaloData.h:200
 TEveCaloData.h:201
 TEveCaloData.h:202
 TEveCaloData.h:203
 TEveCaloData.h:204
 TEveCaloData.h:205
 TEveCaloData.h:206
 TEveCaloData.h:207
 TEveCaloData.h:208
 TEveCaloData.h:209
 TEveCaloData.h:210
 TEveCaloData.h:211
 TEveCaloData.h:212
 TEveCaloData.h:213
 TEveCaloData.h:214
 TEveCaloData.h:215
 TEveCaloData.h:216
 TEveCaloData.h:217
 TEveCaloData.h:218
 TEveCaloData.h:219
 TEveCaloData.h:220
 TEveCaloData.h:221
 TEveCaloData.h:222
 TEveCaloData.h:223
 TEveCaloData.h:224
 TEveCaloData.h:225
 TEveCaloData.h:226
 TEveCaloData.h:227
 TEveCaloData.h:228
 TEveCaloData.h:229
 TEveCaloData.h:230
 TEveCaloData.h:231
 TEveCaloData.h:232
 TEveCaloData.h:233
 TEveCaloData.h:234
 TEveCaloData.h:235
 TEveCaloData.h:236
 TEveCaloData.h:237
 TEveCaloData.h:238
 TEveCaloData.h:239
 TEveCaloData.h:240
 TEveCaloData.h:241
 TEveCaloData.h:242
 TEveCaloData.h:243
 TEveCaloData.h:244
 TEveCaloData.h:245
 TEveCaloData.h:246
 TEveCaloData.h:247
 TEveCaloData.h:248
 TEveCaloData.h:249
 TEveCaloData.h:250
 TEveCaloData.h:251
 TEveCaloData.h:252
 TEveCaloData.h:253
 TEveCaloData.h:254
 TEveCaloData.h:255
 TEveCaloData.h:256
 TEveCaloData.h:257
 TEveCaloData.h:258
 TEveCaloData.h:259
 TEveCaloData.h:260
 TEveCaloData.h:261
 TEveCaloData.h:262
 TEveCaloData.h:263
 TEveCaloData.h:264
 TEveCaloData.h:265
 TEveCaloData.h:266
 TEveCaloData.h:267
 TEveCaloData.h:268
 TEveCaloData.h:269
 TEveCaloData.h:270
 TEveCaloData.h:271
 TEveCaloData.h:272
 TEveCaloData.h:273
 TEveCaloData.h:274
 TEveCaloData.h:275
 TEveCaloData.h:276
 TEveCaloData.h:277
 TEveCaloData.h:278
 TEveCaloData.h:279
 TEveCaloData.h:280
 TEveCaloData.h:281
 TEveCaloData.h:282
 TEveCaloData.h:283
 TEveCaloData.h:284
 TEveCaloData.h:285
 TEveCaloData.h:286
 TEveCaloData.h:287
 TEveCaloData.h:288
 TEveCaloData.h:289
 TEveCaloData.h:290
 TEveCaloData.h:291
 TEveCaloData.h:292
 TEveCaloData.h:293
 TEveCaloData.h:294
 TEveCaloData.h:295
 TEveCaloData.h:296
 TEveCaloData.h:297
 TEveCaloData.h:298
 TEveCaloData.h:299
 TEveCaloData.h:300
 TEveCaloData.h:301
 TEveCaloData.h:302
 TEveCaloData.h:303
 TEveCaloData.h:304
 TEveCaloData.h:305
 TEveCaloData.h:306
 TEveCaloData.h:307
 TEveCaloData.h:308
 TEveCaloData.h:309
 TEveCaloData.h:310
 TEveCaloData.h:311
 TEveCaloData.h:312
 TEveCaloData.h:313
 TEveCaloData.h:314
 TEveCaloData.h:315
 TEveCaloData.h:316
 TEveCaloData.h:317
 TEveCaloData.h:318
 TEveCaloData.h:319
 TEveCaloData.h:320
 TEveCaloData.h:321
 TEveCaloData.h:322
 TEveCaloData.h:323
 TEveCaloData.h:324
 TEveCaloData.h:325