Skip to content

Commit

Permalink
Merge pull request #8304 from Dr15Jones/makeMuonTaggerThreadSafe
Browse files Browse the repository at this point in the history
Make MuonTagger thread safe
  • Loading branch information
cmsbuild committed Mar 17, 2015
2 parents b010347 + 6346525 commit f171bb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
11 changes: 5 additions & 6 deletions RecoBTag/SoftLepton/interface/MuonTagger.h
Expand Up @@ -9,24 +9,23 @@
#include "RecoBTau/JetTagComputer/interface/JetTagComputer.h"
#include "RecoBTag/SoftLepton/interface/LeptonSelector.h"
#include "RecoBTag/SoftLepton/interface/MvaSoftMuonEstimator.h"

#include "TRandom3.h"
#include <mutex>
#include <memory>

class MuonTagger : public JetTagComputer {

public:

MuonTagger(const edm::ParameterSet&);
~MuonTagger();

virtual float discriminator(const TagInfoHelper& tagInfo) const;
virtual float discriminator(const TagInfoHelper& tagInfo) const override;

private:

btag::LeptonSelector m_selector;
TRandom3* random;
edm::FileInPath WeightFile;
MvaSoftMuonEstimator* mvaID;
mutable std::mutex m_mutex;
[[cms::thread_guard("m_mutex")]] std::unique_ptr<MvaSoftMuonEstimator> mvaID;
};

#endif
Expand Down
19 changes: 10 additions & 9 deletions RecoBTag/SoftLepton/src/MuonTagger.cc
Expand Up @@ -3,6 +3,7 @@
// * January 16, 2015

#include <limits>
#include <random>

#include "DataFormats/BTauReco/interface/SoftLeptonTagInfo.h"
#include "DataFormats/BTauReco/interface/CandSoftLeptonTagInfo.h"
Expand All @@ -12,14 +13,8 @@

MuonTagger::MuonTagger(const edm::ParameterSet& conf): m_selector(conf) {
uses("smTagInfos");
random=new TRandom3();
WeightFile=conf.getParameter<edm::FileInPath>("weightFile");
mvaID=new MvaSoftMuonEstimator(WeightFile.fullPath());
}

MuonTagger::~MuonTagger() {
delete mvaID;
delete random;
mvaID.reset(new MvaSoftMuonEstimator(WeightFile.fullPath()));
}


Expand All @@ -28,15 +23,21 @@ float MuonTagger::discriminator(const TagInfoHelper& tagInfo) const {

float bestTag = - std::numeric_limits<float>::infinity(); // default value, used if there are no leptons associated to this jet
const reco::CandSoftLeptonTagInfo& info = tagInfo.get<reco::CandSoftLeptonTagInfo>();

std::mt19937_64 random;
std::uniform_real_distribution<float> dist(0.f,1.f);

//MvaSoftMuonEstimator is not thread safe
std::lock_guard<std::mutex> lock(m_mutex);

// If there are multiple leptons, look for the highest tag result
for (unsigned int i=0; i<info.leptons(); i++) {
const reco::SoftLeptonProperties& properties = info.properties(i);
bool flip(false);
if(m_selector.isNegative()) {
int seed=1+round(10000.*properties.deltaR);
random->SetSeed(seed);
float rndm = random->Uniform(0,1);
random.seed(seed);
float rndm = dist(random);
if(rndm<0.5) flip=true;
}
float sip3d = flip ? -properties.sip3d : properties.sip3d;
Expand Down

0 comments on commit f171bb2

Please sign in to comment.