User Tools

Site Tools


root:storemcnumbers

This is an old revision of the document!


Storing Monte Carlo numbers in a file

Idea

When analyzing background using Monte Carlo truth information, it may be useful to check the real identity of a particle that was considered a Kaon, a Muon, etc.

The following script loops through the whole tree, calculates the percentage of the respective particle and saves the output in a textfile. It needs as input the tree with the truth-information (e.g. in an nTuple) and the name of the variable. Furthermore, the name of the output-file can be specified.

Code

The code was written for a class which was added to ROOT. It should be straightforward to use it in a macro.

.hh File

Add the following method to your header-file:

 void storeMCNumbers(TTree* tree, const char* variablename, const char* filename = "BG_cat.txt");

The last argument prototype gives the default name of the textfile, that is used as an output.

.cc File

void ana::storeMCNumbers(TTree* tree, const char* variablename, const char* filename ){
  
  // --------------------------------------------------------------------------------
  // -- Simple method to read out a histogram containing PID-numbers for particles (x-axis) and their
  // -- frequency (y-axis)
  // -- Needed inputs are: The tree containing the information, a name for the histogram
  // -- and the name of the variable in the tree
  // --------------------------------------------------------------------------------

  gStyle->SetOptTitle(1);

  // -- Declaring some numbers and ranges
  int nbins = 2000001;
  int min = -1000000;
  int max = 1000000;
  
  // -- Creating a histogram to temporarily save the information
  TH1I* histo = new TH1I("histo", "histo", nbins, min, max);
  char out[1000];
  sprintf(out, "%s>>%s",variablename,"histo");
  tree->Draw(out, "","goff");
  //histo->Draw();
  //fillPID(variablename, tree, histo, min);
  
  // -- Draw the histogram (is this needed?)
  //TCanvas *c = new TCanvas();
  //histo->Draw();

  // -- Declare a multimap to automatically sort the PID-numbers according to their frequency in %
  multimap<double,int> testmap;
  multimap<double,int>::iterator ipos;
  for(int i = min; i <= max ; i++){

    if(histo->GetBinContent(i  + TMath::Abs(min)  ) == 0) continue;
    
    std::pair<double,int> dummy;
    dummy.first = (histo->GetBinContent(i + TMath::Abs(min) ) / histo->Integral()) * 100;
    dummy.second = i-1;
    testmap.insert(dummy);
  }
  
  // -- Open a file "BG_cat.txt" to save the output
  fstream PIDfile(filename, ios::out | ios::app);
  
  if(!PIDfile.is_open()){
    cout << "Could not open the output file" << endl;
  }
  

  PIDfile << "\n";
  PIDfile << " ====================== " << endl;
  PIDfile << " --> " << variablename << endl;
  PIDfile << " ====================== " << endl;

  for(ipos = testmap.begin(); ipos != testmap.end(); ++ipos){

    PIDfile << "Percentage " << ipos->first << "\t Particle Number " 
           << ipos->second << endl;
  }

  
  PIDfile.close();
  delete histo;
  //delete c;
  
}
root/storemcnumbers.1231335066.txt.gz · Last modified: 2009/01/07 14:31 by decianm