00001 #ifndef egammaMVACalibNew_H
00002 #define egammaMVACalibNew_H
00003
00004 #include <map>
00005 #include <vector>
00006 #include <iostream>
00007 #include <memory>
00008 #include <string>
00009
00010 #include <TString.h>
00011 #include <TObject.h>
00012 #include <TFile.h>
00013 #include "AsgTools/AsgMessaging.h"
00014
00015 class TTreeFormula;
00016 class TList;
00017 class TTree;
00018 class TPRegexp;
00019 class TObjArray;
00020 class TXMLEngine;
00021 class TH2Poly;
00022 class TAxis;
00023 class TF1;
00024
00025 namespace TMVA { class Reader; }
00026 namespace MVAUtils { class BDT; }
00027
00028
00029
00052 class egammaMVACalib : public asg::AsgMessaging
00053 {
00054 public:
00055 enum egammaType {egPHOTON, egELECTRON, NEGAMMATYPES};
00056 enum ParticleType {UNCONVERTED=0, CONVERTED=1, SiSi=2, ELECTRON=3, NPARTICLETYPES=4, INVALIDPARTICLE=-1};
00057 enum CalibrationType {correctEcluster, correctEaccordion, fullCalibration, NCalibrationTypes };
00058 enum ShiftType {NOSHIFT=0, PEAKTOTRUE, MEANTOTRUE, MEDIANTOTRUE, MEAN10TOTRUE, MEAN20TOTRUE, MEDIAN10TOTRUE, MEDIAN20TOTRUE, NSHIFTCORRECTIONS};
00059
00060 struct ReaderID
00061 {
00062 ParticleType particleType;
00063 int bin;
00064 bool operator< (const ReaderID &other) const
00065 {
00066 if (this->particleType != other.particleType)
00067 return (this->particleType < other.particleType);
00068 return (this->bin < other.bin);
00069 }
00070 };
00071
00072 typedef std::map<TString, TString> AdditionalInfoMap;
00073 typedef std::map< std::pair< egammaMVACalib::ReaderID, egammaMVACalib::ShiftType>, TF1* > ShiftMap;
00074
00077 struct VarFormula
00078 {
00079 float variable;
00080 TString expression;
00081 TString infoType;
00082 TString valueType;
00083 TTreeFormula* formula;
00084 bool external;
00085 };
00086
00099 egammaMVACalib(int particle,
00100 bool useNewBDTs = true,
00101 TString folder = "",
00102 const TString & method = "BDTG",
00103 int calibrationType = correctEaccordion,
00104 bool debug=false,
00105 const TString & etaBinVar = "",
00106 const TString & energyBinVar = "",
00107 const TString & particleTypeVar = "",
00108 TString filePattern = "",
00109 bool ignoreSpectators = true);
00110
00111 virtual ~egammaMVACalib();
00112
00113 void setPeakCorrection(ShiftType shift_type) { m_shiftType = shift_type; };
00115 void setPeakCorrection(TString shift_type);
00116
00118 void useClusterIf0(bool useCluster = true) { m_clusterEif0 = useCluster; }
00119
00121 void setDefinition(const TString & variable, const TString & expression);
00122
00127 void InitTree(TTree* tree, bool doNotify = true);
00128
00133 virtual void LoadVariables(int index);
00134
00137 float getMVAOutput(int index = 0);
00138
00145 float getMVAEnergy(int index = 0, ShiftType shift_type = NSHIFTCORRECTIONS);
00146
00147 float getMVAEnergyPhoton(float ph_rawcl_Es0,
00148 float ph_rawcl_Es1,
00149 float ph_rawcl_Es2,
00150 float ph_rawcl_Es3,
00151 float ph_cl_eta,
00152 float ph_cl_E,
00153 float ph_cl_etaCalo,
00154 float ph_cl_phiCalo,
00155 float ph_ptconv,
00156 float ph_pt1conv,
00157 float ph_pt2conv,
00158 int ph_convtrk1nPixHits,
00159 int ph_convtrk1nSCTHits,
00160 int ph_convtrk2nPixHits,
00161 int ph_convtrk2nSCTHits,
00162 float ph_Rconv);
00163
00164 float getMVAEnergyElectron(float el_rawcl_Es0,
00165 float el_rawcl_Es1,
00166 float el_rawcl_Es2,
00167 float el_rawcl_Es3,
00168 float el_cl_eta,
00169 float el_cl_E,
00170 float el_cl_etaCalo,
00171 float el_cl_phiCalo);
00172
00174 float* getAddress(const TString & input_name);
00175
00179 void setExternal(const TString & input_name);
00180
00182 void printValueInput();
00183
00193 TTree* getMVAResponseTree(TTree *tree, int Nentries = -1,
00194 TString branchName = "", TString copyBranches = "input",
00195 int first_event = 0, bool flatten = false, int update=10000);
00196
00198 float getShift(float Et, ReaderID key, ShiftType shift_type) const;
00199
00201 const TTreeFormula* getFormula(const TString & variable) {
00202 return (m_formulae.count(variable) ? m_formulae[variable].formula : 0);
00203 }
00204
00206 TObjArray* getListOfBranches();
00207
00209 void activateBranches();
00210
00214 int getNdata();
00215
00217 TH2Poly* getTH2Poly() const;
00218
00220 void writeROOTfile(const TString& directory, int particle = (int) INVALIDPARTICLE);
00221
00223 TMVA::Reader* getReader(egammaMVACalib::ReaderID key)
00224 {
00225 std::map< egammaMVACalib::ReaderID, TMVA::Reader* >::const_iterator it = m_readers.find(key);
00226 if (it == m_readers.end()) return 0;
00227 return it->second;
00228 }
00229
00233 egammaMVACalib::ReaderID getReaderID();
00234
00236 MVAUtils::BDT* getBDT(egammaMVACalib::ReaderID key)
00237 {
00238 std::map< egammaMVACalib::ReaderID, MVAUtils::BDT* >::const_iterator it = m_BDTs.find(key);
00239 if (it == m_BDTs.end()) return 0;
00240 return it->second;
00241 }
00242
00244
00245
00246
00247
00248
00250 static void addBranchesFromFormula(TObjArray* branches, TTreeFormula *formula);
00251
00253 static TString getString(TObject*);
00254
00256 static TObjArray* getVariables(const TString &xmlFileName);
00257
00259 static TMVA::Reader* getDummyReader(const TString &xmlFileName);
00260
00262 static TString* getVariables(TMVA::Reader *reader);
00263
00265 static void addReaderInfoToArrays(TMVA::Reader *reader,
00266 TObjArray *trees,
00267 TObjArray *variables,
00268 int index);
00269
00270
00271 protected:
00273 TString getCalibTypeString();
00274
00279 void getReaders(const TString & folder);
00280
00282 void getBDTs(const std::string& folder);
00283
00284 struct XmlVariableInfo {
00285 TString expression;
00286 TString label;
00287 TString varType;
00288 TString nodeName;
00289 };
00290
00295 static std::vector<XmlVariableInfo> parseXml(const TString & xmlFilename);
00296
00298 static std::vector<XmlVariableInfo> parseVariables(TXMLEngine *xml, void* node, const TString & nodeName);
00299
00302 void setupReader(TMVA::Reader* reader, const TString & xml_filename);
00303
00305 void setupBDT(const TString& fileName);
00306
00308 void setupFormulasForReaderID();
00309
00313 void predefineFormula(const TString & name, const TString & expression,
00314 const TString & varInfo, const TString & valueType = "F");
00315
00319 bool parseFileName(const TString & fileName, egammaMVACalib::ReaderID &key);
00320
00324 bool parseFileName(const TString & fileName, egammaMVACalib::ParticleType &pType);
00325
00330 TString getBinName( const TString & binField );
00331
00335 int getBin(float etaMin, float etaMax, float energyMin, float energyMax);
00336
00338 void printReadersInfo() const;
00339
00343 TTreeFormula* defineFormula(const TString & varName,
00344 const TString & expression,
00345 TTree *tree);
00346
00350 static egammaMVACalib::ParticleType getParticleType( const TString & s );
00351
00352
00353 int getParticleType() const;
00354 int getBin() const;
00355
00357 void printFormulae() const;
00358
00360 void checkFormula(TTreeFormula *formula);
00361
00365 void defineInitialEnergyFormula();
00366
00370 static AdditionalInfoMap getUserInfo(const TString & xmlfilename);
00371
00375 TTree* getOutputTree(TString copyBranches, bool deactivateFirst=true);
00376
00378 bool checkBin(int bin, float etaMin, float etaMax, float etMin, float etMax);
00379
00381 void defineShiftFormula(ReaderID key);
00382
00384 const TString& getShiftName(ShiftType shift) const;
00385
00387 void defineClusterEnergyFormula();
00388
00390 void checkShowerDepth(TTree *tree);
00391
00392 int getNreaders() const { return (m_useNewBDTs ? m_BDTs.size() : m_readers.size()); }
00393
00395 TTree* createInternalTree(egammaType, TTree *tree);
00396
00398
00399
00400 egammaMVACalib::egammaType m_egammaType;
00401 bool m_useNewBDTs;
00402 TString fMethodName;
00403 egammaMVACalib::CalibrationType m_calibrationType;
00404
00405 TString m_etaVar, m_energyVar, m_particleTypeVar;
00406 bool m_ignoreSpectators;
00407 bool m_hasEnergyBins;
00408 int m_binMultiplicity;
00409
00410 bool m_clusterEif0;
00411
00412
00413
00414 float *m_particleType, *m_eta, *m_energy, *m_initialEnergy;
00415
00416
00417
00418
00419 TH2Poly* m_hPoly;
00420
00421
00422 TTree *m_tree, *m_input_tree;
00423
00424
00425 bool m_useInternalTree;
00426
00427
00428 TPRegexp *m_fileNamePattern;
00429
00430
00431 float m_mvaOutput;
00432
00433
00434 float m_dummyFloat;
00435
00436 ShiftType m_shiftType;
00437
00438 std::map< egammaMVACalib::ReaderID, egammaMVACalib::AdditionalInfoMap> m_additional_infos;
00439
00440
00441 ShiftMap m_shiftMap;
00442
00443
00444 std::map< egammaMVACalib::ReaderID, TMVA::Reader* > m_readers;
00445
00446
00447 std::map< egammaMVACalib::ReaderID, MVAUtils::BDT* > m_BDTs;
00448
00449
00450 std::map< TString, egammaMVACalib::VarFormula > m_formulae;
00451
00452
00453 TTreeFormula* m_clusterFormula;
00454
00455
00456 float m_ph_rawcl_Es0;
00457 float m_ph_rawcl_Es1;
00458 float m_ph_rawcl_Es2;
00459 float m_ph_rawcl_Es3;
00460 float m_ph_cl_eta;
00461 float m_ph_cl_E;
00462 float m_ph_ptconv;
00463 float m_ph_pt1conv;
00464 float m_ph_pt2conv;
00465 float m_ph_cl_etaCalo;
00466 float m_ph_cl_phiCalo;
00467 int m_ph_convtrk1nPixHits;
00468 int m_ph_convtrk1nSCTHits;
00469 int m_ph_convtrk2nPixHits;
00470 int m_ph_convtrk2nSCTHits;
00471 float m_ph_rawcl_calibHitsShowerDepth;
00472 float m_ph_Rconv;
00473
00474 float m_el_rawcl_Es0;
00475 float m_el_rawcl_Es1;
00476 float m_el_rawcl_Es2;
00477 float m_el_rawcl_Es3;
00478 float m_el_cl_E_TileGap3;
00479 float m_el_cl_eta;
00480 float m_el_cl_E;
00481 float m_el_cl_etaCalo;
00482 float m_el_cl_phiCalo;
00483 float m_el_cl_phi;
00484 float m_el_rawcl_calibHitsShowerDepth;
00485
00486
00487 public:
00488 static std::vector<double> get_radius(double eta);
00489 static double get_shower_depth(double eta,
00490 double raw_cl_0,
00491 double raw_cl_1,
00492 double raw_cl_2,
00493 double raw_cl_3);
00494 private:
00495 class NotifyDispatcher : public TObject
00496 {
00497 public:
00498 NotifyDispatcher() = default;
00499 NotifyDispatcher(const std::vector<TObject*>& objs) : m_objs(objs) { }
00500 void add_object(TObject* obj) { m_objs.push_back(obj); }
00501 virtual Bool_t Notify() {
00502 for (auto obj : m_objs) { obj->Notify(); } return true;
00503 }
00504 private:
00505 std::vector<TObject*> m_objs;
00506 };
00507
00508 NotifyDispatcher m_notify_dispatcher;
00509 };
00510
00511 #endif