Campuses:
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
computing:contrib:root:localdoc [2009/02/19 08:57] – reasons strait | computing:contrib:root:localdoc [2009/06/01 15:47] (current) – more strait | ||
---|---|---|---|
Line 27: | Line 27: | ||
ROOT also makes heavy use of " | ROOT also makes heavy use of " | ||
+ | |||
+ | ROOT is written without the use of namespaces, a C++ concept that helps organize code. This is why all ROOT classes start with " | ||
===== ROOT's Object Orientation ===== | ===== ROOT's Object Orientation ===== | ||
Line 36: | Line 38: | ||
===== Recommendations to ROOT users ===== | ===== Recommendations to ROOT users ===== | ||
- | ==== Use the STL, not ROOT Classes | + | ==== Programming Style ==== |
+ | |||
+ | === Use the STL, not ROOT Classes === | ||
The Standard Template Library, which defines the usual versions of strings, vectors, lists, maps, and so on, is documented [[http:// | The Standard Template Library, which defines the usual versions of strings, vectors, lists, maps, and so on, is documented [[http:// | ||
Line 42: | Line 46: | ||
If you follow this, your code will be easier to port to a different system if you decide to move away from ROOT. You will also be able to communicate better with people outside of the ROOT world and you will have better programming skills for when //you// are outside of the ROOT world. | If you follow this, your code will be easier to port to a different system if you decide to move away from ROOT. You will also be able to communicate better with people outside of the ROOT world and you will have better programming skills for when //you// are outside of the ROOT world. | ||
- | ==== Resist Overuse of Objects | + | === Resist Overuse of Objects === |
ROOT's documentation encourages use of objects when writing procedural code would be easier and more clear. | ROOT's documentation encourages use of objects when writing procedural code would be easier and more clear. | ||
Line 63: | Line 67: | ||
}; | }; | ||
- | This is insanity. | + | This is insanity. |
void Loop(TTree * awesomedata) | void Loop(TTree * awesomedata) | ||
{ | { | ||
+ | yourdataobject * onedatum = 0; | ||
+ | awesomedata-> | ||
for(int i = 0; i < awesomedata-> | for(int i = 0; i < awesomedata-> | ||
awesomedata-> | awesomedata-> | ||
Line 80: | Line 86: | ||
} | } | ||
- | ==== Use Good Names ==== | + | The class " |
+ | |||
+ | === Use Good Names === | ||
Root encourages or uses the following names: | Root encourages or uses the following names: | ||
Line 91: | Line 99: | ||
These names are extremely problematic. | These names are extremely problematic. | ||
- | C++ has, in practical terms, no limit on the length of variable or function names. | + | C++ has, in practical terms, no limit on the length of variable or function names. |
+ | |||
+ | < | ||
+ | |||
+ | It outputs two short nouns stuck together. | ||
+ | |||
+ | === Avoid Unnecessary Pointers === | ||
+ | |||
+ | ROOT examples often declare //every// object as a pointer (with ' | ||
+ | |||
+ | TH1F * queengrid = new TH1F(" | ||
+ | |||
+ | This is dangerous because if you do not call: | ||
+ | |||
+ | delete queengrid; | ||
+ | |||
+ | when you're done with the object, the memory is never released. | ||
+ | |||
+ | TH1F queengrid(" | ||
+ | |||
+ | 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 ' | ||
+ | |||
+ | 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. | ||
+ | |||
+ | // If you declared with ' | ||
+ | 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 ===== | ||
+ | |||
+ | There are three generic options for running ROOT-based code: | ||
+ | |||
+ | - Interpreted in the ROOT (Cint) shell via running the program " | ||
+ | - Compiled via running the program " | ||
+ | - Compiled into a standalone executable | ||
+ | |||
+ | The first two options are widely used and well documented (?) elsewhere. | ||
+ | |||
+ | < | ||
+ | |||
+ | where the single quotes protect the parentheses and double quotes from the shell, which otherwise assigns special meaning to them. To run compiled: | ||
+ | |||
+ | < | ||
+ | |||
+ | or | ||
+ | |||
+ | < | ||
+ | |||
+ | where the second form forces recompilation (sometimes necessary if you move to a new machine, for instance). | ||
+ | |||
+ | The third is rarely used, but probably should be used more. In this case, you write an ordinary C++ program with the function main() that uses some ROOT objects, i.e.: | ||
+ | |||
+ | < | ||
+ | using namespace std; | ||
+ | #include " | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char ** argv) | ||
+ | { | ||
+ | int seed = 0; | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | cout << "Your random number is " << atomlitre.Rndm() << endl; | ||
+ | |||
+ | | ||
+ | | ||
+ | cout << "Your file name is " << filename << endl; | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | and compile it with: | ||
+ | |||
+ | < | ||
+ | |||
+ | and run it with: | ||
+ | |||
+ | < | ||
+ | |||
+ | This has two big advantages. | ||
+ | |||
+ | ====== Making Attractive Plots ====== | ||
+ | |||
+ | The default ROOT plot is quite nice for screen display. | ||
+ | |||
+ | These things can all be modified either by fiddling with the individual objects or by setting things in the active TStyle. | ||
+ | |||
+ | < | ||
+ | void SetOKStyle() | ||
+ | { | ||
+ | TStyle* OKStyle = new TStyle(" | ||
+ | |||
+ | // Colors | ||
+ | |||
+ | //set the background color to white | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | // Get rid of drop shadow on legends | ||
+ | // This doesn' | ||
+ | OKStyle-> | ||
+ | |||
+ | // | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | //use the primary color palette | ||
+ | OKStyle-> | ||
+ | |||
+ | //set the default line color for a histogram to be black | ||
+ | OKStyle-> | ||
+ | |||
+ | //set the default line color for a fit function to be red | ||
+ | OKStyle-> | ||
+ | |||
+ | //make the axis labels black | ||
+ | OKStyle-> | ||
+ | |||
+ | //set the default title color to be black | ||
+ | OKStyle-> | ||
+ | |||
+ | //set the margins | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | //set axis label and title text sizes | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | //set line widths | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | // Misc | ||
+ | |||
+ | //align the titles to be centered | ||
+ | // | ||
+ | |||
+ | //turn off xy grids | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | //set the tick mark style | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | // | ||
+ | OKStyle-> | ||
+ | |||
+ | //set the default stats shown | ||
+ | OKStyle-> | ||
+ | |||
+ | //marker settings | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | // Fonts | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | OKStyle-> | ||
+ | |||
+ | // Set the paper size for output | ||
+ | OKStyle-> | ||
+ | |||
+ | //done | ||
+ | OKStyle-> | ||
+ | |||
+ | cout << "Using OKStyle" | ||
+ | } | ||
+ | </ | ||
+ |