#ifndef HMDCCOSMICSCANDIDATE_H
#define HMDCCOSMICSCANDIDATE_H
#include "hreconstructor.h"
#include "TString.h"
#include "TRandom.h"
#include "hlocation.h"
#include "hgeomvector.h"
using namespace std;
class TH1F;
class TH2F;
class TFile;
class TLine;
class HIterator;
class HCategory;
class HTofHit;
class HTofCluster;
class HMdcSizesCells;
class HMdcSizesCellsLayer;
class HMdcGetContainers;
class HGeomTransform;
class HMdcList12GroupCells;
class HMdcClus;
class HMdcCosmicsCandidate : public HReconstructor {
  protected:
    struct MetaHit {
      Int_t       sec;
      Float_t     tof;
      HGeomVector hit;
      Bool_t      isTOF;    
    };
    MetaHit            metaHits[250];
    Int_t              nMetaHits;
    Int_t              nMetaHitSec[6];
    Int_t              nSectors;
    Float_t            zTofShift;          
    Float_t            zRpcShift;          
    Bool_t             isGeant;
    
    Int_t              nWiresStat[6][4];
    Int_t              numWiresAllMod;
    
    
    HGeomVector        hit1;              
    HGeomVector        hit2;              
    
    struct TrackCand {
      Int_t            metaInd1;          
      Int_t            metaInd2;
      Char_t           modList[6];
      Int_t            nMods;             
      Int_t            nSecs;             
      
      Int_t            nModules;          
      Int_t            nLayers;           
      Int_t            nModSec[6];        
      Int_t            nLaySec[6];        
      Int_t            nWires;            
      Double_t         dCellMean;
      Int_t            nWiresTot;         
      void copy(TrackCand &tc) {
        metaInd1 = tc.metaInd1;
        metaInd2 = tc.metaInd2;
        for(Int_t s=0;s<6;s++) {
          modList[s] = tc.modList[s];
          nModSec[s] = tc.nModSec[s];
          nLaySec[s] = tc.nLaySec[s];
        }
        nMods     = tc.nMods;
        nSecs     = tc.nSecs;
        nModules  = tc.nModules;
        nLayers   = tc.nLayers;
        nWires    = tc.nWires;
        dCellMean = tc.dCellMean;
        nWiresTot = tc.nWiresTot;
      }
      void init(Int_t i1,Int_t i2) {
        metaInd1  = i1;
        metaInd2  = i2;
        nMods     = 0;
        nSecs     = 0;
        nModules  = 0;
        nLayers   = 0;
        nWires    = 0;
        dCellMean = 1.e+30;
        nWiresTot = 0;
        for(Int_t s=0;s<6;s++) modList[s] = nModSec[s] = nLaySec[s] = 0;
      }
      Bool_t trackStatus(void)                 {return nModules>=2 && nLayers>=5 ;}
      void   addMod(Int_t s,Int_t m,Int_t nWr) { 
        if(modList[s]==0) nSecs++;
        modList[s] |= 1<<m;
        nMods++;
        nWiresTot += nWr;
      }
      void   unsetMod(Int_t s,Int_t m)         {if(modStatus(s,m)!=0) {
                                                  modList[s] ^= (1<<m);
                                                  if(modList[s] == 0) nSecs--;} }
      Bool_t secStatus(Int_t s) const          {return modList[s] != 0;}
      Char_t segStatus(Int_t s,Int_t sg) const {return (modList[s]>>(sg*2)) & 3;}
      Char_t modStatus(Int_t s,Int_t m) const  {return (modList[s]>>m) & 1;}
      Int_t  numWiresOutTrack(void) const      {return nWiresTot-nWires;}
      Double_t selectQual(void) const          {return Double_t(nWires)/Double_t(nWiresTot);}
      Int_t  numLayersPerMod(void) const       {return nLayers/nModules;}
    };
    TrackCand          bestTrCand;
    TrackCand          currTrCand;
    
    Double_t           betaMin;
    Double_t           betaMax;
    Float_t            dCellWind[4];       
    Int_t              nLayInModCut;       
    Int_t              maxNWiresOutTr;     
    Double_t           selQualCut;         
    
    Int_t              nEventsTot;
    Int_t              nEventsSel;
    Int_t              nSelTr[9];          
                                           
    Double_t           trackScaling[9];    
                                           
                                           
    Int_t              nSelTrSc[9];
    Int_t              nEventsSelSc;
    
    TRandom            rndm;
    
    HCategory         *pCatTof; 
    UChar_t            useTofCat;
    HCategory         *pCatRpc;
    UChar_t            useRpcCat;
    HCategory         *pCatMdcCal1;
    HLocation          locCal1;
    HCategory         *pCatMdcClus;
    HLocation          locClus;
    HMdcSizesCells*    sizes;              
    HMdcGetContainers* pGetCont;
    Int_t              index;
    Int_t              returnFlag;
    Int_t              mdcsetup[6][4];
    Bool_t             setParContStat;
    
    
    struct LayerSize {
      Double_t             y1;             
      Double_t             y2;             
      Double_t             x2;             
      Double_t             inc;            
      HMdcSizesCellsLayer *pScLay;
    };
    LayerSize          laySize[4];         
    
    TH1F              *hsBeta;
    TH2F              *plStat;
    TH2F              *plQualVsWrLo;
    TH1F              *hsDCell[6][4];
    TH1F              *hsDCellL[6][4][6];
    Bool_t             makeHists;
    TFile             *histFile;
    TString            histFileName;
    TString            histFileOption;
  private:
    void               printParam(void);
    Bool_t             createHists(void);
    void               initVariables(void);
    void               collectTofHits(void);
    void               collectRpcHits(void);
    Bool_t             testBeta(const MetaHit &h1,const MetaHit &h2,Bool_t mHists=kFALSE);
    HMdcClus*          fillCluster(Int_t sec,Int_t seg,HMdcList12GroupCells &listCells,HMdcClus* prevClus);
    Bool_t             fillTrack(TrackCand &bestCand);
    Bool_t             testTrack(void);
    Bool_t             isCrossMod(Int_t sec,Int_t mod);
    void               testSectors(TrackCand &trackCand,Int_t sec,Int_t part=0);
    Bool_t             calcSectors(TrackCand &trackCand);
    
  public:
    HMdcCosmicsCandidate(const Text_t *name, const Text_t *title);
    HMdcCosmicsCandidate(void);
    ~HMdcCosmicsCandidate(void);
    virtual Bool_t     init(void);
    virtual Bool_t     reinit(void);
    virtual Bool_t     finalize(void);
    virtual Int_t      execute(void);
    void               setBetaCut(Double_t min,Double_t max);
    void               setDCellWindow(Float_t wM1,Float_t wM2,Float_t wM3,Float_t wM4);
    void               setNLayPerModCut(Int_t cut)      {nLayInModCut = cut;};
    void               setSelectionCut(Int_t maxLW,Double_t qu) {maxNWiresOutTr = maxLW;
                                                                 selQualCut     = qu;}
    void               makeHistograms(Bool_t fl=kTRUE)  {makeHists = fl;}
    void               setHistFile(const Char_t* dir="./",const Char_t* fileNm="cosmCandHists",
                                   const Char_t* opt="NEW");
    void               setSkipEvent(Bool_t fl=kTRUE)    {returnFlag = fl ? kSkipEvent : 0;}
    void               scaleDownTrack(UInt_t nMdcTr,Double_t sc);
    void               scaleDownTrack(Double_t *sca);
    void               setParContainersStatic(void)     {setParContStat = kTRUE;}
    void               setZShiftForTof(Float_t sh)      {zTofShift = sh;}
    void               setZShiftForRpc(Float_t sh)      {zRpcShift = sh;}
    void               useTofHits(void)                 {useTofCat = 1;}
    void               useRpcHits(void)                 {useRpcCat = 1;}
    void               useTofClusters(void)             {useTofCat = 2;}
    void               useRpcClusters(void)             {useRpcCat = 2;}
 
  ClassDef(HMdcCosmicsCandidate,0)
};
class HCosmicCalibEvSkip : public HReconstructor {
  public:
    HCosmicCalibEvSkip(void);
    HCosmicCalibEvSkip(const Text_t *name, const Text_t *title);
    ~HCosmicCalibEvSkip(void) {}
    virtual Bool_t     init(void)     {return kTRUE;}
    virtual Bool_t     reinit(void)   {return kTRUE;}
    virtual Bool_t     finalize(void) {return kTRUE;}
    virtual Int_t      execute(void);
  
   ClassDef(HCosmicCalibEvSkip,0)
};
  
#endif
 hmdccosmicscandidate.h:10  hmdccosmicscandidate.h:11  hmdccosmicscandidate.h:12  hmdccosmicscandidate.h:13  hmdccosmicscandidate.h:14  hmdccosmicscandidate.h:15  hmdccosmicscandidate.h:16  hmdccosmicscandidate.h:17  hmdccosmicscandidate.h:18  hmdccosmicscandidate.h:19  hmdccosmicscandidate.h:20  hmdccosmicscandidate.h:21  hmdccosmicscandidate.h:22  hmdccosmicscandidate.h:23  hmdccosmicscandidate.h:24  hmdccosmicscandidate.h:25  hmdccosmicscandidate.h:26  hmdccosmicscandidate.h:27  hmdccosmicscandidate.h:28  hmdccosmicscandidate.h:29  hmdccosmicscandidate.h:30  hmdccosmicscandidate.h:31  hmdccosmicscandidate.h:32  hmdccosmicscandidate.h:33  hmdccosmicscandidate.h:34  hmdccosmicscandidate.h:35  hmdccosmicscandidate.h:36  hmdccosmicscandidate.h:37  hmdccosmicscandidate.h:38  hmdccosmicscandidate.h:39  hmdccosmicscandidate.h:40  hmdccosmicscandidate.h:41  hmdccosmicscandidate.h:42  hmdccosmicscandidate.h:43  hmdccosmicscandidate.h:44  hmdccosmicscandidate.h:45  hmdccosmicscandidate.h:46  hmdccosmicscandidate.h:47  hmdccosmicscandidate.h:48  hmdccosmicscandidate.h:49  hmdccosmicscandidate.h:50  hmdccosmicscandidate.h:51  hmdccosmicscandidate.h:52  hmdccosmicscandidate.h:53  hmdccosmicscandidate.h:54  hmdccosmicscandidate.h:55  hmdccosmicscandidate.h:56  hmdccosmicscandidate.h:57  hmdccosmicscandidate.h:58  hmdccosmicscandidate.h:59  hmdccosmicscandidate.h:60  hmdccosmicscandidate.h:61  hmdccosmicscandidate.h:62  hmdccosmicscandidate.h:63  hmdccosmicscandidate.h:64  hmdccosmicscandidate.h:65  hmdccosmicscandidate.h:66  hmdccosmicscandidate.h:67  hmdccosmicscandidate.h:68  hmdccosmicscandidate.h:69  hmdccosmicscandidate.h:70  hmdccosmicscandidate.h:71  hmdccosmicscandidate.h:72  hmdccosmicscandidate.h:73  hmdccosmicscandidate.h:74  hmdccosmicscandidate.h:75  hmdccosmicscandidate.h:76  hmdccosmicscandidate.h:77  hmdccosmicscandidate.h:78  hmdccosmicscandidate.h:79  hmdccosmicscandidate.h:80  hmdccosmicscandidate.h:81  hmdccosmicscandidate.h:82  hmdccosmicscandidate.h:83  hmdccosmicscandidate.h:84  hmdccosmicscandidate.h:85  hmdccosmicscandidate.h:86  hmdccosmicscandidate.h:87  hmdccosmicscandidate.h:88  hmdccosmicscandidate.h:89  hmdccosmicscandidate.h:90  hmdccosmicscandidate.h:91  hmdccosmicscandidate.h:92  hmdccosmicscandidate.h:93  hmdccosmicscandidate.h:94  hmdccosmicscandidate.h:95  hmdccosmicscandidate.h:96  hmdccosmicscandidate.h:97  hmdccosmicscandidate.h:98  hmdccosmicscandidate.h:99  hmdccosmicscandidate.h:100  hmdccosmicscandidate.h:101  hmdccosmicscandidate.h:102  hmdccosmicscandidate.h:103  hmdccosmicscandidate.h:104  hmdccosmicscandidate.h:105  hmdccosmicscandidate.h:106  hmdccosmicscandidate.h:107  hmdccosmicscandidate.h:108  hmdccosmicscandidate.h:109  hmdccosmicscandidate.h:110  hmdccosmicscandidate.h:111  hmdccosmicscandidate.h:112  hmdccosmicscandidate.h:113  hmdccosmicscandidate.h:114  hmdccosmicscandidate.h:115  hmdccosmicscandidate.h:116  hmdccosmicscandidate.h:117  hmdccosmicscandidate.h:118  hmdccosmicscandidate.h:119  hmdccosmicscandidate.h:120  hmdccosmicscandidate.h:121  hmdccosmicscandidate.h:122  hmdccosmicscandidate.h:123  hmdccosmicscandidate.h:124  hmdccosmicscandidate.h:125  hmdccosmicscandidate.h:126  hmdccosmicscandidate.h:127  hmdccosmicscandidate.h:128  hmdccosmicscandidate.h:129  hmdccosmicscandidate.h:130  hmdccosmicscandidate.h:131  hmdccosmicscandidate.h:132  hmdccosmicscandidate.h:133  hmdccosmicscandidate.h:134  hmdccosmicscandidate.h:135  hmdccosmicscandidate.h:136  hmdccosmicscandidate.h:137  hmdccosmicscandidate.h:138  hmdccosmicscandidate.h:139  hmdccosmicscandidate.h:140  hmdccosmicscandidate.h:141  hmdccosmicscandidate.h:142  hmdccosmicscandidate.h:143  hmdccosmicscandidate.h:144  hmdccosmicscandidate.h:145  hmdccosmicscandidate.h:146  hmdccosmicscandidate.h:147  hmdccosmicscandidate.h:148  hmdccosmicscandidate.h:149  hmdccosmicscandidate.h:150  hmdccosmicscandidate.h:151  hmdccosmicscandidate.h:152  hmdccosmicscandidate.h:153  hmdccosmicscandidate.h:154  hmdccosmicscandidate.h:155  hmdccosmicscandidate.h:156  hmdccosmicscandidate.h:157  hmdccosmicscandidate.h:158  hmdccosmicscandidate.h:159  hmdccosmicscandidate.h:160  hmdccosmicscandidate.h:161  hmdccosmicscandidate.h:162  hmdccosmicscandidate.h:163  hmdccosmicscandidate.h:164  hmdccosmicscandidate.h:165  hmdccosmicscandidate.h:166  hmdccosmicscandidate.h:167  hmdccosmicscandidate.h:168  hmdccosmicscandidate.h:169  hmdccosmicscandidate.h:170  hmdccosmicscandidate.h:171  hmdccosmicscandidate.h:172  hmdccosmicscandidate.h:173  hmdccosmicscandidate.h:174  hmdccosmicscandidate.h:175  hmdccosmicscandidate.h:176  hmdccosmicscandidate.h:177  hmdccosmicscandidate.h:178  hmdccosmicscandidate.h:179  hmdccosmicscandidate.h:180  hmdccosmicscandidate.h:181  hmdccosmicscandidate.h:182  hmdccosmicscandidate.h:183  hmdccosmicscandidate.h:184  hmdccosmicscandidate.h:185  hmdccosmicscandidate.h:186  hmdccosmicscandidate.h:187  hmdccosmicscandidate.h:188  hmdccosmicscandidate.h:189  hmdccosmicscandidate.h:190  hmdccosmicscandidate.h:191  hmdccosmicscandidate.h:192  hmdccosmicscandidate.h:193  hmdccosmicscandidate.h:194  hmdccosmicscandidate.h:195  hmdccosmicscandidate.h:196  hmdccosmicscandidate.h:197  hmdccosmicscandidate.h:198  hmdccosmicscandidate.h:199  hmdccosmicscandidate.h:200  hmdccosmicscandidate.h:201  hmdccosmicscandidate.h:202  hmdccosmicscandidate.h:203  hmdccosmicscandidate.h:204  hmdccosmicscandidate.h:205  hmdccosmicscandidate.h:206  hmdccosmicscandidate.h:207  hmdccosmicscandidate.h:208  hmdccosmicscandidate.h:209  hmdccosmicscandidate.h:210  hmdccosmicscandidate.h:211  hmdccosmicscandidate.h:212  hmdccosmicscandidate.h:213  hmdccosmicscandidate.h:214  hmdccosmicscandidate.h:215  hmdccosmicscandidate.h:216  hmdccosmicscandidate.h:217  hmdccosmicscandidate.h:218  hmdccosmicscandidate.h:219  hmdccosmicscandidate.h:220  hmdccosmicscandidate.h:221  hmdccosmicscandidate.h:222  hmdccosmicscandidate.h:223  hmdccosmicscandidate.h:224  hmdccosmicscandidate.h:225  hmdccosmicscandidate.h:226  hmdccosmicscandidate.h:227