tree4.C

Go to the documentation of this file.
00001 #include "TFile.h"
00002 #include "TTree.h"
00003 #include "TBrowser.h"
00004 #include "TH2.h"
00005 #include "TRandom.h"
00006 #include "TClassTable.h"
00007 #include "TSystem.h"
00008 #include "TROOT.h"
00009 #if defined(__CINT__) && !defined(__MAKECINT__) 
00010 #include "../test/libEvent.so"
00011 #else 
00012 #include "../test/Event.h"
00013 #endif
00014 
00015 // This example writes a tree with objects of the class Event. 
00016 // It is a simplified version of $ROOTSYS/test/MainEvent.cxx to 
00017 // write the tree, and $ROOTSYS/test/eventb.C
00018 // It shows:
00019 //   -how to fill a Tree with an event class containing these 
00020 //    data members:
00021 //     char           fType[20];
00022 //     Int_t          fNtrack;
00023 //     Int_t          fNseg;
00024 //     Int_t          fNvertex;
00025 //     UInt_t         fFlag;
00026 //     Float_t        fTemperature;
00027 //     EventHeader    fEvtHdr;
00028 //     TClonesArray  *fTracks;            //->
00029 //     TH1F          *fH;                 //->
00030 //     Int_t          fMeasures[10];
00031 //     Float_t        fMatrix[4][4];
00032 //     Float_t       *fClosestDistance;   //[fNvertex] 
00033 //
00034 //   -the difference in splitting or not splitting a branch
00035 //   -how to read selected branches of the tree,
00036 //    and print the first entry with less than 587 tracks.
00037 //   -how to browse and analyze the Tree via the TBrowser and TTreeViewer
00038 
00039 // This example can be run in many different ways:
00040 //  way1:  .x tree4.C    using the CINT interpreter
00041 //  way2:  .L tree4.C
00042 //          tree4()
00043 //  way3:  .L ../test/libEvent.so
00044 //         .x tree4.C++   using ACLIC
00045 // One can also run the write and read parts in two separate sessions.
00046 // For example following one of the sessions above, one can start the session:
00047 //   .L tree4.C
00048 //   tree4r();
00049 
00050 void tree4w()
00051 {
00052  
00053   //create a Tree file tree4.root
00054   TFile f("tree4.root","RECREATE");
00055  
00056   // Create a ROOT Tree
00057   TTree t4("t4","A Tree with Events");
00058     
00059   // Create a pointer to an Event object
00060   Event *event = new Event();
00061     
00062   // Create two branches, split one.
00063   t4.Branch("event_split", &event,16000,99);
00064   t4.Branch("event_not_split", &event,16000,0);
00065  
00066   // a local variable for the event type 
00067   char etype[20];
00068 
00069   // Fill the tree
00070   for (Int_t ev = 0; ev <100; ev++) {
00071     Float_t sigmat, sigmas;
00072     gRandom->Rannor(sigmat,sigmas);
00073     Int_t ntrack   = Int_t(600 + 600 *sigmat/120.);
00074     Float_t random = gRandom->Rndm(1);
00075     sprintf(etype,"type%d",ev%5);
00076     event->SetType(etype);
00077     event->SetHeader(ev, 200, 960312, random);
00078     event->SetNseg(Int_t(10*ntrack+20*sigmas));
00079     event->SetNvertex(Int_t(1+20*gRandom->Rndm()));
00080     event->SetFlag(UInt_t(random+0.5));
00081     event->SetTemperature(random+20.);
00082     
00083     for(UChar_t m = 0; m < 10; m++) {
00084       event->SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
00085     }
00086     
00087     // fill the matrix
00088     for(UChar_t i0 = 0; i0 < 4; i0++) {
00089       for(UChar_t i1 = 0; i1 < 4; i1++) {
00090         event->SetMatrix(i0,i1,gRandom->Gaus(i0*i1,1));
00091       }
00092     }
00093 
00094     //  Create and fill the Track objects
00095     for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);
00096     
00097     // Fill the tree
00098     t4.Fill(); 
00099     
00100     // Clear the event before reloading it 
00101     event->Clear();
00102   }
00103   
00104   // Write the file header
00105   f.Write();
00106   
00107   // Print the tree contents
00108   t4.Print();
00109 }
00110 
00111 
00112 void tree4r()
00113 {
00114   // check to see if the event class is in the dictionary
00115   // if it is not load the definition in libEvent.so
00116   if (!TClassTable::GetDict("Event")) {
00117     gSystem->Load("$ROOTSYS/test/libEvent");
00118   }    
00119    
00120   // read the tree generated with tree4w 
00121   
00122   //note that we use "new" to create the TFile and TTree objects !
00123   //because we want to keep these objects alive when we leave this function.
00124   TFile *f = new TFile("tree4.root");
00125   TTree *t4 = (TTree*)f->Get("t4");
00126   
00127   // create a pointer to an event object. This will be used
00128   // to read the branch values.
00129   Event *event = new Event();
00130   
00131   // get two branches and set the branch address
00132   TBranch *bntrack = t4->GetBranch("fNtrack");
00133   TBranch *branch  = t4->GetBranch("event_split");
00134   branch->SetAddress(&event);
00135    
00136   Int_t nevent = (Int_t)t4->GetEntries();
00137   Int_t nselected = 0;
00138   Int_t nb = 0;
00139   for (Int_t i=0;i<nevent;i++) {    
00140     //read branch "fNtrack"only
00141     bntrack->GetEntry(i);  
00142           
00143     //reject events with more than 587 tracks          
00144     if (event->GetNtrack() > 587)continue;
00145     
00146     //read complete accepted event in memory 
00147     nb += t4->GetEntry(i);                  
00148     nselected++;
00149     
00150     //print the first accepted event
00151     if (nselected == 1) t4->Show();
00152     
00153     //clear tracks array     
00154     event->Clear();                        
00155   }
00156    
00157   if (gROOT->IsBatch()) return;
00158   new TBrowser();
00159   t4->StartViewer();
00160 }   
00161 
00162 void tree4() {
00163    Event::Reset(); // Allow for re-run this script by cleaning static variables.
00164    tree4w();
00165    Event::Reset(); // Allow for re-run this script by cleaning static variables.
00166    tree4r();
00167 }

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