Skip to content

Commit

Permalink
Merge pull request #4327 from ktf/backport-tls-fixes
Browse files Browse the repository at this point in the history
Backport TLS fixes from #4293 to CMSSW_7_1_X
  • Loading branch information
ktf committed Jun 20, 2014
2 parents c961817 + 20a4305 commit 36d64ae
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 113 deletions.
24 changes: 3 additions & 21 deletions CondFormats/EcalObjects/interface/EcalCondObjectContainer.h
Expand Up @@ -109,30 +109,12 @@ class EcalCondObjectContainer {

inline
Item & operator[]( uint32_t rawId )
#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
{
DetId id(rawId);
switch (id.subdetId()) {
case EcalBarrel :
{
return eb_[rawId];
}
break;
case EcalEndcap :
{
return ee_[rawId];
}
break;
default:
// FIXME (add throw)
thread_local static Item dummy;
return dummy;
}
return (id.subdetId()==EcalBarrel) ? eb_[rawId] : ee_[rawId];

}
#else
{ }
#endif


inline
Item operator[]( uint32_t rawId ) const {
DetId id(rawId);
Expand Down
18 changes: 4 additions & 14 deletions CondFormats/EcalObjects/interface/EcalCondTowerObjectContainer.h
Expand Up @@ -100,23 +100,13 @@ class EcalCondTowerObjectContainer {

inline
Item & operator[]( uint32_t rawId )
#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
{
DetId id(rawId);

if( id.subdetId() == EcalBarrel || id.subdetId() == EcalTriggerTower ) {
return eb_[rawId];
} else if( id.subdetId() == EcalEndcap ) {
return ee_[rawId];
} else {
thread_local static Item dummy;
return dummy;
}
return ( (id.subdetId() == EcalBarrel) | (id.subdetId() == EcalTriggerTower) ) ?
eb_[rawId] :
ee_[rawId];
}
#else
;
#endif


inline
Item operator[]( uint32_t rawId ) const {
DetId id(rawId);
Expand Down
@@ -1,16 +1,15 @@
#include "TrackingTools/TrajectoryCleaning/interface/TrajectoryCleanerBySharedHits.h"
#include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
#include "TrackingTools/TransientTrackingRecHit/interface/RecHitComparatorByPosition.h"
#include <map>
#include <vector>
#include <boost/unordered_map.hpp>

#include "TrackingTools/TrajectoryCleaning/src/OtherHashMaps.h"


//#define DEBUG_PRINT(X) X
#define DEBUG_PRINT(X)

namespace {

// Define when two rechits are equals
struct EqualsBySharesInput {
bool operator()(const TransientTrackingRecHit *h1, const TransientTrackingRecHit *h2) const {
Expand All @@ -25,19 +24,26 @@ struct HashByDetId : std::unary_function<const TransientTrackingRecHit *, std::s
}
};

using RecHitMap = cmsutil::SimpleAllocHashMultiMap<const TransientTrackingRecHit*, Trajectory *, HashByDetId, EqualsBySharesInput>;
using TrajMap = cmsutil::UnsortedDumbVectorMap<Trajectory*, int>;

struct Maps {
Maps() : theRecHitMap(128,256,1024){} // allocate 128 buckets, one row for 256 keys and one row for 512 values
RecHitMap theRecHitMap;
TrajMap theTrajMap;
};

thread_local Maps theMaps;
}

using namespace std;

void TrajectoryCleanerBySharedHits::clean( TrajectoryPointerContainer & tc) const
{
if (tc.size() <= 1) return; // nothing to clean

//typedef boost::unordered_map<const TransientTrackingRecHit*, TIs, HashByDetId, EqualsBySharesInput> RecHitMap;
typedef cmsutil::SimpleAllocHashMultiMap<const TransientTrackingRecHit*, Trajectory *, HashByDetId, EqualsBySharesInput> RecHitMap;

//typedef boost::unordered_map<Trajectory*, int> TrajMap; // for each Trajectory it stores the number of shared hits
typedef cmsutil::UnsortedDumbVectorMap<Trajectory*, int> TrajMap;
auto & theRecHitMap = theMaps.theRecHitMap;

thread_local static RecHitMap theRecHitMap(128,256,1024);// allocate 128 buckets, one row for 256 keys and one row for 512 values
theRecHitMap.clear(10*tc.size()); // set 10*tc.size() active buckets
// numbers are not optimized

Expand All @@ -59,7 +65,7 @@ void TrajectoryCleanerBySharedHits::clean( TrajectoryPointerContainer & tc) cons

DEBUG_PRINT(std::cout << "Using RecHit map" << std::endl);
// for each trajectory fill theTrajMap
thread_local static TrajMap theTrajMap;
auto & theTrajMap = theMaps.theTrajMap;
for (TrajectoryCleaner::TrajectoryPointerIterator
itt = tc.begin(); itt != tc.end(); ++itt) {
if((*itt)->isValid()){
Expand Down
Expand Up @@ -39,44 +39,7 @@ class MinPtTrajectoryFilter final : public TrajectoryFilter {

protected:

bool test( const TrajectoryMeasurement & tm, int foundHits) const
{
//first check min number of hits
if (foundHits < theMinHits ){ return true;}

// check for momentum below limit
// const FreeTrajectoryState& fts = *tm.updatedState().freeTrajectoryState();

auto const & tsos = tm.updatedState();
GlobalVector gtp = tsos.globalMomentum();

//avoid doing twice the check in TBC and QF
static thread_local bool answerMemory=false;
static thread_local GlobalVector ftsMemory;


if ( gtp == ftsMemory) { return answerMemory;}
ftsMemory= gtp;

auto pT2 = gtp.perp2();

//if p_T is way too small: stop
if (pT2<0.0010f) {answerMemory=false; return false;}

// if large enouth go
if (pT2> thePtMin2) { answerMemory=true; return true;}

//if error is way too big: stop
float invError = TrajectoryStateAccessor(*tsos.freeTrajectoryState()).inversePtError();
if (invError > 1.e10f) {answerMemory=false;return false;}

//calculate the actual pT cut:
if ((1.f/std::sqrt(pT2) - theNSigma*invError) > theInvPtMin ) {answerMemory=false; return false;}
// first term if the max value of pT (pT+N*sigma(pT))
// second tern is the cut

answerMemory=true; return true;
}
bool test( const TrajectoryMeasurement & tm, int foundHits) const;

float thePtMin2;
float theInvPtMin;
Expand Down
Expand Up @@ -36,36 +36,7 @@ class ThresholdPtTrajectoryFilter : public TrajectoryFilter {

protected:

bool test( const TrajectoryMeasurement & tm, int foundHits) const
{
//first check min number of hits
if (foundHits < theMinHits ){ return true;}

// check for momentum below limit
const FreeTrajectoryState& fts = *tm.updatedState().freeTrajectoryState();

//avoid doing twice the check in TBC and QF
// We make it thread local so that we avoid race conditions between
// threads, and we make sure there is no cache contention between them.
static thread_local bool answerMemory=false;
static thread_local FreeTrajectoryState ftsMemory;
if (ftsMemory.parameters().vector() == fts.parameters().vector()) { return answerMemory;}
ftsMemory=fts;

//if p_T is way too small: stop
double pT = fts.momentum().perp();
if (pT<0.010) {answerMemory=false; return false;}
//if error is way too big: stop
double invError = TrajectoryStateAccessor(fts).inversePtError();
if (invError > 1.e10) {answerMemory=false;return false;}

//calculate the actual pT cut:
if ((1/pT + theNSigma*invError ) < 1/thePtThreshold ) {answerMemory=false; return false;}
// first term is the minimal value of pT (pT-N*sigma(pT))
// secon term is the cut

answerMemory=true; return true;
}
bool test( const TrajectoryMeasurement & tm, int foundHits) const;

double thePtThreshold;
double theNSigma;
Expand Down
49 changes: 49 additions & 0 deletions TrackingTools/TrajectoryFiltering/src/MinPtTrajectoryFilter.cc
@@ -1 +1,50 @@
#include "TrackingTools/TrajectoryFiltering/interface/MinPtTrajectoryFilter.h"

namespace {
struct TLS {
bool answerMemory=false;
GlobalVector ftsMemory;

};

thread_local TLS tls;
}


bool MinPtTrajectoryFilter::test( const TrajectoryMeasurement & tm, int foundHits) const
{
//first check min number of hits
if (foundHits < theMinHits ){ return true;}

// check for momentum below limit
// const FreeTrajectoryState& fts = *tm.updatedState().freeTrajectoryState();

auto const & tsos = tm.updatedState();
GlobalVector gtp = tsos.globalMomentum();

//avoid doing twice the check in TBC and QF


if ( gtp == tls.ftsMemory) { return tls.answerMemory;}
tls.ftsMemory= gtp;

auto pT2 = gtp.perp2();

//if p_T is way too small: stop
if (pT2<0.0010f) {tls.answerMemory=false; return false;}

// if large enouth go
if (pT2> thePtMin2) { tls.answerMemory=true; return true;}

//if error is way too big: stop
float invError = TrajectoryStateAccessor(*tsos.freeTrajectoryState()).inversePtError();
if (invError > 1.e10f) {tls.answerMemory=false;return false;}

//calculate the actual pT cut:
if ((1.f/std::sqrt(pT2) - theNSigma*invError) > theInvPtMin ) {tls.answerMemory=false; return false;}
// first term if the max value of pT (pT+N*sigma(pT))
// second tern is the cut

tls.answerMemory=true; return true;
}

@@ -1 +1,41 @@
#include "TrackingTools/TrajectoryFiltering/interface/ThresholdPtTrajectoryFilter.h"

namespace {
struct TLS {
bool answerMemory=false;
FreeTrajectoryState ftsMemory;

};

thread_local TLS tls;
}

bool ThresholdPtTrajectoryFilter::test( const TrajectoryMeasurement & tm, int foundHits) const
{
//first check min number of hits
if (foundHits < theMinHits ){ return true;}

// check for momentum below limit
const FreeTrajectoryState& fts = *tm.updatedState().freeTrajectoryState();

//avoid doing twice the check in TBC and QF
// We make it thread local so that we avoid race conditions between
// threads, and we make sure there is no cache contention between them.
if (tls.ftsMemory.parameters().vector() == fts.parameters().vector()) { return tls.answerMemory;}
tls.ftsMemory=fts;

//if p_T is way too small: stop
double pT = fts.momentum().perp();
if (pT<0.010) {tls.answerMemory=false; return false;}
//if error is way too big: stop
double invError = TrajectoryStateAccessor(fts).inversePtError();
if (invError > 1.e10) {tls.answerMemory=false;return false;}

//calculate the actual pT cut:
if ((1/pT + theNSigma*invError ) < 1/thePtThreshold ) {tls.answerMemory=false; return false;}
// first term is the minimal value of pT (pT-N*sigma(pT))
// secon term is the cut

tls.answerMemory=true; return true;
}

0 comments on commit 36d64ae

Please sign in to comment.