My Root tutorial (under construction)
I'll write a proper tutorial some day...
In the meantime, here are a few random tips I get from Sébastien Greder, who must be one of the most skilled C++ coder at lab.
Feel free to go and annoy him sometimes.
Sebastien's tricks
- To compile something, use
----------------------------------------------------------------------------
Mind all the options available.
gSystem->CompileMacro("myMacro.c") ; // instead of ".L myMacro.c+" ----------------------------------------------------------------------------
- Remember any objects inheriting TObject inherits the following useful methods: Dump(), Inspect(), ...
- If you have TString arguments, use references
----------------------------------------------------------------------------
to avoid creating memory-consuming copies.
void myFunction (const TString& myString) { ... } ----------------------------------------------------------------------------
- typedef can be used inside classes within a public access scope.
- Any TObject has a set of 10 free bits (14-23) you can use to store whatever (instead of Bool_t which are 8 bits large).
See the corresponding functions SetBit(), ResetBit(), TestBit(), InvertBit() in $ROOTSYS/include/TObject.h.
If you don't want to save them (as well as fUniqueID, which would allow you to save 64 bits per objects), just type----------------------------------------------------------------------------
If you need more bits, have a look to BIT(n), SETBIT(n,i), CLRBIT(n,i), TESTBIT(n,i) in $ROOTSYS/include/Rtypes.h.
TMyObject::Class()->IgnoreTObjectStreamer() ; ----------------------------------------------------------------------------
- Debugging
- Create a fDebug data member in your class. Use "//!" after its declaration not to store it whilst saving your object.
- A global pointer named gDebug in available.
- Many I/O functions are already provided: Info(), Warning(), Error(), SysError(), Fatal().
- The macro
----------------------------------------------------------------------------
is equivalent to
SafeDelete (myPointer) ; // in $ROOTSYS/include/RConfig.h --------------------------------------------------------------------------------------------------------------------------------------------------------
if ( myPointer != NULL ) { delete myPointer ; myPointer = NULL ; } ----------------------------------------------------------------------------
- In order to smartly manage memory while loading a Root file with several objects, use TRef (or TRefArray for TObjArray) (check the manual).
- To allow Root to zip your object efficiently, create blocks of data member with the same type (don't mix them), e.g.
----------------------------------------------------------------------------
Int_t fMyInt1 ; Int_t fMyInt2 ; Int_t fMyInt3 ; Float_t fMyFloat1 ; Float_t fMyFloat2 ; Float_t fMyFloat3 ; // ... ----------------------------------------------------------------------------
- To allow Root to zip your object efficiently, create blocks of data member with the same type (don't mix them), e.g.
Mathieu's tricks
Here are a few tips even though...
- How to stick pads in a canvas properly?
Using the command----------------------------------------------------------------------------
does not help much. The result's crap.
canvas->Divide (nx,ny,0.,0.) ; ----------------------------------------------------------------------------
You have to create pads with equal dimensions yourself.
The macro below illustrates this and invites the user to work as described here
(i.e. all the MRF_* functions should be moved to a separate file and linked in the current directory).
If some parts of the code does not make sense, comment them to see their impact.
You may need to use my rootlogon.
----------------------------------------------------------------------------
#include <TCanvas.h> #include <TGClient.h> #include <TPad.h> #include <TPaveText.h> #include <TBox.h> #include <TGaxis.h> #include <TH1D.h> void MRF_DrawIntermediateAxis ( Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax ) { TGaxis *axis = new TGaxis (xmin,ymax,xmax,ymax,xmin,xmax,510,gPad->GetLogx()?"G-":"-") ; axis->SetLabelSize(0.) ; axis->Draw() ; return ; } void MRF_HideGridBorders ( Double_t xmin, Double_t xmax, Double_t ymin, Double_t ymax ) { TBox* box = new TBox (xmin,ymin,xmax,ymax) ; box->SetLineColor(kBlack) ; box->SetLineWidth(1) ; box->SetFillStyle(0) ; box->Draw("same") ; return ; } void MRF_DrawTitle ( TString title ) { TPad *pad_title = new TPad ("pad_title","pad_title", 0.00,0.90,1.00,0.99) ; pad_title->Draw() ; pad_title->cd() ; TPaveText *pavetext_title = new TPaveText (0.,0.,1.,1.) ; pavetext_title->AddText("title") ; pavetext_title->SetBorderSize(0) ; pavetext_title->SetFillColor(kWhite) ; pavetext_title->SetFillStyle(1001) ; pavetext_title->SetTextFont(62) ; pavetext_title->SetTextAlign(22) ; pavetext_title->SetTextSize(0.6) ; pavetext_title->Draw() ; gPad->Update() ; return ; } TCanvas* MRF_CreateSpecialCanvas ( TString title ) { TCanvas *canvas = new TCanvas ("canvas","canvas",0,0,gClient->GetDisplayWidth(), gClient->GetDisplayHeight()) ; canvas->cd() ; TPad *pad1 = new TPad ("pad1","pad1", 0.00,0.499,1.00,0.95) ; pad1->SetBottomMargin(0.) ; pad1->SetTopMargin (0.15) ; pad1->Draw() ; pad1->cd() ; pad1->SetNumber(1) ; canvas->cd() ; TPad *pad2 = new TPad ("pad2","pad2", 0.00,0.049,1.00,0.50) ; pad2->SetTopMargin (0.) ; pad2->SetBottomMargin(0.15) ; pad2->Draw() ; pad2->cd() ; pad2->SetNumber(2) ; canvas->cd() ; MRF_DrawTitle ( title ) ; return (canvas) ; } void MRF_SetHistoGraphicalProperties ( TH1* histo, Double_t ymin, Double_t ymax, TString xtitle, TString ytitle) { histo->SetMinimum(ymin) ; histo->SetMaximum(ymax) ; histo->SetXTitle(xtitle) ; histo->GetXaxis()->CenterTitle() ; histo->SetYTitle(ytitle) ; histo->GetYaxis()->CenterTitle() ; return ; } ///// void ztest ( Bool_t setLogx=kTRUE ) { Double_t xmin=0.1, xmax=10., ymin=0.01, ymax=99.99 ; // not equal to 0. and 100. to avoid nasty superimposition (Give it a try, you'll understand) TH1D* histo = new TH1D ("histo","",100,xmin,xmax) ; histo->FillRandom("gaus",1000) ; MRF_SetHistoGraphicalProperties(histo,ymin,ymax,"X test []","Y test []") ; TCanvas *canvas = MRF_CreateSpecialCanvas ("my title (test)") ; canvas->cd(1) ; histo->Draw() ; MRF_HideGridBorders(xmin,xmax,ymin,ymax) ; canvas->cd(2) ; histo->Draw() ; MRF_HideGridBorders(xmin,xmax,ymin,ymax) ; MRF_DrawIntermediateAxis (xmin,xmax,ymin,ymax) ; canvas->cd() ; canvas->SaveAs("ztest.gif") ; return ; } ----------------------------------------------------------------------------
Of course, this piece code could be made even further transparent.
As an exercise, try to hide the MRF_HideGridBorders() and MRF_DrawIntermediateAxis()...
(You might need to redesign everything)