//
// Author: Sophia Schade <mailto: schade@physik.hu-berlin.de>
// Update: $Id: AtlPhiFinder.h,v 1.3 2013/08/15 00:00:43 stamm Exp $
// Copyright: 2010 (C) Sophia Schade
//
#ifndef ATLAS_AtlPhiFinder
#define ATLAS_AtlPhiFinder
#ifndef ATLAS_AtlKinFitterTool
#include <AtlKinFitterTool.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TF1.h>
#endif
#include <fstream>
#include <iostream>

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

class AtlPhiFinder : public AtlKinFitterTool {

  public:
    enum EMode { kCutBased, kKinFit };
    EMode fMode;                      // Indicate the mode of running (eg cut-based or kinematic fit)
    
    Bool_t fDebug;                    // Indicate the mode of running (data, or mc with test-histograms,
                                      // true means mctest)
    Bool_t fUseKaonPurity;            // different purity criteria
    
  private:
    ofstream fDebugOutput;            // Debugging output
    // General tuning histograms
    TH1F *fHistKaonMatchingProb;      // Matching probability of Particles matching the reconstructed Kaons    
    TH1F *fHistTrkChi2ovNDoF;         // Chi2/NDoF for tracks surviving the kinematic fitter
    TH1F *fHistVtxChi2ovNDoF;         // Chi2/NDoF for vertices that fulfil Phi hypothesis
    
    // SIGNAL: Phi -> KK
    // ----------------------
    // standard histograms
    TH1F *fHistPhireco_m_KK;          // Reconstructed invariant mass of the Phi decaying into Kaons
    TH1F *fHistPhireco_pt_KK;         // Reconstructed azimuth angle of the Phi decaying into Kaons
    TH1F *fHistPhireco_phi_KK;        // Reconstructed Pt of the Phi decaying into Kaons
    TH1F *fHistPhireco_eta_KK;        // Reconstructed Eta of the Phi decaying into Kaons
    TH1F *fHistPhireco_N_KK;          // Multilplicity of reconstructed Phi decaying into K+K-r
    TH1F *fHistPhireco_TRK;           // Trks and their particle
    
    // kaons histograms
    TH1F *fHistPhireco_KK_OpeningAngle; // Angle between K+ K-
    TH1F *fHistPhireco_DecayAngle_KK;   // Decay Angle between Phi and K+ in Phis restframe
    TH1F *fHistPhireco_KPlus_pt;        // recosntructed pt  of K+ 
    TH1F *fHistPhireco_KMinus_pt;       // recosntructed pt  of K-  
    TH1F *fHistPhireco_KPlus_eta;       // recosntructed eta of K+  
    TH1F *fHistPhireco_KMinus_eta;      // recosntructed eta of K-   
    TH1F *fHistPhireco_KPlus_phi;       // recosntructed phi of K+   
    TH1F *fHistPhireco_KMinus_phi;      // recosntructed phi of K-   

    // BACKGROUND: Like-sign approach (Phi -> K+K+/ K-K-)
    // ----------------------------------------------------
    TList *fBkgPhiDecays;              // List of combinatorial bkg
    // Phi -> KK (Bkg)
    TH1F *fHistPhireco_m_KK_bkg;       // Reconstructed invariant mass of the Phi decaying into Kaons
    TH1F *fHistPhireco_pt_KK_bkg;      // Reconstructed azimuth angle of the Phi decaying into Kaons
    TH1F *fHistPhireco_phi_KK_bkg;     // Reconstructed Pt of the Phi decaying into Kaons
    TH1F *fHistPhireco_eta_KK_bkg;     // Reconstructed Eta of the Phi decaying into Kaons
    TH1F *fHistPhireco_N_KK_bkg;       // Multilplicity of reconstructed Phi decaying into K+K-
    
    // kaons histograms
    TH1F *fHistPhireco_KK_OpeningAngle_bkg;  // Angle between K+ K+ or K- K- (Bkg)
    TH1F *fHistPhireco_KK_DecayAngle_bkg; // Angle between Phi and  K+/K- (Bkg) in Phis restframe
    TH1F *fHistPhireco_KPlus_pt_bkg;         // recosntructed pt  of K+ 
    TH1F *fHistPhireco_KMinus_pt_bkg;        // recosntructed pt  of K-  
    TH1F *fHistPhireco_KPlus_eta_bkg;        // recosntructed eta of K+  
    TH1F *fHistPhireco_KMinus_eta_bkg;       // recosntructed eta of K-   
    TH1F *fHistPhireco_KPlus_phi_bkg;        // recosntructed phi of K+   
    TH1F *fHistPhireco_KMinus_phi_bkg;       // recosntructed phi of K-

    // ====================
    //    MC TRUTH STUDY
    // ====================

    TH1F *fHistPhi_MC_KKsame;             // MC Truth Matching, pdg codes: 2 Kaons same Mother
    TH1F *fHistPhi_MC_same;               // MC Truth Matching, pdg codes: same Mother
    TH1F *fHistPhireco_m_KK_diff;         // Phi mass (fg-bg)
    TH1F *fHistPhireco_MCParticle_Prob;   // Probability to get right MC Particle
    TH1F *fHistPhireco_Chi_TrkFit;        // Chi^2 from Track with Vtx Fit
    TH1F *fHistPhireco_Chi_VtxFit;        // Chi^2 from Vertex  Fit

    // SIGNAL: Reconstructed Phi candidates that  not fail the MCTruthMatching
    // ---------------------------------------------------------------------

    // standard histograms
    TH1F *fHistPhireco_m_MCsignal;       // Reconstructed invariant mass of the Kaon decaying into kaons
    TH1F *fHistPhireco_pt_MCsignal;      // Reconstructed azimuth angle of the Kaon decaying into kaons
    TH1F *fHistPhireco_phi_MCsignal;     // Reconstructed Pt of the Kaon decaying into kaons
    TH1F *fHistPhireco_eta_MCsignal;     // Reconstructed Eta of the Kaon decaying into kaons
    TH1F *fHistPhireco_N_MCsignal;       // Multilplicity of reconstructed Phi decaying into K+K-

    TH1F *fHistPhireco_Number_Hits_MCsignal;       
    TH1F *fHistPhireco_ChiNDoF_Trks_MCsignal;
    
    TH1F *fHistPhireco_OpeningAngle_MCsignal;   // Opening Angle of the Kaon tracks in the lab frame
    TH1F *fHistPhireco_DecayAngle_MCsignal;     // Decay angle between Phi and K+ in Phis restframe

    // BACKGROUND: Reconstructed Phi candidates that fail the MCTruthMatching
    // ----------------------------------------------------------------------

    TH1F *fHistPhireco_m_MCbkg;       // Reconstructed invariant mass of Phi-candidate
    TH1F *fHistPhireco_pt_MCbkg;      // Reconstructed azimuth angle of Phi-candidate
    TH1F *fHistPhireco_phi_MCbkg;     // Reconstructed Pt of Phi-candidate
    TH1F *fHistPhireco_eta_MCbkg;     // Reconstructed Eta of Phi-candidate
    TH1F *fHistPhireco_N_MCbkg;       // Multilplicity of Phi-candidate

    TH1F *fHistPhireco_Number_Hits_MCbkg;       
    TH1F *fHistPhireco_ChiNDoF_Trks_MCbkg;
    
    TH1F *fHistPhireco_OpeningAngle_MCbkg;   // Opening Angle of the Kaon tracks in the lab frame
    TH1F *fHistPhireco_DecayAngle_MCbkg;     // Decay angle between Phi and K+ in Phis restframe

    // dE/dx HISTOGRAMS: Needed for Kaon tagging
    // -----------------------------------------
    
    TH2F *fHistdEdx_pmag;                    // dE/dx as a function of the momentum for all tracks
                                             // leaving the primary vertex
    TH2F *fHistdEdx_pmag_sig;
    TH2F *fHistdEdx_pmag_MCsig;
    TH2F *fHistdEdx_pmag_bkg;
    TH2F *fHistdEdx_pmag_MCbkg;
    TH2F *fHistdEdx_betagamma;

    TH1F *fHistdEdx_diff_sig;
    TH1F *fHistdEdx_diff_MCsig;              // difference between measured and calculated dEdx   
    TH1F *fHistdEdx_diff_bkg;                // for MC Signal and Background
    TH1F *fHistdEdx_diff_MCbkg; 

    TF1  *fBetheBloch;                       // Fit function
    
  public:
  // selection cuts
  Float_t              fSignalKaon_Pt_min;          // Min. Pt of signal electrons
  Float_t              fSignalKaon_Pt_max;          // Max. Pt of signal electrons
  Float_t              fSignalKaon_Eta_min;         // Min. eta of signal electrons
  Float_t              fSignalKaon_Eta_max;         // Max. eta of signal electrons
  Float_t              fSignalKaon_EtCone20_max;    // Max. Et within a cone of 0.2

  Float_t              fPhi_M_min;                  // Lower boundary for allowed Phi mass range
  Float_t              fPhi_M_max;                  // Upper boundary for allowed Phi mass range
  Float_t              fm_K;                        // Kaon mass hypothesis
  Float_t              fm_Phi;                      // Phi mass from PDG

  Float_t              fVertexChi2ovNDoF_max;       // Quality of Vertex fit
  Float_t              fTrackChi2ovNDoF_max;        // Quality of track fit
  Float_t              fAngle_max;                  // Angle criterion
  Float_t              fMatchingProb_min;           // Quality of MC Matching
  Float_t              fKinFitPvalue_min;           // min prob(chi2,ndof) from kinfit

  Float_t              fdEdx_Delta_max;
  Float_t              fPmag_min;
  Float_t              fPmag_max;

  Float_t              fPhi_Eta_min;
  Float_t              fPhi_Eta_max;

  Float_t              fdEdx_p0;                    // Values from Bethe-Bloch like Fit
  Float_t              fdEdx_p1;
  Float_t              fdEdx_p2;
  Float_t              fdEdx_p3;
  Float_t              fdEdx_p4;
  
  public:
  AtlPhiFinder(const char* name, const char* title);
  virtual ~AtlPhiFinder();
  virtual void SetBranchStatus();
  virtual void BookHistograms();
  virtual void FillHistograms();
  virtual void SetCutDefaults();
  virtual Bool_t AnalyzeEvent();
  void ReconstructPhiCutBased(void);
  void ReconstructPhiKinFit(void);
  void PrintCutValues(void);
  void Terminate();
  Int_t DoTruthMatch(AtlIDTrack *trk1, AtlIDTrack *trk2);
  void FillHistograms_MCTruthStudy(AtlPhiDecayKK *PhiCandidate, char type);
  Double_t* GetLogBins(Int_t BinCount, Double_t MinBinExpo, Double_t MaxBinExpo);
  Float_t Delta_dEdx(AtlIDTrack *trk1);
  void FindSharedTracks();
  
  inline void SetMode(EMode mode = kKinFit)
  { fMode = mode; }
  // Test-mode or Run-mode
  inline void SetDebugOutput(Bool_t mode = kFALSE)
  { fDebug = mode; }
  inline Bool_t IsDebugRun() { return fDebug; }
        
  ClassDef(AtlPhiFinder,0) // Phi decay finder class
};
#endif

 AtlPhiFinder.h:1
 AtlPhiFinder.h:2
 AtlPhiFinder.h:3
 AtlPhiFinder.h:4
 AtlPhiFinder.h:5
 AtlPhiFinder.h:6
 AtlPhiFinder.h:7
 AtlPhiFinder.h:8
 AtlPhiFinder.h:9
 AtlPhiFinder.h:10
 AtlPhiFinder.h:11
 AtlPhiFinder.h:12
 AtlPhiFinder.h:13
 AtlPhiFinder.h:14
 AtlPhiFinder.h:15
 AtlPhiFinder.h:16
 AtlPhiFinder.h:17
 AtlPhiFinder.h:18
 AtlPhiFinder.h:19
 AtlPhiFinder.h:20
 AtlPhiFinder.h:21
 AtlPhiFinder.h:22
 AtlPhiFinder.h:23
 AtlPhiFinder.h:24
 AtlPhiFinder.h:25
 AtlPhiFinder.h:26
 AtlPhiFinder.h:27
 AtlPhiFinder.h:28
 AtlPhiFinder.h:29
 AtlPhiFinder.h:30
 AtlPhiFinder.h:31
 AtlPhiFinder.h:32
 AtlPhiFinder.h:33
 AtlPhiFinder.h:34
 AtlPhiFinder.h:35
 AtlPhiFinder.h:36
 AtlPhiFinder.h:37
 AtlPhiFinder.h:38
 AtlPhiFinder.h:39
 AtlPhiFinder.h:40
 AtlPhiFinder.h:41
 AtlPhiFinder.h:42
 AtlPhiFinder.h:43
 AtlPhiFinder.h:44
 AtlPhiFinder.h:45
 AtlPhiFinder.h:46
 AtlPhiFinder.h:47
 AtlPhiFinder.h:48
 AtlPhiFinder.h:49
 AtlPhiFinder.h:50
 AtlPhiFinder.h:51
 AtlPhiFinder.h:52
 AtlPhiFinder.h:53
 AtlPhiFinder.h:54
 AtlPhiFinder.h:55
 AtlPhiFinder.h:56
 AtlPhiFinder.h:57
 AtlPhiFinder.h:58
 AtlPhiFinder.h:59
 AtlPhiFinder.h:60
 AtlPhiFinder.h:61
 AtlPhiFinder.h:62
 AtlPhiFinder.h:63
 AtlPhiFinder.h:64
 AtlPhiFinder.h:65
 AtlPhiFinder.h:66
 AtlPhiFinder.h:67
 AtlPhiFinder.h:68
 AtlPhiFinder.h:69
 AtlPhiFinder.h:70
 AtlPhiFinder.h:71
 AtlPhiFinder.h:72
 AtlPhiFinder.h:73
 AtlPhiFinder.h:74
 AtlPhiFinder.h:75
 AtlPhiFinder.h:76
 AtlPhiFinder.h:77
 AtlPhiFinder.h:78
 AtlPhiFinder.h:79
 AtlPhiFinder.h:80
 AtlPhiFinder.h:81
 AtlPhiFinder.h:82
 AtlPhiFinder.h:83
 AtlPhiFinder.h:84
 AtlPhiFinder.h:85
 AtlPhiFinder.h:86
 AtlPhiFinder.h:87
 AtlPhiFinder.h:88
 AtlPhiFinder.h:89
 AtlPhiFinder.h:90
 AtlPhiFinder.h:91
 AtlPhiFinder.h:92
 AtlPhiFinder.h:93
 AtlPhiFinder.h:94
 AtlPhiFinder.h:95
 AtlPhiFinder.h:96
 AtlPhiFinder.h:97
 AtlPhiFinder.h:98
 AtlPhiFinder.h:99
 AtlPhiFinder.h:100
 AtlPhiFinder.h:101
 AtlPhiFinder.h:102
 AtlPhiFinder.h:103
 AtlPhiFinder.h:104
 AtlPhiFinder.h:105
 AtlPhiFinder.h:106
 AtlPhiFinder.h:107
 AtlPhiFinder.h:108
 AtlPhiFinder.h:109
 AtlPhiFinder.h:110
 AtlPhiFinder.h:111
 AtlPhiFinder.h:112
 AtlPhiFinder.h:113
 AtlPhiFinder.h:114
 AtlPhiFinder.h:115
 AtlPhiFinder.h:116
 AtlPhiFinder.h:117
 AtlPhiFinder.h:118
 AtlPhiFinder.h:119
 AtlPhiFinder.h:120
 AtlPhiFinder.h:121
 AtlPhiFinder.h:122
 AtlPhiFinder.h:123
 AtlPhiFinder.h:124
 AtlPhiFinder.h:125
 AtlPhiFinder.h:126
 AtlPhiFinder.h:127
 AtlPhiFinder.h:128
 AtlPhiFinder.h:129
 AtlPhiFinder.h:130
 AtlPhiFinder.h:131
 AtlPhiFinder.h:132
 AtlPhiFinder.h:133
 AtlPhiFinder.h:134
 AtlPhiFinder.h:135
 AtlPhiFinder.h:136
 AtlPhiFinder.h:137
 AtlPhiFinder.h:138
 AtlPhiFinder.h:139
 AtlPhiFinder.h:140
 AtlPhiFinder.h:141
 AtlPhiFinder.h:142
 AtlPhiFinder.h:143
 AtlPhiFinder.h:144
 AtlPhiFinder.h:145
 AtlPhiFinder.h:146
 AtlPhiFinder.h:147
 AtlPhiFinder.h:148
 AtlPhiFinder.h:149
 AtlPhiFinder.h:150
 AtlPhiFinder.h:151
 AtlPhiFinder.h:152
 AtlPhiFinder.h:153
 AtlPhiFinder.h:154
 AtlPhiFinder.h:155
 AtlPhiFinder.h:156
 AtlPhiFinder.h:157
 AtlPhiFinder.h:158
 AtlPhiFinder.h:159
 AtlPhiFinder.h:160
 AtlPhiFinder.h:161
 AtlPhiFinder.h:162
 AtlPhiFinder.h:163
 AtlPhiFinder.h:164
 AtlPhiFinder.h:165
 AtlPhiFinder.h:166
 AtlPhiFinder.h:167
 AtlPhiFinder.h:168
 AtlPhiFinder.h:169
 AtlPhiFinder.h:170
 AtlPhiFinder.h:171
 AtlPhiFinder.h:172
 AtlPhiFinder.h:173
 AtlPhiFinder.h:174
 AtlPhiFinder.h:175
 AtlPhiFinder.h:176
 AtlPhiFinder.h:177
 AtlPhiFinder.h:178
 AtlPhiFinder.h:179
 AtlPhiFinder.h:180
 AtlPhiFinder.h:181
 AtlPhiFinder.h:182
 AtlPhiFinder.h:183
 AtlPhiFinder.h:184
 AtlPhiFinder.h:185
 AtlPhiFinder.h:186
 AtlPhiFinder.h:187
 AtlPhiFinder.h:188
 AtlPhiFinder.h:189
 AtlPhiFinder.h:190
 AtlPhiFinder.h:191
 AtlPhiFinder.h:192
 AtlPhiFinder.h:193
 AtlPhiFinder.h:194
 AtlPhiFinder.h:195
 AtlPhiFinder.h:196
 AtlPhiFinder.h:197
 AtlPhiFinder.h:198
 AtlPhiFinder.h:199
 AtlPhiFinder.h:200
 AtlPhiFinder.h:201
 AtlPhiFinder.h:202