// Author: Sebastian Beumler <mailto: beumler@physik.hu-berlin.de>
// Update: $Id: AtlK0sFinder.h,v 1.3 2013/08/15 00:00:42 stamm Exp $
// Copyright: 2010 (C) Sebastian Beumler
//
#ifndef ATLAS_AtlK0sFinder
#define ATLAS_AtlK0sFinder
#ifndef ATLAS_AtlKinFitterTool
#include <AtlKinFitterTool.h>
#include <TH1F.h>
#include <TH2F.h>
#endif
#include <fstream>
#include <iostream>

using namespace std;

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

class AtlK0sFinder : public AtlKinFitterTool {
    
  public:
    enum EMode { kCutBased, kKinFit };
    EMode  fMode;              // Indicate the mode of running (eg cut-based or kinematic fit)
    Bool_t fDebugMode;         // Indicate the mode of running (if debug output should be available)
    
  private:
    ofstream fDebugOutput;     // Debugging output
    Int_t    fNK0s;


    // Bkg decay angle analysis
    Int_t NLambdapos, NLambdaBarpos, NPiPluspos, NProtonpos;
    Int_t NLambdaneg, NLambdaBarneg, NPiPlusneg, NProtonneg;

    
    // General tuning histograms
    TH1F *fHistPionMatchingProb;         // Matching probability of Particles matching the reconstructed Pions    
    TH1F *fHistTrkChi2ovNDoF;            // Chi2/NDoF for tracks surviving the kinematic fitter
    TH1F *fHistVtxChi2ovNDoF;            // Chi2/NDoF for vertices that fulfil K0s hypothesis

    
    // SIGNAL: K0s -> pipi
    // -----------------------

    TH1F *fHistK0sreco_m_PiPi;       // Reconstructed invariant mass of the Kaon decaying into pions
    TH1F *fHistK0sreco_pt_PiPi;      // Reconstructed azimuth angle of the Kaon decaying into pions
    TH1F *fHistK0sreco_phi_PiPi;     // Reconstructed Pt of the Kaon decaying into pions
    TH1F *fHistK0sreco_eta_PiPi;     // Reconstructed Eta of the Kaon decaying into pions
    TH1F *fHistK0sreco_N_PiPi;       // Multilplicity of reconstructed K0s decaying into pi+pi-r
    TH1F *fHistK0sreco_tau_PiPi;      // Lifetime of reconstructed K0s
    TH1F *fHistK0sreco_DecayLength_PiPi;    // DecayLength of reconstructed K0s
    TH1F *fHistK0sreco_OpeningAngle_PiPi;   // Opening Angle of the pion tracks in the lab frame
    TH1F *fHistK0sreco_DecayAngle_PiPi;     //Decay angle between K0s and pi+ in K0s' restframe
    TH1F *fHistK0sreco_PointingAngle_PiPi;  //cos of angle between reconstructed K0s' momentum and vec(PV)-vec(SecVtxa)
    TH1F *fHistK0sreco_R_vtx;               // radial distance from vertex to beam pipe
    TH2F *fHistK0sreco_RvtxMee;             // 2d histogram for r_vtx and M_ee inv. Mass
    // pion histograms
    TH1F *fHistK0sreco_PiPlus_pt;    // recosntructed pt  of pi+ 
    TH1F *fHistK0sreco_PiMinus_pt;   // recosntructed pt  of pi-  
    TH1F *fHistK0sreco_PiPlus_eta;   // recosntructed eta of pi+  
    TH1F *fHistK0sreco_PiMinus_eta;  // recosntructed eta of pi-   
    TH1F *fHistK0sreco_PiPlus_phi;   // recosntructed phi of pi+   
    TH1F *fHistK0sreco_PiMinus_phi;  // recosntructed phi of pi-   
    TH2F *fHistK0sreco_Pions_PtEta;  // recosntructed Pt and eta of both Pions
    

    
    // BACKGROUND: Like-sign approach (K0s->pi+pi+ \ K0s->pi-pi-)
    // -----------------------------------------------------------
    TList *fBkgK0sDecays;            // List of like sign K0s decays
    // K0s -> pipi(Bkg)
    TH1F *fHistK0sreco_m_LS_bkg;       // invariant mass of the Kaon decaying into equally signed pions
    TH1F *fHistK0sreco_pt_LS_bkg;      // azimuth angle of the Kaon decaying into equally signed pions
    TH1F *fHistK0sreco_phi_LS_bkg;     // Pt of the Kaon decaying into equally signed pions
    TH1F *fHistK0sreco_eta_LS_bkg;     // Eta of the Kaon decaying into equally signed pions
    TH1F *fHistK0sreco_N_LS_bkg;       // Multilplicity of reconstructed K0s decaying into equally signed pions
    TH1F *fHistK0sreco_tau_LS_bkg;     // Lifetime of reconstructed K0s decaying into equally signed pions
    TH1F *fHistK0sreco_DecayLength_LS_bkg;  // DecayLength of reconstructed K0s decaying ito equaly signed pions
    
    // pion histograms
    TH1F *fHistK0sreco_PiPlus_pt_LS_bkg;    // recosntructed pt  of pi+ 
    TH1F *fHistK0sreco_PiMinus_pt_LS_bkg;   // recosntructed pt  of pi-  
    TH1F *fHistK0sreco_PiPlus_eta_LS_bkg;   // recosntructed eta of pi+  
    TH1F *fHistK0sreco_PiMinus_eta_LS_bkg;  // recosntructed eta of pi-   
    TH1F *fHistK0sreco_PiPlus_phi_LS_bkg;   // recosntructed phi of pi+   
    TH1F *fHistK0sreco_PiMinus_phi_LS_bkg;  // recosntructed phi of pi-   



    // ====================
    //    MC TRUTH STUDY
    // ====================
    
    TH2F *fHistK0sreco_truthtype;   // truth type of K0s candidates
    TH1F *fHistAllMCK0s_DecayAngle;

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

    TH1F *fHistK0sreco_m_MCbkg;       // Reconstructed invariant mass of K0s-candidate
    TH1F *fHistK0sreco_pt_MCbkg;      // Reconstructed azimuth angle of K0s-candidate
    TH1F *fHistK0sreco_phi_MCbkg;     // Reconstructed Pt of K0s-candidate
    TH1F *fHistK0sreco_eta_MCbkg;     // Reconstructed Eta of K0s-candidate
    TH1F *fHistK0sreco_N_MCbkg;       // Multilplicity of K0s-candidate
    TH1F *fHistK0sreco_tau_MCbkg;     // Lifetime of reconstructed K0s-candidate
    TH1F *fHistK0sreco_DecayLength_MCbkg;    // DecayLength of K0s-candidate
    TH1F *fHistK0sreco_OpeningAngle_MCbkg;   // Opening Angle of the pion tracks in the lab frame
    TH1F *fHistK0sreco_DecayAngle_MCbkg;     // Decay angle between K0s and pi+ in K0s' restframe
    TH1F *fHistK0sreco_PointingAngle_MCbkg;  // cos of angle between reconstructed K0s' momentum and vec(PV)-vec(SecVtx)
    TH1F *fHistK0sreco_R_vtx_MCbkg;          // radial distance from vertex to beam pipe
    TH1F *fHistK0sreco_R_vtx_MCbkg_gamma;    // radial distance from photo-conversion-vertex to beam pipe
    TH2F *fHistK0sreco_RvtxMee_MCbkg;        // 2d histogram for r_vtx and M_ee inv. Mass
    TH1F *fHistK0sreco_Chi2_MCbkg;         // 
    TH1F *fHistK0sreco_pValue_MCbkg;       // 
    TH1F *fHistK0sreco_Chi2Trk_MCbkg;         // 
    TH1F *fHistK0sreco_Chi2Vtx_MCbkg;       // 


    
    // Decay Angle Analysis
    TH1F *fHist_MCK0s_DecayAngle;      // Decay Angle for all mc K0s'
    TH1F *fHist_DecayAngle_0to06;      // Decay Angle from  0     to  0.6
    TH1F *fHist_DecayAngle_06to094;    // Decay Angle from  0.6   to  0.94
    TH1F *fHist_DecayAngle_094to1;     // Decay Angle from  0.94  to  1.0
    TH1F *fHist_DecayAngleNeg_0to06;   // Decay Angle from  0     to -0.6
    TH1F *fHist_DecayAngleNeg_06to094; // Decay Angle from  -0.6  to -0.94
    TH1F *fHist_DecayAngleNeg_094to1;  // Decay Angle from  -0.94 to -1.0

    TH1F *fHist_DA_Comb;   // Decay Angle  Bkg (Combinatorial)
    TH1F *fHist_DA_Gamma;  // Decay Angle  Bkg (Photon Conversions)
    TH1F *fHist_DA_Lambda; // Decay Angle  Bkg (Lambda Decays)

    
    TH1F *fHist_PionsRight_Pt;          // Pt of Pions [DecayAngle from 0.6 to 0.94]
    TH1F *fHist_PionsLeft_Pt;           // Pt of Pions [DecayAngle from -0.6 to -0.94]
    
    // Analyse
    TH1F *fHist_OwnBoost;
    TH1F *fHist_InvMass;
    TH1F *fHist_InvMass_OwnBoost;
    TH1F *fHist_SubView;
    
    
    // SIGNAL: Reconstructed K0s candidates that fail the MCTruthMatching
    // ---------------------------------------------------------------------

    TH1F *fHistK0sreco_m_MCsignal;       // Reconstructed invariant mass of the Kaon decaying into pions
    TH1F *fHistK0sreco_pt_MCsignal;      // Reconstructed azimuth angle of the Kaon decaying into pions
    TH1F *fHistK0sreco_phi_MCsignal;     // Reconstructed Pt of the Kaon decaying into pions
    TH1F *fHistK0sreco_eta_MCsignal;     // Reconstructed Eta of the Kaon decaying into pions
    TH1F *fHistK0sreco_N_MCsignal;       // Multilplicity of reconstructed K0s decaying into pi+pi
    TH1F *fHistK0sreco_tau_MCsignal;      // Lifetime of reconstructed K0s
    TH1F *fHistK0sreco_DecayLength_MCsignal;    // DecayLength of reconstructed K0s
    TH1F *fHistK0sreco_OpeningAngle_MCsignal;   // Opening Angle of the pion tracks in the lab frame
    TH1F *fHistK0sreco_DecayAngle_MCsignal;     //Decay angle between K0s and pi+ in K0s' restframe
    TH1F *fHistK0sreco_PointingAngle_MCsignal;  //cos of angle between reconstructed K0s' momentum and vec(PV)-vec(SecVt
    TH1F *fHistK0sreco_R_vtx_MCsignal;          // radial distance from vertex to beam pipe
    TH2F *fHistK0sreco_RvtxMee_MCsignal;        // 2d histogram for r_vtx and M_ee inv. Mass

    TH1F *fHistK0sreco_Chi2_MCsignal;         // 
    TH1F *fHistK0sreco_pValue_MCsignal;       // 
    TH1F *fHistK0sreco_Chi2Trk_MCsignal;         // 
    TH1F *fHistK0sreco_Chi2Vtx_MCsignal;       // 
    
  public:
  // selection cuts
  Float_t  fSignalPion_Pt_min;          // Min. Pt of signal electrons
  Float_t  fSignalPion_Pt_max;          // Max. Pt of signal electrons
  Float_t  fSignalPion_Eta_min;         // Min. eta of signal electrons
  Float_t  fSignalPion_Eta_max;         // Max. eta of signal electrons
  Float_t  fSignalPion_EtCone20_max;    // Max. Et within a cone of 0.2

  Float_t  fMatchingProb_min;           // Min. of Matching probability
  Float_t  fK0s_M_min;                   // Lower boundary for allowed Z mass range
  Float_t  fK0s_M_max;                  // Upper boundary for allowed Z mass range
  Float_t  fm_pi;                       // Pion mass hypothesis

  Float_t  fVertexChi2ovNDoF_max;       // Quality of Vertex fit
  Float_t  fTrackChi2ovNDoF_max;        // Quality of Track fit
  Float_t  fKinFitPvalue_min;           // Minimal probability of Chi2/NDoF required
                                        // after converging kinematic fit
  Float_t  fTransvDecayLength_min;      // Minimal probability of Chi2/NDoF required
  Float_t  fPointingAngle_min;          // Minimal cos(PointingAngle)w
  Float_t  fDecayAngle_max;             // Maximum of DecayAngle (Angle between flightdirection of K0s
                                        // and momentum of pi+ in K0s' rerstframe)
  Float_t  fOpeningAngle_min;           // Angle between pion trks
  

  
  
  public:
  AtlK0sFinder(const char* name, const char* title);
  virtual ~AtlK0sFinder();
  virtual void SetBranchStatus();
  virtual void BookHistograms();
  virtual void FillHistograms();
  virtual void SetCutDefaults();
  virtual Bool_t AnalyzeEvent();
  void ReconstructK0sCutBased(void);
  void ReconstructK0sKinFit(void);
  void PrintCutValues(void);
  Int_t DoTruthMatch(AtlK0sDecayPiPi *K0sCandidate); 
  Bool_t IsLambdaCandidate(HepVertex *vtx);
  void FillHistograms_MCTruthStudy(AtlK0sDecayPiPi *K0sCandidate, char type);
  virtual void Terminate(void);
  
  inline void SetMode(EMode mode = kKinFit)
  { fMode = mode; }
  inline void SetDebugMode(Bool_t DebugMode = kFALSE)
  { fDebugMode = DebugMode; }
  inline Bool_t IsDebugRun() { return fDebugMode; }

  
  ClassDef(AtlK0sFinder,0) // K0s decay finder class
};
#endif

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