///////////////////////////////////////////////////////////////////
// CalibrationDataInterfaceROOT.h, (c) ATLAS Detector software
///////////////////////////////////////////////////////////////////

#ifndef ANALYSISCALIBRATIONDATAINTERFACEROOT_H
#define ANALYSISCALIBRATIONDATAINTERFACEROOT_H


#include <CalibrationDataInterfaceBase.h>

#include "TObject.h"
#include "Rtypes.h"
#include <vector>
#include <string>
#include <map>
#include <utility>

class TObject;
class TFile;

namespace Analysis 
{

  /** @class CalibrationDataInterfaceROOT 

      This tool provides an interface to flavour tagging performance estimates.

      A separate instance should be used for each different tagging algorithm.
      For each instance, all appropriate jet collections and tagger operating points
      need to be specified.

      The model:
      - b-jets:
      *   data-MC scale factor (factorised 2D function of eta, pt)
      *   MC reference
      *   the product is the data efficiency; alternatively, the scale factor may be used
      - c-jets:
      *   as for b-jets, but with a different MC reference
      - light-flavour jets:
      *   data-MC scale factor (factorised 2D function of eta, pt)
      *   MC reference
      Besides the results, it is also possible to retrieve associated uncertainties. This need not
      be configured, and a choice as to the uncertainty component can be made on a case-by-case
      basis.

      The idea is to use the same physical ROOT file that is also accessed through COOL, but to do
      so in a stand-alone fashion, so that there is no COOL or Athena dependence. Apart from this,
      the same infrastructure and limitations as with COOL access (@class CalibrationDataInterfaceTool)
      hold: in particular, "channel" (jet author) aliasing is possible. The difference is that
      run-time information is to be provided in a text file to be interpreted using ROOT's TEnv and
      

      DB representation (all residing under a common root folder):
      .../<jet author>/<tagger>/<operating point>/<flavour>/<object name>

      @author  Frank Filthaut <F.Filthaut@science.ru.nl>
  */  

  // Package all jet variables into a single structure

  struct CalibrationDataVariables {
    std::string jetAuthor;
    double jetPt;
    double jetEta;
  };

  enum Uncertainty { None, Total, Statistical, Systematic };

  class CalibrationDataInterfaceROOT : public CalibrationDataInterfaceBase {
    public:
      CalibrationDataInterfaceROOT(const std::string& taggerName, std::string configname = "BTagCalibration.env", std::string pathname = "");

       /** default destructor */
      virtual ~CalibrationDataInterfaceROOT();
      
      /**
	 Main interface methods accessing the flavour tagging performance information.
	 Note that for both of the following, the label is assumed to adhere to the
	 TruthInfo conventions (see package PhysicsAnalysis/JetTagging/JetTagInfo).
      */

      /** retrieve either the total uncertainty or only the statistical or systematic components.
	  Note that this enum is a duplication of that in class ICalibrationDataInterfaceTool:
	  not desirable but nontrivial to get around */
    

      /** efficiency retrieval */
      CalibResult getEfficiency (const CalibrationDataVariables& variables, const std::string& label,
				 const std::string& OP, Uncertainty unc = None) const;
      
      /** "MC" efficiency retrieval */
      CalibResult getMCEfficiency (const CalibrationDataVariables& variables, const std::string& label,
				   const std::string& OP, Uncertainty unc = None) const;
      
      /** efficiency scale factor retrieval */
      CalibResult getScaleFactor (const CalibrationDataVariables& variables, const std::string& label,
				  const std::string& OP, Uncertainty unc = None) const;

      /**  "MC" inefficiency scale factor retrieval    */
      CalibResult getInefficiencyScaleFactor(const CalibrationDataVariables& variables, const std::string& label,
					     const std::string& OP, Uncertainty unc = None) const;

      /**   inefficiency retrieval */
      CalibResult getInefficiency (const CalibrationDataVariables& variables, const std::string& label,
				   const std::string& OP, Uncertainty unc = None) const;

      /**     "MC" inefficiency retrieval */
      CalibResult getMCInefficiency (const CalibrationDataVariables& variables, const std::string& label,
				     const std::string& OP, Uncertainty unc = None) const;
      
    private:

      /** @brief auxiliary function to retrieve values of function arguments */
      void retrieveFunctionArguments(const CalibrationDataVariables& variables,
				     const std::string& object, Double_t* x) const;

      /** @brief pointer to the TFile object providing access to the calibrations */
      TFile* m_fileEff;
      TFile* m_fileSF;

      /** @brief jet author aliases (there is no single CalibrationBroker object here to
	  take care of this, so we do it in this class) */
      std::map<std::string, std::string> m_aliases;

      /** @brief cache the objects themselves (so that the user will not have to delete
	  them after each call etc.) */
      mutable std::map<std::string, TObject*> m_objects;

      /** @brief utility function taking care of object retrieval */
      TObject* retrieveTObject(const std::string& name, bool eff) const;

      /** @brief associated alias retrieval method */
      std::string getAlias(const std::string& author) const;

      /** @ brief construct the full object pathname from its individual components */
      std::string fullName(const std::string& author, const std::string& name) const;

    }; 
} // end of namespace

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