Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32579
  • 博文数量: 8
  • 博客积分: 206
  • 博客等级: 入伍新兵
  • 技术积分: 115
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-23 03:16
文章分类

全部博文(8)

文章存档

2011年(8)

最近访客

分类: Python/Ruby

2011-03-23 03:43:49

PyROOT
  • pyROOT provides bindings for the classes in the root libraries
  • it allows you to replace CINT with its interpreted c++ with (i)python

Installation
  • compile ROOT with python support
  • make sure that $ROOTSYS/lib is in your PYTHONPATH and LD_LIBRARY_PATH variables

Example 1 - TBrowser
  1. In [2]: import ROOT

  2. In [3]: tb = ROOT.TBrowser()

Example 2 - Opening TFiles
  1. In [4]: f = ROOT.TFile("examples/tree.root")
  2. In [5]: f.ls()
  3. TFile** examples/tree.root
  4. TFile* examples/tree.root
  5. KEY: TTree aTree;1 tree title
  6. In [6]: t = f.Get("aTree")
  7. In [7]: t.Print()
  8. ******************************************************************************
  9. *Tree :aTree : tree title *
  10. *Entries : 1000000 : Total = 24071514 bytes File Size = 19821249 *
  11. * : : Tree compression factor = 1,21 *
  12. ******************************************************************************
  13. *Br 0 :normal : normal/D *
  14. *Entries : 1000000 : Total Size= 8023900 bytes File Size = 7227246 *
  15. *Baskets : 251 : Basket Size= 32000 bytes Compression= 1,11 *
  16. *............................................................................*
  17. *Br 1 :uniform : uniform/D *
  18. *Entries : 1000000 : Total Size= 8024155 bytes File Size = 5452842 *
  19. *Baskets : 251 : Basket Size= 32000 bytes Compression= 1,47 *
  20. *............................................................................*
  21. *Br 2 :var : var/D *
  22. *Entries : 1000000 : Total Size= 8023135 bytes File Size = 7134158 *
  23. *Baskets : 251 : Basket Size= 32000 bytes Compression= 1,12 *
  24. *............................................................................*
  25. In [8]: t.
  26. Display all 326 possibilities? (y or n)

Example III - simple graphs
  1. import ROOT

  2. import numpy as n

  3. x = n.linspace(0, 4*n.pi,101)

  4. y = n.cos(x)

  5. g = ROOT.TGraph(len(x), x,y)

  6. g.SetTitle("cosine in x=[%.1f, %.1f]" % (x[0], x[-1]))

  7. g.GetXaxis().SetTitle("x")
  8. g.GetYaxis().SetTitle("y")

  9. g.Draw("AL")

Example IV - writing treesin C++:
  1. {
  2.     TFile* f = new TFile("tree.root", "recreate");
  3.     TTree* t = new TTree("aTree", "tree title");

  4.     double n;
  5.     double u;
  6.     double x;

  7.     t->Branch("normal", &n, "normal/D");
  8.     t->Branch("uniform", &u, "uniform/D");
  9.     t->Branch("var", &x, "var/D");

  10.     for (int i=0; i < 1000000; ++i) {
  11.         n = gRandom->Gaus();
  12.         u = gRandom->Uniform();
  13.         x = 2*u + 3 + n;
  14.         t->Fill();
  15.     }

  16.     f->Write();
  17.     f->Close();
  18. }
in python:
  1. #!/usr/bin/env python

  2. import ROOT
  3. import numpy as n

  4. print "Writing a tree"

  5. f = ROOT.TFile("tree.root", "recreate")
  6. t = ROOT.TTree("name_of_tree", "tree title")


  7. # create 1 dimensional float arrays (python's float datatype corresponds to c++ doubles)
  8. # as fill variables
  9. n = n.zeros(1, dtype=float)

  10. u = n.zeros(1, dtype=float)

  11. # create the branches and assign the fill-variables to them
  12. t.Branch('normal', n, 'normal/D')
  13. t.Branch('uniform', u, 'uniform/D')

  14. # create some random numbers, fill them into the fill varibles and call Fill()
  15. for i in xrange(100000):
  16.     n[0] = ROOT.gRandom.Gaus()

  17.     u[0] = ROOT.gRandom.Uniform()
  18.     t.Fill()

  19. # write the tree into the output file and close the file
  20. f.Write()
  21. f.Close()

Examples V - Drawing
  1. In [1]: import ROOT

  2. In [2]: f = ROOT.TFile("examples/tree.root")

  3. In [3]: t = f.Get("aTree")

  4. In [4]: t.Draw("uniform")

Example VI - ProcessLine
  1. #!/usr/bin/env python
  2. from ROOT import TTree, TFile, AddressOf, gROOT

  3. # Make a tree
  4. f = TFile('myTest.root','RECREATE')
  5. t = TTree('MyTree','My test tree')

  6. # Create a struct
  7. gROOT.ProcessLine(\
  8.   "struct MyStruct{\
  9.     Int_t someInt;\
  10.     Double_t someDouble;\

  11.   };")

  12. from ROOT import MyStruct

  13. # Create branches in the tree
  14. s = MyStruct()

  15. t.Branch('rootInt',AddressOf(s,'someInt'),'someInt/I')
  16. t.Branch('rootDouble',AddressOf(s,'someDouble'),'someDouble/D')

  17. # Fill tree
  18. for i in range(100):
  19.   s.someInt = i

  20.   s.someDouble = i
  21.   t.Fill()

  22. f.Write()

  23. f.Close()


阅读(3565) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~