]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ALIFAST/AliFast.cxx
Fix coding convention violation
[u/mrichter/AliRoot.git] / ALIFAST / AliFast.cxx
1
2 //////////////////////////////////////////////////////////////////////////
3 //                                                                      //
4 // AliFast                                                              //
5 //                                                                      //
6 // Main class to control the AliFast program.                           //
7 //                                                                      //
8 // This class is a general framework for programs that needs to:        //
9 //    - Initialise some parameters                                      //
10 //    - Loop on events                                                  //
11 //    - Print results and save histograms, etc                          //
12 //                                                                      //
13 // The event processor AliFast::Make loops on a list of Makers          //
14 // where each maker performs some task on the event data and generates  //
15 // results.                                                             //
16 // New Makers can be inserted by a user without modifying this class.   //
17 // Note that the order in which the Makers are called is the order      //
18 // of insertion in the list of Makers.                                  //
19 // Each Maker is responsible for creating its branch of the Tree.       //
20 // The following table shows the list of makers currently implemented   //
21 // The default option to Save the Maker info in the Tree is mentioned.  //
22 //                                                                      //
23 //    Maker name        Save in Tree                                    //
24 //    ==========        ============                                    //
25 //    MCMaker             NO                                            //
26 //    TrackMaker          NO                                            //
27 //                                                                      //
28 // Makers must derive from the base class AliFMaker.                    //
29 // AliFMaker provides a common interface to all Makers.                 //
30 // Each Maker is responsible for defining its own parameters and        //
31 // histograms.                                                          //
32 // Each Maker has its own list of histograms.                           //
33 // Each Maker has an associated companion class corresponding to the    //
34 // type of physics object reconstructed by the Maker.                   //
35 // For example, AliFClusterMaker creates AliFCluster objects.           //
36 //              AliFTriggerMaker creates one single AliFTrigger object. //
37 // The pointer supporting the created object(s) is defined in AliFMaker //
38 //   fFruits may point to a single object (eg. AliFTrigger) or to a    //
39 //           TClonesArray of objects (eg. AliFCluster).                 //
40 //                                                                      //
41 // The function AliFast::Maketree must be called after the creation     //
42 // of the AliFast object to create a Root Tree.                         //
43 //                                                                      //
44 // An example of main program/macro to use AliFast is given below:      //
45 //========================================================================
46 //void umain(Int_t nevents=100)
47 //{
48 //   gROOT->Reset();
49 //   gSystem->Load("libalifast.so");  // dynamically link the compiled shared library
50 //
51 //   // Open the root output file
52 //   TFile file("alifast.root","recreate","AliFast root file",2);
53 //   
54 //   AliFast alifast("alifast");     // create main object to run alifast
55 //
56 //   User user;           // create an object of the User class defined in user.C
57 //
58 //   alifast.Init();      // Initialise event (maker histograms,etc)
59 //   alifast.MakeTree();  // Create the Root tree
60 //
61 //   gROOT->LoadMacro("user.C");  // compile/interpret user file
62 //
63 //   for (Int_t i=0; i<nevents; i++) {
64 //      if (i%100 == 0) printf("In loop:%d\n",i);
65 //      alifast.Make(i);       // Generate and reconstruct event
66 //      user.FillHistograms(); // User has possibility to decide if store event here!
67 //      alifast.FillTree();
68 //      alifast.Clear();       // Clear all event lists
69 //   }
70 //   alifast.Finish();
71 //
72 //   // save objects in Root file
73 //   alifast.Write();  //save main alifast object (and run parameters)
74 //}
75 //========================================================================
76 //                                                                      //
77 // This example illustrates how to:                                     //
78 //    - Load a shared library                                           //
79 //    - Open a Root file                                                //
80 //    - Initialise AliFast                                              //
81 //    - Load some user code (interpreted)                               //
82 //      This user code may redefine some Maker parameters               //
83 //    - Make a loop on events                                           //
84 //    - Save histograms and the main AliFast object and its Makers      //
85 //                                                                      //
86 //========================================================================
87 //  An example of a User class is given below:                          //
88 //========================================================================
89 //
90 //#ifndef user_H
91 //#define user_H
92 //
93 //////////////////////////////////////////////////////////////////////////
94 //                                                                      //
95 // User                                                                 //
96 //                                                                      //
97 // Example of a user class to perform user specific tasks when running  //
98 // the ALIfast program.                                                 //
99 //                                                                      //
100 // This class illustrates:                                              //
101 //   - How to set run parameters                                        //
102 //   - How to create and fill histograms                                //
103 //                                                                      //
104 //////////////////////////////////////////////////////////////////////////
105 //
106 //class TH1F;
107 //class AliFast;
108 //class AliFClusterMaker;
109 //class AliFPhotonMaker;
110 //
111 //class User {
112 //
113 //private:
114 //   TH1F             *fhist1;       //pointer to histogram
115 //   TH1F             *fhist2;       //pointer to histogram
116 //   TH1F             *fhist3;       //pointer to histogram
117 //public:
118 //               User();
119 //   void        FillHistograms();
120 //   void        SetRunParameters();
121 //
122 //#endif
123 //};
124 //
125 //_________________________________________________________________________
126 //User::User() 
127 //{
128 //   SetRunParameters();  //change default parameters
129 //
130 //         Create a few histograms
131 //   fhist1 = new TH1F("hist1","Number of tracks per event",100,0,100);
132 //   fhist2 = new TH1F("hist2","Number of clusters",100,0,100);
133 //   fhist3 = new TH1F("hist3","Number of isolated muons",20,0,20);
134 //}
135 //
136 //_________________________________________________________________________
137 //void User::FillHistograms()
138 //{
139 ////   fhist1.Fill(event->GetNtracks());
140 ////   fhist2.Fill(event->GetNclusters));
141 ////   fhist3.Fill(event->GetNIsoMuons());
142 //}
143 //
144 //_________________________________________________________________________
145 //void User::SetRunParameters()
146 //{
147 //  // change Alifast default parameters
148 //
149 //   gAliFast->SetSmearMuonOpt(0);
150 //   gAliFast->ClusterMaker()->SetGranBarrelEta(0.12);
151 //   gAliFast->PhotonMaker()->SetMinPT(6.);
152 //   gAliFast->TriggerMaker()->SetMuoEtaCoverage(2.8);
153 //
154 //}
155 //======================end of User class=================================
156 //
157 //////////////////////////////////////////////////////////////////////////
158
159 //class TChain;
160
161 #include <TBrowser.h>
162 #include <TChain.h>
163 #include <TROOT.h>
164 #include <TTree.h>
165
166 #include "AliFast.h"
167 #include "AliFTrackMaker.h"
168
169
170 R__EXTERN AliRun * gAlice;
171
172 AliFast *gAliFast;
173
174 ClassImp(AliFast)
175
176
177 //_____________________________________________________________________________
178 AliFast::AliFast(): 
179   AliRun("alifast","The ALICE fast simulation"),
180   fVersion(0),
181   fVersionDate(0),
182   fMode(0),
183   fTestTrack(0),
184   fTree(0),
185   fMakers(0),
186   fMCMaker(0),
187   fTrackMaker(0),
188   fDet(new AliFDet("Detector","Make AliFast detector")),
189   fLuminosity(0),
190   fBfield(0),
191   fSmearing(0),
192   fSUSYcodeLSP(0),
193   fTrackFinding(0),
194   fFDisplay(0)
195 {
196   //
197   // Default constructor
198   //
199
200    gAliFast     = this;
201    gAlice       = (AliRun*)this;
202 }
203
204 //_____________________________________________________________________________
205 AliFast::AliFast(const char *name, const char *title): 
206   AliRun(name,title),
207   fVersion(1), //AliFAST version number and release date
208   fVersionDate(150399),
209   fMode(0),
210   fTestTrack(0),
211   fTree(0),
212   fMakers(new TList()), // support list for the lists of AliFast objects
213   fMCMaker(0),
214   fTrackMaker(new AliFTrackMaker("TrackMaker","Make AliFast tracks")),
215   fDet(new AliFDet("Detector","Make AliFast detector")), // create detectors
216   fLuminosity(0),
217   fBfield(0),
218   fSmearing(0),
219   fSUSYcodeLSP(0),
220   fTrackFinding(0),
221   fFDisplay(0)
222 {
223   //
224   // Standard constructor
225   //
226
227    gAliFast      = this;
228    
229    SetDefaultParameters();
230
231    gROOT->GetListOfBrowsables()->Add(this,"AliFast");
232
233    // create "standard" makers and add them to the list of makers 
234    // (in AliFMaker constructor)
235    // Note that the order in which makers are added to the list of makers 
236    // is important makers will be processed in this order !!
237
238    //fMCMaker       = new AliFMCMaker("MCMaker","Make MC events");
239
240 }
241
242 //_______________________________________________________________________
243 AliFast::AliFast(const AliFast& afast):
244   AliRun(afast),
245   fVersion(0),
246   fVersionDate(0),
247   fMode(0),
248   fTestTrack(0),
249   fTree(0),
250   fMakers(0),
251   fMCMaker(0),
252   fTrackMaker(0),
253   fDet(0),
254   fLuminosity(0),
255   fBfield(0),
256   fSmearing(0),
257   fSUSYcodeLSP(0),
258   fTrackFinding(0),
259   fFDisplay(0)
260 {
261   //
262   // Copy constructor for AliRun
263   //
264   afast.Copy(*this);
265 }
266
267 //_____________________________________________________________________________
268 AliFast::~AliFast()
269 {
270   //
271   // Destructor
272   //
273 //   fMakers->Delete();
274 //   delete fMakers;
275 }
276
277
278 //______________________________________________________________________________
279 void AliFast::Browse(TBrowser *b)
280 {
281   //
282   // To allow browsing of the class
283   //
284
285   if( b == 0) return;
286
287   if (fTree) b->Add(fTree,fTree->GetName());
288   // fca
289   b->Add(&fHistBrowser, "Histograms");
290   b->Add(&fBigBang, "BigBang");
291   // fca
292
293   TIter next(fMakers);
294   AliFMaker *maker;
295   while ((maker = (AliFMaker*)next())) {
296      b->Add(maker,maker->GetName());
297    }
298 }
299
300 //_____________________________________________________________________________
301 void AliFast::Clear(Option_t *option)
302 {
303   //
304   //    Reset lists of event objects
305   //
306    TIter next(fMakers);
307    AliFMaker *maker;
308    while ((maker = (AliFMaker*)next())) {
309       maker->Clear(option);
310    }
311 }
312
313 //_____________________________________________________________________________
314 void AliFast::Draw(Option_t */*option*/) const
315 {
316   //
317   //    Insert current event in graphics pad list
318   // Check if the Event Display object has been created
319   //
320    if (!fFDisplay) {
321       Error("Draw","You must create an AliFDisplay object first");
322       return;
323    }
324 }
325
326 //_____________________________________________________________________________
327 void  AliFast::GetTreeEvent(Int_t event)
328 {
329   //
330   //    Read event from Tree
331   //
332    if (fTree) fTree->GetEvent(event);
333    fEvent = event;  
334 }
335
336 //_____________________________________________________________________________
337 void AliFast::Init()
338 {
339   //
340   //  Initialise detector
341   //
342    AliFDet *detector=gAliFast->Detector();  
343    detector->InitDetParam(); 
344
345 //    Initialise makers
346    TIter next(fMakers);
347    AliFMaker *maker;
348    TObject *objfirst, *objlast;
349    while ((maker = (AliFMaker*)next())) {
350      // save last created histogram in current Root directory
351       objlast = gDirectory->GetList()->Last();
352
353      // Initialise maker
354       maker->Init();
355
356      // Add Maker histograms in Maker list of histograms
357       if (objlast) objfirst = gDirectory->GetList()->After(objlast);
358       else         objfirst = gDirectory->GetList()->First();
359       while (objfirst) {
360          maker->Histograms()->Add(objfirst);
361          objfirst = gDirectory->GetList()->After(objfirst);
362       }
363    }
364
365 }
366
367 //_____________________________________________________________________________
368 void AliFast::Paint(Option_t */*option*/)
369 {
370   //
371   //    Paint AliFast objects
372   //
373 }
374
375 //_____________________________________________________________________________
376 void AliFast::PrintInfo()
377 {
378   //
379   //     Gives information about versions etc.
380   //
381    printf("\n\n");
382    printf("**************************************************************\n");
383    printf("*             AliFast version:00%1d     last update  %6d    *\n",
384                                                        fVersion, fVersionDate);
385    printf("**************************************************************\n");
386    printf("*                                                            *\n");
387    printf("*     Simulates and reconstructs  events on particle level   *\n");
388    printf("*     Package by: Yiota Foka and Elzbieta Richter-Was        *\n");
389    printf("*           Design based on ATLFast++                        *\n");
390    printf("*         by R. Brun and E. Richter-Was                      *\n");
391    printf("**************************************************************\n");
392    printf("\n\n");
393
394 //     Print info for detector geometry
395    AliFDet *detector=gAliFast->Detector();
396    detector->PrintDetInfo(); 
397
398 //     Print info for all defined Makers
399    TIter next(fMakers);
400    AliFMaker *maker;
401    while ((maker = (AliFMaker*)next())) {
402       maker->PrintInfo();
403    }
404 }
405
406 //_____________________________________________________________________________
407 void AliFast::FillTree()
408 {
409   //
410   //  Fill the ROOT tree, looping on all active branches
411   //
412   // Clean generated particles (depending on option Save)
413   // MCMaker()->CleanParticles();
414   //
415
416   // Now ready to fill the Root Tree
417    if(fTree) fTree->Fill();
418 }
419
420 //_____________________________________________________________________________
421 void AliFast::InitChain(TChain *chain)
422 {
423   //
424   //  Initialize branch addresses for all makers in a TChain
425   //
426
427    if (chain == 0) return;
428
429    fTree = chain;
430
431    TIter next(fMakers);
432    AliFMaker *maker;
433    while ((maker = (AliFMaker*)next())) {
434       maker->SetChainAddress(chain);
435    }
436 }
437
438 //_____________________________________________________________________________
439 void AliFast::MakeTree(const char* name, const char*title)
440 {
441   //
442   //  Create a ROOT tree
443   //  Loop on all makers to create the Root branch (if any)
444   //
445
446    if (fTree) return;
447
448    fTree = new TTree(name,title);
449
450    TIter next(fMakers);
451    AliFMaker *maker;
452    while ((maker = (AliFMaker*)next())) {
453       maker->MakeBranch();
454    }
455 }
456
457 //_____________________________________________________________________________
458 void AliFast::SetDefaultParameters()
459 {
460   //
461   //    Setters for flags and switches
462   //
463    SetLuminosity();
464    SetBfield();
465    SetSmearing();
466    SetSUSYcodeLSP();
467    SetTrackFinding();
468 }
469
470 //_____________________________________________________________________________
471 void AliFast::Make(Int_t i)
472 {
473   //
474   // Main maker method
475   //
476    fEvent = i;
477
478 //   Loop on all makers
479    TIter next(fMakers);
480    AliFMaker *maker;
481    while ((maker = (AliFMaker*)next())) {
482       maker->Make();
483    }
484
485 }
486
487 //_____________________________________________________________________________
488 void AliFast::FillClone()
489 {
490   //
491   // Fill Makers fruits clones
492   //
493    
494    TIter next(fMakers);
495    AliFMaker *maker;
496    while ((maker = (AliFMaker*)next())) {
497       maker->FillClone();
498    }
499 }
500
501 //_____________________________________________________________________________
502 void AliFast::Finish()
503 {
504   //
505   //    Terminate a run
506   //   place to make operations on histograms, normalization,etc.
507   //
508
509    TIter next(fMakers);
510    AliFMaker *maker;
511    while ((maker = (AliFMaker*)next())) {
512       maker->Finish();
513    }
514 }
515
516 //_____________________________________________________________________________
517 void AliFast::SortDown(Int_t n1, Float_t *a, Int_t *index, Bool_t down) const
518 {
519 //  sort the n1 elements of array a.
520 //  In output the array index contains the indices of the sorted array.
521 //  if down is false sort in increasing order (default is decreasing order)
522 //   This is a translation of the CERNLIB routine sortzv (M101)
523 //   based on the quicksort algorithm
524
525    Int_t i,i1,n,i2,i3,i33,i222,iswap,n2;
526    Int_t i22 = 0;
527    Float_t ai;
528    n = n1;
529    if (n <= 0) return;
530    if (n == 1) {index[0] = 0; return;}
531    for (i=0;i<n;i++) index[i] = i+1;
532    for (i1=2;i1<=n;i1++) {
533       i3 = i1;
534       i33 = index[i3-1];
535       ai  = a[i33-1];
536       while(1) {
537          i2 = i3/2;
538          if (i2 <= 0) break;
539          i22 = index[i2-1];
540          if (ai <= a[i22-1]) break;
541          index[i3-1] = i22;
542          i3 = i2;
543       }
544       index[i3-1] = i33;
545    }
546
547    while(1) {
548       i3 = index[n-1];
549       index[n-1] = index[0];
550       ai = a[i3-1];
551       n--;
552       if(n-1 < 0) {index[0] = i3; break;}
553       i1 = 1;
554       while(2) {
555          i2 = i1+i1;
556          if (i2 <= n) i22 = index[i2-1];
557          if (i2-n > 0) {index[i1-1] = i3; break;}
558          if (i2-n < 0) {
559             i222 = index[i2];
560             if (a[i22-1] - a[i222-1] < 0) {
561                 i2++;
562                 i22 = i222;
563             }
564          }
565          if (ai - a[i22-1] > 0) {index[i1-1] = i3; break;}
566          index[i1-1] = i22;
567          i1 = i2;
568       }
569    }
570    if (!down) return;
571    n2 = n1/2;
572    for (i=0;i<n1;i++) index[i]--;
573    for (i=0;i<n2;i++) {
574       iswap         = index[i];
575       index[i]      = index[n1-i-1];
576       index[n1-i-1] = iswap;
577    }
578 }
579 //______________________________________________________________________________
580 void AliFast::Streamer(TBuffer &R__b)
581 {
582   //
583   // Stream an object of class AliFast.
584   //
585    if (R__b.IsReading()) {
586       UInt_t R__s, R__c;
587       if (!gAliFast) gAliFast = this;
588       gROOT->GetListOfBrowsables()->Add(this,"AliFast");
589       
590       Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
591       
592       AliFast::Class()->ReadBuffer(R__b, this, R__v, R__s, R__c);
593
594       fTree = (TTree*)gDirectory->Get("T");
595    } else {
596        AliFast::Class()->WriteBuffer(R__b,this);
597    }
598 }
599
600
601
602
603