RDataFrame EnableImplicitMT() results in wrong results

I am new to RDataFrame and trying to learn it step by step.
I could represent my results from regular pyROOT loop using:

    df_fit = filtered_df.Define("tof", "tof_fit(tNewCalHit, dToRefPointNewCalHit, dToLineNewCalHit, layerNewCalHit)")\

where the function is:

double tof_fit(const RVec<double> &t_hit, const RVec<double> &r_hit, const RVec<double> &d_hit, const RVec<int> &layer_hit){
    int n_hits = t_hit.size();
    map <int, vector <Hit> > layer_hits;
    for (int i=0; i < n_hits; ++i){
        Hit hit(t_hit[i], r_hit[i], d_hit[i], layer_hit[i]);
    // TCanvas canvas_debug;

    vector<Hit> selected_hits;
    for (int layer=0; layer<10; ++layer){
        vector <Hit>& arr = layer_hits[layer];
        if (arr.size() == 0) continue;
        Hit closest_hit = *min_element(layer_hits[layer].begin(), layer_hits[layer].end(), [](const Hit &hit1, const Hit &hit2){return hit1.d < hit2.d;} );

    const int n_sel_hits = selected_hits.size();
    if (n_sel_hits <= 1) return 0.;

    double r[n_sel_hits];
    double t[n_sel_hits];
    double r_err[n_sel_hits];
    double t_err[n_sel_hits];
    for(int i=0; i < n_sel_hits; ++i){
        r[i] = selected_hits[i].r;
        t[i] = selected_hits[i].t;
        r_err[i] = 0.;
        t_err[i] = 10.;
    TGraphErrors gr(n_sel_hits, r, t, r_err, t_err);

    gr.Fit("pol1", "Q");
    // gr.Draw("AP");
    // gr.SetMarkerStyle(20);
    // gr.SetTitle("Fit of a TOF vs distance to impact point; d, [mm];TOF, [ns]");
    // canvas_debug.Update();
    // sleep(3);
    TF1 fit = *gr.GetFunction("pol1");
    // if (fit.GetChisquare() > 1.) continue;
    double tof = fit.GetParameter(0);
    if (tof <= 0.) return 0.;
    return tof;

But if I add
in the beginning in my code it outputs wrong results! (It seems half of the points just missing in my final histo).

I assume there is something not “thread-safe” with my function?


ROOT Version: 6.22
Platform: RedHat CentOS 7.8.2003
Compiler: g++ 9.2.0
pyROOT: python3

Hi @FoxWise,
I believe you are right and that function is not thread-safe due to the Fit call. @moneta, can you confirm whether TGraphErrors::Fit is thread-safe?


@FoxWise at the cost of some performance you can add a std::mutex and a std::lock_guard to tof_fit to make it thread-safe. You can actually figure out which line(s) are unsafe by reducing the section protected by the lock until you recover the broken behavior.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.