//  
// Author: Oliver Maria Kind <mailto: kind@mail.desy.de>
// Update: $Id: AtlSelector.h,v 1.76 2017/06/15 19:55:58 mergelm Exp $
// Copyright: 2008 (C) Oliver Maria Kind
//
#ifndef ATLAS_AtlSelector
#define ATLAS_AtlSelector
#ifndef ROOT_TSelector
#include <TSelector.h>
#endif
#ifndef ROOT_TList
#include <TList.h>
#endif
#ifndef ROOT_TChain
#include <TChain.h>
#endif
#ifndef ROOT_TFile
#include <TFile.h>
#endif
#ifndef ROOT_TStopwatch
#include <TStopwatch.h>
#endif
#ifndef ATLAS_AtlEvent
#include <AtlEvent.h>
#endif
#ifndef ATLAS_AtlAnalysisTool
#include <AtlAnalysisTool.h>
#endif
#ifndef ATLAS_AtlTriggerConf
#include <AtlTriggerConf.h>
#endif
#ifndef ROOT_TEntryList
#include <TEntryList.h>
#endif
#ifndef ROOT_TH1F
#include <TH1F.h>
#endif
#ifndef ROOT_TH1D
#include <TH1D.h>
#endif

class TString;
class TROOT;
class TSystem;
class AtlEvtReaderBase;
class AtlEvtWriterBase;
class TChainElement;

class AtlSelector : public TSelector {
    
  public:
    enum EIOMode { kApp,
		   kD3PDSgTop,
		   kD3PDSgTop_v2,
		   kD3PDCKM,
		   kMemNtuple,
		   kCustom,
    };

    static const Int_t fgNumLepChannels = 2;
    static const Int_t fgNumJetMults = 11;
    
    enum ELepChannel {
	kElectron = BIT(0), // Electron channel
	kMuon     = BIT(1), // Muon channel
	kLepton   = BIT(2)  // Electron + Muon merged channel
    };
    
    enum EJetMult {
	kOneJet        = BIT(0),
	kTwoJet        = BIT(1),
	kThreeJet      = BIT(2),
	kFourJet       = BIT(3),
	kFiveJet       = BIT(4),
	kSixJet        = BIT(5),
	k4To6Jets      = BIT(6),
	kOneOrMoreJets = BIT(7),
	kTwoOrMoreJets = BIT(8),
	kThreeOrMoreJets = BIT(9),
	kAllJets       = BIT(10)
    };

    Bool_t fDoTruthTree; // Read and process TruthTree information. Caution: this is a very time-consuming operation (Run2 SgTop-D3PDs only; default=false)
    
  protected:
    EIOMode       fInputMode;   // Defines input file mode
    EIOMode       fOutputMode;  // Defines mode for the output tree (if any)

    TTree      *fTree;                  // Pointer to the currently analysed tree or chain
    TTree      *fCurrentTree;           // Currently analysed tree
    TObjArray  *fBookkeepingList;       // List of chain elements needed for bookkeeping
    Int_t       fNBookkeeping;          // No. of bookkeeping files
    AtlEvtReaderBase *fEvtReader;       // Reader for input events
    AtlEvtReaderBase *fEvtReaderUser;   // User-provided reader for input events (owned!)
    AtlEvtWriterBase *fEvtWriter;       // Writer for output event
    AtlEvent   *fEvent;                 // Point to event object in memory
    TFile      *fOutputFile;            // Output file
    TString    *fOutputFilename;        // Name of the output file
    Bool_t      fIsChain;               // Input is chain or tree ?
    TString    *fOutputTreeName;        // Name of the A++ output tree (if any)
    TString    *fOutputTreeTitle;       // Title of the A++ output tree (if any)
    TTree      *fOutputTree;            // A++ output tree (if any)
    TTree      *fOutputTriggerConfTree; // Needed for copying the trigger config dbases into the output file in case of written out A++ events 
    Int_t       fProcessedEvents;       // No. of processed events (w/o weights)
    Int_t       fAcceptedEvents;        // No. of accepted events (w/o weights) selected by ProcessCut()
    Int_t       fPreAcceptedEvents;     // No. of pre-accepted events (w/o weights) selected by ProcessPreCut()
    Int_t       fJetBinEvents;          // No. of events in jet bin (w/o weights)
    Float_t     fProcessedEventsW;      // No. of processed events (weighted)
    Float_t     fAcceptedEventsW;       // No. of accepted events (weighted) selected by ProcessCut()
    Float_t     fAcceptedEventsB;       // No. of accepted events (weighted) selected by ProcessCut()
    Float_t     fPreAcceptedEventsW;    // No. of pre-accepted events (weighted) selected by ProcessPreCut()
    Float_t     fJetBinEventsW;         // No. of events in jet bin (weighted)
    Float_t     fPreTaggedEvents;       // No. of events before b-tagging
    Float_t     fPreTaggedEventsW;       // No. of events before b-tagging (weighted)
    TStopwatch  fStopwatch;             // Stopwatch used for process monitoring
    Long64_t    fEntry;                 // Global entry number of the current event in input chain
    TList      *fListOfTools;           // List of analysis tools
    Bool_t      fWriteEntryList;        // Switch for writing an entry list
    TEntryList *fEntryList;             // Global entry list for all files in a chain
    TH1F       *fHistEvents;            // Histogram for no. of processed and accepted events etc.
    TH1F       *fHistEvtWeights;        // Histogram of event weights
    TH1D       *fHistCutflow_NoWeights;                 // SgTopD3PD CutFlow Histogram
    TH1D       *fHistCutflow_genWeights;                // SgTopD3PD CutFlow Histogram
    TH1D       *fHistCutflow_genXpileupWeights;         // SgTopD3PD CutFlow Histogram
    TH1D       *fHistCutflow_genXpileupXzvertexWeights; // SgTopD3PD CutFlow Histogram
    TH1D       *fHistCutflow_allEventsHFOR_NoWeights;   // SgTopD3PD CutFlow Histogram
    TH1D       *fHistCutflow_allEventsHFOR_genWeights;  // SgTopD3PD CutFlow Histogram
    ProcInfo_t *fProcInfo;              // Current process information
    MemInfo_t  *fMemInfo;               // Current memory usage information
    Bool_t      fPrintEvent;            // Print the content of every event (useful for testing/debugging)
    Bool_t      fPrintObjectTable;      // Print Root's object table for every event (useful for tracking memory leaks)
    Bool_t      fPassedSelection;       // variable for storing the selection decisions for the event
    Bool_t      fCountUnfilteredEvents; // Count the no. of all unfiltered events (in case the input A++ file does not contain a JobInfo histogram)
    
  public:
    AtlSelector(const char* OutputFilename);
    virtual ~AtlSelector();

    virtual Int_t     Version() const { return 2; }
    virtual void      Begin(TTree *tree);
    virtual void      SlaveBegin(TTree *tree);
    virtual void      Init(TTree *tree = 0);
    virtual Bool_t    Notify();
    virtual Bool_t    Process(Long64_t entry);
    virtual void      SetOption(const char *option) { fOption = option; }
    virtual void      SetObject(TObject *obj) { fObject = obj; }
    virtual void      SetInputList(TList *input) { fInput = input; }
    virtual TList*    GetOutputList() const { return fOutput; }
    virtual void      SlaveTerminate();
    virtual void      Terminate();
    virtual void      SetBranchStatus() = 0;
    virtual void      InitEvent() {;}
    virtual void      BookHistograms()  = 0;
    virtual void      FillHistograms()  = 0;
    virtual void      ProcessFill();
    virtual void      SetBranches();
    virtual Int_t     GetEntry(Long64_t entry = 0);
    virtual void      SetCutDefaults();
    virtual void      Clear(Option_t *otpion = "");
    virtual void      Print(Option_t *option = "") const;
    void              SetOutputTree(const char* name, const char* title);
    void              ProcessInfo();
    void              PrintSummary();
    void              AddTool(AtlAnalysisTool *tool);
    AtlAnalysisTool*  GetTool(const char* ClassName,
			      const char* ToolName = "", Bool_t force = kFALSE);
    AtlTriggerConf*   LoadTriggerConfig();
    static const char* GetJetMultEnum(UInt_t jetmult);
    static const char* GetJetMultLabel(EJetMult jetmult);
    static const char* GetJetMultLabel(Int_t i);
    static const char* GetLeptonLabel(ELepChannel lep);
    static const char* GetLeptonLabel(Int_t i);
    static const char* GetLeptonEnum(UInt_t lep);
    
    inline TTree* GetTree() { return fTree; }
    inline TTree* GetCurrentTree() const { return fCurrentTree; }
    inline AtlEvent* GetEvent() const { return fEvent; }
    inline Float_t GetPreTagEvtWeight() const { return fEvent->GetPreTagEvtWeight(); }
    inline Float_t GetTagEvtWeight() const { return fEvent->GetTagEvtWeight(); }
    inline TFile* GetOutputFile() { return fOutputFile; }
    inline virtual Bool_t ProcessCut() { return kTRUE; }
    inline virtual Bool_t ProcessPreCut() { return kTRUE; }
    inline Bool_t GetWriteEntryList() const { return fWriteEntryList; }
    inline Bool_t PassedSelection() const { return fPassedSelection; }
    inline void SetWriteEntryList(Bool_t WriteEntryList)
    { fWriteEntryList = WriteEntryList; }
    inline TList* GetListOfTools() const { return fListOfTools; }
    inline Int_t GetProcessedEvents() const { return fProcessedEvents; }
    inline Bool_t HasOutputTree() const {
	//
	// Is there an A++ tree written to the output .root file ?
	//
	return ( fOutputTreeName->Length() > 0 );
    }
    inline Bool_t IsFirstEvent() const { return fProcessedEvents == 0; }
    inline void SetPrintEvent(Bool_t PrintEvent) {
	//
	// Switch for printing every event
	// (useful for testing/debugging)
	//
	fPrintEvent = PrintEvent;
    }
    inline void SetPrintObjectTable(Bool_t PrintObjectTable) {
	// Switch for printing the Root's object table
	// (useful for tracking memory leaks)
	fPrintObjectTable = PrintObjectTable;
    }
    inline void SetPassedSelection(Bool_t result) { fPassedSelection = result; }
   
    void SetInputMode(EIOMode inputMode);
    void SetEvtReader(AtlEvtReaderBase * reader);

  protected:
    void BookJobInfoHistograms();
    void SetSumw2(TDirectory *dir);
    void ChangeOutputFile();
    void DoBookkeeping(TFile *InputFile);
    
    ClassDef(AtlSelector,0) // ATLAS analysis selector
};
#endif

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