Report problems to ATLAS LXR Team (with time and IP address indicated)

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: linux ]
Version: head ] [ nightly ] [ GaudiDev ]
  Links to LXR source navigation pages for stable releases [ 12.*.* ]   [ 13.*.* ]   [ 14.*.* ]   [ 15.*.* ] 

001 //////////////////////////////////////////////////////////////////////////////////////////////
002 ///    Author: A. Wildauer
003 ///    CERN, January 19, 2005
004 ///
005 /////////////////////////////////////////////////////////////////////////////////////////////
006 
007 #include "JetTagTools/LikelihoodTool.h"
008 
009 #include "GaudiKernel/DataObject.h"
010 #include "GaudiKernel/MsgStream.h"
011 
012 #include "GaudiKernel/ITHistSvc.h"
013 #include "TH1.h"
014 #include <cmath>
015 
016 namespace Analysis
017 {
018 
019 LikelihoodTool::LikelihoodTool(const std::string& t, const std::string& n, const IInterface* p) :
020         AlgTool(t,n,p),
021         m_histoSvc(0),
022         m_allLhVariables(std::vector<std::string>()),
023         m_useTheseLhVariables(std::vector<std::string>()),
024         m_mapOfAllHistos(std::vector<std::map<std::string, TH1*>* >()),
025         m_histoLimitsMap(std::map< std::string, HistoLimits>() ),
026         m_lhVariableValues(std::map<std::string, std::vector<double> >()),
027         m_likelihoodVector(std::vector<double>())
028 {
029     declareInterface<LikelihoodTool>(this);
030     declareProperty("allLhVariables", m_allLhVariables);
031     declareProperty("useTheseLhVariables", m_useTheseLhVariables);
032 }
033 
034 LikelihoodTool::~LikelihoodTool()
035 {
036     for (std::vector< std::map<std::string, TH1*>* >::iterator itr = m_mapOfAllHistos.begin();
037             itr != m_mapOfAllHistos.end(); ++itr)
038     {
039         delete (*itr);
040     }
041 }
042 
043 StatusCode LikelihoodTool::initialize()
044 {
045    MsgStream mlog(msgSvc(),name());
046 
047    std::string histoservicename = "THistSvc";
048    StatusCode sc = service(histoservicename, m_histoSvc);
049    if (sc.isFailure())
050    {
051      mlog << MSG::FATAL << histoservicename << " not found!" << endreq;
052    }
053    return sc;
054 }
055 
056 StatusCode LikelihoodTool::finalize()
057 {
058   return StatusCode::SUCCESS;
059 }
060 
061 void LikelihoodTool::readInHistosFromFile(const std::string& refFileName)
062 {
063     MsgStream mlog(msgSvc(),name());
064     // the tmpHistoMap belongs to m_mapOfAllHistos and is deleted at the end in the destructor of LikelihoodTool()
065     std::map<std::string, TH1*>* tmpHistoMap  = new std::map<std::string, TH1*>();
066     for (std::vector<std::string>::iterator itr = m_useTheseLhVariables.begin() ;
067             itr != m_useTheseLhVariables.end()  ; ++itr )
068     {
069         const std::string& histoName = *itr;
070 
071         std::string fullPath(refFileName+histoName);
072 
073         mlog << MSG::VERBOSE <<  "Retrieving histo: " << "/" << fullPath << endreq;
074 
075         StatusCode sc = m_histoSvc->regHist("/"+fullPath);
076         if(sc.isFailure())
077         {
078           mlog << MSG::DEBUG <<  "Could not register histo /" << fullPath << endreq;
079         }
080 
081         TH1* tmphisto(0);
082         sc = m_histoSvc->getHist("/"+fullPath,tmphisto);
083         if(sc.isFailure())
084         {
085           mlog << MSG::DEBUG <<  "Could not get histo /" << fullPath << endreq;
086         }
087         if (tmphisto == 0) mlog << MSG::DEBUG <<  "I COULD NOT RETRIEVE HISTO: " << "/" << fullPath << endreq;
088 
089         /** Normalize if necessary */
090         double norm(tmphisto->Integral());
091         if (norm!=1.)
092         {
093           mlog << MSG::INFO <<  "Normalizing histo: " << fullPath << endreq;
094           tmphisto->Scale(1./norm);
095         }
096 
097         tmpHistoMap->insert(
098           std::pair<std::string, TH1*>(
099             histoName, tmphisto
100             )
101         );
102 
103         /** Retrieve and set histo limits for this histogram */
104         unsigned int bins(tmphisto->GetNbinsX());
105         const TAxis* xAxis = tmphisto->GetXaxis();
106         double minx(xAxis->GetXmin());
107         double maxx(xAxis->GetXmax());
108 
109         m_histoLimitsMap[histoName] = HistoLimits(bins, minx, maxx);
110     }
111     m_mapOfAllHistos.push_back(tmpHistoMap);
112 }
113 
114 void LikelihoodTool::setLhVariableValue(const std::string& lhVariable, std::vector<double>& value)
115 {
116     MsgStream mlog(msgSvc(),name());
117     mlog << MSG::DEBUG << "Trying to set vector LH variable: " << lhVariable << endreq;
118     m_lhVariableValues[lhVariable]=value;
119 }
120 
121 void LikelihoodTool::setLhVariableValue(const std::string& lhVariable, double value)
122 {
123     std::vector<double> tmpVector;
124     tmpVector.push_back(value);
125     setLhVariableValue(lhVariable, tmpVector);
126 }
127 
128 std::vector<double> LikelihoodTool::calculateLikelihood()
129 {
130 /**
131 Running on all Hbb, Huu, tth, ttbar events we have:
132 
133 There are 1537821 jets.
134 There are 680153 b jets. 447493 have a sec vtx (65.793%)
135 There are 607057 light jets. 17715 have a sec vtx (2.91818%)
136 */
137     double eb(0.65793);
138     double eu(0.0291818);
139     bool calcLHForSecVtx(false);
140     m_likelihoodVector.clear();
141     MsgStream mlog(msgSvc(),name());
142     std::vector<double> probDensityPerEventClassAllVariables;
143     probDensityPerEventClassAllVariables.resize(m_mapOfAllHistos.size());
144     for (unsigned int i = 0 ; i < m_mapOfAllHistos.size(); ++i)
145     {
146       probDensityPerEventClassAllVariables[i]=1.;
147     }
148     /// loop over the different variables
149     for (std::vector<std::string>::iterator itr1 = m_useTheseLhVariables.begin() ;
150             itr1 != m_useTheseLhVariables.end()  ; ++itr1 )
151     {
152         std::vector<double>& thisVariable = (*(m_lhVariableValues.find(*itr1))).second;
153 
154         // if weight for sec vtx has to be calculated behave differently
155         if ((*itr1)=="vtxmassBU")
156         {
157           calcLHForSecVtx=true;
158           if (thisVariable[0] == -1.) // no vertex
159           {
160             mlog << MSG::VERBOSE <<  "SecVtxTag had no SecVtx - returning " << (1.-eb)/(2.-eb-eu) << endreq;
161             probDensityPerEventClassAllVariables[0] = (1.-eb)/(2.-eb-eu);
162             probDensityPerEventClassAllVariables[1] = (1.-eu)/(2.-eb-eu);
163 //             mlog << MSG::VERBOSE <<  "Signal LH is: "
164 //                                  << probDensityPerEventClassAllVariables[0]/(probDensityPerEventClassAllVariables[0]+probDensityPerEventClassAllVariables[1]) 
165 //                                  << "\tBackground LH is: "
166 //                                  << probDensityPerEventClassAllVariables[1]/(probDensityPerEventClassAllVariables[0]+probDensityPerEventClassAllVariables[1]) << endreq;
167             m_likelihoodVector=probDensityPerEventClassAllVariables;
168             return m_likelihoodVector;
169           }
170         }
171         // get HistoLimits for this variable
172         const HistoLimits& histoLimits = (*(m_histoLimitsMap.find(*itr1))).second;
173         mlog << MSG::VERBOSE << "Variable: " << (*itr1)
174              << " appears " << thisVariable.size() << " times. Histolimits: ("
175              << histoLimits.xmin << ", " << histoLimits.xmax << ")."  << endreq;
176         // variable might have more than one entry per jet/event
177         for (std::vector<double>::iterator varItr  = thisVariable.begin();
178                                            varItr != thisVariable.end(); ++varItr )
179         {
180             double sum(0.);
181             double value(*varItr);
182             mlog << MSG::VERBOSE <<  "Variable value is: " << value << endreq;
183             if (value >= histoLimits.xmax)
184             {
185               mlog << MSG::VERBOSE <<  "Variable was too high("<<value<<"), set it to: " << histoLimits.xmax - 0.0001 << endreq;
186               value = histoLimits.xmax - 0.0001;
187             } else if (value <= histoLimits.xmin) {
188               mlog << MSG::VERBOSE <<  "Variable was too low("<<value<<"), set it to: " << histoLimits.xmax + 0.0001 << endreq;
189               value = histoLimits.xmin + 0.0001;
190             }
191 
192             std::vector<double> tmpVector;
193             /// loop over the different input histogram sets
194             unsigned int inputFilesCounter(1);
195             for (std::vector< std::map<std::string, TH1*>* >::iterator
196                     itr2 = m_mapOfAllHistos.begin() ;
197                     itr2 != m_mapOfAllHistos.end() ;
198                     ++itr2 )
199             {
200                 mlog << MSG::VERBOSE <<  "Inputfile " << inputFilesCounter << ": I try to find histo: " << *itr1 << endreq;
201                 TH1* tmpHisto = (*((*itr2)->find(*itr1))).second;
202 
203                 if (tmpHisto == 0) mlog << MSG::VERBOSE <<  "tmpHisto is ZERO!!" << endreq;
204                 // get the bin only once
205                 // it will be the same for all input files in m_mapOfAllHistos
206                 int bin((tmpHisto->GetXaxis())->FindBin(value));
207                 double tmp(tmpHisto->GetBinContent(bin));
208                 mlog << MSG::VERBOSE <<  "Value " << value << " is in bin " << bin << endreq;
209                 mlog << MSG::VERBOSE <<  "The histo height of this bin is: " << tmp << endreq;
210                 tmpVector.push_back(tmp);
211                 sum += tmp;
212                 inputFilesCounter++;
213             }
214 
215             mlog << MSG::VERBOSE <<  "The sum of the histo heights : " << sum << endreq;
216 
217             unsigned int classCount(0);
218             for (std::vector<double>::iterator itr3 = tmpVector.begin();  itr3 != tmpVector.end(); ++itr3)
219             {
220                 probDensityPerEventClassAllVariables[classCount] *= (*itr3) /sum;
221                 classCount++;
222             }
223 ////// debug output
224         }
225     }
226 
227     // if likelihood for sec vtx has to be calculated behave differently
228     if (calcLHForSecVtx)
229     {
230 //         mlog << MSG::VERBOSE <<  "SecVtxTag had SecVtx - returning (without sec vtx taken into account): "
231 //                              << probDensityPerEventClassAllVariables[0] << ", "
232 //                              << probDensityPerEventClassAllVariables[1] << endreq;
233 //         mlog << MSG::VERBOSE <<  "Signal LH would be: "
234 //                              << probDensityPerEventClassAllVariables[0]/(probDensityPerEventClassAllVariables[0]+probDensityPerEventClassAllVariables[1]) 
235 //                              << "\tBackground LH would be: "
236 //                              << probDensityPerEventClassAllVariables[1]/(probDensityPerEventClassAllVariables[0]+probDensityPerEventClassAllVariables[1]) << endreq;
237         probDensityPerEventClassAllVariables[0] *= eb;
238         probDensityPerEventClassAllVariables[1] *= eu;
239         mlog << MSG::VERBOSE <<  "SecVtxTag had SecVtx - returning: "
240                              << probDensityPerEventClassAllVariables[0] << ", "
241                              << probDensityPerEventClassAllVariables[1] << endreq;
242         mlog << MSG::VERBOSE <<  "Signal LH would be: "
243                              << probDensityPerEventClassAllVariables[0]/(probDensityPerEventClassAllVariables[0]+probDensityPerEventClassAllVariables[1]) 
244                              << "\tBackground LH would be: "
245                              << probDensityPerEventClassAllVariables[1]/(probDensityPerEventClassAllVariables[0]+probDensityPerEventClassAllVariables[1]) << endreq;
246     }
247 //     for (std::vector<double>::iterator itr4 = probDensityPerEventClassAllVariables.begin();  itr4 != probDensityPerEventClassAllVariables.end(); ++itr4)
248 //     {
249 //       mlog << MSG::VERBOSE <<  "Pj(x1,...,xn) = " << *itr4 << endreq;
250 //     }
251     m_likelihoodVector=probDensityPerEventClassAllVariables;
252     return m_likelihoodVector;
253 }
254 
255 std::vector<double> LikelihoodTool::tagLikelihood()
256 {
257   return m_likelihoodVector;
258 }
259 
260 double LikelihoodTool::calculateWeight()
261 {
262     double weight(0.);
263 /**
264 Running on all Hbb, Huu, tth, ttbar events we have:
265 
266 There are 1537821 jets.
267 There are 680153 b jets. 447493 have a sec vtx (65.793%)
268 There are 607057 light jets. 17715 have a sec vtx (2.91818%)
269 */
270     double eb(0.65793);
271     double eu(0.0291818);
272     double addToWeight(0.); // this is added to the weight in case of secvtx finder
273     MsgStream mlog(msgSvc(),name());
274     /// loop over the different variables
275     for (std::vector<std::string>::iterator itr1 = m_useTheseLhVariables.begin() ;
276             itr1 != m_useTheseLhVariables.end()  ; ++itr1 )
277     {
278         std::vector<double>& thisVariable = (*(m_lhVariableValues.find(*itr1))).second;
279 
280         // if weight for sec vtx has to be calculated behave differently
281         if ((*itr1)=="vtxmassBU")
282         {
283           if (thisVariable[0] == -1.) // no vertex
284           {
285             mlog << MSG::VERBOSE <<  "SecVtxTag had no SecVtx - returning " << log((1-eb)/(1-eu)) << endreq;
286             return log((1-eb)/(1-eu));
287           } else
288           {
289             addToWeight = log(eb/eu);
290             mlog << MSG::VERBOSE <<  "SecVtxTag had SecVtx - adding " << addToWeight << " to final weight" << endreq;
291           }
292         }
293 
294         // get HistoLimits for this variable
295         const HistoLimits& histoLimits = (*(m_histoLimitsMap.find(*itr1))).second;
296         mlog << MSG::VERBOSE << "Variable: " << (*itr1)
297              << " appears " << thisVariable.size() << " times. Histolimits: ("
298              << histoLimits.xmin << ", " << histoLimits.xmax << ")."  << endreq;
299         // variable might have more than one entry per jet/event
300         for (std::vector<double>::iterator varItr  = thisVariable.begin();
301                                            varItr != thisVariable.end(); ++varItr )
302         {
303             double value(*varItr);
304 //             mlog << MSG::VERBOSE <<  "Variable value is: " << value << endreq;
305             if (value >= histoLimits.xmax)
306             {
307               mlog << MSG::VERBOSE <<  "Variable was too high("<<value<<"), set it to: " << histoLimits.xmax - 0.0001 << endreq;
308               value = histoLimits.xmax - 0.0001;
309             } else if (value <= histoLimits.xmin) {
310               mlog << MSG::VERBOSE <<  "Variable was too low("<<value<<"), set it to: " << histoLimits.xmax + 0.0001 << endreq;
311               value = histoLimits.xmin + 0.0001;
312             }
313 
314             std::vector<double> tmpVector;
315             /// loop over the different input histogram sets
316             unsigned int inputFilesCounter(1);
317             for (std::vector< std::map<std::string, TH1*>* >::iterator
318                     itr2 = m_mapOfAllHistos.begin() ;itr2 != m_mapOfAllHistos.end() ;++itr2 )
319             {
320 //                 mlog << MSG::VERBOSE <<  "Inputfile " << inputFilesCounter << ": I try to find histo: " << *itr1 << endreq;
321                 TH1* tmpHisto = (*((*itr2)->find(*itr1))).second;
322 
323                 if (tmpHisto == 0) mlog << MSG::VERBOSE <<  "tmpHisto is ZERO!!" << endreq;
324                 // get the bin only once
325                 // it will be the same for all input files in m_mapOfAllHistos
326                 int bin((tmpHisto->GetXaxis())->FindBin(value));
327                 double tmp(tmpHisto->GetBinContent(bin));
328 //                 mlog << MSG::VERBOSE <<  "Value " << value << " is in bin " << bin << endreq;
329 //                 mlog << MSG::VERBOSE <<  "The histo height of this bin is: " << tmp << endreq;
330                 tmpVector.push_back(tmp);
331                 inputFilesCounter++;
332             }
333             weight += log(tmpVector[0]/tmpVector[1]);
334 //             mlog << MSG::VERBOSE <<  "The sum of the histo heights : " << sum << endreq;
335         }
336     }
337     mlog << MSG::VERBOSE <<  "Returning : " << weight << " + " << addToWeight << endreq;
338     return (weight + addToWeight);
339 }
340 
341 void LikelihoodTool::printStatus()
342 {
343   MsgStream mlog(msgSvc(),name());
344 //   mlog << MSG::VERBOSE << "I have a pointer to the histoSvc? : " << m_histoSvc << endreq;
345   mlog << MSG::INFO << "Currently I hold histos of " <<m_mapOfAllHistos.size() << " input files" << endreq;
346 
347 }
348 
349 }

source navigation ] diff markup ] identifier search ] general search ]

Due to the LXR bug, the updates fail sometimes to remove references to deleted files. The Saturday's full rebuilds fix these problems
This page was automatically generated by the LXR engine. Valid HTML 4.01!