ROOT study

Divide

hHitTime_Ratio[i] = new TH1F(*hHitTime_Num[i]); 
hHitTime_Ratio[i]->SetName("hFRSignal"); 
hHitTime_Ratio[i]->Divide(hHitTime_Den[i]);

Sub Canvas and Pads

TCanvas *c1 = new TCanvas(“c1”, “c1”, w, h);
c1->SetWindowSize( w+(w-c1->GetWw()), h+(h-c1->GetWh()) );
c1->Divide(2, 1); // … or … gPad->Divide(2, 1);

TVirtualPad *c1_1 = c1->cd(1); // enter the first pad
gPad->Divide(2, 2); // … or … c1_1->Divide(2, 2);
// enter the 4 sub-pads and do stuff
c1_1->cd(1); // … or … (c1->cd(1))->cd(1);
(new TText(0.1, 0.5, “c1 … 1 … 1”))->Draw(); gPad->Update();
c1_1->cd(2); // … or … (c1->cd(1))->cd(2);
(new TText(0.1, 0.5, “c1 … 1 … 2”))->Draw(); gPad->Update();
c1_1->cd(3); // … or … (c1->cd(1))->cd(3);
(new TText(0.1, 0.5, “c1 … 1 … 3”))->Draw(); gPad->Update();
c1_1->cd(4); // … or … (c1->cd(1))->cd(4);
(new TText(0.1, 0.5, “c1 … 1 … 4”))->Draw(); gPad->Update();

TF1

TF1 * fit1 = new TF1("fit1","[0]*exp([1]*x)",3e-6,9e-6);
fit1->SetParameters(0.4,0.4e5);
f3->Fit(fit1,"","",3e-6,8e-6);
gStyle->SetOptFit(0111);

gStyle->SetOptFit(1011);

Time

TDatime

TDatime * Time = new TDatime();
cout<<Time->AsString()<<" "<<Time->GetDate()<<" "<<Time->GetTime()<<endl;
// output: Fri Jul 30 09:56:02 2021 20210730 95602
// ...
c1->SaveAs(Form("MuonTracks-%d-%d.pdf",Time->GetDate(),Time->GetTime()));

Get max and mim from TGraph

TMath::MaxElement(n , g->GetY())

Draw his from saved .root file in TCanvas

   TFile f1("jetht_mad.root","read");
   TCanvas *c1 = (TCanvas*)f1.Get("Canvas_1");
   TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
   
   TFile f2("jet_HT_data.root","read");
   TCanvas *c2 = (TCanvas*)f2.Get("c1");
   TH1D *h1;
   
   TList* l = c2-> GetListOfPrimitives();
   TIter next(l);
   TObject *found, *obj;
   while ((obj=next())) {
      if (obj->InheritsFrom(TH1D::Class())) {
         h1 = (TH1D*)obj;
      }
   }
   

   TCanvas c3;
   h1->Draw();
   ae->Draw("L");
TFile *_file0 = TFile::Open("S2areaMuYield_SlineItvScan_210205_tot-Itv-4_TBEAM_0WithReflection_5_hT_reflection_5.0.root");
TCanvas *c1 = (TCanvas*)_file0->Get("c0_reflection_hT");
TH1D *aee = (TH1D*)c1->GetPrimitive("hT"); // get the name of the histogram, can be obtained by c1->ls()

Reset (refresh) the histogram

hT->Reset("ICESM");

link

Subdivide the Canvas

c->cd(1); 
gPad->Divide(2, 2);
//then always …
c->cd(1); 
gPad->cd(i); // i = 1 … 4 (4 = 2 * 2)

//the first row to take 80% and 2nd row to take 20% of the space.

   TCanvas *c = new TCanvas("c", "c", 800,800);
   c->Draw();
   TPad *p1 = new TPad("p1","p1",0.1,0.2,0.9,1.);
   p1->Draw();
   p1->Divide(2,1);
   TPad *p11 = (TPad*)p1->cd(1);
   p11->SetFillColor(kRed);
   p11->Draw();
   TPad *p12 = (TPad*)p1->cd(2);
   p12->SetFillColor(kRed-1);
   p12->Draw();

   c->cd(0);
   TPad *p2 = new TPad("p1","p1",0.1,0.,0.9,0.2);
   p2->Draw();
   p2->Divide(2,1);
   TPad *p21 = (TPad*)p2->cd(1);
   p21->SetFillColor(kBlue);
   p21->Draw();
   TPad *p22 = (TPad*)p2->cd(2);
   p22->SetFillColor(kBlue-1);
   p22->Draw();

Auto code by TTree

TTree mytree;
mytree.MakeClass("mycode");

This will generate mycode.h and mycode.C

My modification:

  • Replace the initialization of TTree with TString
  • Delete the inherit fChain(0)
  • Add fIdentidier as the name of the file with out '.root'

TList

TList* glist = new TList();
glist->Add(g1);
glist->Add(g2);
file_out->Add(glist);
   TList *list = f1->GetListOfKeys();
   for (Int_t i=0;i<list->GetEntries();++i){
     TKey *key = (TKey*)list->At(i);
     TString name = key->GetName();
     TObject *o1 = f1->Get(name.Data());
     TString cname(o1->ClassName());
     if (!cname.Contains("TH1")) continue;
   }

TList (adding trees)

Tree* t1  = TxtToTree(...);
Tree* t2  = TxtToTree(...);

TList *list = new TList;

	list->Add(t1);
	list->Add(t2);
	list->Add(t3);
        //...
        
TTree *newtree = TTree::MergeTrees(list);
newtree->SetName("newtree");

TBox

TBox * b = new TBox(-25,-20,25,20);
//b->SetFillStyle(3003);
b->SetFillColorAlpha(10,0.0);
b->SetLineStyle(1);
b->SetLineColor(2);
b->SetLineWidth(10);
b->Draw();

External shell command

.!ls
.! mv abc.txt def.txt
gSystem->Exec(“ls”);
gROOT->ProcessLine(Form(".!mkdir %s",filename.Data()));
gROOT->Macro("rootlogon.C");

Vector in branch

std::vector<Double_t>* DiffusionVertexX;
tree->Branch("DiffusionVertexX", "vector<Double_t>", &DiffusionVertexX);

TChain

TChain should be created in advance of files to be added.

TChain * tree = new TChain("tree","tree");
tree->Add("/home/had/zhangce/g-2/MuYield/Root/MuYield_statistic_compain_1.root");
//...

TCutG

build a circle cut:

  const Int_t n = 30;
  Double_t x[n+1],y[n+1];

  Double_t rcut = 21;
  Double_t dphi = TMath::TwoPi()/n;

  for (Int_t i=0;i<n;i++) {
    x[i] = rcut*TMath::Cos(i*dphi);
    y[i] = rcut*TMath::Sin(i*dphi);
  }
  
  x[n] = x[0]; y[n] = y[0];
  TCutG *mcpcut = new TCutG("mcpcut",n+1,x,y);