Go to the U of M home page
School of Physics & Astronomy
School of Physics and Astronomy Wiki

User Tools


computing:contrib:root:localdoc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
computing:contrib:root:localdoc [2009/02/19 09:52] straitcomputing:contrib:root:localdoc [2009/06/01 15:47] (current) – more strait
Line 71: Line 71:
   void Loop(TTree * awesomedata)   void Loop(TTree * awesomedata)
   {   {
 +    yourdataobject * onedatum = 0;
 +    awesomedata->SetBranchAddress("yourdataobject", &onedatum);
     for(int i = 0; i < awesomedata->GetEntries(); i++){     for(int i = 0; i < awesomedata->GetEntries(); i++){
       awesomedata->GetEntry(i);       awesomedata->GetEntry(i);
Line 83: Line 85:
     Loop(awesomedata);     Loop(awesomedata);
   }   }
 +
 +The class "yourdataobject" needs only contain the data and not any functions.
  
 === Use Good Names === === Use Good Names ===
Line 100: Line 104:
  
 It outputs two short nouns stuck together.  It probably won't be very descriptive, but at least it will be searchable. It outputs two short nouns stuck together.  It probably won't be very descriptive, but at least it will be searchable.
 +
 +=== Avoid Unnecessary Pointers ===
 +
 +ROOT examples often declare //every// object as a pointer (with '*') and with dynamically allocated memory (with 'new').  For example:
 +
 +  TH1F * queengrid = new TH1F("it's a grid", "with queens", 10, 0, 10);
 +
 +This is dangerous because if you do not call:
 +
 +  delete queengrid;
 +
 +when you're done with the object, the memory is never released.  This is fine if you're just drawing one plot, but in a large program, you can quickly lose track of things like this and you will then wonder why it uses 3GB of RAM.  The alternative is:
 +
 +  TH1F queengrid("it's a grid", "with queens", 10, 0, 10);
 +
 +which, besides being safer, is also less typing.
 +
 +Now!  Graphical objects like histograms that are meant to appear on the screen //do// need to be allocated with 'new', because otherwise they will disappear from the screen when their function ends.  However, suppose you want to make a histogram, fill it and do a fit, but all you care about is the result of the fit; you never need to see the histogram.  Then declare the histogram without '*' and 'new'.
 +
 +For non-graphical objects like TRandom3, it's an easier call.  Don't make them pointers unless you have a particular reason to.  Despite the example you see that says to do this:
 +
 +  TRandom3 * laserfocus = new TRandom3();
 +
 +instead do simply:
 +
 +  TRandom3 laserfocus;
 +
 +These generate the same random numbers, but the first one stays in memory until you delete it explicitly, while the second stays in memory until the bottom of the function.  By the way, to pass each of these to another function, the functions are declared as:
 +
 +  // If you declared with '*' and 'new'
 +  void IWantRandomNumbers(TRandom3 * laserfocus)
 +  
 +  // If you simply declared the object:
 +  // This copies the object: the sequence of random
 +  // numbers will not be advanced in the calling function
 +  void IWantRandomNumbers(TRandom3 laserfocus)
 +  
 +  // This uses the object itself: the sequence of random
 +  // numbers *will* be advanced in the calling function
 +  void IWantRandomNumbers(TRandom3 & laserfocus)
  
 ===== How to Run Code ===== ===== How to Run Code =====
Line 157: Line 201:
 <code>printrandomnumber 17 datafile.root</code> <code>printrandomnumber 17 datafile.root</code>
  
-This has two big advantages.  First, you do not need to type all of those quotation marks and parentheses.  Second, your compiled program can run on machines that don't have ROOT installed.  Third, your code is less likely to stop working when the ROOT version changes.  Fourth, it probably runs faster because it doesn't have all the overhead of running root itself.  Fifth, your code looks much more like the ordinary C++ that most programmers are familiar with. Ok, more than two.+This has two big advantages.  First, you do not need to type all of those quotation marks and parentheses.  Second, your compiled program can run on machines that don't have ROOT installed.  Third, your code is less likely to stop working when the ROOT version changes.  Fourth, it probably runs faster because it doesn't have all the overhead of running root itself.  Fifth, your code looks much more like the ordinary C++ that most programmers are familiar with. Sixth, the compile error messages from g++ are **much** more helpful than those from the ROOT compiler. Ok, more than two.
  
 ====== Making Attractive Plots ====== ====== Making Attractive Plots ======
Line 183: Line 227:
  
   // Get rid of drop shadow on legends   // Get rid of drop shadow on legends
 +  // This doesn't seem to work.  Call SetBorderSize(1) directly on your TLegends
   OKStyle->SetLegendBorderSize(1);   OKStyle->SetLegendBorderSize(1);
  
Line 204: Line 249:
   //set the default title color to be black   //set the default title color to be black
   OKStyle->SetTitleColor(kBlack);   OKStyle->SetTitleColor(kBlack);
- 
-  // Sizes 
  
   //set the margins   //set the margins
computing/contrib/root/localdoc.1235058762.txt.gz · Last modified: 2009/02/19 09:52 by strait