//this file looks like plain C, but it's actually -*- c++ -*-
#ifndef __TQPlotter__
#define __TQPlotter__

#include "QFramework/TQPresenter.h"
#include "QFramework/TQStringUtils.h"

class TPad;
class TCanvas;
class TLegend;
class TH1;
class TGraphErrors;
class TGraphAsymmErrors;


class TQSampleDataReader;

class TQPlotter : public TQPresenter {

protected:

  TDirectory* objects; //!
 
  virtual void drawLabels(TQTaggable& tags);
   
  void setAxisLabels(TQTaggable& tags);
  TString makeHistogramIdentifier(TQNamedTaggable* process);

  virtual double getHistogramUpperLimit(TQTaggable& tags, TList * histograms, double lower, bool includeErrors = false);
  virtual bool checkConsistency(TH1 * &hMaster, TObjArray * histograms);

  bool includeSystematics(TQTaggable& tags);
  void setErrors(TQTaggable& tags, const TString& hname);

  TH1* getMaximumHistogram(TCollection* histos);

  void applyBlinding(TQTaggable& tags, TCollection* sigHistos, TCollection* backgroundHistos, TCollection* dataHistos);

  virtual TObjArray* collectHistograms(TQTaggable& tags);
  virtual TObjArray * getHistograms(TObjArray* processes, const TString& tagFilter, const TString& histogramName, const TString& namePrefix, TQTaggable& aliases, TQTaggable& options);
 
  virtual bool collectOptScanSimplifiedSystHistograms(std::vector<TH1*>& histos, TQTaggable& tags);
  
  virtual void addObject(TNamed* obj, const TString& key = "");
  virtual void addObject(TGraph* obj, TString key = "");
  virtual void addObject(TH1* obj, const TString& key = "");
  virtual void addObject(TCollection* obj, const TString& key = "");
  virtual void removeObject(const TString& key, bool deleteObject = false);

  virtual void clearObjects();
  virtual void deleteObjects();
 
  virtual void applyStyle(TQTaggable& tags, TAxis* a, const TString& key, double distscaling = 1., double sizescaling = 1.);
  virtual void applyStyle(TQTaggable& tags, TH1* hist, const TString& key, double xscaling = 1., double yscaling = 1.);
  virtual void applyGeometry(TQTaggable& tags, TAxis* a, const TString& key, double distscaling = 1., double sizescaling = 1., bool force=true);
  virtual void applyGeometry(TQTaggable& tags, TH1* hist, const TString& key, double xscaling = 1., double yscaling = 1., bool force=true);
  virtual void applyStyle(TQTaggable& tags, TGraph* g, const TString& key, double xscaling = 1., double yscaling = 1.);
  virtual void applyGeometry(TQTaggable& tags, TGraph* g, const TString& key, double xscaling = 1., double yscaling = 1., bool force=true);

  virtual bool plotAndSaveAsInternal(const TString& histogram, const TString& saveAs, TQTaggable& tags) = 0;
  
public:

  virtual int getNProcesses(const TString& tagFilter);
 
  virtual void printObjects();
  template <class Type>
  Type* getObject(const TString& key){
    // retrieve a graphics object by name and cast to the given class
    return dynamic_cast<Type*>(this->getTObject(key));
  }
  TObject* getTObject(const TString& key);
  
  TQPlotter();
  TQPlotter(TQSampleFolder * baseSampleFolder);
  TQPlotter(TQSampleDataReader * dataSource);

  virtual bool addData (TString path, TString options = "");
  virtual bool addBackground(TString path, TString options = "");
  virtual bool addSignal (TString path, TString options = "");

  virtual bool plotAndSaveAs(const TString& histogram, const TString& saveAs, const char* inputTags);
  virtual bool plotAndSaveAs(const TString& histogram, const TString& saveAs, const TString& inputTags);
  virtual bool plotAndSaveAs(const TString& histogram, const TString& saveAs, TQTaggable& inputTags);
  virtual bool plotAndSaveAs(const TString& histogram, const TString& saveAs, TQTaggable * inputTags = 0);

  virtual ~TQPlotter(); 

  virtual void reset();

  int sanitizeProcesses();

  static TString createAxisTagsAsString(const TString& prefix, const TString& title, double xCoeff, double yCoeff, double constCoeff, double wMin, double wMax, double xCoord, double yCoord, int nDiv = 101);
  static bool createAxisTags(TQTaggable& tags, const TString& prefix, const TString& title, double xCoeff, double yCoeff, double constCoeff, double wMin, double wMax, double xCoord, double yCoord, int nDiv = 101);
  static TString createAxisTagsAsConfigString(const TString& prefix, const TString& title, double xCoeff, double yCoeff, double constCoeff, double wMin, double wMax, double xCoord, double yCoord, int nDiv = 101);
  static TQTaggable* createAxisTags(const TString& prefix, const TString& title, double xCoeff, double yCoeff, double constCoeff, double wMin, double wMax, double xCoord, double yCoord, int nDiv = 101);

  static void estimateRangeY(TH1* h, double& min, double &max, double tolerance = std::numeric_limits<double>::infinity());
  static void estimateRangeY(TGraphErrors* g, double& min, double &max, double tolerance = std::numeric_limits<double>::infinity());
  static void estimateRangeY(TGraphAsymmErrors* g, double& min, double &max, double tolerance = std::numeric_limits<double>::infinity());
  static void getRange(TGraphErrors* g, double &xlow, double &xhigh, double &ylow, double &yhigh, bool get_xrange=true, bool get_yrange=true, double maxQerr = std::numeric_limits<double>::infinity());
  static TH1* includeSystematics(const TH1* hTotalBkg, TQFolder* sysFolder, TQTaggable& tags);
  static void applyErrors(const TH1* hSource, TH1* hTarget, const TH1* hSys, TQTaggable& tags);
  
  TGraphAsymmErrors* getRatioErrorGraph(TH1* hTotakBkg);
  TGraphAsymmErrors* getRatioGraph(TH1* h_data, TH1* hTotakBkg, bool invert, double ratioContentThreshold, bool verbose);

  TString getScaleFactorList(TString histname);

  ClassDefOverride(TQPlotter, 0); //Base class for QFramework plotters

};

#endif
 TQPlotter.h:1
 TQPlotter.h:2
 TQPlotter.h:3
 TQPlotter.h:4
 TQPlotter.h:5
 TQPlotter.h:6
 TQPlotter.h:7
 TQPlotter.h:8
 TQPlotter.h:9
 TQPlotter.h:10
 TQPlotter.h:11
 TQPlotter.h:12
 TQPlotter.h:13
 TQPlotter.h:14
 TQPlotter.h:15
 TQPlotter.h:16
 TQPlotter.h:17
 TQPlotter.h:18
 TQPlotter.h:19
 TQPlotter.h:20
 TQPlotter.h:21
 TQPlotter.h:22
 TQPlotter.h:23
 TQPlotter.h:24
 TQPlotter.h:25
 TQPlotter.h:26
 TQPlotter.h:27
 TQPlotter.h:28
 TQPlotter.h:29
 TQPlotter.h:30
 TQPlotter.h:31
 TQPlotter.h:32
 TQPlotter.h:33
 TQPlotter.h:34
 TQPlotter.h:35
 TQPlotter.h:36
 TQPlotter.h:37
 TQPlotter.h:38
 TQPlotter.h:39
 TQPlotter.h:40
 TQPlotter.h:41
 TQPlotter.h:42
 TQPlotter.h:43
 TQPlotter.h:44
 TQPlotter.h:45
 TQPlotter.h:46
 TQPlotter.h:47
 TQPlotter.h:48
 TQPlotter.h:49
 TQPlotter.h:50
 TQPlotter.h:51
 TQPlotter.h:52
 TQPlotter.h:53
 TQPlotter.h:54
 TQPlotter.h:55
 TQPlotter.h:56
 TQPlotter.h:57
 TQPlotter.h:58
 TQPlotter.h:59
 TQPlotter.h:60
 TQPlotter.h:61
 TQPlotter.h:62
 TQPlotter.h:63
 TQPlotter.h:64
 TQPlotter.h:65
 TQPlotter.h:66
 TQPlotter.h:67
 TQPlotter.h:68
 TQPlotter.h:69
 TQPlotter.h:70
 TQPlotter.h:71
 TQPlotter.h:72
 TQPlotter.h:73
 TQPlotter.h:74
 TQPlotter.h:75
 TQPlotter.h:76
 TQPlotter.h:77
 TQPlotter.h:78
 TQPlotter.h:79
 TQPlotter.h:80
 TQPlotter.h:81
 TQPlotter.h:82
 TQPlotter.h:83
 TQPlotter.h:84
 TQPlotter.h:85
 TQPlotter.h:86
 TQPlotter.h:87
 TQPlotter.h:88
 TQPlotter.h:89
 TQPlotter.h:90
 TQPlotter.h:91
 TQPlotter.h:92
 TQPlotter.h:93
 TQPlotter.h:94
 TQPlotter.h:95
 TQPlotter.h:96
 TQPlotter.h:97
 TQPlotter.h:98
 TQPlotter.h:99
 TQPlotter.h:100
 TQPlotter.h:101
 TQPlotter.h:102
 TQPlotter.h:103
 TQPlotter.h:104
 TQPlotter.h:105
 TQPlotter.h:106
 TQPlotter.h:107
 TQPlotter.h:108
 TQPlotter.h:109
 TQPlotter.h:110
 TQPlotter.h:111
 TQPlotter.h:112
 TQPlotter.h:113
 TQPlotter.h:114