Skip to content
Snippets Groups Projects
AsgElectronChargeIDSelectorTool.cxx 24.5 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
/*
  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
*/

/**
   @class AsgElectronChargeIDSelectorTool
   @brief Electron selector tool to select objects in Asgena using an underlying pure ROOT tool.
 
   @author Karsten Koeneke
   @date   October 2012
   09-APR-2014, convert to ASGTool (Jovan Mitrevski)
   22-AUG-2016, copied from AsgElectronLikelihoodTool (Kazuya Mochizuki) 
*/

// Include this class's header
#include "ElectronPhotonSelectorTools/AsgElectronChargeIDSelectorTool.h"




// STL includes
#include <string>
#include <cstdint>
#include <cmath>

//EDM includes
#include "xAODEgamma/Electron.h"
#include "xAODTracking/Vertex.h"
#include "xAODTracking/VertexContainer.h"
#include "xAODCaloEvent/CaloCluster.h"
#include "xAODEventInfo/EventInfo.h"
#include "TROOT.h"
#include "TKey.h"
#include "TClass.h"
#include "TEnv.h"
#include "TFile.h"
#include "TObjString.h"
#include "TObjArray.h"

#include "AsgDataHandles/ReadHandle.h"
#include "AsgTools/CurrentContext.h"
#include "PathResolver/PathResolver.h"


//=============================================================================
// Standard constructor
//=============================================================================
AsgElectronChargeIDSelectorTool::AsgElectronChargeIDSelectorTool(const std::string& myname) :
  AsgTool(myname) //,m_cutOnBDT(0)//,m_configFile("")//,m_rootTool(0)
{
  // Declare the needed properties
  declareProperty("WorkingPoint",m_WorkingPoint="","The Working Point");
  //declareProperty("ConfigFile",m_configFile="","The config file to use");
  declareProperty("usePVContainer", m_usePVCont=true, "Whether to use the PV container");
  declareProperty("nPVdefault", m_nPVdefault = 0, "The default number of PVs if not counted");
  //declareProperty("primaryVertexContainer", m_primVtxContKey="PrimaryVertices", "The primary vertex container name" );
    
  declareProperty("TrainingFile",  m_trainingFile="", "The input ROOT file name holding training" );
  declareProperty("CutOnBDT",m_cutOnBDT=0,"Cut on BDT discriminant");
  m_pid_name=myname.data();
}


//=============================================================================
// Standard destructor
//=============================================================================
AsgElectronChargeIDSelectorTool::~AsgElectronChargeIDSelectorTool()
{
  for (auto bdt: m_v_bdts) if (bdt) delete bdt;
}


//=============================================================================
// Asgena initialize method
//=============================================================================
StatusCode AsgElectronChargeIDSelectorTool::initialize()
{
  m_pid_name.ToLower(); //KM: List of 97% OPs with different PIDs below
  std::string op_name="loose";
  bool op_isUserSpecified=false;
  if (m_cutOnBDT==0) {  //when cutOnBDT is unmodified, adjust it to the 97% OP in each PID menu
    if      (m_pid_name.Contains("tight") ) op_name="tight" , m_cutOnBDT=-0.109249;//Tight  (with data): -0.109249
    else if (m_pid_name.Contains("medium")) op_name="medium", m_cutOnBDT=-0.257081;//Medium (with data): -0.257081
    else                                                      m_cutOnBDT=-0.337671;//Loose  (with data): -0.337671
  }
  else op_isUserSpecified=true;
  m_pid_name="loose";//Now only one training is provided, using loose PID but OP varies for differnt PID
    
  std::string display= op_isUserSpecified ? "user specified":"97% signal-eff";
  ATH_MSG_INFO("OP to use: " << op_name <<", with cut on BDT: "<<m_cutOnBDT<<", which corresponds to "<<display<<" working point.");
    
  std::string TrainingFile;
  if (!m_trainingFile.empty()) {  //If the property was set by the user, take that.
        
    TrainingFile= PathResolverFindCalibFile( m_trainingFile );
    if(TrainingFile==""){//Error if it cant find the conf
      ATH_MSG_ERROR("Could not locate " << m_trainingFile );
      return StatusCode::FAILURE;
    }
    else ATH_MSG_INFO("trainingfile loaded from: " << TrainingFile );

  }
  else {
    ATH_MSG_ERROR("Could not find configuration file: \""<< m_trainingFile<<"\"");
    return StatusCode::FAILURE;
  }
    
  unsigned nfold=1;
  TFile* bdtfile = TFile::Open(TrainingFile.data());
  if (!bdtfile) {
    ATH_MSG_ERROR("Input file found to be empty!! "<< TrainingFile);
    return StatusCode::FAILURE;
  }
  else {
    TIter next(bdtfile->GetListOfKeys());
    TKey *key;
    while ((key = (TKey*)next())) {
      TClass *clas = gROOT->GetClass(key->GetClassName());
      if (!clas->InheritsFrom("TDirectoryFile")) continue;
      TDirectory *td = (TDirectoryFile*)key->ReadObj();
      std::string dirName =td->GetName();
      if (dirName.find(m_pid_name)!=std::string::npos) {
        std::string foldconf=dirName.substr(dirName.rfind("_")+1,-1);
        // std::string f_index=foldconf.substr(0,foldconf.find("o"));
        std::string s_nfold=foldconf.substr(foldconf.find("o")+1,-1);
        nfold=atoi(s_nfold.data());
        break;
      }
    }
  }
    
  ATH_MSG_INFO("ECIDS nfold configuration: "<<nfold);
    
  TObjArray* toa= (TObjArray*) bdtfile->Get("/ECIDS_"+m_pid_name+TString::Format("_0o%d",nfold)+"/variables");
  std::string commaSepVars="";
  if (toa) {
    TObjString *tos= 0;
    if (toa->GetEntries()>0) tos= (TObjString*) toa->At(0);
    commaSepVars=tos->GetString().Data();
    ATH_MSG_INFO("Variables for ECIDS= "<<commaSepVars);
  }
  else ATH_MSG_FATAL("Cannot access the list of input variables @"<<bdtfile->GetName()<<":/ECIDS_"+m_pid_name+TString::Format("_0o%d",nfold)+"/variables");
    
  //prepare m_inputVars
  m_inputVars.clear();
  while (commaSepVars.find(",")!=std::string::npos) {
    m_inputVars.push_back(commaSepVars.substr(0,commaSepVars.find(",")));
    commaSepVars.erase(0,commaSepVars.find(",")+1);
  }
  m_inputVars.push_back(commaSepVars.substr(0,-1));//push back the last element

  for (unsigned i_fold=0; i_fold<nfold; i_fold++) {
    TString treename="/ECIDS_"+m_pid_name+TString::Format("_%do%d",i_fold,nfold)+"/BDT";
    //std::cout<<"Trying to access a ttree with name: "<<treename<<std::endl;
    TTree* tree = (TTree*)bdtfile->Get(treename);
    m_v_bdts.push_back(new MVAUtils::BDT(tree));
  }
    
  ///-----------End of text config----------------------------

  // Setup primary vertex key handle
  ATH_CHECK( m_primVtxContKey.initialize(m_usePVCont) );
    
  m_cutPosition_bdt = m_acceptInfo.addCut( "bdt", "pass bdt" );
    
  return StatusCode::SUCCESS ;
}

//=============================================================================
// The main accept method: the actual cuts are applied here
//=============================================================================
asg::AcceptData AsgElectronChargeIDSelectorTool::accept(const xAOD::Electron* el, double mu ) const
{
  //Backwards compatibility
  return accept(Gaudi::Hive::currentContext(), el, mu );
}
asg::AcceptData AsgElectronChargeIDSelectorTool::accept(const EventContext& ctx,  const xAOD::Electron* eg, double mu ) const
{
    
  double bdt=calculate(ctx,eg,mu);

  ATH_MSG_VERBOSE("\t accept( ctx, el, mu ), bdt="<<bdt);

  asg::AcceptData acceptBDT(&m_acceptInfo);
  acceptBDT.clear();

  acceptBDT.setCutResult(m_cutPosition_bdt,bdt>m_cutOnBDT);
   
  return acceptBDT;
}

//=============================================================================
// Accept method for EFCaloLH in the trigger; do full LH if !CaloCutsOnly
//=============================================================================
asg::AcceptData AsgElectronChargeIDSelectorTool::accept(const xAOD::Egamma* eg, double mu ) const
{
  //Backwards compatibility
  return accept(Gaudi::Hive::currentContext(), eg, mu );
}
asg::AcceptData AsgElectronChargeIDSelectorTool::accept(const EventContext& ctx,  const xAOD::Egamma* eg, double mu) const
{
  double bdt=calculate(ctx,eg,mu);
    
  ATH_MSG_VERBOSE("\t accept( ctx, eg, mu ), bdt="<<bdt);

  asg::AcceptData acceptBDT(&m_acceptInfo);    
  acceptBDT.clear();
    
  acceptBDT.setCutResult(m_cutPosition_bdt,bdt>m_cutOnBDT);
    
  return acceptBDT;
}

//=============================================================================
// The main result method: the actual likelihood is calculated here
//=============================================================================
double AsgElectronChargeIDSelectorTool::calculate( const EventContext& ctx, const xAOD::Electron* eg, double mu ) const
{
    
  ATH_MSG_VERBOSE("\t AsgElectronChargeIDSelectorTool::calculate( &ctx, *eg, mu= "<<(&ctx)<<", "<<eg<<", "<<mu<<" )");
    
  if ( !eg ) {
    ATH_MSG_ERROR ("Failed, no egamma object.");
    return -1;
  }
    
  const xAOD::CaloCluster* cluster = eg->caloCluster();
  if ( !cluster ) {
    ATH_MSG_ERROR ("Failed, no cluster.");
    return -1;
  }
    
  const double energy =  cluster->e();
  const float eta = cluster->etaBE(2);
  if ( fabs(eta) > 300.0 ) {
    ATH_MSG_ERROR ("Failed, eta range.");
    return -1;
  }
    
  double et = 0.;// transverse energy of the electron (using the track eta)
  if (eg->trackParticle() )
    et     = ( cosh(eg->trackParticle()->eta()) != 0.) ? energy/cosh(eg->trackParticle()->eta()) : 0.;
  else et  = ( cosh(eta) != 0.) ? energy/cosh(eta) : 0.;
    
    
  // number of track hits and other track quantities
  uint8_t nSCT(0);
  float trackqoverp(0.0);
  float trackqoverpsig(0.0);
  int   charge(0.0);
  int   lifeSign(0.0);
  float trackchi2(0.0);
  float avgCharge_SCTw(0.0);
  float d0(0.0);
  float z0(0.0);
  float phi0(0.0);
  float theta(0.0);
  float EoverP(0.0);
  float d0sigma(0.0);
  double dpOverp(0.0);
  float TRT_PID(0.0);
  //double trans_TRT_PID(0.0);
  float deltaPhi1=0, deltaPhi2=0;
  float deltaPhiFromLM=0;
  float deltaPhiRescaled2=0;//deltaEta=0,
  //double rTRT(0.0);
    
  TVector2 el_cluster; el_cluster.SetMagPhi(cluster->energyBE(2)/cosh(eta),cluster->phiBE(2));
    
  bool allFound = true;
  // retrieve associated TrackParticle
  const xAOD::TrackParticle* t = eg->trackParticle();
  if (t) {
    trackqoverp = t->qOverP();
    charge= t->charge();
    d0 = t->d0();

    if(std::find(m_inputVars.begin(),m_inputVars.end(), "z0sinTheta" )!= m_inputVars.end()) {
      z0 = t->z0();
      theta = t->theta();
    }

    if(std::find(m_inputVars.begin(),m_inputVars.end(), "chi2oftrackfit" )!= m_inputVars.end())
      trackchi2 = t->chiSquared();
        
    phi0 = t->phi() + (d0>=0? M_PI/2 : -M_PI/2);
    TVector2 d0_direction;    d0_direction.SetMagPhi(fabs(d0),phi0);
    float inner_product = el_cluster.X()*d0_direction.X() + el_cluster.Y()*d0_direction.Y();
    lifeSign = inner_product>=0? 1 : -1;
        
    EoverP   = energy * fabs(t->qOverP());
    if(std::find(m_inputVars.begin(),m_inputVars.end(), "d0Err" )!= m_inputVars.end() or
       std::find(m_inputVars.begin(),m_inputVars.end(), "d0Sig" )!= m_inputVars.end()) {
      float vard0 = t->definingParametersCovMatrix()(0,0);
      if (vard0 > 0) {
        d0sigma=sqrtf(vard0);
      }
    }
        
    //KM: calculation of SCT-weighted charge
    float charge = 0, SCT = 0;
    for (unsigned TPit = 0; TPit < eg->nTrackParticles(); TPit++) {
      uint8_t temp_NSCTHits;
      if(eg->trackParticle(TPit)) {
	eg->trackParticle(TPit)->summaryValue(temp_NSCTHits, xAOD::numberOfSCTHits);
	
	SCT += temp_NSCTHits;
	charge += temp_NSCTHits*eg->trackParticle(TPit)->charge();
      }
      else ATH_MSG_WARNING("This electron has no track particle associated!!! Assigning #SCT-hits= 0!!! " );
    }
    avgCharge_SCTw= SCT!=0 ? eg->charge()*charge/SCT : 0;
        
    const std::vector<float>&cov= t->definingParametersCovMatrixVec();
    trackqoverpsig= cov[14];
        
    if(std::find(m_inputVars.begin(),m_inputVars.end(), "nSctHits" )!= m_inputVars.end() )
      allFound = allFound && t->summaryValue(nSCT, xAOD::numberOfSCTHits);
        
    //Transform the TRT PID output for use in the LH tool.
    double fEpsilon = 1.0e-30;  // to avoid zero division
    double pid_tmp = TRT_PID;
    if (pid_tmp >= 1.0) pid_tmp = 1.0 - 1.0e-15;  //this number comes from TMVA
    else if (pid_tmp <= fEpsilon) pid_tmp = fEpsilon;
        
    if(std::find(m_inputVars.begin(),m_inputVars.end(), "deltaPoverP" )!= m_inputVars.end() ) {
      unsigned int index;
      if( t->indexOfParameterAtPosition(index, xAOD::LastMeasurement) ) {
                
        double refittedTrack_LMqoverp  =
          t->charge() / sqrt(std::pow(t->parameterPX(index), 2) +
                             std::pow(t->parameterPY(index), 2) +
                             std::pow(t->parameterPZ(index), 2));
                
        dpOverp = 1 - trackqoverp/(refittedTrack_LMqoverp);
      }
    }
        
  }
  else {
    allFound=false;
    ATH_MSG_WARNING ( "Failed, no track particle: et= " << et << "eta= " << eta );
  }
    
  float Rphi(0);//float Reta(0), Rphi(0),  Rhad1(0), Rhad(0), ws3(0), w2(0), f1(0), Eratio(0), f3(0);
  allFound = allFound && eg->showerShapeValue(Rphi, xAOD::EgammaParameters::Rphi);// rphi e233/e237
  // allFound = allFound && eg->trackCaloMatchValue(deltaEta, xAOD::EgammaParameters::deltaEta1);
    
  // difference between the cluster phi (sampling 2) and the eta of the track extrapolated from the last measurement point.
  allFound = allFound && eg->trackCaloMatchValue(deltaPhiRescaled2, xAOD::EgammaParameters::deltaPhiRescaled2);
    
  //if(m_map_inputs.find("deltaphi1"              )!= m_map_inputs.end())
  if(std::find(m_inputVars.begin(),m_inputVars.end(), "deltaphi1" )!= m_inputVars.end() )
    allFound = allFound && eg->trackCaloMatchValue(deltaPhi1, xAOD::EgammaParameters::deltaPhi1);
  // if(m_map_inputs.find("deltaphi2"              )!= m_map_inputs.end() or
  //    m_map_inputs.find("deltaDeltaPhiFirstAndLM")!= m_map_inputs.end())
  if(std::find(m_inputVars.begin(),m_inputVars.end(), "deltaphi2"              )!= m_inputVars.end() or
     std::find(m_inputVars.begin(),m_inputVars.end(), "deltaDeltaPhiFirstAndLM")!= m_inputVars.end() )
    allFound = allFound && eg->trackCaloMatchValue(deltaPhi2, xAOD::EgammaParameters::deltaPhi2);
  //if(m_map_inputs.find("deltaDeltaPhiFirstAndLM")!= m_map_inputs.end())
  if(std::find(m_inputVars.begin(),m_inputVars.end(), "deltaDeltaPhiFirstAndLM" )!= m_inputVars.end() )
    allFound = allFound && eg->trackCaloMatchValue(deltaPhiFromLM, xAOD::EgammaParameters::deltaPhiFromLastMeasurement);
    
  // Get the number of primary vertices in this event
  // double ip = static_cast<double>(m_nPVdefault);
  // if(mu < 0) // use npv if mu is negative (not given)
  //   ip = static_cast<double>(m_usePVCont ? this->getNPrimVertices() : m_nPVdefault);
  // else ip = mu;
    
  if (!allFound) ATH_MSG_FATAL("Missing input variable for ECIDS BDT calculation");
    
  const xAOD::EventInfo* eventInfo = nullptr;
  if (evtStore()->retrieve(eventInfo,"EventInfo").isFailure()) ATH_MSG_WARNING ( " Cannot access to event info " );
  // lumiBlock = eventInfo->lumiBlock(), runNumber = eventInfo->runNumber(), eventNumber=eventInfo->eventNumber();
  //ATH_MSG_DEBUG("event_num%bdt_size="<<eventInfo->eventNumber()<<"%"<<unsigned(m_v_bdts.size())<<"= "<<eventInfo->eventNumber()%unsigned(m_v_bdts.size()));
  unsigned bdt_index=eventInfo->eventNumber()%unsigned(m_v_bdts.size());
    
  std::vector<float> v_inputs;
  for (auto var: m_inputVars) {
    if (var == "pt"                     ) v_inputs.push_back(et                	     );
    if (var == "eta"                    ) v_inputs.push_back(eta               	     );
    if (var == "abs_eta"                ) v_inputs.push_back(fabs(eta)         	     );
    if (var == "avgCharge_SCTw"         ) v_inputs.push_back(avgCharge_SCTw    	     );
    if (var == "d0"                     ) v_inputs.push_back(d0                	     );
    if (var == "ld0"                    ) v_inputs.push_back(lifeSign*d0       	     );
    if (var == "cd0"                    ) v_inputs.push_back(charge*d0         	     );
    if (var == "EoverP"                 ) v_inputs.push_back(EoverP            	     );
    if (var == "deltaphi1"              ) v_inputs.push_back(deltaPhi1         	     );
    if (var == "deltaphiRes"            ) v_inputs.push_back(deltaPhiRescaled2 	     );
    if (var == "Rphi"                   ) v_inputs.push_back(Rphi              	     );
    if (var == "qoverpSig"              ) v_inputs.push_back(trackqoverpsig    	     );
    if (var == "nSctHits"               ) v_inputs.push_back(nSCT              	     );
    if (var == "z0sinTheta"             ) v_inputs.push_back(z0*sin(theta)     	     );
    if (var == "d0Err"                  ) v_inputs.push_back(d0sigma           	     );
    if (var == "d0Sig"                  ) v_inputs.push_back(d0/d0sigma        	     );
    if (var == "deltaphi2"              ) v_inputs.push_back(deltaPhi2         	     );
    if (var == "chi2oftrackfit"         ) v_inputs.push_back(trackchi2         	     );
    if (var == "deltaPoverP"            ) v_inputs.push_back(dpOverp           	     );
    if (var == "deltaDeltaPhiFirstAndLM") v_inputs.push_back(deltaPhi2-deltaPhiFromLM);
  }

  ATH_MSG_VERBOSE("\t\t event# "                                   <<eventInfo->eventNumber()                                                                                                                         <<std::endl<<
		  "xAOD variables: pt                           = "<< et                       <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"pt"                     )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: eta                     = "<< eta                      <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"eta"                    )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: abs_eta                 = "<< fabs(eta)                <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"abs_eta"                )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: avgCharge_SCTw          = "<< avgCharge_SCTw           <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"avgCharge_SCTw"         )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: d0                      = "<< d0                       <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"d0"                     )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: ld0                     = "<< lifeSign*d0              <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"ld0"                    )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: cd0                     = "<< charge*d0                <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"cd0"                    )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: EoverP                  = "<< EoverP                   <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"EoverP"                 )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: deltaphi1               = "<< deltaPhi1                <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"deltaphi1"              )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: deltaphiRes             = "<< deltaPhiRescaled2        <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"deltaphiRes"            )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: Rphi                    = "<< Rphi                     <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"Rphi"                   )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: qoverpSig               = "<< trackqoverpsig           <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"qoverpSig"              )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: nSctHits                = "<< unsigned(nSCT)           <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"nSctHits"               )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: z0sinTheta              = "<< z0*sin(theta)            <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"z0sinTheta"             )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: d0Err                   = "<< d0sigma                  <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"d0Err"                  )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: d0Sig                   = "<< d0/d0sigma               <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"d0Sig"                  )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: deltaphi2               = "<< deltaPhi2                <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"deltaphi2"              )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: chi2oftrackfit          = "<< trackchi2                <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"chi2oftrackfit"         )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: deltaPoverP             = "<< dpOverp                  <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"deltaPoverP"            )!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: deltaDeltaPhiFirstandLM = "<< deltaPhi2-deltaPhiFromLM <<",\t isRequested= "<<(std::find(m_inputVars.begin(),m_inputVars.end(),"deltaDeltaPhiFirstAndLM")!=m_inputVars.end() )<<std::endl<<
                  "\t\t xAOD variables: AllFound                = "<<allFound            );
  
  ////KM: dumping variables, only variables used by BDT
  // std::cout<<"\t\t event# "<<eventInfo->eventNumber()<<std::endl;
  // unsigned i=0;
  // for (auto inputVar: m_inputVars) {
  //   std::cout<<"\t kmdebug: "<<inputVar<<"\t = "<<v_inputs[i]<<std::endl; i++;
  // }
  
  //double bdt_output = m_v_bdts.at(bdt_index)->GetGradBoostMVA(m_v_bdts.at(bdt_index)->GetPointers());
  double bdt_output = m_v_bdts.at(bdt_index)->GetGradBoostMVA(v_inputs);
  ATH_MSG_DEBUG("ECIDS-BDT= "<<bdt_output);
  //std::cout<<"\t kmdebug: \t ECIDS-BDT= "<<bdt_output<<std::endl;

  return bdt_output;
}

//=============================================================================
// Calculate method for EFCaloLH in the trigger; do full LH if !CaloCutsOnly
//=============================================================================
double AsgElectronChargeIDSelectorTool::calculate( const xAOD::Egamma* eg, double mu ) const
{
  //Backward compatibility
  return calculate(Gaudi::Hive::currentContext(), eg, mu);
}

double AsgElectronChargeIDSelectorTool::calculate( const EventContext& ctx, const xAOD::Egamma* eg, double mu ) const
{
  ATH_MSG_VERBOSE("AsgElectronChargeIDSelectorTool::calculate( &ctx ="<<(&ctx)<<", *eg "<<eg<<", mu= "<<mu<< " )");
  ATH_MSG_WARNING("Method not implemented for egamma object! Reurning -1!!");
    
  return -9;
}

//=============================================================================
asg::AcceptData AsgElectronChargeIDSelectorTool::accept(const xAOD::IParticle* part) const
{
  //Backward compatibility
  return accept(Gaudi::Hive::currentContext(), part);
}
asg::AcceptData AsgElectronChargeIDSelectorTool::accept(const EventContext& ctx, const xAOD::IParticle* part) const
{
  if(part->type() == xAOD::Type::Electron){
    const xAOD::Electron* el = static_cast<const xAOD::Electron*>(part);
    return accept(ctx, el);
  }
  
  ATH_MSG_ERROR("Input is not an electron");
  return asg::AcceptData(&m_acceptInfo);
}

double AsgElectronChargeIDSelectorTool::calculate(const xAOD::IParticle* part) const
{
  //Backward compatibility
  return calculate(Gaudi::Hive::currentContext(), part);
}

double AsgElectronChargeIDSelectorTool::calculate(const EventContext& ctx, const xAOD::IParticle* part) const
{
  if(part->type() == xAOD::Type::Electron){
    const xAOD::Electron* el = static_cast<const xAOD::Electron*>(part);
    return calculate(ctx, el);
  }
  
  ATH_MSG_ERROR ( "Input is not an electron!!" );
  return -19;
}

//=============================================================================
// Helper method to get the number of primary vertices
// We don't want to iterate over all vertices in the event for each electron!!! 
//=============================================================================
unsigned int AsgElectronChargeIDSelectorTool::getNPrimVertices(const EventContext& ctx) const
{
  unsigned int nVtx(0);
  SG::ReadHandle<xAOD::VertexContainer> vtxCont (m_primVtxContKey, ctx); 
  for ( unsigned int i = 0; i < vtxCont->size(); i++ ) {
      const xAOD::Vertex* vxcand = vtxCont->at(i);
      if ( vxcand->nTrackParticles() >= 2 ) nVtx++;
  }
  return nVtx;
}