#include "QFramework/TQAnalysisAlgorithm.h"



#include <string>

//#define _DEBUG_


#include "QFramework/TQAnalysisJob.h"
#include "QFramework/TQCompiledCut.h"
#include "QFramework/TQIterator.h"
#include "QFramework/TQSample.h"
#include "QFramework/TQToken.h"
#include "QFramework/TQLibrary.h"
#include "QFramework/TQCut.h"
#include "QFramework/TQAlgorithm.h"
#include "TTree.h"


#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>

#include <stdexcept>

using std::cout;
using std::endl;


ClassImp(TQAnalysisAlgorithm)

TQAnalysisAlgorithm::TQAnalysisAlgorithm(const TString& name, TQCut* myCut):
   fCut(myCut)
{
  // constructor
  this->SetName(name);


}

//__________________________________________________________________________________|___________

void TQAnalysisAlgorithm::setBaseCut(TQCut* myCut){
    this->fCut = myCut;
}
//__________________________________________________________________________________|___________

bool TQAnalysisAlgorithm::initializeSampleFolder(TQSampleFolder* s){
  // called for each sample folder
  if (!s) {
    return false;
  }

  fCut->initializeSampleFolder(s);

  return true;
}

//__________________________________________________________________________________|___________

bool TQAnalysisAlgorithm::finalizeSampleFolder(TQSampleFolder* s){
  // called for each sample folder  
  if (!s) {
    return false;
  }
  
  fCut->finalizeSampleFolder(s);
  return true;
}
//__________________________________________________________________________________|___________

bool TQAnalysisAlgorithm::initialize(TQSample* s){
  // called at the beginning of each sample
  if (!s) {
    return false;
  }
  
  TQToken* treeTok = s->getTreeToken();
  TTree* tree = nullptr;
  if (treeTok) tree = static_cast<TTree*>(treeTok->getContent());
  
  this->fSample = s;
  if (tree && tree->GetEntries()>0 && !fCut->initialize(s)) {
    ERRORclass("Failed to initialize cuts for sample with path '%s'",s->getPath().Data());
    //don't forget to hand back the token before returning!
    s->returnToken(treeTok);
    return false;
  }
  
  if (treeTok) s->returnToken(treeTok);
    
  this->fUseWeights= false;
  s->getTagBool("usemcweights", fUseWeights, true);
  return true;
}

//__________________________________________________________________________________|___________

bool TQAnalysisAlgorithm::finalize(){
  // called at the end of each sample

  fCut->finalize();
  return true;
}

//__________________________________________________________________________________|___________

bool TQAnalysisAlgorithm::execute(){
  // called at the beginning of each event

  // you probably don't need to do anything here
  return true;
}

//__________________________________________________________________________________|___________

bool TQAnalysisAlgorithm::cleanup(){
  // called at the end of each event

 fCut->analyse(1., fUseWeights);

  // TODO: think about how to handle event weights!

  return true;
}

//__________________________________________________________________________________|___________

TObjArray * TQAnalysisAlgorithm::getListOfBranches(){
  if (!fCut) return nullptr;
  return fCut->getListOfBranches();
}

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