tree0.C

Go to the documentation of this file.
00001 // Author: Heiko.Scheit@mpi-hd.mpg.de
00002 //
00003 //  simple Event class example
00004 //
00005 // execute as: .x tree0.C++
00006 //
00007 //  You have to copy it first to a directory where you have write access!
00008 //  Note that .x tree0.C cannot work with this example
00009 //  
00010 //
00011 ///////////////////////////////
00012 //  Effect of ClassDef() and ClassImp() macros
00013 //===============================================
00014 //
00015 // After running this macro create an instance of Det and Event
00016 //
00017 //   Det d;
00018 //   Event e;
00019 //
00020 // now you can see the effect of the  ClassDef() and ClassImp() macros.
00021 // (for the Det class these commands are commented!)
00022 // For instance 'e' now knows who it is:
00023 //
00024 //   cout<<e.Class_Name()<<endl;
00025 //
00026 // whereas d does not.
00027 //
00028 // The methods that are added by the ClassDef()/Imp() marcro can be listed with
00029 // .class
00030 //   .class Event
00031 //   .class Det
00032 ///////////////////
00033 
00034 #include <TRandom.h>
00035 #include <TTree.h>
00036 #include <TCanvas.h>
00037 #include <TStyle.h>
00038 
00039 #include <Riostream.h>
00040 
00041 //class Det  : public TObject  {
00042 class Det {  // each detector gives an energy and time signal 
00043 public:
00044   Double_t e; //energy
00045   Double_t t; //time
00046 
00047 //  ClassDef(Det,1)
00048 };
00049 
00050 //ClassImp(Det)
00051 
00052 //class Event { //TObject is not required by this example
00053 class Event : public TObject {
00054 public:
00055   
00056   Det a; // say there are two detectors (a and b) in the experiment
00057   Det b;
00058   ClassDef(Event,1)
00059 };
00060 
00061 ClassImp(Event)
00062 
00063 void tree0() {
00064   // create a TTree   
00065   TTree *tree = new TTree("tree","treelibrated tree");
00066   Event *e = new Event;
00067   
00068   // create a branch with energy 
00069   tree->Branch("event",&e);
00070   
00071   // fill some events with random numbers
00072   Int_t nevent=10000;
00073   for (Int_t iev=0;iev<nevent;iev++) {
00074     if (iev%1000==0) cout<<"Processing event "<<iev<<"..."<<endl;
00075     
00076     Float_t ea,eb;
00077     gRandom->Rannor(ea,eb); // the two energies follow a gaus distribution
00078     e->a.e=ea;
00079     e->b.e=eb;
00080     e->a.t=gRandom->Rndm();  // random
00081     e->b.t=e->a.t + gRandom->Gaus(0.,.1);  // identical to a.t but a gaussian
00082                                            // 'resolution' was added with sigma .1
00083 
00084     tree->Fill();  // fill the tree with the current event
00085   }  
00086 
00087   // start the viewer
00088   // here you can investigate the structure of your Event class
00089   tree->StartViewer();
00090 
00091   //gROOT->SetStyle("Plain");   // uncomment to set a different style
00092   gStyle->SetPalette(1);        // use precomputed color palette 1
00093 
00094   // now draw some tree variables 
00095   TCanvas *c1 = new TCanvas();
00096   c1->Divide(2,2);
00097   c1->cd(1); 
00098   tree->Draw("a.e");  //energy of det a
00099   tree->Draw("a.e","3*(-.2<b.e && b.e<.2)","same");  // same but with condition on energy b; scaled by 3
00100   c1->cd(2); 
00101   tree->Draw("b.e:a.e","","colz");        // one energy against the other
00102   c1->cd(3); 
00103   tree->Draw("b.t","","e");    // time of b with errorbars
00104   tree->Draw("a.t","","same"); // overlay time of detector a
00105   c1->cd(4);
00106   tree->Draw("b.t:a.t");       // plot time b again time a
00107 
00108   cout<<endl;
00109   cout<<"You can now examine the structure of your tree in the TreeViewer"<<endl;
00110   cout<<endl;
00111 }
00112 

Generated on Tue Jul 5 15:45:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1