]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliRun.cxx
G4 compatibility changes
[u/mrichter/AliRoot.git] / STEER / AliRun.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17 $Log$
18 Revision 1.28  2000/04/05 06:51:06  fca
19 Workaround for an HP compiler problem
20
21 Revision 1.27  2000/03/22 18:08:07  fca
22 Rationalisation of the virtual MC interfaces
23
24 Revision 1.26  2000/03/22 13:42:26  fca
25 SetGenerator does not replace an existing generator, ResetGenerator does
26
27 Revision 1.25  2000/02/23 16:25:22  fca
28 AliVMC and AliGeant3 classes introduced
29 ReadEuclid moved from AliRun to AliModule
30
31 Revision 1.24  2000/01/19 17:17:20  fca
32 Introducing a list of lists of hits -- more hits allowed for detector now
33
34 Revision 1.23  1999/12/03 11:14:31  fca
35 Fixing previous wrong checking
36
37 Revision 1.21  1999/11/25 10:40:08  fca
38 Fixing daughters information also in primary tracks
39
40 Revision 1.20  1999/10/04 18:08:49  fca
41 Adding protection against inconsistent Euclid files
42
43 Revision 1.19  1999/09/29 07:50:40  fca
44 Introduction of the Copyright and cvs Log
45
46 */
47
48 ///////////////////////////////////////////////////////////////////////////////
49 //                                                                           //
50 //  Control class for Alice C++                                              //
51 //  Only one single instance of this class exists.                           //
52 //  The object is created in main program aliroot                            //
53 //  and is pointed by the global gAlice.                                     //
54 //                                                                           //
55 //   -Supports the list of all Alice Detectors (fModules).                 //
56 //   -Supports the list of particles (fParticles).                           //
57 //   -Supports the Trees.                                                    //
58 //   -Supports the geometry.                                                 //
59 //   -Supports the event display.                                            //
60 //Begin_Html
61 /*
62 <img src="picts/AliRunClass.gif">
63 */
64 //End_Html
65 //Begin_Html
66 /*
67 <img src="picts/alirun.gif">
68 */
69 //End_Html
70 //                                                                           //
71 ///////////////////////////////////////////////////////////////////////////////
72
73 #include <TFile.h>
74 #include <TRandom.h>
75 #include <TBRIK.h> 
76 #include <TNode.h> 
77 #include <TCint.h> 
78 #include <TSystem.h>
79 #include <TObjectTable.h>
80
81 #include "TParticle.h"
82 #include "AliRun.h"
83 #include "AliDisplay.h"
84 #include "AliMC.h"
85
86 #include <stdlib.h>
87 #include <stdio.h>
88 #include <string.h>
89  
90 AliRun *gAlice;
91
92 static AliHeader *header;
93
94 ClassImp(AliRun)
95
96 //_____________________________________________________________________________
97 AliRun::AliRun()
98 {
99   //
100   // Default constructor for AliRun
101   //
102   header=&fHeader;
103   fRun       = 0;
104   fEvent     = 0;
105   fCurrent   = -1;
106   fModules = 0;
107   fGenerator = 0;
108   fTreeD     = 0;
109   fTreeK     = 0;
110   fTreeH     = 0;
111   fTreeE     = 0;
112   fTreeR     = 0;
113   fParticles = 0;
114   fGeometry  = 0;
115   fDisplay   = 0;
116   fField     = 0;
117   fMC       = 0;
118   fNdets     = 0;
119   fImedia    = 0;
120   fTrRmax    = 1.e10;
121   fTrZmax    = 1.e10;
122   fInitDone  = kFALSE;
123   fLego      = 0;
124   fPDGDB     = 0;        //Particle factory object!
125   fHitLists  = 0;
126 }
127
128 //_____________________________________________________________________________
129 AliRun::AliRun(const char *name, const char *title)
130   : TNamed(name,title)
131 {
132   //
133   //  Constructor for the main processor.
134   //  Creates the geometry
135   //  Creates the list of Detectors.
136   //  Creates the list of particles.
137   //
138   Int_t i;
139   
140   gAlice     = this;
141   fTreeD     = 0;
142   fTreeK     = 0;
143   fTreeH     = 0;
144   fTreeE     = 0;
145   fTreeR     = 0;
146   fTrRmax    = 1.e10;
147   fTrZmax    = 1.e10;
148   fGenerator = 0;
149   fInitDone  = kFALSE;
150   fLego      = 0;
151   fField     = 0;
152   
153   gROOT->GetListOfBrowsables()->Add(this,name);
154   //
155   // create the support list for the various Detectors
156   fModules = new TObjArray(77);
157   //
158   // Create the TNode geometry for the event display
159   
160   BuildSimpleGeometry();
161   
162
163   fNtrack=0;
164   fHgwmk=0;
165   fCurrent=-1;
166   header=&fHeader;
167   fRun       = 0;
168   fEvent     = 0;
169   //
170   // Create the particle stack
171   fParticles = new TClonesArray("TParticle",100);
172   
173   fDisplay = 0;
174   //
175   // Create default mag field
176   SetField();
177   //
178   fMC      = gMC;
179   //
180   // Prepare the tracking medium lists
181   fImedia = new TArrayI(1000);
182   for(i=0;i<1000;i++) (*fImedia)[i]=-99;
183   //
184   // Make particles
185   fPDGDB     = TDatabasePDG::Instance();        //Particle factory object!
186   //
187   // Create HitLists list
188   fHitLists  = new TList();
189 }
190
191 //_____________________________________________________________________________
192 AliRun::~AliRun()
193 {
194   //
195   // Defaullt AliRun destructor
196   //
197   delete fImedia;
198   delete fField;
199   delete fMC;
200   delete fGeometry;
201   delete fDisplay;
202   delete fGenerator;
203   delete fLego;
204   delete fTreeD;
205   delete fTreeK;
206   delete fTreeH;
207   delete fTreeE;
208   delete fTreeR;
209   if (fModules) {
210     fModules->Delete();
211     delete fModules;
212   }
213   if (fParticles) {
214     fParticles->Delete();
215     delete fParticles;
216   }
217   delete fHitLists;
218 }
219
220 //_____________________________________________________________________________
221 void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
222 {
223   //
224   //  Add a hit to detector id
225   //
226   TObjArray &dets = *fModules;
227   if(dets[id]) ((AliModule*) dets[id])->AddHit(track,vol,hits);
228 }
229
230 //_____________________________________________________________________________
231 void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
232 {
233   //
234   // Add digit to detector id
235   //
236   TObjArray &dets = *fModules;
237   if(dets[id]) ((AliModule*) dets[id])->AddDigit(tracks,digits);
238 }
239
240 //_____________________________________________________________________________
241 void AliRun::Browse(TBrowser *b)
242 {
243   //
244   // Called when the item "Run" is clicked on the left pane
245   // of the Root browser.
246   // It displays the Root Trees and all detectors.
247   //
248   if (fTreeK) b->Add(fTreeK,fTreeK->GetName());
249   if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
250   if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
251   if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
252   if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
253   
254   TIter next(fModules);
255   AliModule *detector;
256   while((detector = (AliModule*)next())) {
257     b->Add(detector,detector->GetName());
258   }
259 }
260
261 //_____________________________________________________________________________
262 void AliRun::Build()
263 {
264   //
265   // Initialize Alice geometry
266   // Dummy routine
267   //
268 }
269  
270 //_____________________________________________________________________________
271 void AliRun::BuildSimpleGeometry()
272 {
273   //
274   // Create a simple TNode geometry used by Root display engine
275   //
276   // Initialise geometry
277   //
278   fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
279   new TMaterial("void","Vacuum",0,0,0);  //Everything is void
280   TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
281   brik->SetVisibility(0);
282   new TNode("alice","alice","S_alice");
283 }
284
285 //_____________________________________________________________________________
286 void AliRun::CleanDetectors()
287 {
288   //
289   // Clean Detectors at the end of event
290   //
291   TIter next(fModules);
292   AliModule *detector;
293   while((detector = (AliModule*)next())) {
294     detector->FinishEvent();
295   }
296 }
297
298 //_____________________________________________________________________________
299 void AliRun::CleanParents()
300 {
301   //
302   // Clean Particles stack.
303   // Set parent/daughter relations
304   //
305   TClonesArray &particles = *(gAlice->Particles());
306   TParticle *part;
307   int i;
308   for(i=0; i<fNtrack; i++) {
309     part = (TParticle *)particles.UncheckedAt(i);
310     if(!part->TestBit(Daughters_Bit)) {
311       part->SetFirstDaughter(-1);
312       part->SetLastDaughter(-1);
313     }
314   }
315 }
316
317 //_____________________________________________________________________________
318 Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
319 {
320   //
321   // Return the distance from the mouse to the AliRun object
322   // Dummy routine
323   //
324   return 9999;
325 }
326
327 //_____________________________________________________________________________
328 void AliRun::DumpPart (Int_t i)
329 {
330   //
331   // Dumps particle i in the stack
332   //
333   TClonesArray &particles = *fParticles;
334   ((TParticle*) particles[i])->Print();
335 }
336
337 //_____________________________________________________________________________
338 void AliRun::DumpPStack ()
339 {
340   //
341   // Dumps the particle stack
342   //
343   TClonesArray &particles = *fParticles;
344   printf(
345          "\n\n=======================================================================\n");
346   for (Int_t i=0;i<fNtrack;i++) 
347     {
348       printf("-> %d ",i); ((TParticle*) particles[i])->Print();
349       printf("--------------------------------------------------------------\n");
350     }
351   printf(
352          "\n=======================================================================\n\n");
353 }
354
355 //_____________________________________________________________________________
356 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
357                       Float_t maxField, char* filename)
358 {
359   //
360   //  Set magnetic field parameters
361   //  type      Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
362   //  version   Magnetic field map version (only 1 active now)
363   //  scale     Scale factor for the magnetic field
364   //  maxField  Maximum value for the magnetic field
365
366   //
367   // --- Sanity check on mag field flags
368   if(type<0 || type > 2) {
369     Warning("SetField",
370             "Invalid magnetic field flag: %5d; Helix tracking chosen instead\n"
371            ,type);
372     type=2;
373   }
374   if(fField) delete fField;
375   if(version==1) {
376     fField = new AliMagFC("Map1"," ",type,version,scale,maxField);
377   } else if(version<=3) {
378     fField = new AliMagFCM("Map2-3",filename,type,version,scale,maxField);
379     fField->ReadField();
380   } else {
381     Warning("SetField","Invalid map %d\n",version);
382   }
383 }
384
385 //_____________________________________________________________________________
386 void AliRun::FillTree()
387 {
388   //
389   // Fills all AliRun TTrees
390   //
391   if (fTreeK) fTreeK->Fill();
392   if (fTreeH) fTreeH->Fill();
393   if (fTreeD) fTreeD->Fill();
394   if (fTreeR) fTreeR->Fill();
395 }
396  
397 //_____________________________________________________________________________
398 void AliRun::FinishPrimary()
399 {
400   //
401   // Called  at the end of each primary track
402   //
403   
404   //  static Int_t count=0;
405   //  const Int_t times=10;
406   // This primary is finished, purify stack
407   PurifyKine();
408
409   // Write out hits if any
410   if (gAlice->TreeH()) {
411     gAlice->TreeH()->Fill();
412   }
413   
414   // Reset Hits info
415   gAlice->ResetHits();
416
417   //
418   //  if(++count%times==1) gObjectTable->Print();
419 }
420
421 //_____________________________________________________________________________
422 void AliRun::FinishEvent()
423 {
424   //
425   // Called at the end of the event.
426   //
427   
428   //Update the energy deposit tables
429   Int_t i;
430   for(i=0;i<fEventEnergy.GetSize();i++) {
431     fSummEnergy[i]+=fEventEnergy[i];
432     fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
433   }
434   fEventEnergy.Reset();
435   
436   // Clean detector information
437   CleanDetectors();
438   
439   // Write out the kinematics
440   if (fTreeK) {
441     CleanParents();
442     fTreeK->Fill();
443   }
444   
445   // Write out the digits
446   if (fTreeD) {
447     fTreeD->Fill();
448     ResetDigits();
449   }
450   
451   // Write out reconstructed clusters  
452   if (fTreeR) {
453     fTreeR->Fill();
454   }
455
456   // Write out the event Header information
457   if (fTreeE) fTreeE->Fill();
458   
459   // Reset stack info
460   ResetStack();
461   
462   // Write Tree headers
463   //  Int_t ievent = fHeader.GetEvent();
464   //  char hname[30];
465   //  sprintf(hname,"TreeK%d",ievent);
466   if (fTreeK) fTreeK->Write();
467   //  sprintf(hname,"TreeH%d",ievent);
468   if (fTreeH) fTreeH->Write();
469   //  sprintf(hname,"TreeD%d",ievent);
470   if (fTreeD) fTreeD->Write();
471   //  sprintf(hname,"TreeR%d",ievent);
472   if (fTreeR) fTreeR->Write();
473
474   ++fEvent;
475 }
476
477 //_____________________________________________________________________________
478 void AliRun::FinishRun()
479 {
480   //
481   // Called at the end of the run.
482   //
483
484   // Clean detector information
485   TIter next(fModules);
486   AliModule *detector;
487   while((detector = (AliModule*)next())) {
488     detector->FinishRun();
489   }
490   
491   //Output energy summary tables
492   EnergySummary();
493   
494   // file is retrieved from whatever tree
495   TFile *File = 0;
496   if (fTreeK) File = fTreeK->GetCurrentFile();
497   if ((!File) && (fTreeH)) File = fTreeH->GetCurrentFile();
498   if ((!File) && (fTreeD)) File = fTreeD->GetCurrentFile();
499   if ((!File) && (fTreeE)) File = fTreeE->GetCurrentFile();
500   if( NULL==File ) {
501     Error("FinishRun","There isn't root file!");
502     exit(1);
503   }
504   File->cd();
505   fTreeE->Write();
506   
507   // Clean tree information
508   delete fTreeK; fTreeK = 0;
509   delete fTreeH; fTreeH = 0;
510   delete fTreeD; fTreeD = 0;
511   delete fTreeR; fTreeR = 0;
512   delete fTreeE; fTreeE = 0;
513   
514   // Write AliRun info and all detectors parameters
515   Write();
516   
517   // Close output file
518   File->Write();
519 }
520
521 //_____________________________________________________________________________
522 void AliRun::FlagTrack(Int_t track)
523 {
524   //
525   // Flags a track and all its family tree to be kept
526   //
527   int curr;
528   TParticle *particle;
529
530   curr=track;
531   while(1) {
532     particle=(TParticle*)fParticles->UncheckedAt(curr);
533     
534     // If the particle is flagged the three from here upward is saved already
535     if(particle->TestBit(Keep_Bit)) return;
536     
537     // Save this particle
538     particle->SetBit(Keep_Bit);
539     
540     // Move to father if any
541     if((curr=particle->GetFirstMother())==-1) return;
542   }
543 }
544  
545 //_____________________________________________________________________________
546 void AliRun::EnergySummary()
547 {
548   //
549   // Print summary of deposited energy
550   //
551
552   Int_t ndep=0;
553   Float_t edtot=0;
554   Float_t ed, ed2;
555   Int_t kn, i, left, j, id;
556   const Float_t zero=0;
557   Int_t ievent=fHeader.GetEvent()+1;
558   //
559   // Energy loss information
560   if(ievent) {
561     printf("***************** Energy Loss Information per event (GEV) *****************\n");
562     for(kn=1;kn<fEventEnergy.GetSize();kn++) {
563       ed=fSummEnergy[kn];
564       if(ed>0) {
565         fEventEnergy[ndep]=kn;
566         if(ievent>1) {
567           ed=ed/ievent;
568           ed2=fSum2Energy[kn];
569           ed2=ed2/ievent;
570           ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,zero))/ed;
571         } else 
572           ed2=99;
573         fSummEnergy[ndep]=ed;
574         fSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,zero));
575         edtot+=ed;
576         ndep++;
577       }
578     }
579     for(kn=0;kn<(ndep-1)/3+1;kn++) {
580       left=ndep-kn*3;
581       for(i=0;i<(3<left?3:left);i++) {
582         j=kn*3+i;
583         id=Int_t (fEventEnergy[j]+0.1);
584         printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
585       }
586       printf("\n");
587     }
588     //
589     // Relative energy loss in different detectors
590     printf("******************** Relative Energy Loss per event ********************\n");
591     printf("Total energy loss per event %10.3f GeV\n",edtot);
592     for(kn=0;kn<(ndep-1)/5+1;kn++) {
593       left=ndep-kn*5;
594       for(i=0;i<(5<left?5:left);i++) {
595         j=kn*5+i;
596         id=Int_t (fEventEnergy[j]+0.1);
597         printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
598       }
599       printf("\n");
600     }
601     for(kn=0;kn<75;kn++) printf("*"); 
602     printf("\n");
603   }
604   //
605   // Reset the TArray's
606   //  fEventEnergy.Set(0);
607   //  fSummEnergy.Set(0);
608   //  fSum2Energy.Set(0);
609 }
610
611 //_____________________________________________________________________________
612 AliModule *AliRun::GetModule(const char *name)
613 {
614   //
615   // Return pointer to detector from name
616   //
617   return (AliModule*)fModules->FindObject(name);
618 }
619  
620 //_____________________________________________________________________________
621 AliDetector *AliRun::GetDetector(const char *name)
622 {
623   //
624   // Return pointer to detector from name
625   //
626   return (AliDetector*)fModules->FindObject(name);
627 }
628  
629 //_____________________________________________________________________________
630 Int_t AliRun::GetModuleID(const char *name)
631 {
632   //
633   // Return galice internal detector identifier from name
634   //
635   Int_t i=-1;
636   TObject *mod=fModules->FindObject(name);
637   if(mod) i=fModules->IndexOf(mod);
638   return i;
639 }
640  
641 //_____________________________________________________________________________
642 Int_t AliRun::GetEvent(Int_t event)
643 {
644   //
645   // Connect the Trees Kinematics and Hits for event # event
646   // Set branch addresses
647   //
648
649   // Reset existing structures
650   ResetStack();
651   ResetHits();
652   ResetDigits();
653   
654   // Delete Trees already connected
655   if (fTreeK) delete fTreeK;
656   if (fTreeH) delete fTreeH;
657   if (fTreeD) delete fTreeD;
658   if (fTreeR) delete fTreeR;
659
660   // Get header from file
661   if(fTreeE) fTreeE->GetEntry(event);
662   else Error("GetEvent","Cannot file Header Tree\n");
663   
664   // Get Kine Tree from file
665   char treeName[20];
666   sprintf(treeName,"TreeK%d",event);
667   fTreeK = (TTree*)gDirectory->Get(treeName);
668   if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticles);
669   else    Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
670   
671   // Get Hits Tree header from file
672   sprintf(treeName,"TreeH%d",event);
673   fTreeH = (TTree*)gDirectory->Get(treeName);
674   if (!fTreeH) {
675     Error("GetEvent","cannot find Hits Tree for event:%d\n",event);
676   }
677   
678   // Get Digits Tree header from file
679   sprintf(treeName,"TreeD%d",event);
680   fTreeD = (TTree*)gDirectory->Get(treeName);
681   if (!fTreeD) {
682     Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
683   }
684   
685   
686   // Get Reconstruct Tree header from file
687   sprintf(treeName,"TreeR%d",event);
688   fTreeR = (TTree*)gDirectory->Get(treeName);
689   if (!fTreeR) {
690     //    printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
691   }
692  
693   // Set Trees branch addresses
694   TIter next(fModules);
695   AliModule *detector;
696   while((detector = (AliModule*)next())) {
697     detector->SetTreeAddress();
698   }
699   
700   if (fTreeK) fTreeK->GetEvent(0);
701   fNtrack = Int_t (fParticles->GetEntries());
702   return fNtrack;
703 }
704
705 //_____________________________________________________________________________
706 TGeometry *AliRun::GetGeometry()
707 {
708   //
709   // Import Alice geometry from current file
710   // Return pointer to geometry object
711   //
712   if (!fGeometry) fGeometry = (TGeometry*)gDirectory->Get("AliceGeom");
713   //
714   // Unlink and relink nodes in detectors
715   // This is bad and there must be a better way...
716   //
717   
718   TIter next(fModules);
719   AliModule *detector;
720   while((detector = (AliModule*)next())) {
721     detector->SetTreeAddress();
722     TList *dnodes=detector->Nodes();
723     Int_t j;
724     TNode *node, *node1;
725     for ( j=0; j<dnodes->GetSize(); j++) {
726       node = (TNode*) dnodes->At(j);
727       node1 = fGeometry->GetNode(node->GetName());
728       dnodes->Remove(node);
729       dnodes->AddAt(node1,j);
730     }
731   }
732   return fGeometry;
733 }
734
735 //_____________________________________________________________________________
736 void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
737                           Float_t &e, Float_t *vpos, Float_t *polar,
738                           Float_t &tof)
739 {
740   //
741   // Return next track from stack of particles
742   //
743   TVector3 pol;
744   fCurrent=-1;
745   TParticle *track;
746   for(Int_t i=fNtrack-1; i>=0; i--) {
747     track=(TParticle*) fParticles->UncheckedAt(i);
748     if(!track->TestBit(Done_Bit)) {
749       //
750       // The track has not yet been processed
751       fCurrent=i;
752       ipart=track->GetPdgCode();
753       pmom[0]=track->Px();
754       pmom[1]=track->Py(); 
755       pmom[2]=track->Pz();
756       e     =track->Energy();
757       vpos[0]=track->Vx();
758       vpos[1]=track->Vy();
759       vpos[2]=track->Vz();
760       track->GetPolarisation(pol);
761       polar[0]=pol.X();
762       polar[1]=pol.Y();
763       polar[2]=pol.Z();
764       tof=track->T();
765       track->SetBit(Done_Bit);
766       break;
767     }
768   }
769   mtrack=fCurrent;
770   //
771   // stop and start timer when we start a primary track
772   Int_t nprimaries = fHeader.GetNprimary();
773   if (fCurrent >= nprimaries) return;
774   if (fCurrent < nprimaries-1) {
775     fTimer.Stop();
776     track=(TParticle*) fParticles->UncheckedAt(fCurrent+1);
777     //    track->SetProcessTime(fTimer.CpuTime());
778   }
779   fTimer.Start();
780 }
781
782 //_____________________________________________________________________________
783 Int_t AliRun::GetPrimary(Int_t track)
784 {
785   //
786   // return number of primary that has generated track
787   //
788   int current, parent;
789   TParticle *part;
790   //
791   parent=track;
792   while (1) {
793     current=parent;
794     part = (TParticle *)fParticles->UncheckedAt(current);
795     parent=part->GetFirstMother();
796     if(parent<0) return current;
797   }
798 }
799  
800 //_____________________________________________________________________________
801 void AliRun::InitMC(const char *setup)
802 {
803   //
804   // Initialize the Alice setup
805   //
806
807   gROOT->LoadMacro(setup);
808   gInterpreter->ProcessLine("Config();");
809
810   gMC->DefineParticles();  //Create standard MC particles
811
812   TObject *objfirst, *objlast;
813
814   fNdets = fModules->GetLast()+1;
815
816   //
817   //=================Create Materials and geometry
818   gMC->Init();
819
820    TIter next(fModules);
821    AliModule *detector;
822    while((detector = (AliModule*)next())) {
823       detector->SetTreeAddress();
824       objlast = gDirectory->GetList()->Last();
825       
826       // Add Detector histograms in Detector list of histograms
827       if (objlast) objfirst = gDirectory->GetList()->After(objlast);
828       else         objfirst = gDirectory->GetList()->First();
829       while (objfirst) {
830         detector->Histograms()->Add(objfirst);
831         objfirst = gDirectory->GetList()->After(objfirst);
832       }
833    }
834    SetTransPar(); //Read the cuts for all materials
835    
836    MediaTable(); //Build the special IMEDIA table
837    
838    //Initialise geometry deposition table
839    fEventEnergy.Set(gMC->NofVolumes()+1);
840    fSummEnergy.Set(gMC->NofVolumes()+1);
841    fSum2Energy.Set(gMC->NofVolumes()+1);
842    
843    //Compute cross-sections
844    gMC->BuildPhysics();
845    
846    //Write Geometry object to current file.
847    fGeometry->Write();
848    
849    fInitDone = kTRUE;
850 }
851
852 //_____________________________________________________________________________
853 void AliRun::MediaTable()
854 {
855   //
856   // Built media table to get from the media number to
857   // the detector id
858   //
859   Int_t kz, nz, idt, lz, i, k, ind;
860   //  Int_t ibeg;
861   TObjArray &dets = *gAlice->Detectors();
862   AliModule *det;
863   //
864   // For all detectors
865   for (kz=0;kz<fNdets;kz++) {
866     // If detector is defined
867     if((det=(AliModule*) dets[kz])) {
868         TArrayI &idtmed = *(det->GetIdtmed()); 
869         for(nz=0;nz<100;nz++) {
870         // Find max and min material number
871         if((idt=idtmed[nz])) {
872           det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
873           det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
874         }
875       }
876       if(det->LoMedium() > det->HiMedium()) {
877         det->LoMedium() = 0;
878         det->HiMedium() = 0;
879       } else {
880         if(det->HiMedium() > fImedia->GetSize()) {
881           Error("MediaTable","Increase fImedia from %d to %d",
882                 fImedia->GetSize(),det->HiMedium());
883           return;
884         }
885         // Tag all materials in rage as belonging to detector kz
886         for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
887           (*fImedia)[lz]=kz;
888         }
889       }
890     }
891   }
892   //
893   // Print summary table
894   printf(" Traking media ranges:\n");
895   for(i=0;i<(fNdets-1)/6+1;i++) {
896     for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
897       ind=i*6+k;
898       det=(AliModule*)dets[ind];
899       if(det)
900         printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
901                det->HiMedium());
902       else
903         printf(" %6s: %3d -> %3d;","NULL",0,0);
904     }
905     printf("\n");
906   }
907 }
908
909 //____________________________________________________________________________
910 void AliRun::SetGenerator(AliGenerator *generator)
911 {
912   //
913   // Load the event generator
914   //
915   if(!fGenerator) fGenerator = generator;
916 }
917
918 //____________________________________________________________________________
919 void AliRun::ResetGenerator(AliGenerator *generator)
920 {
921   //
922   // Load the event generator
923   //
924   if(fGenerator)
925     Warning("ResetGenerator","Replacing generator %s with %s\n",
926             fGenerator->GetName(),generator->GetName());
927   fGenerator = generator;
928 }
929
930 //____________________________________________________________________________
931 void AliRun::SetTransPar(char* filename)
932 {
933   //
934   // Read filename to set the transport parameters
935   //
936
937
938   const Int_t ncuts=10;
939   const Int_t nflags=11;
940   const Int_t npars=ncuts+nflags;
941   const char pars[npars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
942                                "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
943                                "BREM","COMP","DCAY","DRAY","HADR","LOSS",
944                                "MULS","PAIR","PHOT","RAYL"};
945   char line[256];
946   char detName[7];
947   char* filtmp;
948   Float_t cut[ncuts];
949   Int_t flag[nflags];
950   Int_t i, itmed, iret, ktmed, kz;
951   FILE *lun;
952   //
953   // See whether the file is there
954   filtmp=gSystem->ExpandPathName(filename);
955   lun=fopen(filtmp,"r");
956   delete [] filtmp;
957   if(!lun) {
958     Warning("SetTransPar","File %s does not exist!\n",filename);
959     return;
960   }
961   //
962   printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
963   printf(" *%59s\n","*");
964   printf(" *       Please check carefully what you are doing!%10s\n","*");
965   printf(" *%59s\n","*");
966   //
967   while(1) {
968     // Initialise cuts and flags
969     for(i=0;i<ncuts;i++) cut[i]=-99;
970     for(i=0;i<nflags;i++) flag[i]=-99;
971     itmed=0;
972     for(i=0;i<256;i++) line[i]='\0';
973     // Read up to the end of line excluded
974     iret=fscanf(lun,"%[^\n]",line);
975     if(iret<0) {
976       //End of file
977       fclose(lun);
978       printf(" *%59s\n","*");
979       printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
980       return;
981     }
982     // Read the end of line
983     fscanf(lun,"%*c");
984     if(!iret) continue;
985     if(line[0]=='*') continue;
986     // Read the numbers
987     iret=sscanf(line,"%s %d %f %f %f %f %f %f %f %f %f %f %d %d %d %d %d %d %d %d %d %d %d",
988                 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
989                 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
990                 &flag[8],&flag[9],&flag[10]);
991     if(!iret) continue;
992     if(iret<0) {
993       //reading error
994       Warning("SetTransPar","Error reading file %s\n",filename);
995       continue;
996     }
997     // Check that the module exist
998     AliModule *mod = GetModule(detName);
999     if(mod) {
1000       // Get the array of media numbers
1001       TArrayI &idtmed = *mod->GetIdtmed();
1002       // Check that the tracking medium code is valid
1003       if(0<=itmed && itmed < 100) {
1004         ktmed=idtmed[itmed];
1005         if(!ktmed) {
1006           Warning("SetTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
1007           continue;
1008         }
1009         // Set energy thresholds
1010         for(kz=0;kz<ncuts;kz++) {
1011           if(cut[kz]>=0) {
1012             printf(" *  %-6s set to %10.3E for tracking medium code %4d for %s\n",
1013                    pars[kz],cut[kz],itmed,mod->GetName());
1014             gMC->Gstpar(ktmed,pars[kz],cut[kz]);
1015           }
1016         }
1017         // Set transport mechanisms
1018         for(kz=0;kz<nflags;kz++) {
1019           if(flag[kz]>=0) {
1020             printf(" *  %-6s set to %10d for tracking medium code %4d for %s\n",
1021                    pars[ncuts+kz],flag[kz],itmed,mod->GetName());
1022             gMC->Gstpar(ktmed,pars[ncuts+kz],Float_t(flag[kz]));
1023           }
1024         }
1025       } else {
1026         Warning("SetTransPar","Invalid medium code %d *\n",itmed);
1027         continue;
1028       }
1029     } else {
1030       Warning("SetTransPar","Module %s not present\n",detName);
1031       continue;
1032     }
1033   }
1034 }
1035
1036 //_____________________________________________________________________________
1037 void AliRun::MakeTree(Option_t *option)
1038 {
1039   //
1040   //  Create the ROOT trees
1041   //  Loop on all detectors to create the Root branch (if any)
1042   //
1043
1044   char hname[30];
1045   //
1046   // Analyse options
1047   char *K = strstr(option,"K");
1048   char *H = strstr(option,"H");
1049   char *E = strstr(option,"E");
1050   char *D = strstr(option,"D");
1051   char *R = strstr(option,"R");
1052   //
1053   if (K && !fTreeK) {
1054     sprintf(hname,"TreeK%d",fEvent);
1055     fTreeK = new TTree(hname,"Kinematics");
1056     //  Create a branch for particles
1057     fTreeK->Branch("Particles",&fParticles,4000);
1058   }
1059   if (H && !fTreeH) {
1060     sprintf(hname,"TreeH%d",fEvent);
1061     fTreeH = new TTree(hname,"Hits");
1062     fTreeH->SetAutoSave(1000000000); //no autosave
1063   }
1064   if (D && !fTreeD) {
1065     sprintf(hname,"TreeD%d",fEvent);
1066     fTreeD = new TTree(hname,"Digits");
1067   }
1068   if (R && !fTreeR) {
1069     sprintf(hname,"TreeR%d",fEvent);
1070     fTreeR = new TTree(hname,"Reconstruction");
1071   }
1072   if (E && !fTreeE) {
1073     fTreeE = new TTree("TE","Header");
1074     //  Create a branch for Header
1075     fTreeE->Branch("Header","AliHeader",&header,4000);
1076   }
1077   //
1078   // Create a branch for hits/digits for each detector
1079   // Each branch is a TClonesArray. Each data member of the Hits classes
1080   // will be in turn a subbranch of the detector master branch
1081   TIter next(fModules);
1082   AliModule *detector;
1083   while((detector = (AliModule*)next())) {
1084      if (H || D || R) detector->MakeBranch(option);
1085   }
1086 }
1087
1088 //_____________________________________________________________________________
1089 Int_t AliRun::PurifyKine(Int_t lastSavedTrack, Int_t nofTracks)
1090 {
1091   //
1092   // PurifyKine with external parameters
1093   //
1094   fHgwmk = lastSavedTrack;
1095   fNtrack = nofTracks;
1096   PurifyKine();
1097   return fHgwmk;
1098 }
1099
1100 //_____________________________________________________________________________
1101 void AliRun::PurifyKine()
1102 {
1103   //
1104   // Compress kinematic tree keeping only flagged particles
1105   // and renaming the particle id's in all the hits
1106   //
1107   TClonesArray &particles = *fParticles;
1108   int nkeep=fHgwmk+1, parent, i;
1109   TParticle *part, *partnew, *father;
1110   int *map = new int[particles.GetEntries()];
1111
1112   // Save in Header total number of tracks before compression
1113   fHeader.SetNtrack(fHeader.GetNtrack()+fNtrack-fHgwmk);
1114
1115   // Preset map, to be removed later
1116   for(i=0; i<fNtrack; i++) {
1117     if(i<=fHgwmk) map[i]=i ; else map[i] = -99 ;}
1118   // Second pass, build map between old and new numbering
1119   for(i=fHgwmk+1; i<fNtrack; i++) {
1120     part = (TParticle *)particles.UncheckedAt(i);
1121     if(part->TestBit(Keep_Bit)) {
1122       
1123       // This particle has to be kept
1124       map[i]=nkeep;
1125       if(i!=nkeep) {
1126         
1127         // Old and new are different, have to copy
1128         partnew = (TParticle *)particles.UncheckedAt(nkeep);
1129         // Change due to a bug in the HP compiler
1130         //      *partnew = *part;
1131         memcpy(partnew,part,sizeof(TParticle));
1132       } else partnew = part;
1133       
1134       // as the parent is always *before*, it must be already
1135       // in place. This is what we are checking anyway!
1136       if((parent=partnew->GetFirstMother())>fHgwmk) {
1137         if(map[parent]==-99) printf("map[%d] = -99!\n",parent);
1138         partnew->SetFirstMother(map[parent]);
1139       }
1140       nkeep++;
1141     }
1142   }
1143   fNtrack=nkeep;
1144   
1145   // Fix daughters information
1146   for (i=0; i<fNtrack; i++) {
1147     part = (TParticle *)particles.UncheckedAt(i);
1148     parent = part->GetFirstMother();
1149     if(parent>=0) {
1150       father = (TParticle *)particles.UncheckedAt(parent);
1151       if(father->TestBit(Daughters_Bit)) {
1152       
1153         if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
1154         if(i>father->GetLastDaughter())  father->SetLastDaughter(i);
1155       } else {
1156         // Iitialise daughters info for first pass
1157         father->SetFirstDaughter(i);
1158         father->SetLastDaughter(i);
1159         father->SetBit(Daughters_Bit);
1160       }
1161     }
1162   }
1163   
1164 #ifdef old
1165   // Now loop on all detectors and reset the hits
1166   AliHit *OneHit;
1167   TIter next(fModules);
1168   AliModule *detector;
1169   while((detector = (AliModule*)next())) {
1170     if (!detector->Hits()) continue;
1171     TClonesArray &vHits=*(detector->Hits());
1172     if(vHits.GetEntries() != detector->GetNhits())
1173       printf("vHits.GetEntries()!=detector->GetNhits(): %d != %d\n",
1174              vHits.GetEntries(),detector->GetNhits());
1175     for (i=0; i<detector->GetNhits(); i++) {
1176       OneHit = (AliHit *)vHits.UncheckedAt(i);
1177       OneHit->SetTrack(map[OneHit->GetTrack()]);
1178     }
1179   }
1180 #else
1181
1182   // Now loop on all registered hit lists
1183   TIter next(fHitLists);
1184   TCollection *hitList;
1185   while((hitList = (TCollection*)next())) {
1186     TIter nexthit(hitList);
1187     AliHit *hit;
1188     while((hit = (AliHit*)nexthit())) {
1189       hit->SetTrack(map[hit->GetTrack()]);
1190     }
1191   }
1192 #endif
1193
1194   fHgwmk=nkeep-1;
1195   particles.SetLast(fHgwmk);
1196   delete [] map;
1197 }
1198
1199 //_____________________________________________________________________________
1200 void AliRun::Reset()
1201 {
1202   //
1203   //  Reset all Detectors & kinematics & trees
1204   //
1205   char hname[30];
1206   //
1207   ResetStack();
1208   ResetHits();
1209   ResetDigits();
1210
1211   // Initialise event header
1212   fHeader.Reset(fRun,fEvent);
1213
1214   if(fTreeK) {
1215     fTreeK->Reset();
1216     sprintf(hname,"TreeK%d",fEvent);
1217     fTreeK->SetName(hname);
1218   }
1219   if(fTreeH) {
1220     fTreeH->Reset();
1221     sprintf(hname,"TreeH%d",fEvent);
1222     fTreeH->SetName(hname);
1223   }
1224   if(fTreeD) {
1225     fTreeD->Reset();
1226     sprintf(hname,"TreeD%d",fEvent);
1227     fTreeD->SetName(hname);
1228   }
1229   if(fTreeR) {
1230     fTreeR->Reset();
1231     sprintf(hname,"TreeR%d",fEvent);
1232     fTreeR->SetName(hname);
1233   }
1234 }
1235
1236 //_____________________________________________________________________________
1237 void AliRun::ResetDigits()
1238 {
1239   //
1240   //  Reset all Detectors digits
1241   //
1242   TIter next(fModules);
1243   AliModule *detector;
1244   while((detector = (AliModule*)next())) {
1245      detector->ResetDigits();
1246   }
1247 }
1248
1249 //_____________________________________________________________________________
1250 void AliRun::ResetHits()
1251 {
1252   //
1253   //  Reset all Detectors hits
1254   //
1255   TIter next(fModules);
1256   AliModule *detector;
1257   while((detector = (AliModule*)next())) {
1258      detector->ResetHits();
1259   }
1260 }
1261
1262 //_____________________________________________________________________________
1263 void AliRun::ResetPoints()
1264 {
1265   //
1266   // Reset all Detectors points
1267   //
1268   TIter next(fModules);
1269   AliModule *detector;
1270   while((detector = (AliModule*)next())) {
1271      detector->ResetPoints();
1272   }
1273 }
1274
1275 //_____________________________________________________________________________
1276 void AliRun::RunMC(Int_t nevent, const char *setup)
1277 {
1278   //
1279   // Main function to be called to process a galice run
1280   // example
1281   //    Root > gAlice.Run(); 
1282   // a positive number of events will cause the finish routine
1283   // to be called
1284   //
1285
1286   // check if initialisation has been done
1287   if (!fInitDone) InitMC(setup);
1288   
1289   // Create the Root Tree with one branch per detector
1290   MakeTree("KHDER");
1291
1292   gMC->ProcessRun(nevent);
1293
1294   // End of this run, close files
1295   if(nevent>0) FinishRun();
1296 }
1297
1298 //_____________________________________________________________________________
1299 void AliRun::RunLego(const char *setup,Int_t ntheta,Float_t themin,
1300                      Float_t themax,Int_t nphi,Float_t phimin,Float_t phimax,
1301                      Float_t rmin,Float_t rmax,Float_t zmax)
1302 {
1303   //
1304   // Generates lego plots of:
1305   //    - radiation length map phi vs theta
1306   //    - radiation length map phi vs eta
1307   //    - interaction length map
1308   //    - g/cm2 length map
1309   //
1310   //  ntheta    bins in theta, eta
1311   //  themin    minimum angle in theta (degrees)
1312   //  themax    maximum angle in theta (degrees)
1313   //  nphi      bins in phi
1314   //  phimin    minimum angle in phi (degrees)
1315   //  phimax    maximum angle in phi (degrees)
1316   //  rmin      minimum radius
1317   //  rmax      maximum radius
1318   //  
1319   //
1320   //  The number of events generated = ntheta*nphi
1321   //  run input parameters in macro setup (default="Config.C")
1322   //
1323   //  Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1324   //Begin_Html
1325   /*
1326     <img src="picts/AliRunLego1.gif">
1327   */
1328   //End_Html
1329   //Begin_Html
1330   /*
1331     <img src="picts/AliRunLego2.gif">
1332   */
1333   //End_Html
1334   //Begin_Html
1335   /*
1336     <img src="picts/AliRunLego3.gif">
1337   */
1338   //End_Html
1339   //
1340
1341   // check if initialisation has been done
1342   if (!fInitDone) InitMC(setup);
1343
1344   //Create Lego object  
1345   fLego = new AliLego("lego",ntheta,themin,themax,nphi,phimin,phimax,rmin,rmax,zmax);
1346
1347   //Run Lego Object
1348   fLego->Run();
1349   
1350   // Create only the Root event Tree
1351   MakeTree("E");
1352   
1353   // End of this run, close files
1354   FinishRun();
1355 }
1356
1357 //_____________________________________________________________________________
1358 void AliRun::SetCurrentTrack(Int_t track)
1359
1360   //
1361   // Set current track number
1362   //
1363   fCurrent = track; 
1364 }
1365  
1366 //_____________________________________________________________________________
1367 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
1368                       Float_t *vpos, Float_t *polar, Float_t tof,
1369                       const char *mecha, Int_t &ntr, Float_t weight)
1370
1371   //
1372   // Load a track on the stack
1373   //
1374   // done     0 if the track has to be transported
1375   //          1 if not
1376   // parent   identifier of the parent track. -1 for a primary
1377   // pdg    particle code
1378   // pmom     momentum GeV/c
1379   // vpos     position 
1380   // polar    polarisation 
1381   // tof      time of flight in seconds
1382   // mecha    production mechanism
1383   // ntr      on output the number of the track stored
1384   //
1385   TClonesArray &particles = *fParticles;
1386   TParticle *particle;
1387   Float_t mass;
1388   const Int_t firstdaughter=-1;
1389   const Int_t lastdaughter=-1;
1390   const Int_t KS=0;
1391   //  const Float_t tlife=0;
1392   
1393   //
1394   // Here we get the static mass
1395   // For MC is ok, but a more sophisticated method could be necessary
1396   // if the calculated mass is required
1397   // also, this method is potentially dangerous if the mass
1398   // used in the MC is not the same of the PDG database
1399   //
1400   mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
1401   Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
1402                         pmom[1]*pmom[1]+pmom[2]*pmom[2]);
1403   
1404   //printf("Loading particle %s mass %f ene %f No %d ip %d pos %f %f %f mom %f %f %f KS %d m %s\n",
1405   //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],KS,mecha);
1406   
1407   particle=new(particles[fNtrack]) TParticle(pdg,KS,parent,-1,firstdaughter,
1408                                              lastdaughter,pmom[0],pmom[1],pmom[2],
1409                                              e,vpos[0],vpos[1],vpos[2],tof);
1410   //                                         polar[0],polar[1],polar[2],tof,
1411   //                                         mecha,weight);
1412   ((TParticle*)particles[fNtrack])->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
1413   ((TParticle*)particles[fNtrack])->SetWeight(weight);
1414   if(!done) particle->SetBit(Done_Bit);
1415   
1416   if(parent>=0) {
1417     particle=(TParticle*) fParticles->UncheckedAt(parent);
1418     particle->SetLastDaughter(fNtrack);
1419     if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
1420   } else { 
1421     //
1422     // This is a primary track. Set high water mark for this event
1423     fHgwmk=fNtrack;
1424     //
1425     // Set also number if primary tracks
1426     fHeader.SetNprimary(fHgwmk+1);
1427     fHeader.SetNtrack(fHgwmk+1);
1428   }
1429   ntr = fNtrack++;
1430 }
1431
1432 //_____________________________________________________________________________
1433 void AliRun::KeepTrack(const Int_t track)
1434
1435   //
1436   // flags a track to be kept
1437   //
1438   TClonesArray &particles = *fParticles;
1439   ((TParticle*)particles[track])->SetBit(Keep_Bit);
1440 }
1441  
1442 //_____________________________________________________________________________
1443 void AliRun::StepManager(Int_t id) 
1444 {
1445   //
1446   // Called at every step during transport
1447   //
1448
1449   //
1450   // --- If lego option, do it and leave 
1451   if (fLego)
1452     fLego->StepManager();
1453   else {
1454     Int_t copy;
1455     //Update energy deposition tables
1456     AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
1457   
1458     //Call the appropriate stepping routine;
1459     AliModule *det = (AliModule*)fModules->At(id);
1460     if(det) det->StepManager();
1461   }
1462 }
1463
1464 //_____________________________________________________________________________
1465 void AliRun::Streamer(TBuffer &R__b)
1466 {
1467   //
1468   // Stream an object of class AliRun.
1469   //
1470   if (R__b.IsReading()) {
1471     Version_t R__v = R__b.ReadVersion(); if (R__v) { }
1472     TNamed::Streamer(R__b);
1473     if (!gAlice) gAlice = this;
1474     gROOT->GetListOfBrowsables()->Add(this,"Run");
1475     fTreeE = (TTree*)gDirectory->Get("TE");
1476     if (fTreeE) fTreeE->SetBranchAddress("Header", &header);
1477     else    Error("Streamer","cannot find Header Tree\n");
1478     R__b >> fNtrack;
1479     R__b >> fHgwmk;
1480     R__b >> fDebug;
1481     fHeader.Streamer(R__b);
1482     R__b >> fModules;
1483     R__b >> fParticles;
1484     R__b >> fField; 
1485     //    R__b >> fMC;
1486     R__b >> fNdets;
1487     R__b >> fTrRmax;
1488     R__b >> fTrZmax;
1489     R__b >> fGenerator;
1490     if(R__v>1) {
1491       R__b >> fPDGDB;        //Particle factory object!
1492       fTreeE->GetEntry(0);
1493     } else {
1494       fHeader.SetEvent(0);
1495       fPDGDB     = TDatabasePDG::Instance();        //Particle factory object!
1496     }
1497   } else {
1498     R__b.WriteVersion(AliRun::IsA());
1499     TNamed::Streamer(R__b);
1500     R__b << fNtrack;
1501     R__b << fHgwmk;
1502     R__b << fDebug;
1503     fHeader.Streamer(R__b);
1504     R__b << fModules;
1505     R__b << fParticles;
1506     R__b << fField;
1507     //    R__b << fMC;
1508     R__b << fNdets;
1509     R__b << fTrRmax;
1510     R__b << fTrZmax;
1511     R__b << fGenerator;
1512     R__b << fPDGDB;        //Particle factory object!
1513   }
1514