//
//    Author: Felix Thomas <mailto: Felix.Thomas@mailbox.tu-dresden.de>
//    Update: $Id: AtlTopPairFinder.h,v 1.14 2013/08/15 00:00:43 stamm Exp $
//    Copyright: 2010 (C) Felix Thomas
//

#ifndef ATLAS_AtlTopPairFinder
#define ATLAS_AtlTopPairFinder
#ifndef ATLAS_AtlKinFitterTool
#include <AtlKinFitterTool.h>
#endif
#ifndef HEP_HepDatabasePDG
#include <HepDatabasePDG.h>
#endif
#ifndef ROOT_AtlSelector
#include <AtlSelector.h>
#endif
#ifndef ATLAS_AtlTopPairDocumenter
#include <AtlTopPairDocumenter.h>
#endif
#include <fstream>
#include <iostream>

class TH1F;
class TH2F;
class AtlEvent;
class TTree;
class TString;
class TROOT;
class TFile;

class AtlTopPairFinder : public AtlKinFitterTool {

    AtlTopPair* fBestTopPair;
    
    TList *fLeptons;                            // Merged list of electrons and muons
    TList *fJets;                               // List of light jets
    TList *fKinFitJets;                         // List of jets that is used by the kinematic fitter
    TList *fBJets;                              // List of b-Jets
    
    TVector2 fEtMiss;                                          
    Float_t  fEtMissMag;                         // Missing ET Magnitude
    Float_t  fEtMissPhi;                         // Missing ET Phi
    
    Int_t    fNTopPairCandidates;                // Number of Top Pair Candidates in Event
    Double_t fBestChi2Prob;                              
    // Objects that are used in the fitting procedure
    HepParticle *fLepton;   // Current lepton used as input for the fit
    HepParticle *fNeutrino; // Current neutrino (missing Et) used as input for the fit                                                                         
    AtlJet      *fLepBJet;  // Current b-jet candidate used as input for the fit
    AtlJet      *fHadBJet;  // Currend b-jet candidate used as input for the fit
    AtlJet      *fHadJet1;  // Current light jet candidate used as input for the fit
    AtlJet      *fHadJet2;  // Currend light jet candidate used as input for the fit

    TMatrixD cov_lep;
    TMatrixD cov_nu;
    TMatrixD cov_LepBJet;
    TMatrixD cov_HadJet1;
    TMatrixD cov_HadJet2;
    TMatrixD cov_HadBJet;


    // Pull matrices
    TMatrixD fLeptonpull;
    TMatrixD fNeutrinopull;
    TMatrixD fLepbjetpull; 
    TMatrixD fHadjet1pull; 
    TMatrixD fHadjet2pull; 
    TMatrixD fHadbjetpull;
    
    
  // Histograms

    AtlTopPairDocumenter *fAllTopPairDocumenter;
    AtlTopPairDocumenter *fElectronChannelTopPairDocumenter;
    AtlTopPairDocumenter *fMuonChannelTopPairDocumenter;

  // All decays
    TH1F *fHistNbIterAll;                                // Nr. of Iterations of all fits
    TH1F *fHistNbIterConv;                               // Nr. of Iterations of converging fits
    TH1F *fHistNTopPairs;                                // Nr. of top pairs satisfying the cut
    TH1F *fHistNTopPairCandidates;
    TH2F *fHistTopPairsVsCandidates;
    TH2F *fHistChi2ProbVsCandidates;
    TH2F *fHistChi2VsCandidates;
    TH2F *fHistSimpleChi2VsChi2;

 public:
 
    enum ETmassConstraint {
    kSameTmass,                                             // both tops have to have the same mass
    kFixedTmass                                             // top mass is fixed on value fTop_Mass
    };

    AtlTopPairFinder::ETmassConstraint  fTmassconstraint;   // specifies which constraint is used
    Double_t      fMaxChi2;		                            // Sets Max. Chi2 of fit
    Double_t      fMinChi2Prob;                             // Sets Min. Chi2Prob of fit
    
    // True W and top masses for the fit
    Double_t fW_Mass;     // Pole mass of the W used in  the fit
    Double_t fW_Width;    // Width of the W used in the fit
    Double_t fTop_Mass;   // Pole mass of the t-quark used in the fit
    Double_t fTop_Width;  // Width of the t-quark used in the fit


    // variables regarding the selection cuts    
    Bool_t   fQuietMode;

    // Switch for histogram filling
    Bool_t fAlwaysFillHistos; // if this is kTRUE, it also fills histograms if
                              // fParent->PassedSelection() == kFALSE or fQuietMode == kTRUE
    
    // BJet selection cuts   
    AtlBTag::ETagger fBJetTagger;           // Taggerinformation for b-jets
    Float_t          fBJetWeightMin;             // minimum weight to be a b-jet
    Int_t            fIterMax;
    Int_t	        fBJets_N_Min;                  // Minimum number of required b-jets
    Int_t            fBJets_N_Min_InHadCombo;  // Minimum Number of required b-jets in combo
    Int_t            fJets_N_Max;                    // Take care that the jet list is sorted. The fitter takes only the first fJets_N_Max number of jets from the fJets list.
    Bool_t           fApplyBTagInReco;        // Is b-tag information applied in the reconstruction?
    
    // Additional Cuts for Jets used in Reconstruction
    Bool_t fIsFwdElectronMode; // Use only forward electrons when analyzing jet-electrons? If set to kFALSE, only central electrons will be analyzed.
    
 public:

    AtlTopPairFinder(const char* name, const char* title);
    virtual ~AtlTopPairFinder();
    virtual void SetBranchStatus();
    virtual void BookHistograms();
    virtual void FillHistograms();
    virtual void SetCutDefaults();
    virtual void Clear();
    virtual void Print() const;
    virtual Bool_t AnalyzeEvent();
    virtual void Terminate();
    
    inline AtlTopPair* GetBestTopPair() { return fBestTopPair; }
    
    inline TList* GetKinFitJets() { return fKinFitJets; }
    inline Int_t GetJets_N_Max() { return fJets_N_Max; }
    inline void SetMinChi2Prob(Double_t value) { fMinChi2Prob = value; }
    inline void SetJets(TList* list) { fJets = list; }
    inline void SetKinFitJets(TList* list) { fKinFitJets = list; }
    inline void SetLeptons(TList* list) { fLeptons = list; }
    inline void SetEtmiss(TVector2 etmiss) { fEtMiss = etmiss; }
    inline void SetBJetTagger(AtlBTag::ETagger tagger) { fBJetTagger = tagger; }
    inline void SetBJetWeightMin(Float_t value) { fBJetWeightMin = value; }

 private:
    void    InitEvent();
    void    ReconstructionKinFit(TList* Leptons, TList* LepBJets, TList* HadJets1, TList* HadJets2, TList* HadBJets);
    void    DoFit();
    Double_t GetEtaNuStartingValue(TLorentzVector PBjet, TLorentzVector PLepton, TVector2 ETMiss);
    
    ClassDef(AtlTopPairFinder,0)  // Atlas Top Pair Finder
};
#endif
 AtlTopPairFinder.h:1
 AtlTopPairFinder.h:2
 AtlTopPairFinder.h:3
 AtlTopPairFinder.h:4
 AtlTopPairFinder.h:5
 AtlTopPairFinder.h:6
 AtlTopPairFinder.h:7
 AtlTopPairFinder.h:8
 AtlTopPairFinder.h:9
 AtlTopPairFinder.h:10
 AtlTopPairFinder.h:11
 AtlTopPairFinder.h:12
 AtlTopPairFinder.h:13
 AtlTopPairFinder.h:14
 AtlTopPairFinder.h:15
 AtlTopPairFinder.h:16
 AtlTopPairFinder.h:17
 AtlTopPairFinder.h:18
 AtlTopPairFinder.h:19
 AtlTopPairFinder.h:20
 AtlTopPairFinder.h:21
 AtlTopPairFinder.h:22
 AtlTopPairFinder.h:23
 AtlTopPairFinder.h:24
 AtlTopPairFinder.h:25
 AtlTopPairFinder.h:26
 AtlTopPairFinder.h:27
 AtlTopPairFinder.h:28
 AtlTopPairFinder.h:29
 AtlTopPairFinder.h:30
 AtlTopPairFinder.h:31
 AtlTopPairFinder.h:32
 AtlTopPairFinder.h:33
 AtlTopPairFinder.h:34
 AtlTopPairFinder.h:35
 AtlTopPairFinder.h:36
 AtlTopPairFinder.h:37
 AtlTopPairFinder.h:38
 AtlTopPairFinder.h:39
 AtlTopPairFinder.h:40
 AtlTopPairFinder.h:41
 AtlTopPairFinder.h:42
 AtlTopPairFinder.h:43
 AtlTopPairFinder.h:44
 AtlTopPairFinder.h:45
 AtlTopPairFinder.h:46
 AtlTopPairFinder.h:47
 AtlTopPairFinder.h:48
 AtlTopPairFinder.h:49
 AtlTopPairFinder.h:50
 AtlTopPairFinder.h:51
 AtlTopPairFinder.h:52
 AtlTopPairFinder.h:53
 AtlTopPairFinder.h:54
 AtlTopPairFinder.h:55
 AtlTopPairFinder.h:56
 AtlTopPairFinder.h:57
 AtlTopPairFinder.h:58
 AtlTopPairFinder.h:59
 AtlTopPairFinder.h:60
 AtlTopPairFinder.h:61
 AtlTopPairFinder.h:62
 AtlTopPairFinder.h:63
 AtlTopPairFinder.h:64
 AtlTopPairFinder.h:65
 AtlTopPairFinder.h:66
 AtlTopPairFinder.h:67
 AtlTopPairFinder.h:68
 AtlTopPairFinder.h:69
 AtlTopPairFinder.h:70
 AtlTopPairFinder.h:71
 AtlTopPairFinder.h:72
 AtlTopPairFinder.h:73
 AtlTopPairFinder.h:74
 AtlTopPairFinder.h:75
 AtlTopPairFinder.h:76
 AtlTopPairFinder.h:77
 AtlTopPairFinder.h:78
 AtlTopPairFinder.h:79
 AtlTopPairFinder.h:80
 AtlTopPairFinder.h:81
 AtlTopPairFinder.h:82
 AtlTopPairFinder.h:83
 AtlTopPairFinder.h:84
 AtlTopPairFinder.h:85
 AtlTopPairFinder.h:86
 AtlTopPairFinder.h:87
 AtlTopPairFinder.h:88
 AtlTopPairFinder.h:89
 AtlTopPairFinder.h:90
 AtlTopPairFinder.h:91
 AtlTopPairFinder.h:92
 AtlTopPairFinder.h:93
 AtlTopPairFinder.h:94
 AtlTopPairFinder.h:95
 AtlTopPairFinder.h:96
 AtlTopPairFinder.h:97
 AtlTopPairFinder.h:98
 AtlTopPairFinder.h:99
 AtlTopPairFinder.h:100
 AtlTopPairFinder.h:101
 AtlTopPairFinder.h:102
 AtlTopPairFinder.h:103
 AtlTopPairFinder.h:104
 AtlTopPairFinder.h:105
 AtlTopPairFinder.h:106
 AtlTopPairFinder.h:107
 AtlTopPairFinder.h:108
 AtlTopPairFinder.h:109
 AtlTopPairFinder.h:110
 AtlTopPairFinder.h:111
 AtlTopPairFinder.h:112
 AtlTopPairFinder.h:113
 AtlTopPairFinder.h:114
 AtlTopPairFinder.h:115
 AtlTopPairFinder.h:116
 AtlTopPairFinder.h:117
 AtlTopPairFinder.h:118
 AtlTopPairFinder.h:119
 AtlTopPairFinder.h:120
 AtlTopPairFinder.h:121
 AtlTopPairFinder.h:122
 AtlTopPairFinder.h:123
 AtlTopPairFinder.h:124
 AtlTopPairFinder.h:125
 AtlTopPairFinder.h:126
 AtlTopPairFinder.h:127
 AtlTopPairFinder.h:128
 AtlTopPairFinder.h:129
 AtlTopPairFinder.h:130
 AtlTopPairFinder.h:131
 AtlTopPairFinder.h:132
 AtlTopPairFinder.h:133
 AtlTopPairFinder.h:134
 AtlTopPairFinder.h:135
 AtlTopPairFinder.h:136
 AtlTopPairFinder.h:137
 AtlTopPairFinder.h:138
 AtlTopPairFinder.h:139
 AtlTopPairFinder.h:140
 AtlTopPairFinder.h:141
 AtlTopPairFinder.h:142
 AtlTopPairFinder.h:143
 AtlTopPairFinder.h:144
 AtlTopPairFinder.h:145
 AtlTopPairFinder.h:146
 AtlTopPairFinder.h:147
 AtlTopPairFinder.h:148
 AtlTopPairFinder.h:149
 AtlTopPairFinder.h:150
 AtlTopPairFinder.h:151
 AtlTopPairFinder.h:152
 AtlTopPairFinder.h:153
 AtlTopPairFinder.h:154
 AtlTopPairFinder.h:155
 AtlTopPairFinder.h:156
 AtlTopPairFinder.h:157
 AtlTopPairFinder.h:158