您的位置:首页 > 编程语言 > Python开发

直播——git+python+root

2013-05-28 00:51 232 查看
Boss+同学给了一个小人物,把excel里面的图数据用root处理一下——需要拟合。虽然是半夜,但是挺有兴致,看看需要多久搞定这个。

声明:操作是在台式机上进行的,文章是在笔记本上写的,代码全部是重新敲的而不是copy-paste,不保证无拼写错误。重在理解。

5:38 Step1 git

git是版本控制软件。利用git,你可以记录你改了你的软件的哪些部分、保存快照并且非常方便的切换到某个快照(类似于photoshop里的历史功能) 。这里只演示了git最简单的用法,没有用到git最强大的的切换快照功能。关于git的全部命令可以参考[1]这里[2]还有一个github的15分钟教程。

先告诉git我们是谁。

git config --global user.name "Ding Xuefeng"

git config --global user.email "dingxf@ihep.ac.cn"

git config --global color.ui true

再建立目录,初始化

mkdir ~/git/project1

cd ~/git/project1

git init #这句话会在project1目录下建立一个.git目录,git的数据就保存在这里

git add . #git只会保存和改变它“追踪”的文件。add .的意思是追踪当前目录(并递归遍历子目录)的所有文件

git commit -am "mkdir and init git." #把追踪的文件的当前状态保存为快照,并添加注释。

5:44 Step2 看看怎么用python读取excel的数据

google一下,找到这个[3],下载这个[4],例子在这里[5]

安装: unzip master以后su切换到root用户,然后python setup.py install再ctrl+d切换回普通用户

试试

纠结了一下想用ipython,发现sl没有,可能需要从source file安装,不能yum,遂放弃

vim也没有配置好,:set tabstop=4

7:16 C++处理python得到的数据

纠结了很久,本来希望在C++中调用python程序,看上去太复杂遂放弃。方案:

先用python把excel转换成txt文件,然后用c++读txt文件。搞定。

保存快照:

git commit -am "use python to read xls and output to txt file"

接下来写root部分。root是用C++写的,所以我们可以把root当成一个普通的库通过include头文件和装载动态库来使用root。root提供的root-config命令可以帮助我们指定头文件和动态库的位置。cxx文件是很久之前写的,实际工作的时候是复制粘贴来的,这里解释一下。

先是软件的流程图

从txt读3组x,y,yerr,每组都用TGraphError画出来,3个TGraphError用TMultiGraph画在一个TCanvas上面,然后把这个Canvas保存到一个root文件中。

接下来分别实现各个功能。注意这里的首先-接下来是功能实现的顺序,而不是程序的执行顺序。

首先是从txt文件读数据。读取的是普通文件,用C++提供的fstream就可以了。如果以后要读取root文件,需要用到TFile。



需要读取的时候input>>readedContent;就会从file中读出一个数字,并且文件指针向后移动。

然后是用TGraphError画图



TGraphError类构造函数中第一个参数datanum是点的个数,第二个是x数组,第三个是y数组,第四个是xerr数组,第五个是yerr数组。注意x[i]是一个数组指针,x[0],x[1],x[2]分别是第一组、第二组、第三组数据的x数组的指针。0表示没有xerr。x,y,ey是3个vector<double*>。

接下来是把3个TGraphError画到一个TCanvas中

先创建一个Canvas,cd到这个Canvas再创建一个TMultiGraph,分别构造3个TGraphError对象,然后用TMultiGraph的Add功能把这3个TGraphErrorAdd到TMultiGraph中。如果你要问我图中那些诡异的数字怎么来的,我用鼠标拖好以后保存成.C文件得到的。







再添加图例

这里用到TLegend对象。使用起来很简单,一看就懂。





构造函数中NULL表示图例没有标题,#pm是±,是TLatex的语法,参考[6]http://root.cern.ch/root/html534/TLatex.html “lp”表示图例中画出line+marker

最后,把TCanvas保存成root文件



同样不要忘记input.close()哦
大功告成。

9:55 无力吐槽 搞定了 懒的写日志了 回去睡觉了

ps 中途被tcsh和bash+LANGUAGE坑了无数次 还是bash好用



做回好人 附上源文件

py文件

#!/usr/bin/env python

# _*_ coding: utf-8 _*_

import string

#import numpy as np

#import matplotlib.pyplot as plt

from xlrd import open_workbook

book = open_workbook('data.xlsx')

sheet0 = book.sheet_by_index(0)

outputFileName = "data.txt"

outputFile = file(outputFileName,"w")

dataset = 3

outputFile.write(str(dataset)+'\n')

for i in range(dataset):

datanum = int(sheet0.col_values(4*i+1,0,1)[0])

#print datanum

outputFile.write(str(datanum)+'\n')

wavelen = sheet0.col_values(4*i+0,2,2+datanum)

abs = sheet0.col_values(4*i+1,2,2+datanum)

#print abs

err = sheet0.col_values(4*i+2,2,2+datanum)

for j in range(datanum):

#print j

outputFile.write(str(wavelen[j])+' '+str(abs[j])+' '+str(err[j])+'\n')

outputFile
a607
.close()

cxx文件

#include <iostream>

#include <string>

#include <fstream>

#include <vector>

#include "TCanvas.h"

#include "TGraph.h"

#include "TFile.h"

#include "TGraphErrors.h"

#include "TMultiGraph.h"

#include "TStyle.h"

#include "TLegend.h"

using namespace std;

int main(){

//read data from txt file

//open txt file

ifstream input;

input.open("data.txt");

if(!(input.is_open())) {

cout<<"Cannot get data: data.txt"<<endl;

return 1;

}

TCanvas *c1 = new TCanvas("c1","Data with error bars",200,10,700,500);

c1->SetGrid();

TMultiGraph *mg=new TMultiGraph();

TLegend* leg = new TLegend(0.1,0.7,0.48,0.9);

int dataset,datanum;

input>>dataset;

vector<TGraphErrors*> gr;

vector<double*> x,y,ey;

for(int i = 0;i<dataset;i++){

input>>datanum;

x.push_back(new double [datanum]);

y.push_back(new double [datanum]);

ey.push_back(new double [datanum]);

for(int k=0;k<datanum;k++){

double datax,datay,dataey;

input>>datax>>datay>>dataey;

cout<<datax<<" "<<datay<<" "<<dataey<<endl;

x.back()[k]=datax;

y.back()[k]=datay;

ey.back()[k]=dataey;

}

gr.push_back(new TGraphErrors(datanum,x[i],y[i],0,ey[i]));

cout<<gr[i]<<endl;

(gr[i])->SetMarkerColor(2+i);

(gr[i])->SetLineColor(2+i);

(gr[i])->SetMarkerStyle(20+i);

//gr[i]->SetLogy();

mg->Add(gr[i]);

}

input.close();

gr[0]->SetTitle("Graph with Errorbar");

mg->Draw("ALP");

leg->AddEntry(gr[0],"1 25.0(#pm0.1)#circC","p");

leg->AddEntry(gr[1],"2 22(#pm1)#circC","p");

leg->AddEntry(gr[2],"3 21.5#circC","p");

leg->SetFillColor(0);

leg->Draw();

TString filename("result.root");

TFile* f = new TFile(filename, "RECREATE");

//c1->SetLogy();

c1->Write();

f->Close();

input.close();

return 0;

}

还有图 当然这个图是我用鼠标改过的~



参考资料

[1]http://www.git-tower.com/files/cheatsheet/Git_Cheat_Sheet_grey.pdf

[2]http://try.github.com/

[3]http://www.python-excel.org/

[4]https://nodeload.github.com/python-excel/xlrd/zip/master

[5]http://www.simplistix.co.uk/presentations/python-excel.pdf

[6]http://root.cern.ch/root/html534/TLatex.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: