groups

Link to this snippet:


Download to Code Collector

language: C++
licence: Other

Reading and writing TEvent's

options: send to code collectorview all nepahwin's snippets
#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"
#include "TClassTable.h"
#include "TSystem.h"
#include "TROOT.h"

void tree4w() {

  TFile f("tree4.root","RECREATE");
  TTree t4("t4","A Tree with Events");
  SEvent *event = new SEvent();
  t4.Branch("event_split", &event,16000,99);
  t4.Branch("event_not_split", &event,16000,0);

  char etype[20];

  for (Int_t ev = 0; ev <100; ev++) {
    Float_t sigmat, sigmas;
    gRandom->Rannor(sigmat,sigmas);
    Int_t ntrack   = Int_t(600 + 600 *sigmat/120.);
    Float_t random = gRandom->Rndm(1);
    event->SetHeader(ev, 200, 960312, random);

    for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);

    t4.Fill();

    event->Clear();
  }

  f.Write();
  t4.Print();

}

void tree4r() {

  TFile *f = new TFile("tree4.root");
  TTree *t4 = (TTree*)f->Get("t4");

  SEvent *event = new SEvent();

  TBranch *bntrack = t4->GetBranch("fNtrack");
  TBranch *branch  = t4->GetBranch("event_split");
  branch->SetAddress(&event);

  Int_t nevent = (Int_t)t4->GetEntries();
  Int_t nselected = 0;
  Int_t nb = 0;
  for (Int_t i=0;i<nevent;i++) {
    bntrack->GetEntry(i);

    if (event->GetNtrack() > 587)continue;

    nb += t4->GetEntry(i);
    nselected++;

    if (nselected == 1) t4->Show();

    event->Clear();
  }

  if (gROOT->IsBatch()) return;
  new TBrowser();
  t4->StartViewer();
}

void tree4() {
   SEvent::Reset(); // Allow for re-run this script by cleaning static variables.
   tree4w();
   SEvent::Reset(); // Allow for re-run this script by cleaning static variables.
   tree4r();
}