Event.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: Event.cxx 36320 2010-10-12 19:00:12Z brun $
00002 // Author: Rene Brun   19/08/96
00003 
00004 ////////////////////////////////////////////////////////////////////////
00005 //
00006 //                       Event and Track classes
00007 //                       =======================
00008 //
00009 //  The Event class is a naive/simple example of an event structure.
00010 //     public:
00011 //        char           fType[20];
00012 //        char          *fEventName;         //run+event number in character format
00013 //        Int_t          fNtrack;
00014 //        Int_t          fNseg;
00015 //        Int_t          fNvertex;
00016 //        UInt_t         fFlag;
00017 //        Double32_t     fTemperature;
00018 //        Int_t          fMeasures[10];
00019 //        Double32_t     fMatrix[4][4];
00020 //        Double32_t    *fClosestDistance; //[fNvertex] indexed array! 
00021 //        EventHeader    fEvtHdr;
00022 //        TClonesArray  *fTracks;
00023 //        TRefArray     *fHighPt;            //array of High Pt tracks only
00024 //        TRefArray     *fMuons;             //array of Muon tracks only
00025 //        TRef           fLastTrack;         //pointer to last track
00026 //        TRef           fHistoWeb;          //EXEC:GetHistoWeb reference to an histogram in a TWebFile
00027 //        TH1F          *fH;
00028 //        TBits          fTriggerBits;       //Bits triggered by this event.
00029 //
00030 //   The EventHeader class has 3 data members (integers):
00031 //     public:
00032 //        Int_t          fEvtNum;
00033 //        Int_t          fRun;
00034 //        Int_t          fDate;
00035 //
00036 //
00037 //   The Event data member fTracks is a pointer to a TClonesArray.
00038 //   It is an array of a variable number of tracks per event.
00039 //   Each element of the array is an object of class Track with the members:
00040 //     private:
00041 //        Float_t      fPx;           //X component of the momentum
00042 //        Float_t      fPy;           //Y component of the momentum
00043 //        Float_t      fPz;           //Z component of the momentum
00044 //        Float_t      fRandom;       //A random track quantity
00045 //        Float_t      fMass2;        //The mass square of this particle
00046 //        Float_t      fBx;           //X intercept at the vertex
00047 //        Float_t      fBy;           //Y intercept at the vertex
00048 //        Float_t      fMeanCharge;   //Mean charge deposition of all hits of this track
00049 //        Float_t      fXfirst;       //X coordinate of the first point
00050 //        Float_t      fXlast;        //X coordinate of the last point
00051 //        Float_t      fYfirst;       //Y coordinate of the first point
00052 //        Float_t      fYlast;        //Y coordinate of the last point
00053 //        Float_t      fZfirst;       //Z coordinate of the first point
00054 //        Float_t      fZlast;        //Z coordinate of the last point
00055 //        Double32_t   fCharge;       //Charge of this track
00056 //        Double32_t   fVertex[3];    //Track vertex position
00057 //        Int_t        fNpoint;       //Number of points for this track
00058 //        Short_t      fValid;        //Validity criterion
00059 //        Int_t        fNsp;          //Number of points for this track with a special value
00060 //        Double32_t  *fPointValue;   //[fNsp] a special quantity for some point.
00061 //        TBits        fTriggerBits;  //Bits triggered by this track.
00062 //
00063 //   An example of a batch program to use the Event/Track classes is given
00064 //   in this directory: MainEvent.
00065 //   Look also in the same directory at the following macros:
00066 //     - eventa.C  an example how to read the tree
00067 //     - eventb.C  how to read events conditionally
00068 //
00069 //   During the processing of the event (optionally) also a large number
00070 //   of histograms can be filled. The creation and handling of the
00071 //   histograms is taken care of by the HistogramManager class.
00072 //
00073 //   Note:  This version of the class Event (see EventMT.h and EventMT.cxx
00074 //   for an alternative) uses static variables to improve performance (by
00075 //   reducing the number of memory allocations).  Consequently, only one
00076 //   instance of the class Event should be in use at a time (a 2nd instance 
00077 //   would share the array of Tracks with the first instance).
00078 //
00079 ////////////////////////////////////////////////////////////////////////
00080 
00081 #include "TRandom.h"
00082 #include "TDirectory.h"
00083 #include "TProcessID.h"
00084 
00085 #include "Event.h"
00086 
00087 
00088 ClassImp(EventHeader)
00089 ClassImp(Event)
00090 ClassImp(Track)
00091 ClassImp(HistogramManager)
00092 
00093 TClonesArray *Event::fgTracks = 0;
00094 TH1F *Event::fgHist = 0;
00095 
00096 //______________________________________________________________________________
00097 Event::Event() : fIsValid(kFALSE)
00098 {
00099    // Create an Event object.
00100    // When the constructor is invoked for the first time, the class static
00101    // variable fgTracks is 0 and the TClonesArray fgTracks is created.
00102 
00103    if (!fgTracks) fgTracks = new TClonesArray("Track", 1000);
00104    fTracks = fgTracks;
00105    fHighPt = new TRefArray;
00106    fMuons  = new TRefArray;
00107    fNtrack = 0;
00108    fH      = 0;
00109    Int_t i0,i1;
00110    for (i0 = 0; i0 < 4; i0++) {
00111       for (i1 = 0; i1 < 4; i1++) {
00112          fMatrix[i0][i1] = 0.0;
00113       }
00114    }
00115    for (i0 = 0; i0 <10; i0++) fMeasures[i0] = 0;
00116    for (i0 = 0; i0 <20; i0++) fType[i0] = 0;
00117    fClosestDistance = 0;
00118    fEventName = 0;
00119    fWebHistogram.SetAction(this);
00120 }
00121 
00122 //______________________________________________________________________________
00123 Event::~Event()
00124 {
00125    Clear();
00126    if (fH == fgHist) fgHist = 0;
00127    delete fH; fH = 0;
00128    delete fHighPt; fHighPt = 0;
00129    delete fMuons;  fMuons = 0;
00130    delete [] fClosestDistance;
00131    if (fEventName) delete [] fEventName;
00132 }
00133 
00134 //______________________________________________________________________________
00135 void Event::Build(Int_t ev, Int_t arg5, Float_t ptmin) {
00136   fIsValid = kTRUE;
00137   char etype[20];
00138   Float_t sigmat, sigmas;
00139   gRandom->Rannor(sigmat,sigmas);
00140   Int_t ntrack   = Int_t(arg5 +arg5*sigmat/120.);
00141   Float_t random = gRandom->Rndm(1);
00142 
00143   //Save current Object count
00144   Int_t ObjectNumber = TProcessID::GetObjectCount();
00145   Clear();
00146   fHighPt->Delete();
00147   fMuons->Delete();
00148   
00149   Int_t nch = 15;
00150   if (ev >= 100)   nch += 3;
00151   if (ev >= 10000) nch += 3;
00152   if (fEventName) delete [] fEventName;
00153   fEventName = new char[nch];
00154   sprintf(fEventName,"Event%d_Run%d",ev,200);
00155   sprintf(etype,"type%d",ev%5);
00156   SetType(etype);
00157   SetHeader(ev, 200, 960312, random);
00158   SetNseg(Int_t(10*ntrack+20*sigmas));
00159   SetNvertex(Int_t(1+20*gRandom->Rndm()));
00160   SetFlag(UInt_t(random+0.5));
00161   SetTemperature(random+20.);
00162 
00163   for(UChar_t m = 0; m < 10; m++) {
00164      SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
00165   }
00166   for(UChar_t i0 = 0; i0 < 4; i0++) {
00167     for(UChar_t i1 = 0; i1 < 4; i1++) {
00168        SetMatrix(i0,i1,gRandom->Gaus(i0*i1,1));
00169     }
00170   }
00171 
00172   fTriggerBits.SetBitNumber((UInt_t)(64*gRandom->Rndm(1)));
00173   fTriggerBits.SetBitNumber((UInt_t)(64*gRandom->Rndm(1)));
00174   fTriggerBits.SetBitNumber((UInt_t)(64*gRandom->Rndm(1)));
00175 
00176   //  Create and Fill the Track objects
00177   for (Int_t t = 0; t < ntrack; t++) AddTrack(random,ptmin);
00178   
00179   //Restore Object count 
00180   //To save space in the table keeping track of all referenced objects
00181   //we assume that our events do not address each other. We reset the 
00182   //object count to what it was at the beginning of the event.
00183   TProcessID::SetObjectCount(ObjectNumber);
00184 }  
00185 
00186 //______________________________________________________________________________
00187 Track *Event::AddTrack(Float_t random, Float_t ptmin)
00188 {
00189    // Add a new track to the list of tracks for this event.
00190    // To avoid calling the very time consuming operator new for each track,
00191    // the standard but not well know C++ operator "new with placement"
00192    // is called. If tracks[i] is 0, a new Track object will be created
00193    // otherwise the previous Track[i] will be overwritten.
00194 
00195    TClonesArray &tracks = *fTracks;
00196    Track *track = new(tracks[fNtrack++]) Track(random);
00197    //Save reference to last Track in the collection of Tracks
00198    fLastTrack = track;
00199    //Save reference in fHighPt if track is a high Pt track
00200    if (track->GetPt() > ptmin)   fHighPt->Add(track);
00201    //Save reference in fMuons if track is a muon candidate
00202    if (track->GetMass2() < 0.11) fMuons->Add(track);
00203    return track;
00204 }
00205 
00206 //______________________________________________________________________________
00207 void Event::Clear(Option_t * /*option*/)
00208 {
00209    fTracks->Clear("C"); //will also call Track::Clear
00210    fHighPt->Delete();
00211    fMuons->Delete();
00212    fTriggerBits.Clear();
00213 }
00214 
00215 //______________________________________________________________________________
00216 void Event::Reset(Option_t * /*option*/)
00217 {
00218 // Static function to reset all static objects for this event
00219 //   fgTracks->Delete(option);
00220 
00221    delete fgTracks; fgTracks = 0;
00222    fgHist   = 0;
00223 }
00224 
00225 //______________________________________________________________________________
00226 void Event::SetHeader(Int_t i, Int_t run, Int_t date, Float_t random)
00227 {
00228    fNtrack = 0;
00229    fEvtHdr.Set(i, run, date);
00230    if (!fgHist) fgHist = new TH1F("hstat","Event Histogram",100,0,1);
00231    fH = fgHist;
00232    fH->Fill(random);
00233 }
00234 
00235 //______________________________________________________________________________
00236 void Event::SetMeasure(UChar_t which, Int_t what) {
00237    if (which<10) fMeasures[which] = what;
00238 }
00239 
00240 //______________________________________________________________________________
00241 void Event::SetRandomVertex() {
00242    // This delete is to test the relocation of variable length array
00243    if (fClosestDistance) delete [] fClosestDistance;
00244    if (!fNvertex) {
00245       fClosestDistance = 0;
00246       return;
00247    }
00248    fClosestDistance = new Double32_t[fNvertex];
00249    for (Int_t k = 0; k < fNvertex; k++ ) {
00250       fClosestDistance[k] = gRandom->Gaus(1,1);
00251    }
00252 }
00253 
00254 //______________________________________________________________________________
00255 Track::Track(const Track &orig) : TObject(orig)
00256 {
00257    // Copy a track object
00258 
00259    fPx = orig.fPx;
00260    fPy = orig.fPy;
00261    fPz = orig.fPx; 
00262    fRandom = orig.fRandom;
00263    fMass2 = orig.fMass2;
00264    fBx = orig.fBx;
00265    fBy = orig.fBy;
00266    fMeanCharge = orig.fMeanCharge;
00267    fXfirst = orig.fXfirst;
00268    fXlast  = orig.fXlast;
00269    fYfirst = orig.fYfirst;
00270    fYlast  = orig.fYlast;
00271    fZfirst = orig.fZfirst;
00272    fZlast  = orig.fZlast;
00273    fCharge = orig.fCharge;
00274 
00275    fVertex[0] = orig.fVertex[0];
00276    fVertex[1] = orig.fVertex[1];
00277    fVertex[2] = orig.fVertex[2];
00278    fNpoint = orig.fNpoint;
00279    fNsp = orig.fNsp;
00280    if (fNsp) {
00281       fPointValue = new Double32_t[fNsp];
00282       for(int i=0; i<fNsp; i++) {
00283          fPointValue[i] = orig.fPointValue[i];
00284       }
00285    } else {
00286       fPointValue = 0;
00287    }
00288    fValid  = orig.fValid;
00289 
00290    fTriggerBits = orig.fTriggerBits;
00291 
00292 }
00293 
00294 //______________________________________________________________________________
00295 Track::Track(Float_t random) : TObject(),fTriggerBits(64)
00296 {
00297    // Create a track object.
00298    // Note that in this example, data members do not have any physical meaning.
00299 
00300    Float_t a,b,px,py;
00301    gRandom->Rannor(px,py);
00302    fPx = px;
00303    fPy = py;
00304    fPz = TMath::Sqrt(px*px+py*py);
00305    fRandom = 1000*random;
00306    if (fRandom < 10) fMass2 = 0.106;
00307    else if (fRandom < 100) fMass2 = 0.8;
00308    else if (fRandom < 500) fMass2 = 4.5;
00309    else if (fRandom < 900) fMass2 = 8.9;
00310    else  fMass2 = 9.8;
00311    gRandom->Rannor(a,b);
00312    fBx = 0.1*a;
00313    fBy = 0.1*b;
00314    fMeanCharge = 0.01*gRandom->Rndm(1);
00315    gRandom->Rannor(a,b);
00316    fXfirst = a*10;
00317    fXlast  = b*10;
00318    gRandom->Rannor(a,b);
00319    fYfirst = a*12;
00320    fYlast  = b*16;
00321    gRandom->Rannor(a,b);
00322    fZfirst = 50 + 5*a;
00323    fZlast  = 200 + 10*b;
00324    fCharge = Double32_t(Int_t(3*gRandom->Rndm(1)) - 1);
00325 
00326    fTriggerBits.SetBitNumber((UInt_t)(64*gRandom->Rndm(1)));
00327    fTriggerBits.SetBitNumber((UInt_t)(64*gRandom->Rndm(1)));
00328    fTriggerBits.SetBitNumber((UInt_t)(64*gRandom->Rndm(1)));
00329 
00330    fVertex[0] = gRandom->Gaus(0,0.1);
00331    fVertex[1] = gRandom->Gaus(0,0.2);
00332    fVertex[2] = gRandom->Gaus(0,10);
00333    fNpoint = Int_t(60+10*gRandom->Rndm(1));
00334    fNsp = Int_t(3*gRandom->Rndm(1));
00335    if (fNsp) {
00336       fPointValue = new Double32_t[fNsp];
00337       for(int i=0; i<fNsp; i++) {
00338          fPointValue[i] = i+1;
00339       }
00340    } else {
00341       fPointValue = 0;
00342    }
00343    fValid  = Int_t(0.6+gRandom->Rndm(1));
00344 }
00345 
00346 //______________________________________________________________________________
00347 void Track::Clear(Option_t * /*option*/)
00348 {
00349    fTriggerBits.Clear(); 
00350    delete [] fPointValue; 
00351    fPointValue=0; 
00352 }
00353 
00354 //______________________________________________________________________________
00355 HistogramManager::HistogramManager(TDirectory *dir)
00356 {
00357    // Create histogram manager object. Histograms will be created
00358    // in the "dir" directory.
00359 
00360    // Save current directory and cd to "dir".
00361    TDirectory *saved = gDirectory;
00362    dir->cd();
00363 
00364    fNtrack      = new TH1F("hNtrack",    "Ntrack",100,575,625);
00365    fNseg        = new TH1F("hNseg",      "Nseg",100,5800,6200);
00366    fTemperature = new TH1F("hTemperature","Temperature",100,19.5,20.5);
00367    fPx          = new TH1F("hPx",        "Px",100,-4,4);
00368    fPy          = new TH1F("hPy",        "Py",100,-4,4);
00369    fPz          = new TH1F("hPz",        "Pz",100,0,5);
00370    fRandom      = new TH1F("hRandom",    "Random",100,0,1000);
00371    fMass2       = new TH1F("hMass2",     "Mass2",100,0,12);
00372    fBx          = new TH1F("hBx",        "Bx",100,-0.5,0.5);
00373    fBy          = new TH1F("hBy",        "By",100,-0.5,0.5);
00374    fMeanCharge  = new TH1F("hMeanCharge","MeanCharge",100,0,0.01);
00375    fXfirst      = new TH1F("hXfirst",    "Xfirst",100,-40,40);
00376    fXlast       = new TH1F("hXlast",     "Xlast",100,-40,40);
00377    fYfirst      = new TH1F("hYfirst",    "Yfirst",100,-40,40);
00378    fYlast       = new TH1F("hYlast",     "Ylast",100,-40,40);
00379    fZfirst      = new TH1F("hZfirst",    "Zfirst",100,0,80);
00380    fZlast       = new TH1F("hZlast",     "Zlast",100,0,250);
00381    fCharge      = new TH1F("hCharge",    "Charge",100,-1.5,1.5);
00382    fNpoint      = new TH1F("hNpoint",    "Npoint",100,50,80);
00383    fValid       = new TH1F("hValid",     "Valid",100,0,1.2);
00384 
00385    // cd back to original directory
00386    saved->cd();
00387 }
00388 
00389 //______________________________________________________________________________
00390 HistogramManager::~HistogramManager()
00391 {
00392    // Clean up all histograms.
00393 
00394    // Nothing to do. Histograms will be deleted when the directory
00395    // in which tey are stored is closed.
00396 }
00397 
00398 //______________________________________________________________________________
00399 void HistogramManager::Hfill(Event *event)
00400 {
00401    // Fill histograms.
00402 
00403    fNtrack->Fill(event->GetNtrack());
00404    fNseg->Fill(event->GetNseg());
00405    fTemperature->Fill(event->GetTemperature());
00406 
00407    for (Int_t itrack = 0; itrack < event->GetNtrack(); itrack++) {
00408       Track *track = (Track*)event->GetTracks()->UncheckedAt(itrack);
00409       fPx->Fill(track->GetPx());
00410       fPy->Fill(track->GetPy());
00411       fPz->Fill(track->GetPz());
00412       fRandom->Fill(track->GetRandom());
00413       fMass2->Fill(track->GetMass2());
00414       fBx->Fill(track->GetBx());
00415       fBy->Fill(track->GetBy());
00416       fMeanCharge->Fill(track->GetMeanCharge());
00417       fXfirst->Fill(track->GetXfirst());
00418       fXlast->Fill(track->GetXlast());
00419       fYfirst->Fill(track->GetYfirst());
00420       fYlast->Fill(track->GetYlast());
00421       fZfirst->Fill(track->GetZfirst());
00422       fZlast->Fill(track->GetZlast());
00423       fCharge->Fill(track->GetCharge());
00424       fNpoint->Fill(track->GetNpoint());
00425       fValid->Fill(track->GetValid());
00426    }
00427 }

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