Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make calls to TH2F::Projection thread-safe #21995

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions RecoVertex/BeamSpotProducer/src/PVFitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ ________________________________________________________________**/
#include "Minuit2/MnMigrad.h"
#include "Minuit2/MnPrint.h"
#include "TF1.h"
#include "TDirectory.h"
#include "TMinuitMinimizer.h"

#include <iostream>
#include <sstream>
using namespace std ;

PVFitter::PVFitter(const edm::ParameterSet& iConfig,
Expand Down Expand Up @@ -318,9 +320,14 @@ bool PVFitter::runFitter() {

if ( pvStore_.size() <= minNrVertices_ ) return false;

TH1F *h1PVx = (TH1F*) hPVx->ProjectionX("h1PVx", 0, -1, "e");
TH1F *h1PVy = (TH1F*) hPVy->ProjectionX("h1PVy", 0, -1, "e");
TH1F *h1PVz = (TH1F*) hPVx->ProjectionY("h1PVz", 0, -1, "e");
//need to create a unique histogram name to avoid ROOT trying to re-use one
// also tell ROOT to hide its global state
TDirectory::TContext guard{nullptr};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dr15Jones : mainly for my own education, could you please explain what this line is meant to? Thank you!
Also the class documentation in root doesn't look very explanatory to me:
https://root.cern.ch/doc/master/classTDirectory_1_1TContext.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This temporarily sets the global gDirectory pointer to nullptr which avoids accidental sharing across threads. When TDirectory::TContext is deleted, it rests gDirectory back to what it was before the constructor was called.

std::ostringstream str;
str <<this;
std::unique_ptr<TH1D> h1PVx{ hPVx->ProjectionX( (std::string("h1PVx")+str.str()).c_str(), 0, -1, "e") };
std::unique_ptr<TH1D> h1PVy{ hPVy->ProjectionX( (std::string("h1PVy")+str.str()).c_str(), 0, -1, "e") };
std::unique_ptr<TH1D> h1PVz{ hPVx->ProjectionY( (std::string("h1PVz")+str.str()).c_str(), 0, -1, "e") };

//Use our own copy for thread safety
// also do not add to global list of functions
Expand Down