//____________________________________________________________________
//
//
// Author: Soeren Stamm <mailto: stamm@physik.hu-berlin.de>
// Update: $Id: AtlWjetsScaleFactorTool.cxx,v 1.1 2015/11/27 09:18:47 stamm Exp $
// Copyright: 2015 (C) Soeren Stamm
//
#ifndef ATLAS_AtlWjetsScaleFactorTool
#include <AtlWjetsScaleFactorTool.h>
#endif
#include <iostream>

using namespace std;

const Double_t AtlWjetsScaleFactorTool::fgWjetsSF_el[41] = {
    1.27782, 1.31514, 1.3039,  1.2874,  1.27433,
    1.22216, 1.21848, 1.20525, 1.14273, 1.15883,
    1.11091, 1.19356, 1.14998, 1.08237, 1.06008,
    1.16418, 1.07582, 1.08043, 1.1444,  1.10589,
    0.98765, 1.02782, 1.09187, 0.975726, 1.00977,
    1.02593, 0.947341, 0.987128, 1.03481, 1.09191,
    0.918242, 1.02247, 1.02147, 0.890984, 0.972405,
    1.31816, 0.83809, 1.09929, 1.09481, 1.24821,
    1.03161 };

const Double_t AtlWjetsScaleFactorTool::fgWjetsSF_mu[41] = {
    1.24262,  1.3441,   1.21622,  1.25679,  1.14936,
    1.22594,  1.21616,  1.16556,  1.21355,  1.18106,
    1.20481,  1.17082,  1.16274,  1.06071,  1.06033,
    1.10203,  1.08698,  1.11646,  1.12294,  1.16231,
    0.932065, 1.05363,  1.02453,  1.00237,  0.898718,
    1.16953,  1.02912,  0.895753, 0.842689, 0.98445,
    1.02685,  1.04646,  1.11947,  1.01368,  0.877994,
    1.07017,  0.816822, 0.83959,  1.11853,  1.06517,
    0.917841};

#ifndef __CINT__
ClassImp(AtlWjetsScaleFactorTool);
#endif


//____________________________________________________________________
    
AtlWjetsScaleFactorTool::AtlWjetsScaleFactorTool(const char *name,
						     const char* title) :
    AtlAnalysisTool(name, title) {
    //
    // Default constructor
    //
    fLeptons = 0;
}

//____________________________________________________________________

AtlWjetsScaleFactorTool::~AtlWjetsScaleFactorTool() {
    //
    // Default destructor
    //
}

//____________________________________________________________________

void AtlWjetsScaleFactorTool::Clear(Option_t *option) {
    //
    // Clear this tool at the beginning of each event
    //
}

//____________________________________________________________________

void AtlWjetsScaleFactorTool::SetBranchStatus() {
    //
    // Turn on all needed branches
    //
    fTree->SetBranchStatus("fEventHeader*", kTRUE);
    fTree->SetBranchStatus("fElectrons*", kTRUE);
    fTree->SetBranchStatus("fMuons*", kTRUE);
    fTree->SetBranchStatus("fN_AntiKt4LCTopoJets*",       kTRUE);
    fTree->SetBranchStatus("fAntiKt4LCTopoJets*",         kTRUE);
}
//____________________________________________________________________

void AtlWjetsScaleFactorTool::BookHistograms() {
    //
    // Book Histograms
    //
}

//____________________________________________________________________

void AtlWjetsScaleFactorTool::FillHistograms() {
    //
    // Fill Histograms
    //

}

//____________________________________________________________________

void AtlWjetsScaleFactorTool::Print() const {
    //
    // Print tool information
    //
    cout << endl
	 << "========================================================" << endl
	 << "  W+jets Scale Factor Tool " << endl
	 << "========================================================" << endl
	 << endl;
}

//____________________________________________________________________

Bool_t AtlWjetsScaleFactorTool::AnalyzeEvent() {
    //
    // Calculate the scale factor for an event
    //

    // Manipulate wjets weight based on pt(W)
    HepParticle *lepton = (HepParticle*) fLeptons->At(0);

    TVector2 met = fEvent->GetEnergySum()->GetMissingEt();
    TVector2 lep = lepton->P3().XYvector();
    Double_t Pt_W = (met+lep).Mod();
    
    Int_t sfbin = TMath::Min(TMath::FloorNint(Pt_W/5.), 40);
    if ( lepton->IsElectron() ) {
	fEvent->SetTagEvtWeight(GetTagEvtWeight()*fgWjetsSF_el[sfbin]);
    } else {
    	fEvent->SetTagEvtWeight(GetTagEvtWeight()*fgWjetsSF_mu[sfbin]);
    }

    return kTRUE;
}

//___________________________________________________________

void AtlWjetsScaleFactorTool::Terminate() {
    //
    // Executed at the end of the tool
    //
}
 AtlWjetsScaleFactorTool.cxx:1
 AtlWjetsScaleFactorTool.cxx:2
 AtlWjetsScaleFactorTool.cxx:3
 AtlWjetsScaleFactorTool.cxx:4
 AtlWjetsScaleFactorTool.cxx:5
 AtlWjetsScaleFactorTool.cxx:6
 AtlWjetsScaleFactorTool.cxx:7
 AtlWjetsScaleFactorTool.cxx:8
 AtlWjetsScaleFactorTool.cxx:9
 AtlWjetsScaleFactorTool.cxx:10
 AtlWjetsScaleFactorTool.cxx:11
 AtlWjetsScaleFactorTool.cxx:12
 AtlWjetsScaleFactorTool.cxx:13
 AtlWjetsScaleFactorTool.cxx:14
 AtlWjetsScaleFactorTool.cxx:15
 AtlWjetsScaleFactorTool.cxx:16
 AtlWjetsScaleFactorTool.cxx:17
 AtlWjetsScaleFactorTool.cxx:18
 AtlWjetsScaleFactorTool.cxx:19
 AtlWjetsScaleFactorTool.cxx:20
 AtlWjetsScaleFactorTool.cxx:21
 AtlWjetsScaleFactorTool.cxx:22
 AtlWjetsScaleFactorTool.cxx:23
 AtlWjetsScaleFactorTool.cxx:24
 AtlWjetsScaleFactorTool.cxx:25
 AtlWjetsScaleFactorTool.cxx:26
 AtlWjetsScaleFactorTool.cxx:27
 AtlWjetsScaleFactorTool.cxx:28
 AtlWjetsScaleFactorTool.cxx:29
 AtlWjetsScaleFactorTool.cxx:30
 AtlWjetsScaleFactorTool.cxx:31
 AtlWjetsScaleFactorTool.cxx:32
 AtlWjetsScaleFactorTool.cxx:33
 AtlWjetsScaleFactorTool.cxx:34
 AtlWjetsScaleFactorTool.cxx:35
 AtlWjetsScaleFactorTool.cxx:36
 AtlWjetsScaleFactorTool.cxx:37
 AtlWjetsScaleFactorTool.cxx:38
 AtlWjetsScaleFactorTool.cxx:39
 AtlWjetsScaleFactorTool.cxx:40
 AtlWjetsScaleFactorTool.cxx:41
 AtlWjetsScaleFactorTool.cxx:42
 AtlWjetsScaleFactorTool.cxx:43
 AtlWjetsScaleFactorTool.cxx:44
 AtlWjetsScaleFactorTool.cxx:45
 AtlWjetsScaleFactorTool.cxx:46
 AtlWjetsScaleFactorTool.cxx:47
 AtlWjetsScaleFactorTool.cxx:48
 AtlWjetsScaleFactorTool.cxx:49
 AtlWjetsScaleFactorTool.cxx:50
 AtlWjetsScaleFactorTool.cxx:51
 AtlWjetsScaleFactorTool.cxx:52
 AtlWjetsScaleFactorTool.cxx:53
 AtlWjetsScaleFactorTool.cxx:54
 AtlWjetsScaleFactorTool.cxx:55
 AtlWjetsScaleFactorTool.cxx:56
 AtlWjetsScaleFactorTool.cxx:57
 AtlWjetsScaleFactorTool.cxx:58
 AtlWjetsScaleFactorTool.cxx:59
 AtlWjetsScaleFactorTool.cxx:60
 AtlWjetsScaleFactorTool.cxx:61
 AtlWjetsScaleFactorTool.cxx:62
 AtlWjetsScaleFactorTool.cxx:63
 AtlWjetsScaleFactorTool.cxx:64
 AtlWjetsScaleFactorTool.cxx:65
 AtlWjetsScaleFactorTool.cxx:66
 AtlWjetsScaleFactorTool.cxx:67
 AtlWjetsScaleFactorTool.cxx:68
 AtlWjetsScaleFactorTool.cxx:69
 AtlWjetsScaleFactorTool.cxx:70
 AtlWjetsScaleFactorTool.cxx:71
 AtlWjetsScaleFactorTool.cxx:72
 AtlWjetsScaleFactorTool.cxx:73
 AtlWjetsScaleFactorTool.cxx:74
 AtlWjetsScaleFactorTool.cxx:75
 AtlWjetsScaleFactorTool.cxx:76
 AtlWjetsScaleFactorTool.cxx:77
 AtlWjetsScaleFactorTool.cxx:78
 AtlWjetsScaleFactorTool.cxx:79
 AtlWjetsScaleFactorTool.cxx:80
 AtlWjetsScaleFactorTool.cxx:81
 AtlWjetsScaleFactorTool.cxx:82
 AtlWjetsScaleFactorTool.cxx:83
 AtlWjetsScaleFactorTool.cxx:84
 AtlWjetsScaleFactorTool.cxx:85
 AtlWjetsScaleFactorTool.cxx:86
 AtlWjetsScaleFactorTool.cxx:87
 AtlWjetsScaleFactorTool.cxx:88
 AtlWjetsScaleFactorTool.cxx:89
 AtlWjetsScaleFactorTool.cxx:90
 AtlWjetsScaleFactorTool.cxx:91
 AtlWjetsScaleFactorTool.cxx:92
 AtlWjetsScaleFactorTool.cxx:93
 AtlWjetsScaleFactorTool.cxx:94
 AtlWjetsScaleFactorTool.cxx:95
 AtlWjetsScaleFactorTool.cxx:96
 AtlWjetsScaleFactorTool.cxx:97
 AtlWjetsScaleFactorTool.cxx:98
 AtlWjetsScaleFactorTool.cxx:99
 AtlWjetsScaleFactorTool.cxx:100
 AtlWjetsScaleFactorTool.cxx:101
 AtlWjetsScaleFactorTool.cxx:102
 AtlWjetsScaleFactorTool.cxx:103
 AtlWjetsScaleFactorTool.cxx:104
 AtlWjetsScaleFactorTool.cxx:105
 AtlWjetsScaleFactorTool.cxx:106
 AtlWjetsScaleFactorTool.cxx:107
 AtlWjetsScaleFactorTool.cxx:108
 AtlWjetsScaleFactorTool.cxx:109
 AtlWjetsScaleFactorTool.cxx:110
 AtlWjetsScaleFactorTool.cxx:111
 AtlWjetsScaleFactorTool.cxx:112
 AtlWjetsScaleFactorTool.cxx:113
 AtlWjetsScaleFactorTool.cxx:114
 AtlWjetsScaleFactorTool.cxx:115
 AtlWjetsScaleFactorTool.cxx:116
 AtlWjetsScaleFactorTool.cxx:117
 AtlWjetsScaleFactorTool.cxx:118
 AtlWjetsScaleFactorTool.cxx:119
 AtlWjetsScaleFactorTool.cxx:120
 AtlWjetsScaleFactorTool.cxx:121
 AtlWjetsScaleFactorTool.cxx:122
 AtlWjetsScaleFactorTool.cxx:123
 AtlWjetsScaleFactorTool.cxx:124
 AtlWjetsScaleFactorTool.cxx:125
 AtlWjetsScaleFactorTool.cxx:126
 AtlWjetsScaleFactorTool.cxx:127
 AtlWjetsScaleFactorTool.cxx:128
 AtlWjetsScaleFactorTool.cxx:129
 AtlWjetsScaleFactorTool.cxx:130
 AtlWjetsScaleFactorTool.cxx:131
 AtlWjetsScaleFactorTool.cxx:132
 AtlWjetsScaleFactorTool.cxx:133
 AtlWjetsScaleFactorTool.cxx:134
 AtlWjetsScaleFactorTool.cxx:135
 AtlWjetsScaleFactorTool.cxx:136
 AtlWjetsScaleFactorTool.cxx:137
 AtlWjetsScaleFactorTool.cxx:138
 AtlWjetsScaleFactorTool.cxx:139
 AtlWjetsScaleFactorTool.cxx:140
 AtlWjetsScaleFactorTool.cxx:141