This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
playground:playground [2008/12/01 15:09] – decianm | playground:playground [2015/03/04 13:46] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | ===== Idea ===== | + | |
- | Instead of writing C++ macros which are intepreted by CINT, one can think of adding a class to ROOT itself. This may be particularly useful for methods that draw and save histograms, apply cuts, etc. The advantage over macros is the stability, as the code is compiled and then executed, and not interpreted by CINT. | + | |
- | + | ||
- | ===== Example ===== | + | |
- | Suppose you want to write a (simple) class with a method that creates a ROOT file. You need the following three files: **ana.cc**, **ana.hh** and a **Makefile**. | + | |
- | + | ||
- | ==== .cc and .hh File ==== | + | |
- | Your **.cc** file should look somehow like this: | + | |
- | + | ||
- | < | + | |
- | #include " | + | |
- | + | ||
- | using namespace std; | + | |
- | + | ||
- | ClassImp(ana) | + | |
- | // ----------------------------------------------------- | + | |
- | // Constructor | + | |
- | // ----------------------------------------------------- | + | |
- | ana::ana() { | + | |
- | cout << "This is the AnalysisClass, | + | |
- | } | + | |
- | // ------------------------------------------------------ | + | |
- | // Destructor | + | |
- | // ------------------------------------------------------ | + | |
- | ana:: | + | |
- | } | + | |
- | // ---------------------------------------- | + | |
- | // test | + | |
- | // ---------------------------------------- | + | |
- | void ana:: | + | |
- | + | ||
- | TFile *bla = new TFile(" | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Your **.hh** file should look like: | + | |
- | < | + | |
- | #ifndef ANA | + | |
- | #define ANA | + | |
- | + | ||
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | + | ||
- | class ana: public TObject { | + | |
- | + | ||
- | public: | + | |
- | + | ||
- | ana(); | + | |
- | ~ana(); | + | |
- | + | ||
- | void test(); | + | |
- | + | ||
- | ClassDef(ana, | + | |
- | + | ||
- | }; | + | |
- | + | ||
- | # | + | |
- | </ | + | |
- | + | ||
- | ==== Some Notes ==== | + | |
- | * You **must** write a constructor yourself. Normally in C++, if you forget your constructor, | + | |
- | * The **ClassImp(ana)** and the **ClassDef(ana, | + | |
- | + | ||
- | ==== Makefile ==== | + | |
- | The Makefile should look like this: | + | |
- | + | ||
- | < | + | |
- | # =============================== | + | |
- | # Makefile for ana | + | |
- | # =============================== | + | |
- | + | ||
- | ROOTGLIBS | + | |
- | ROOTCFLAGS | + | |
- | + | ||
- | CXX = g++ | + | |
- | CXXFLAGS | + | |
- | SOFLAGS | + | |
- | + | ||
- | CXXFLAGS | + | |
- | + | ||
- | ANACLASSES = ana.o anaDict.o | + | |
- | + | ||
- | # =============================== | + | |
- | ana: ana.cc | + | |
- | # ------------------------------- | + | |
- | $(CXX) $(CXXFLAGS) -c ana.cc -o ana.o | + | |
- | $(ROOTSYS)/ | + | |
- | $(CXX) $(CXXFLAGS) -c anaDict.cc -o anaDict.o | + | |
- | $(CXX) $(SOFLAGS) $(ANACLASSES) -o libAnaClasses.so $(ROOTGLIBS) | + | |
- | + | ||
- | # =============================== | + | |
- | clean: | + | |
- | rm -f ana.o anaDict.o anaDict.cc anaDict.h libAnaClasses.so | + | |
- | # =============================== | + | |
- | </ | + | |
- | + | ||
- | ==== Some Notes ==== | + | |
- | * When doing copy & paste: Don't forget to replace the leading whitespaces with a tab. | + | |
- | * After typing '' | + | |
- | * I don't know anything about Makefiles. Any improvement of knowledge about them would therefore be helpful... | + | |
- | + | ||
- | ==== Loading the Class in ROOT ==== | + | |
- | * Start ROOT and type '' | + | |
- | * Create an instance of your object. Type '' | + | |
- | * Execute the method. Type '' | + | |
- | + | ||
- | ===== Troubleshooting ===== | + | |
- | * Always compile and run the class on the same machine. Otherwise you may get problems with different ROOT versions, different architectures (32- vs. 64-bit), etc. | + | |
- | * If your code compiles correctly, but you get errors like '' | + | |
- | * Check if '' | + | |
- | * Otherwise you have to add '' | + | |
- | * Did you think of the constructor? | + | |
- | * There may be also be something missing in the Makefile, meaning that not everything is linked correctly. | + | |
- | * The described procedure works for me on lxplus and locally (leopard.physik.uzh.ch). | + | |
- | + | ||
- | ===== Links ===== | + | |
- | The original articles about the addition of a class to ROOT are provided [[http:// | + | |
- | + | ||
- | + | ||
- | + | ||