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
soft:root [2020/08/19 08:30] Ce Zhangsoft:root [2023/05/24 09:51] (current) – [ROOT study] Ce Zhang
Line 1: Line 1:
 =====ROOT study===== =====ROOT study=====
 +==== Divide ====
 +<code>
 +hHitTime_Ratio[i] = new TH1F(*hHitTime_Num[i]); 
 +hHitTime_Ratio[i]->SetName("hFRSignal"); 
 +hHitTime_Ratio[i]->Divide(hHitTime_Den[i]);
 +</code>
 +==== Sub Canvas and Pads ====
 +<code>
 +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();
 +</code>
 +
 +==== TF1 ====
 +<code>
 +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);
 +</code>
 +
 +[[https://root.cern.ch/root/htmldoc/guides/users-guide/FittingHistograms.html|gStyle->SetOptFit(1011);]]
 +
 +==== Time ====
 +
 +TDatime
 +
 +<code>
 +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()));
 +</code>
 +==== Get max and mim from TGraph ====
 +
 +<code>
 +TMath::MaxElement(n , g->GetY())
 +</code>
 +
 +==== Draw his from saved .root file in TCanvas ====
 +
 +<code>
 +   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");
 +</code>
 +
 +<code>
 +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()
 +
 +</code>
 +====Reset (refresh) the histogram====
 +
 +<code>
 +hT->Reset("ICESM");
 +</code>
 +[[https://root.cern/doc/master/classTH1.html#a537509270db4e59efb39619b94c3a9a4|link]]
 +
 +====Subdivide the Canvas====
 +
 +<code>
 +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();
 +
 +</code>
 +
 ==== Auto code by TTree==== ==== Auto code by TTree====
 <code> <code>
 TTree mytree; TTree mytree;
-mytree.MakeClass("mycode")+mytree.MakeClass("mycode");
 </code> </code>
 +
 +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====
  
Line 25: Line 159:
 </code> </code>
  
 +====TList (adding trees)====
 +<code>
 +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");
 +
 +</code>
 ====TBox==== ====TBox====
 <code> <code>
Line 61: Line 211:
  
  
-====TList (adding trees)==== 
-<code> 
-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"); 
- 
-</code> 
  
 ====TCutG==== ====TCutG====