Root file is not storing histograms correctly

Hello,

The code that I want to write is one that stores histograms in a root file, but the code is not filling the histograms properly. In theory, the code should fill histograms based on a certain value that I call q2 from the function pdf.xfxQ2( id, x, q2) and plot pdf.xfxQ2( id, x, q2) vs. x for that q2. Although, the histograms do not appear this way when I open the root file. Please see if my code has something missing or if something needs to be changed. Hope my explanation makes sense. If not, please ask questions. Hope to hear a solution soon.

-Charlie Clark

Code:

#include "LHAPDF/PDF.h"
#include <iostream>
#include <fstream>
//ROOT header files
#include "TFile.h"
#include "TH2F.h"

using namespace std;


/// Demo of an analytic/custom PDF class
///
/// Returns the same value for all light flavors (inc. gluons)
struct AnalyticPDF : public LHAPDF::PDF {

  AnalyticPDF() {
    info().set_entry("Flavors", "-5,-4,-3,-2,-1,0,1,2,3,4,5");
  }

  double _xfxQ2(int id, double x, double q2) const {
    if (abs(id) > 5 && id != 0) return 0;
    return 0.15 * sin(20.0*x) * sin(20.0*q2);
  }

  void _xfxQ2(double x, double q2, std::vector<double>& ret) const {
    for (int id(-5); id<5; ++id)
      _xfxQ2(id,x,q2);
  }

  bool inRangeX(double x) const { return true; }
  bool inRangeQ2(double q2) const { return true; }

};


int main(int argc, const char* argv[]) {
  //gSystem->Load(" -I/usr/local/Cellar/root/6.26.06_2/include");
  //ofstream MyFile("analyticpdf.txt");
  TFile * f = new TFile("analyticPDF.root","RECREATE");
  int n = 9;
  TH2F * hg[n];
  //Idea: array of histograms for each parton
  for (int i = 0; i < n; i++){
    TString hist = Form("h%d",i);
    TString title = Form("Q^2 = %f GeV^2, gluon", 10*exp(1.1513*i) );//
    hg[i] = new TH2F(hist,title,100,0.,1.,100,-0.2,0.2);
    hg[i]->SetMarkerStyle(3);
  }
  AnalyticPDF apdf;
  LHAPDF::PDF& pdf = apdf;
  for (double x = 0; x < 1.0; x += 0.1) {
    for (double logq2 = 1; logq2 < 6; logq2 += 0.5) {
      const double q2 = pow(10, logq2);
      //cout << x << " " << q2 << " " << pdf.xfxQ2(21, x, q2) << endl;
      for (int i = 0; i < n; i++){
        if(q2 == 10*exp(1.1513*i)) {
          hg[i]->Fill( x, pdf.xfxQ2(21, x, 10*exp(1.1513*i)));
          hg[i]->Write();
        }
      }//Filling histograms
    }//Q^2 values
  }//x values
  //MyFile.close();

  f->Close();
  return 0;
}

_ROOT Version: 6.26/06
_Platform: Mac OS
_Compiler: C++


Try first Writing the histograms only after you have finished Filling them, i.e. move hg[i]->Write(); outside the loop over x, making another loop over the n histograms to Write each.
n should be const

  const int n = 9;

Also, try drawing the histograms in an interactive session and see if they are actually created and filled. Instead of hg[i]->Write() you can create a TCanvas and Divide it in 3x3 in your case, then draw a histogram in each pad.

  //...
  TCanvas *c = new TCanvas();
  c->Divide(3,3);
  for (int i = 0; i < n; i++) {
    c->cd(i+1);
    hg[i]->Draw();
  }
  //...

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