Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ReComputeMET.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 #include "caf_util/ReComputeMET.hpp"
00004 #include "cafe/Config.hpp"
00005 
00006 #include "TBranchElement.h"
00007 #include "TDirectory.h"
00008 
00009 using namespace cafe;
00010 using namespace std;
00011 using namespace metid;
00012 
00013 namespace caf_util {
00014 
00015   ReComputeMET::ReComputeMET(const char *name) : cafe::Processor(name), 
00016                                                  _tree(0),_mcvars("_nchunks"),
00017                                                  _useDefaultElectron(false),
00018                                                  _useDefaultMuon(false)
00019   {
00020     cafe::Config config(name);
00021 
00022     _fromBranch = config.get("From", "");
00023     _toBranch   = config.get("To", "");
00024     _treeName   = config.get("Tree", name);
00025     _vars       = config.getVString("Variables", " ,");
00026 
00027     if(_fromBranch.empty()) {
00028       throw std::runtime_error(std::string("ReComputeMET[") + name + "] : From: branch in config file not set");
00029     }
00030     if(_toBranch.empty()) {
00031       throw std::runtime_error(std::string("ReComputeMET[") + name + "] : To: branch in config file not set");
00032     }
00033     out() << "ReComputeMET[" << name << "] From: " << _fromBranch << " To: " << _toBranch << std::endl ;
00034     out() << "ReComputeMET[" << name << "] Output Tree: " << _treeName << std::endl;
00035 
00036     _jetBranch  = config.get("jetBranch","JCCB"); 
00037     _muonBranch = config.get("muonBranch",""); 
00038     _electronBranch = config.get("electronBranch","");
00039     _useDefaultJet  = config.get("useDefaultGoodJetDef",true);
00040 
00041     if(_muonBranch.empty()) {
00042       _muonBranch = "Muon";
00043       _useDefaultMuon = true;
00044     }
00045     if(_electronBranch.empty()) {
00046       _electronBranch = "EMscone";
00047       _useDefaultElectron = true;
00048     }
00049 
00050     string jes_str  = config.get("JES","NotShifted");
00051     if (jes_str == "ShiftedPos") _jes = ShiftedPos ;
00052     else if (jes_str == "ShiftedNeg") _jes = ShiftedNeg ;
00053     else if (jes_str == "NotShifted") _jes = NotShifted ;
00054     else {
00055       err () << "ReComputeMET WARNING: JES config string \"" 
00056              << jes_str << "\" is not recongnized! Will use not shifted JES."
00057              << endl ;
00058       _jes = NotShifted ;
00059     }
00060       
00061     float jetConeSize = config.get("jetConeSize",0.5) ;
00062     if (jetConeSize != 0.5 && jetConeSize != 0.7) {
00063       err() << "RecomputeMET ERROR: jetConeSize should either 0.5 or 0.7, you value is \""
00064             << jetConeSize << "\". WILL USE 0.5 as default !" << endl ;
00065       jetConeSize = 0.5;
00066     }
00067     string jetAlgoType;
00068     if (jetConeSize==0.5) jetAlgoType = "JCCB";
00069     if (jetConeSize==0.7) jetAlgoType = "JCCA";
00070 
00071     out() << "ReComputeMET will use " << jetAlgoType << " jets. You're input jet branch should correspond to this jet algorithm" << endl;
00072 
00073     _valgo.push_back("corr"+jetAlgoType);
00074     _valgo.push_back("corrmu"+jetAlgoType);
00075     _valgo.push_back("smear_corr"+jetAlgoType);
00076     _valgo.push_back("smear_corrmu"+jetAlgoType);
00077 
00078     _debug = config.get("Debug", 0);
00079 
00080   }
00081 
00082   void ReComputeMET::inputFileOpened(TFile *file)
00083   {
00084     if(TTree *tmb_tree = (TTree *)file->Get("TMBTree")) {
00085       _tree = dynamic_cast<TTree*>(gROOT->FindObject(_treeName.c_str()));
00086       if (_tree == 0) {
00087         _tree = new TTree(_treeName.c_str(), fullName().c_str());
00088         tmb_tree->AddFriend(_tree);
00089       }
00090 
00091       _branch = _tree->GetBranch(_toBranch.c_str()) ;
00092       if (_branch == 0) {
00093         if (TBranchElement *br = (TBranchElement*) tmb_tree->GetBranch(_fromBranch.c_str())) {
00094           _result = new TMBMet(); 
00095           _branch = _tree->Branch(_toBranch.c_str(), &_result, 4096); 
00096         } else {
00097           err() << "ReComputeMET[" << name() << "] No such branch: " << _fromBranch << std::endl;
00098         } // TBranchElement
00099       } // _branch == 0
00100     } else {
00101       err() << "ReComputeMET[" << name() << "] No TMBTree" << std::endl;
00102     }
00103 
00104   }
00105 
00106   void ReComputeMET::inputFileClosing(TFile *file)
00107   {
00108     if(TTree* oldtree = dynamic_cast<TTree *>(gROOT->Get(_treeName.c_str()))) { oldtree->Delete(); }
00109     _tree = 0;
00110     _branch = 0;
00111   }
00112 
00113   bool ReComputeMET::processEvent(cafe::Event &event)
00114   {
00115     if (!_result) return false;
00116     const TMBMet* oldmet = event.get<TMBMet>(_fromBranch.c_str(), _vars);
00117 
00118     // accessing input branches
00119     Collection<TMBJet> jetcol(event.getCollection<TMBJet>(_jetBranch.c_str()));
00120 
00121     // keep cell based MET untouched
00122     _result->setMET(oldmet->getMET());
00123     _result->setMETnoeta(oldmet->getMETnoeta());
00124     _result->setMETC(oldmet->getMETC());
00125     _result->setMETD(oldmet->getMETD());
00126     _result->setMETA(oldmet->getMETA());
00127     _result->setMETB(oldmet->getMETB());
00128     _result->setVertex(oldmet->getZvertex());
00129 
00130     // recompute MET corrections
00131     const vector<BMetQualInfo>* voldmetqual = oldmet->getMetQualInfo(); 
00132     vector<BMetQualInfo>::const_iterator it;
00133     vector<BMetQualInfo> vnewmetqual;
00134 
00135     for (int ialgo=0;ialgo<4;ialgo++) {
00136 
00137       string requestedalgo = _valgo[ialgo];
00138 
00139       for (it=voldmetqual->begin();it!=voldmetqual->end();it++) {
00140         string thisalgo = it->getJetAlgo();
00141         if (strcmp(requestedalgo.c_str(),thisalgo.c_str()) != 0) continue;
00142 
00143         bool isMuCorrected = (requestedalgo.find("mu")<requestedalgo.size());
00144         bool isSmeared = (requestedalgo.find("smear")<requestedalgo.size());
00145 
00146         if (_debug) cout << "ReComputeMET, requestedalgo=" << requestedalgo << " isMuCorrected=" << isMuCorrected
00147                          << " isSmeared=" << isSmeared << endl;
00148 
00149         BMetStruct oldMETcorr = it->getMETcorr();       
00150         BMetStruct oldCHcorr  = it->getCHcorr();
00151         BMetStruct oldJEScorr = it->getJEScorr();
00152         BMetStruct oldEMcorr  = it->getEMcorr();
00153         BMetStruct oldMUcorr  = it->getMUcorr();
00154         BMetStruct oldMUCalcorr = it->getMUCalcorr(); 
00155         BMetStruct oldBJcorr  = it->getBJcorr();
00156         BMetStruct oldbasemet = oldMETcorr - oldCHcorr - oldJEScorr - oldEMcorr - oldMUcorr - oldMUCalcorr;
00157 
00158         BMetStruct newCHcorr;
00159         BMetStruct newJEScorr;
00160         BMetStruct newEMcorr;
00161         BMetStruct newMUcorr;
00162         BMetStruct newMUCalcorr;
00163         BMetStruct newBJcorr;
00164 
00165         //--------------------------------
00166         // Compute EM correction
00167         //--------------------------------
00168         if (_useDefaultElectron) {
00169           newEMcorr = oldEMcorr;
00170         } else {
00171           Collection<TMBEMCluster> emcol(event.getCollection<TMBEMCluster>(_electronBranch.c_str()));
00172           for (Collection<TMBEMCluster>::const_iterator it_em = emcol.begin(); it_em != emcol.end(); ++it_em) { 
00173              // not exactly what is in d0correct:
00174              float emuncor = it_em->uncorrE();
00175              float emx = it_em->Vect().Px() - emuncor*cos(it_em->Phi())*sin(it_em->Theta());
00176              float emy = it_em->Vect().Py() - emuncor*sin(it_em->Phi())*sin(it_em->Theta());
00177              float emz = it_em->Vect().Pz() - emuncor*cos(it_em->Theta());
00178              float ems = sqrt(emx*emx + emy*emy);
00179              if (_debug) cout << "ReComputeMET : EM : "
00180                               << setw(10) << emx << " , "
00181                               << setw(10) << emy << " , "
00182                               << setw(10) << emz << " , "
00183                               << setw(10) << ems << endl;
00184              newEMcorr.AddVis(emx,emy,emz,ems);
00185           } // for em
00186           newEMcorr.ComputeMet();
00187           if (_debug) {
00188             cout << " old EM correction: " << endl;
00189             oldEMcorr.Print(cout);
00190             cout << " new EM correction: " << endl;
00191             newEMcorr.Print(cout);
00192           }
00193         } // else _useDefaultElectron
00194 
00195         //--------------------------------
00196         // Compute Muon correction
00197         //--------------------------------
00198         // using all muons you put into the input branch
00199         if (_useDefaultMuon) {
00200           newMUcorr = oldMUcorr;
00201           newMUCalcorr = oldMUCalcorr;
00202         } else {
00203           Collection<TMBMuon> mucol(event.getCollection<TMBMuon>(_muonBranch.c_str()));
00204           int imu = 0;
00205           for(Collection<TMBMuon>::const_iterator it_mu = mucol.begin(); it_mu != mucol.end(); ++it_mu) {
00206 
00207             float mux = it_mu->Px();
00208             float muy = it_mu->Py();
00209             float muz = it_mu->Pz();
00210             float mus = it_mu->Pt(); 
00211 
00212             newMUcorr.AddVis(mux,muy,muz,mus);
00213             float mutheta = it_mu->Theta();
00214             float muphi   = it_mu->Phi();
00215             float eloss   = it_mu->eloss();
00216             mux = eloss * sin(mutheta) * cos(muphi);
00217             muy = eloss * sin(mutheta) * sin(muphi);
00218             muz = eloss * cos(mutheta);
00219             mus = -1. * sqrt(mux*mux + muy*muy);
00220 
00221             if (_debug) cout << "ReComputeMET : MUC " << imu << " : "
00222                              << setw(10) << mux << " , "
00223                              << setw(10) << muy << " , "
00224                              << setw(10) << muz << " , "
00225                              << setw(10) << mus << endl;
00226 
00227             newMUCalcorr.Add(mux,muy,muz,mus);
00228             ++imu;
00229           } // for muon
00230           newMUcorr.ComputeMet();
00231           newMUCalcorr.ComputeMet();
00232           if (_debug) {
00233             cout << " old MU correction: " << endl;
00234             oldMUcorr.Print(cout);
00235             cout << " new MU correction: " << endl;
00236             newMUcorr.Print(cout);
00237           }
00238         } // else _useDefaultMuon
00239 
00240         //--------------------------------------------------------------------
00241         // Compute good/bad jet corrections : CH fraction and JES corrections
00242         //--------------------------------------------------------------------
00243         // using only good jets you put into the input branch
00244         int ijet = 0;
00245         for(Collection<TMBJet>::const_iterator it_jet = jetcol.begin(); it_jet != jetcol.end(); ++it_jet) {
00246 
00247           TMBJet jet = *it_jet;
00248           if (isMuCorrected) jet.ActAsJESMUCorrected(); else jet.ActAsJESCorrected();
00249           if (isSmeared) {
00250             if (isMuCorrected) jet.ActAsSmearedMU(); else jet.ActAsSmeared();
00251           } // isSmeared
00252 
00253           if (jet.isGood() && !jet.isEM()) {    
00254 
00255             if (!_useDefaultJet) {
00256             // not exactly what is in d0correct:
00257             float chf = jet.chETfraction();
00258             float chx = chf*cos(jet.Phi());
00259             float chy = chf*sin(jet.Phi());
00260             float chz = chf*cos(jet.Theta());
00261             float chs = fabs(jet.sET()) * jet.chETfraction();
00262             if (_debug) cout << "ReComputeMET : CH  " << ijet << " : "
00263               << setw(10) << chx << " , "
00264               << setw(10) << chy << " , "
00265               << setw(10) << chz << " , "
00266               << setw(10) << chs << endl;
00267             newCHcorr.AddVis(chx,chy,chz,chs);
00268             }
00269 
00270             // JES correction
00271             // Warning : the z component cannot be obtained from jetcorr function "forMET"
00272             // Take the correction from jet instead of MET
00273             float jesx, jesy, jesz, jess, metjes, jetjes, smearfactor;
00274             if (isMuCorrected) {
00275               metjes = jet.JESMU_metC();
00276               if(_jes == ShiftedPos) 
00277                 metjes +=  sqrt(pow(jet.JESMU_metdC_stat(),2)+pow(jet.JESMU_metdC_sys(),2)) ;
00278               else if(_jes == ShiftedNeg) 
00279                 metjes -=  sqrt(pow(jet.JESMU_metdC_stat(),2)+pow(jet.JESMU_metdC_sys(),2)) ;
00280 
00281               jetjes = jet.JESMU_C();
00282               if(_jes == ShiftedPos) 
00283                 metjes +=  sqrt(pow(jet.JESMU_dC_stat_up(),2)+pow(jet.JESMU_dC_sys_up(),2)) ;
00284               else if(_jes == ShiftedNeg) 
00285                 metjes -=  sqrt(pow(jet.JESMU_dC_stat_down(),2)+pow(jet.JESMU_dC_sys_down(),2)) ;
00286 
00287               smearfactor = jet.SmearingFactorMU();
00288             } else {
00289               metjes = jet.JES_metC();
00290               if(_jes == ShiftedPos) 
00291                 metjes +=  sqrt(pow(jet.JES_metdC_stat(),2)+pow(jet.JES_metdC_sys(),2)) ;
00292               else if(_jes == ShiftedNeg) 
00293                 metjes -=  sqrt(pow(jet.JES_metdC_stat(),2)+pow(jet.JES_metdC_sys(),2)) ;
00294 
00295               jetjes = jet.JES_C();
00296               if(_jes == ShiftedPos) 
00297                 metjes +=  sqrt(pow(jet.JES_dC_stat_up(),2)+pow(jet.JES_dC_sys_up(),2)) ;
00298               else if(_jes == ShiftedNeg) 
00299                 metjes -=  sqrt(pow(jet.JES_dC_stat_down(),2)+pow(jet.JES_dC_sys_down(),2)) ;
00300 
00301               smearfactor = jet.SmearingFactor();
00302             }
00303             jesx = metjes * cos(jet.Phi());
00304             jesy = metjes * sin(jet.Phi());
00305             jesz = jetjes != 0 ? jet.Pz() * (1./jetjes - 1.) : 0;
00306             jess = jet.sET() * (jetjes - 1.);
00307             if (isSmeared) {
00308               jesx *= smearfactor;
00309               jesy *= smearfactor;
00310               jesz  = jetjes != 0 && smearfactor != 0 ? jet.Pz()  * (1./jetjes/smearfactor - 1.) : 0;
00311               jess  = jet.sET() * (jetjes*smearfactor - 1.);
00312             } // if isSmeared
00313 
00314             if (_debug) cout << "ReComputeMET : JES " << ijet << " : "
00315                              << setw(10) << jesx << " , "
00316                              << setw(10) << jesy << " , "
00317                              << setw(10) << jesz << " , "
00318                              << setw(10) << jess << endl;
00319             newJEScorr.Add(jesx,jesy,jesz,jess);
00320 
00321           } // isGood
00322 
00323           // @@ pxCH, pyCH are not stored in CAF for the moment !!
00324           /*
00325             if (jet.isBad()) {
00326             float bjx = jet.pxCH();
00327             float bjy = jet.pyCH();
00328             float bjz = jet.pzCH();
00329             float bjs = fabs(jet.sET()) * jet.chETfraction();
00330             if (_debug) cout << "ReComputeMET : BJ  " << ijet << " : "
00331             << setw(10) << bjx << " , "
00332             << setw(10) << bjy << " , "
00333             << setw(10) << bjz << " , "
00334             << setw(10) << bjs << endl;
00335             newBJcorr.Add(bjx,bjy,bjz,bjs);
00336 
00337             } // isBad
00338           */
00339 
00340           ++ijet;
00341         } // for jets
00342         if (_useDefaultJet) { newCHcorr = oldCHcorr; newBJcorr = oldBJcorr; }
00343         if (_debug) {
00344            cout << " old CH correction: " << endl;
00345            oldCHcorr.Print(cout);
00346            cout << " new CH correction: " << endl;
00347            newCHcorr.Print(cout);
00348         }
00349 
00350         newCHcorr.ComputeMet();
00351         newJEScorr.ComputeMet();
00352         newBJcorr.ComputeMet();
00353 
00354         //--------------------------------
00355         // Compute final MissingET
00356         //--------------------------------
00357         BMetStruct newMETcorr = oldbasemet + newCHcorr + newJEScorr + newEMcorr + newMUcorr + newMUCalcorr;
00358 
00359         BMetQualInfo result;
00360         result.setJetAlgo(thisalgo);
00361 
00362         result.setMETcorr(newMETcorr);
00363         result.setCHcorr(newCHcorr);
00364         result.setJEScorr(newJEScorr);
00365         result.setEMcorr(newEMcorr);
00366         result.setMUcorr(newMUcorr);
00367         result.setMUCalcorr(newMUCalcorr);
00368         result.setBJcorr(newBJcorr);
00369 
00370         // no recomputing of isolation variables for the moment
00371         result.setIsoPhiGoodJet(it->getIsoPhiGoodJet());
00372         result.setIsoPhiMu(it->getIsoPhiMu());
00373         result.setIsoPhiEM(it->getIsoPhiEM());
00374         result.setIsoPhiBadJet(it->getIsoPhiBadJet());
00375         result.setIsoPhiUE(it->getIsoPhiUE());
00376         result.setIsoPhiBadTower(it->getIsoPhiBadTower());
00377 
00378         vnewmetqual.push_back(result);
00379         break;
00380       } // for voldmetqual
00381 
00382     } // ialgo
00383 
00384     _result->setMetQualInfo(vnewmetqual);
00385 
00386     return true  ;
00387   }
00388 
00389 } // namespace
00390 
00391 ClassImp(caf_util::ReComputeMET);

Generated on Tue Mar 28 10:13:04 2006 for CAF by doxygen 1.3.4