//  
// Author: Thomas Kintscher <mailto: kintscher@physik.hu-berlin.de>
// Update: $Id: AtlDstarFinder.h,v 1.4 2015/08/25 11:36:09 mergelm Exp $
// Copyright: 2010 (C) Thomas Kintscher
//
#ifndef ATLAS_AtlDstarFinder
#define ATLAS_AtlDstarFinder
#ifndef ATLAS_AtlKinFitterTool
#include <AtlKinFitterTool.h>
#endif
#ifndef ROOT_TH1F
#include <TH1F.h>
#endif
#ifndef ROOT_TH2F
#include <TH2F.h>
#endif
#include <fstream>

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

class AtlDstarFinder : public AtlKinFitterTool {

  public:
    enum EMode { kCutBased, kKinFit };
    EMode fMode;                       // Indicate the analysis mode (cut-based or using the kinematic fitter)
    Bool_t fDebug;                     // Toggle debug output (prints failed truth matching results)

  private:
    ofstream fDebugStream;             // Debug output

    // Lambda -> p pi(signal)
    TH1F *fHistD0reco_m_kaon_pi;
    TH1F *fHistLambdareco_m_PPi;       // Reconstructed invariant mass of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_m_PiPi;      // Reconstructed invariant mass with pion+pion hypothesis
    TH1F *fHistLambdareco_m_ee;        // Reconstructed invariant mass with ee hypothesis
    TH1F *fHistLambdareco_pt_PPi;      // Reconstructed azimuth angle of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_phi_PPi;     // Reconstructed Pt of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_eta_PPi;     // Reconstructed Eta of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_N_PPi;       // Multilplicity of reconstructed Lambda decaying into proton+pion-r
    TH1F *fHistLambdareco_tau_PPi;     // Lifetime of reconstructed lambdas
    TH2F *fHistLambdareco_truth_PPi;   // Pdg codes of truth-matched particles
    TH1F *fHistLambdareco_R_vtx;       // radial distance from vertex to beam pipe
    TH1F *fHistLambdareco_src_div;     // Angular divergence of lambda source and primary vertex
    TH1F *fHistLambdareco_tdcy_len;    // Transverse decay length of lambda
    TH1F *fHistLambdareco_oangle;      // Opening angle between proton/pion
    TH1F *fHistLambdareco_dangle;      // Decay angle between proton/pion (in lambda frame)

    TH1F *fHistLambdareco_Proton_pt;   // reconstructed pt  of proton
    TH1F *fHistLambdareco_Pion_pt;     // reconstructed pt  of pion
    TH1F *fHistLambdareco_Proton_eta;  // reconstructed eta of proton 
    TH1F *fHistLambdareco_Pion_eta;    // reconstructed eta of pion
    TH1F *fHistLambdareco_Proton_phi;  // reconstructed phi of proton  
    TH1F *fHistLambdareco_Pion_phi;    // reconstructed phi of pion

    TH1F *fHistLambdareco_Piplus_pt;   // reconstructed pt  of pion+
    TH1F *fHistLambdareco_Piplus_eta;  // reconstructed eta of pion+
    TH1F *fHistLambdareco_Piplus_phi;  // reconstructed phi of pion+
    TH1F *fHistLambdareco_Piminus_pt;  // reconstructed pt  of pion-
    TH1F *fHistLambdareco_Piminus_eta; // reconstructed eta of pion-
    TH1F *fHistLambdareco_Piminus_phi; // reconstructed phi of pion-

    TH1F *fHistLambdareco_Prplus_pt;   // reconstructed pt  of proton
    TH1F *fHistLambdareco_Prplus_eta;  // reconstructed eta of proton
    TH1F *fHistLambdareco_Prplus_phi;  // reconstructed phi of proton
    TH1F *fHistLambdareco_Prminus_pt;  // reconstructed pt  of antiproton
    TH1F *fHistLambdareco_Prminus_eta; // reconstructed eta of antiproton
    TH1F *fHistLambdareco_Prminus_phi; // reconstructed phi of antiproton
    
    // combinatorial background (same charged particles)
    TList *fBkgLambdaDecays;           // List of combinatorial bkg.

    // Lambda -> pipi(Bkg)
    TH1F *fHistLambdareco_m_PPi_bkg;       // Reconstructed invariant mass of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_pt_PPi_bkg;      // Reconstructed azimuth angle of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_phi_PPi_bkg;     // Reconstructed Pt of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_eta_PPi_bkg;     // Reconstructed Eta of the Lambda decaying into proton+pion
    TH1F *fHistLambdareco_N_PPi_bkg;       // Multiplicity of reconstructed Lambda decaying into proton+pion-r
    TH1F *fHistLambdareco_tau_PPi_bkg;     // Lifetime of reconstructed lambda background
    TH1F *fHistLambdareco_truth_PPi_bkg;   // Pdg codes of truth-matched particles
    TH1F *fHistLambdareco_R_vtx_bkg;       // radial distance from vertex to beam pipe (bkg)
    TH1F *fHistLambdareco_src_div_bkg;     // Angular divergence of lambda source and primary vertex
    TH1F *fHistLambdareco_tdcy_len_bkg;    // Transverse decay length of lambda
    TH1F *fHistLambdareco_oangle_bkg;      // Opening angle between proton/pion
    TH1F *fHistLambdareco_dangle_bkg;      // Decay angle between proton/pion (in lambda frame)

    TH1F *fHistLambdareco_Proton_pt_bkg;   // reconstructed pt  of proton 
    TH1F *fHistLambdareco_Pion_pt_bkg;     // reconstructed pt  of pion  
    TH1F *fHistLambdareco_Proton_eta_bkg;  // reconstructed eta of proton  
    TH1F *fHistLambdareco_Pion_eta_bkg;    // reconstructed eta of pion   
    TH1F *fHistLambdareco_Proton_phi_bkg;  // reconstructed phi of proton   
    TH1F *fHistLambdareco_Pion_phi_bkg;    // reconstructed phi of pion   

    TH1F *fHistLambdareco_Piplus_pt_bkg;   // reconstructed pt  of pion+
    TH1F *fHistLambdareco_Piplus_eta_bkg;  // reconstructed eta of pion+
    TH1F *fHistLambdareco_Piplus_phi_bkg;  // reconstructed phi of pion+
    TH1F *fHistLambdareco_Piminus_pt_bkg;  // reconstructed pt  of pion-
    TH1F *fHistLambdareco_Piminus_eta_bkg; // reconstructed eta of pion-
    TH1F *fHistLambdareco_Piminus_phi_bkg; // reconstructed phi of pion-

    TH1F *fHistLambdareco_Prplus_pt_bkg;   // reconstructed pt  of proton
    TH1F *fHistLambdareco_Prplus_eta_bkg;  // reconstructed eta of proton
    TH1F *fHistLambdareco_Prplus_phi_bkg;  // reconstructed phi of proton
    TH1F *fHistLambdareco_Prminus_pt_bkg;  // reconstructed pt  of antiproton
    TH1F *fHistLambdareco_Prminus_eta_bkg; // reconstructed eta of antiproton
    TH1F *fHistLambdareco_Prminus_phi_bkg; // reconstructed phi of antiproton

    // MC signal and background histos
    TH1F *fHistLambdareco_MC_m;            // reconstructed lambda mass
    TH1F *fHistLambdareco_MC_m_bkg;        // reconstructed lambda mass (bkg)
    TH1F *fHistLambdareco_MC_m_pipi;       // reconstructed lambda mass (pi/pi hypothesis)
    TH1F *fHistLambdareco_MC_m_pipi_bkg;   // reconstructed lambda mass (pi/pi hyp.) (bkg)
    TH1F *fHistLambdareco_MC_m_ppi;        // reconstructed lambda mass (p/pi hypothesis)
    TH1F *fHistLambdareco_MC_m_ppi_bkg;    // reconstructed lambda mass (p/pi hyp.) (bkg)
    TH1F *fHistLambdareco_MC_m_ee;         // reconstructed lambda mass (ee hypothesis)
    TH1F *fHistLambdareco_MC_m_ee_bkg;     // reconstructed lambda mass (ee hyp.) (bkg)
    TH1F *fHistLambdareco_MC_m_pipi_o;     // reconstructed lambda mass (pi/pi hypothesis) (orig. momenta)
    TH1F *fHistLambdareco_MC_m_pipi_o_bkg; // reconstructed lambda mass (pi/pi hyp.) (bkg) (o.m.)
    TH1F *fHistLambdareco_MC_m_ppi_o;      // reconstructed lambda mass (p/pi hypothesis) (o.m.)
    TH1F *fHistLambdareco_MC_m_ppi_o_bkg;  // reconstructed lambda mass (p/pi hyp.) (bkg) (o.m.)
    TH1F *fHistLambdareco_MC_m_ee_o;       // reconstructed lambda mass (ee hypothesis) (o.m.)
    TH1F *fHistLambdareco_MC_m_ee_o_bkg;   // reconstructed lambda mass (ee hyp.) (bkg) (o.m.)
    TH1F *fHistLambdareco_MC_pt;           // reconstructed lambda pt
    TH1F *fHistLambdareco_MC_pt_bkg;       // reconstructed lambda pt (bkg)
    TH1F *fHistLambdareco_MC_phi;          // reconstructed lambda phi
    TH1F *fHistLambdareco_MC_phi_bkg;      // reconstructed lambda phi (bkg)
    TH1F *fHistLambdareco_MC_eta;          // reconstructed lambda eta
    TH1F *fHistLambdareco_MC_eta_bkg;      // reconstructed lambda eta (bkg)
    TH1F *fHistLambdareco_MC_R_vtx;        // reconstructed lambda radius
    TH1F *fHistLambdareco_MC_R_vtx_bkg;    // reconstructed lambda radius (bkg);
    TH1F *fHistLambdareco_MC_tdcy_len;     // reconstructed lambda transverse decay length
    TH1F *fHistLambdareco_MC_tdcy_len_bkg; // reconstructed lambda transverse decay length (bkg)
    TH1F *fHistLambdareco_MC_tau;          // reconstructed lambda lifetime
    TH1F *fHistLambdareco_MC_tau_bkg;      // reconstructed lambda lifetime (bkg)
    TH1F *fHistLambdareco_MC_src_div;      // reconstructed lambda angle to primary vtx
    TH1F *fHistLambdareco_MC_src_div_bkg;  // reconstructed lambda angle to primary vtx (bkg)
    TH1F *fHistLambdareco_MC_dangle;       // reconstructed lambda decay angle
    TH1F *fHistLambdareco_MC_dangle_bkg;   // reconstructed lambda decay angle (bkg)
    TH1F *fHistLambdareco_MC_oangle;       // reconstructed lambda opening angle
    TH1F *fHistLambdareco_MC_oangle_bkg;   // reconstructed lambda opening angle (bkg)
    TH1F *fHistLambdareco_MC_coangle;      // reconstructed lambda cos opening angle
    TH1F *fHistLambdareco_MC_coangle_bkg;  // reconstructed lambda cos opening angle (bkg)
    TH1F *fHistLambdareco_MC_z0_Pi;        // reconstructed z0 of pion track
    TH1F *fHistLambdareco_MC_z0_Pi_bkg;    // reconstructed z0 of pion track (bkg)
    TH1F *fHistLambdareco_MC_d0_Pi;        // reconstructed d0 (track param.) of pion track
    TH1F *fHistLambdareco_MC_d0_Pi_bkg;    // reconstructed d0 (track param.) of pion track (bkg)
    TH1F *fHistLambdareco_MC_trk_chi2;     // reconstructed chi2/ndf of pion track
    TH1F *fHistLambdareco_MC_trk_chi2_bkg; // reconstructed chi2/ndf of pion track (bkg)
    TH1F *fHistLambdareco_MC_pt_Pi;
    TH1F *fHistLambdareco_MC_pt_Pi_bkg;
    TH1F *fHistLambdareco_MC_Pi_angle;
    TH1F *fHistLambdareco_MC_Pi_angle_bkg;
    
    TH2F *fHistLambdareco_MC_m_dangle;
    TH2F *fHistLambdareco_MC_m_dangle_bkg;

    // Misc. stuff
    TH1F *fHistLambdareco_chi2_vtx;        // Chi2/NDoF of vertex fits
    TH1F *fHistLambdareco_chi2_trk;        // Chi2/NDoF of track fits
    TH1F *fHistLambdareco_prob_mc;         // Truth matching probabilities
    TH1F *fHistLambdareco_eff;             // Efficiency histogram

    Int_t fN_MCFail;                       // how often did the truth matching fail
    Int_t fN_Fits;                         // count of successful fits
    Int_t fN_MCProbMiss;                   // how often was truth matching prob below threshold
    Int_t fN_SameOriginMiss;               // count of truth matched tracks with different mother

public:
    // selection cuts
    Float_t              fSignal_Pt_min;          // Min. Pt of signal pion/proton
    Float_t              fSignal_Pt_max;          // Max. Pt of signal pion/proton
    Float_t              fSignal_Eta_min;         // Min. eta of signal pion/proton
    Float_t              fSignal_Eta_max;         // Max. eta of signal pion/proton

    Float_t              fDstar_M_min;                // Lower boundary for allowed Lambda mass range
    Float_t              fDstar_M_max;                // Upper boundary for allowed Lambda mass range
    Float_t              fm_e;                        // Electron mass hypothesis
    Float_t              fm_lambda;                   // Lambda mass hypothesis
    Float_t              fm_pi;                       // Pion mass hypothesis
    Float_t              fm_proton;                   // Proton mass hypothesis
    Float_t              fm_kaon;
    Float_t              fm_D0;
    Float_t              fm_Dstar;

    Float_t              fOAngle_max;                 // Maximum opening angle of D0/Pi
    Float_t              fAngleToPrimary_max;         // Maximum angle divergence to primary vertex
    Float_t              fVertexChi2ovNDoF_max;       // Quality of vertex fit
    Float_t              fTrackChi2ovNDoF_max;        // Quality of track fit
    Float_t              fKinFitPvalue_min;           // Quality of kinematic fit
    Float_t              fMatchingProb_min;           // Quality of truth matching
    Float_t              fPhotonMass_max;             // Maximum invariant mass of e+/e- test
    Float_t              fD0_Pi_min;
    Float_t              fD0_Pi_max;
    Float_t              fPi3_pt_max;

    TKinFitter PerformFit(HepParticle trk1, HepParticle trk2, AtlIDTrack *trk3,
			  TLorentzVector &FitP_trk1, TLorentzVector &FitP_trk2, TLorentzVector &FitP_trk3, 
			  TMatrixD *cov_trk1, TMatrixD *cov_trk2, TMatrixD *cov_trk3);
    Int_t DoTruthMatch(AtlIDTrack *trk1, AtlIDTrack *trk2, AtlIDTrack *trk3);
    void FillMCHistograms(AtlDstarDecayDPi *decay, Bool_t signal);
    Bool_t IsConversionVertex(HepVertex *vtx);
    Bool_t IsKaonVertex(HepVertex *vtx);
    Bool_t IsLambdaVertex(HepVertex *vtx);
    void FindSharedTracks();
    
public:
    AtlDstarFinder(const char* name, const char* title);
    virtual ~AtlDstarFinder();
    virtual void SetBranchStatus();
    virtual void BookHistograms();
    virtual void FillHistograms();
    virtual void SetCutDefaults();
    virtual Bool_t AnalyzeEvent();
    void ReconstructLambdaKinFit(void);
    void PrintCutValues(void);
    void Terminate();
    Double_t GetModifiedEventWeight();

    inline void SetMode(EMode mode = kKinFit) { fMode = mode; }
    void SetDebugOutput(Bool_t mode = kFALSE) { fDebug = mode; }
    inline Bool_t IsDebugRun() { return fDebug; }

    ClassDef(AtlDstarFinder, 0) // Lambda decay finder class
};
#endif

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