? ? ? ? ?在ROOT和C++编程中,有很多约定俗成的规定和命名规范。有些是为了程序便于在不同编译环境的计算机上移植,有些是为了增加程序的可阅读性。记住这些约定,有助于程序员理解程序,并理解ROOT的头文件、源文件以及其他程序员的代码。本篇基本是翻译了,有亿点点水。
?一、编码约定
? 以 T 开头的类:TLine、TTree ? 非类类型以 _t 结尾:Int_t ? 数据成员以 f 开头:fTree ? 成员函数以大写开头:Loop() ? 常量以 k 开头:kInitialSize、kRed ? 全局变量以 g 开头:gEnv ? 静态数据成员以 fg 开头:fgTokenClient ? 枚举类型以 E 开头:EColorLevel ? 局部变量和参数以小写开头:nbytes ? Getter 和 setter 以 Get 和 Set 开头:SetLast()、GetFirst()
? ? ? ? 上述是最基本的ROOT约定。为防止翻译造成的偏差,原文如下。
From the first days of ROOT development, it was decided to use a set of coding conventions. This allows a consistency throughout the source code. Learning these will help you identify what type of information you are dealing with and enable you to understand the code better and quicker. Of course, you can use whatever convention you want but if you are going to submit some code for inclusion into the ROOT sources, you will need to use these. These are the coding conventions: ? Classes begin with T: TLine, TTree ? Non-class types end with _t: Int_t ? Data members begin with f: fTree ? Member functions begin with a capital: Loop() ? Constants begin with k: kInitialSize, kRed ? Global variables begin with g: gEnv ? Static data members begin with fg: fgTokenClient ? Enumeration types begin with E: EColorLevel ? Locals and parameters begin with a lower case: nbytes ? Getters and setters begin with Get and Set: SetLast(), GetFirst()
?二、非类类型
? Char_t 有符号字符型 1 字节 ? UChar_t 无符号字符型 1 字节 ? Short_t 有符号短整型 2 字节 ? UShort_t 无符号短整型 2 字节 ? Int_t 有符号整型 4 字节 ? UInt_t 无符号整型 4 字节 ? Long64_t 可移植的有符号长整型 8 字节 ? ULong64_t 可移植无符号长整型 8 字节 ? Float_t Float 4 字节 ? Double_t Float 8 字节 ? Double32_t 内存中的 Double 8 字节,写为 Float 4 字节 ? Bool_t 布尔型(0=假,1=真)
? ? ? ? 创造非类类型(non-class type)是为了便于代码在不同字长计算机和编译环境之间互相移植。如果您对C/C++已经有一定了解,您就已经知道int在不同的计算机上,甚至不同的教材中就拥有多种位数的解释。为了避免这个情况,我们创造了Int_t,在C/C++的编程中也常用此类方法,来防止因为环境造成的代码bug。本质上,这是一个统一的规范。同样地,原文如下。
Different machines may have different lengths for the same type. The most famous example is the int type. It may be 16 bits on some old machines and 32 bits on some newer ones. To ensure the size of your variables, use these pre defined types in ROOT: ? Char_t Signed Character 1 byte ? UChar_t Unsigned Character 1 byte ? Short_t Signed Short integer 2 bytes ? UShort_t Unsigned Short integer 2 bytes ? Int_t Signed integer 4 bytes ? UInt_tUnsigned integer 4 bytes ? Long64_t Portable signed long integer 8 bytes ? ULong64_t Portable unsigned long integer 8 bytes ? Float_t Float 4 bytes ? Double_t Float 8 bytes ? Double32_t Double 8 bytes in memory, written as a Float 4 bytes ? Bool_t Boolean (0=false, 1=true) If you do not want to save a variable on disk, you can use int or Int_t, the result will be the same and the interpreter or the compiler will treat them in exactly the same way.
?三、公共基类
TObject 提供协议,即(抽象)成员函数,用于: ? 对象 I/O(Read()、Write()) ? 错误处理(Warning()、Error()、SysError()、Fatal()) ? 排序(IsSortable()、Compare()、IsEqual()、Hash()) ? 检查(Dump()、Inspect()) ? 打印 (Print()) ? 绘图(Draw()、Paint()、ExecuteEvent()) ? 位处理(SetBit()、TestBit()) ? 内存分配(operatornew 和delete,IsOnHeap()) ? 访问元信息(IsA()、InheritsFrom()) ? 对象浏览(Browse(),IsFolder())
? ? ? ? 公共基类是对于每个类都共同继承的类,在这里我们常用的Draw(),就可以对THistogram、TGraph?、TTree等作用。画,都可以画。具体用法还需实践,原文如下。
In ROOT, almost all classes inherit from a common base class called TObject. This kind of architecture is also used in the Java language. The TObject class provides default behavior and protocol for all objects in the ROOT system. The main advantage of this approach is that it enforces the common behavior of the derived classes and consequently it ensures the consistency of the whole system. See “The Role of TObject”. TObject provides protocol, i.e. (abstract) member functions, for: ? Object I/O (Read(), Write()) ? Error handling (Warning(), Error(), SysError(), Fatal()) ? Sorting (IsSortable(), Compare(), IsEqual(), Hash()) ? Inspection (Dump(), Inspect()) ? Printing (Print()) ? Drawing (Draw(), Paint(), ExecuteEvent()) ? Bit handling (SetBit(), TestBit()) ? Memory allocation (operatornew and delete, IsOnHeap()) ? Access to meta information (IsA(), InheritsFrom()) ? Object browsing (Browse(), IsFolder())
四、全局变量
? gROOT保存并指向当前会话信息,可以管理ROOT打开的类。 ? gFile指向ROOT会话中当前打开的文件。 ? gDirectory指向当前目录。 ? gPad指向激活画板。 ? gRandom指向当前随机数生成器。默认情况下它指向基于梅森旋转生成器的TRandom3对象。 ? gEnv是具有当前会话的所有环境设置的全局变量。
五、变量命名规则
小驼峰命名规则:第一个单词小写,其他单词首字母大写。写法如:priAveEnergy
大驼峰命名规则:第一个单词首字母大写,其他单词首字母也大写。写法如:PriAveEnergy
具体内容如需学习,还请各位查阅官方的用户指导书Page47-Page50。
【资料】
1、ROOT官网——https://root.cern/?
2、ROOT官方指导书——《ROOTUsersGuide》
如有错误请指正。
|