]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALGetter.cxx
7d951384641b579d4dfd39b1fe9246a01dd533ae
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALGetter.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 /* $Id$ */
17
18 //_________________________________________________________________________
19 //  A singleton. This class should be used in the analysis stage to get 
20 //  reconstructed objects: Digits, RecPoints, TrackSegments and RecParticles,
21 //  instead of directly reading them from galice.root file. This container 
22 //  ensures, that one reads Digits, made of these particular digits, RecPoints, 
23 //  made of these particular RecPoints, TrackSegments and RecParticles. 
24 //  This becomes non trivial if there are several identical branches, produced with
25 //  different set of parameters. 
26 //
27 //  An example of how to use (see also class AliEMCALAnalyser):
28 //  AliEMCALGetter * gime = AliEMCALGetter::GetInstance("galice.root","test") ;
29 //  for(Int_t irecp = 0; irecp < gime->NRecParticles() ; irecp++)
30 //     AliEMCALRecParticle * part = gime->RecParticle(1) ;
31 //     ................
32 //  gime->Event(event) ;    // reads new event from galice.root
33 //                  
34 //*-- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (RRC KI & SUBATECH)
35 //*--         Completely redesigned by Dmitri Peressounko March 2001  
36 //
37 //*-- YS June 2001 : renamed the original AliEMCALIndexToObject and make
38 //*--         systematic usage of TFolders without changing the interface        
39 //////////////////////////////////////////////////////////////////////////////
40
41 // --- ROOT system ---
42
43 #include <TFile.h>
44 #include <TROOT.h>
45 #include <TSystem.h>
46 #include <TF1.h>
47 #include <TGraph.h>
48 //#include <TCanvas.h>
49 //#include <TFrame.h>
50
51 // --- Standard library ---
52
53 // --- AliRoot header files ---
54
55 #include "AliEMCAL.h"
56 #include "AliEMCALGetter.h"
57 #include "AliEMCALLoader.h"
58 #include "AliHeader.h"  
59 #include "AliMC.h"
60 #include "AliRunLoader.h"
61 #include "AliStack.h"  
62 #include "AliEMCALRawStream.h"
63 #include "AliRawReaderFile.h"
64
65 ClassImp(AliEMCALGetter)
66   
67 AliEMCALGetter * AliEMCALGetter::fgObjGetter = 0 ; 
68 AliEMCALLoader * AliEMCALGetter::fgEmcalLoader = 0;
69 Int_t AliEMCALGetter::fgDebug = 0;
70 TString AliEMCALGetter::fVersion = "";
71
72 //  TFile * AliEMCALGetter::fgFile = 0 ; 
73
74 //____________________________________________________________________________ 
75 AliEMCALGetter::AliEMCALGetter(const char* headerFile, const char* version, Option_t * openingOption) 
76 {
77   // ctor only called by Instance()
78
79   // initialize data members
80   SetDebug(0) ; 
81   //fBTE = 0 ; 
82   
83   fLoadingStatus = "" ; 
84
85   fgObjGetter=this;
86   
87   OpenFile(headerFile,version,openingOption);
88 }
89
90
91 //____________________________________________________________________________ 
92 AliEMCALGetter::~AliEMCALGetter()
93 {
94   //PH  AliRunLoader * rl = AliRunLoader::GetRunLoader(fgEmcalLoader->GetTitle());
95   //PH  delete rl;
96   fgEmcalLoader = 0 ;
97   fgObjGetter = 0; 
98   fVersion = "";
99 }
100
101 //____________________________________________________________________________ 
102 void AliEMCALGetter::OpenFile(const char* headerFile, const char* version, Option_t * openingOption) {
103   fVersion = version;
104   AliRunLoader* rl = AliRunLoader::GetRunLoader(version) ; 
105   if (!rl) {
106     rl = AliRunLoader::Open(headerFile, version, openingOption);
107     if (!rl) {
108       Fatal("AliEMCALGetter", "Could not find the Run Loader for %s - %s",headerFile, version) ; 
109       return ;
110     } 
111     if (rl->GetAliRun() == 0x0) {
112       rl->LoadgAlice();
113       gAlice = rl->GetAliRun(); // should be removed
114     }
115   } 
116   fgEmcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetLoader("EMCALLoader"));
117   if ( !fgEmcalLoader ) 
118     Error("AliEMCALGetter", "Could not find EMCALLoader") ; 
119   else 
120     fgEmcalLoader->SetTitle(version);
121 }
122
123 //____________________________________________________________________________ 
124 void AliEMCALGetter::Reset()
125 {
126   // resets things in case the getter is called consecutively with different files
127   // the EMCAL Loader is already deleted by the Run Loader
128
129 }
130
131 //____________________________________________________________________________ 
132 AliEMCALClusterizer * AliEMCALGetter::Clusterizer()
133
134   // return pointer to Clusterizer Tree
135   AliEMCALClusterizer * rv ; 
136   rv =  dynamic_cast<AliEMCALClusterizer *>(EmcalLoader()->Reconstructioner()) ;
137   if (!rv) {
138     Event(0, "R") ; 
139     rv =  dynamic_cast<AliEMCALClusterizer*>(EmcalLoader()->Reconstructioner()) ;
140   }
141   return rv ; 
142 }
143
144
145 //____________________________________________________________________________ 
146 TClonesArray * AliEMCALGetter::Digits() const  
147 {
148   // asks the Loader to return the Digits container 
149
150   TClonesArray * rv = 0 ; 
151   rv = EmcalLoader()->Digits() ; 
152
153   if( !rv ) {
154     EmcalLoader()->MakeDigitsArray() ; 
155     rv = EmcalLoader()->Digits() ;
156   }
157   return rv ; 
158 }
159
160 //____________________________________________________________________________ 
161 AliEMCALDigitizer * AliEMCALGetter::Digitizer() 
162
163   // return pointer to Digitizer Tree
164   AliEMCALDigitizer * rv ; 
165   rv =  dynamic_cast<AliEMCALDigitizer *>(EmcalLoader()->Digitizer()) ;
166   if (!rv) {
167     Event(0, "D") ; 
168     rv =  dynamic_cast<AliEMCALDigitizer *>(EmcalLoader()->Digitizer()) ;
169   }
170   return rv ; 
171 }
172
173 //____________________________________________________________________________ 
174 TObjArray * AliEMCALGetter::ECARecPoints() const 
175 {
176   // asks the Loader to return the EMC RecPoints container 
177
178   TObjArray * rv = 0 ; 
179   
180   rv = EmcalLoader()->ECARecPoints() ; 
181   if (!rv) {
182     EmcalLoader()->MakeRecPointsArray() ;
183     rv = EmcalLoader()->ECARecPoints() ; 
184   }
185   return rv ; 
186 }
187
188 //____________________________________________________________________________ 
189 TClonesArray * AliEMCALGetter::TrackSegments() const 
190 {
191   // asks the Loader to return the TrackSegments container 
192
193   TClonesArray * rv = 0 ; 
194   
195   rv = EmcalLoader()->TrackSegments() ; 
196   if (!rv) {
197     EmcalLoader()->MakeTrackSegmentsArray() ;
198     rv = EmcalLoader()->TrackSegments() ; 
199   }
200   return rv ; 
201 }
202
203 //____________________________________________________________________________ 
204 AliEMCALTrackSegmentMaker * AliEMCALGetter::TrackSegmentMaker() 
205
206   // return pointer to TrackSegmentMaker Tree
207   AliEMCALTrackSegmentMaker * rv ; 
208   rv =  dynamic_cast<AliEMCALTrackSegmentMaker *>(EmcalLoader()->TrackSegmentMaker()) ;
209   if (!rv) {
210     Event(0, "T") ; 
211     rv =  dynamic_cast<AliEMCALTrackSegmentMaker *>(EmcalLoader()->TrackSegmentMaker()) ;
212   }
213   return rv ; 
214 }
215
216 //____________________________________________________________________________ 
217 TClonesArray * AliEMCALGetter::RecParticles() const  
218 {
219   // asks the Loader to return the TrackSegments container 
220
221   TClonesArray * rv = 0 ; 
222   
223   rv = EmcalLoader()->RecParticles() ; 
224   if (!rv) {
225     EmcalLoader()->MakeRecParticlesArray() ;
226     rv = EmcalLoader()->RecParticles() ; 
227   }
228   return rv ; 
229 }
230 //____________________________________________________________________________ 
231 void AliEMCALGetter::Event(Int_t event, const char* opt) 
232 {
233   // Reads the content of all Tree's S, D and R
234
235   if ( event >= MaxEvent() ) {
236     Error("Event", "%d not found in TreeE !", event) ; 
237     return ; 
238   }
239
240   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
241   // checks if we are dealing with test-beam data
242 //   TBranch * btb = rl->TreeE()->GetBranch("AliEMCALBeamTestEvent") ;
243 //   if(btb){
244 //     if(!fBTE)
245 //       fBTE = new AliEMCALBeamTestEvent() ;
246 //     btb->SetAddress(&fBTE) ;
247 //     btb->GetEntry(event) ;
248 //   }
249 //   else{
250 //     if(fBTE){
251 //       delete fBTE ;
252 //       fBTE = 0 ;
253 //     }
254 //   }
255
256   // Loads the type of object(s) requested
257   
258   rl->GetEvent(event) ;
259
260   if( strstr(opt,"X") || (strcmp(opt,"")==0) )
261     ReadPrimaries() ;
262
263   if(strstr(opt,"H") )
264     ReadTreeH();
265
266   if(strstr(opt,"S") )
267     ReadTreeS() ;
268
269   if( strstr(opt,"D") )
270     ReadTreeD() ;
271
272   if( strstr(opt,"R") )
273     ReadTreeR() ;
274
275   if( strstr(opt,"T") )
276     ReadTreeT() ;
277
278   if( strstr(opt,"P") )
279     ReadTreeP() ;
280
281   if( strstr(opt,"W") )
282     ReadRaw(event) ;
283   
284 }
285
286
287 //____________________________________________________________________________ 
288 Int_t AliEMCALGetter::EventNumber() const
289   {
290   // return the current event number
291   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
292   return static_cast<Int_t>(rl->GetEventNumber()) ;   
293 }
294
295 //____________________________________________________________________________ 
296   TClonesArray * AliEMCALGetter::Hits() const   
297 {
298   // asks the loader to return  the Hits container 
299   
300   TClonesArray * rv = 0 ; 
301   
302   rv = EmcalLoader()->Hits() ; 
303   if ( !rv ) {
304     EmcalLoader()->LoadHits("read"); 
305     rv = EmcalLoader()->Hits() ; 
306   }
307   return rv ; 
308 }
309
310 //____________________________________________________________________________ 
311 AliEMCALGetter * AliEMCALGetter::Instance(const char* alirunFileName, const char* version, Option_t * openingOption) 
312 {
313   // Creates and returns the pointer of the unique instance
314   // Must be called only when the environment has changed
315
316   if(!fgObjGetter){ // first time the getter is called 
317     fgObjGetter = new AliEMCALGetter(alirunFileName, version, openingOption) ;
318   }
319   else { // the getter has been called previously
320     AliRunLoader * rl = AliRunLoader::GetRunLoader(fVersion);
321     if (rl == 0)  fgObjGetter->OpenFile(alirunFileName, version, openingOption) ; 
322     else if ( rl->GetFileName() == alirunFileName ) {// the alirunFile has the same name
323       // check if the file is already open
324       TFile * galiceFile = dynamic_cast<TFile *>(gROOT->FindObject(rl->GetFileName()) ) ; 
325       
326       if ( !galiceFile ) 
327         fgObjGetter->OpenFile(alirunFileName, version, openingOption);
328       
329       else {  // the file is already open check the version name
330         TString currentVersionName = rl->GetEventFolder()->GetName() ; 
331         TString newVersionName(version) ; 
332         if (currentVersionName == newVersionName) 
333           if(fgDebug)
334             ::Warning( "Instance", "Files with version %s already open", currentVersionName.Data() ) ;  
335         else {    
336           fgEmcalLoader->SetTitle(version); fVersion = version;
337         }
338       }
339     }
340     else { 
341       AliRunLoader * rl = AliRunLoader::GetRunLoader(fVersion);
342       if ( strstr(version, AliConfig::GetDefaultEventFolderName()) ) // false in case of merging
343         delete rl ; 
344       fgObjGetter->OpenFile(alirunFileName, version, openingOption) ;      
345     }
346   }
347   if (!fgObjGetter) 
348     ::Error("AliEMCALGetter::Instance", "Failed to create the EMCAL Getter object") ;
349   else 
350     if (fgDebug)
351       Print() ;
352
353   return fgObjGetter ;
354 }
355
356 //____________________________________________________________________________ 
357 AliEMCALGetter *  AliEMCALGetter::Instance()
358 {
359   // Returns the pointer of the unique instance already defined
360   
361   if(!fgObjGetter && fgDebug)
362      ::Warning("AliEMCALGetter::Instance", "Getter not initialized") ;
363
364    return fgObjGetter ;
365            
366 }
367
368 //____________________________________________________________________________ 
369 Int_t AliEMCALGetter::MaxEvent() const 
370 {
371   // returns the number of events in the run (from TE)
372
373   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
374   return static_cast<Int_t>(rl->GetNumberOfEvents()) ; 
375 }
376
377 //____________________________________________________________________________ 
378 TParticle * AliEMCALGetter::Primary(Int_t index) const
379 {
380   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
381   return rl->Stack()->Particle(index) ; 
382
383
384 //____________________________________________________________________________ 
385 Int_t AliEMCALGetter::NPrimaries() const
386 {
387   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
388   return (rl->GetHeader())->GetNtrack(); 
389
390
391 //____________________________________________________________________________ 
392 AliEMCAL * AliEMCALGetter:: EMCAL() const  
393 {
394   // returns the EMCAL object 
395   AliEMCALLoader *    loader = 0;
396   static AliEMCALLoader * oldloader = 0;
397   static AliEMCAL * emcal = 0;
398
399   loader = EmcalLoader();
400
401   if (loader != oldloader ) {
402     emcal = dynamic_cast<AliEMCAL*>(loader->GetModulesFolder()->FindObject("EMCAL")) ;  
403     oldloader = loader;
404   }
405   if (!emcal) 
406     if (fgDebug)
407       Warning("EMCAL", "EMCAL module not found in module folders: %s", EmcalLoader()->GetModulesFolder()->GetName() ) ; 
408   return emcal ; 
409 }  
410
411
412
413 //____________________________________________________________________________ 
414 AliEMCALPID * AliEMCALGetter::PID() 
415
416   // return pointer to PID Tree
417   AliEMCALPID * rv ; 
418   rv =  dynamic_cast<AliEMCALPID *>(EmcalLoader()->PIDTask()) ;
419   if (!rv) {
420     Event(0, "P") ; 
421     rv =  dynamic_cast<AliEMCALPID *>(EmcalLoader()->PIDTask()) ;
422   }
423   return rv ; 
424 }
425
426 //____________________________________________________________________________ 
427 AliEMCALGeometry * AliEMCALGetter::EMCALGeometry() const 
428 {
429   // Returns EMCAL geometry
430
431   AliEMCALGeometry * rv = 0 ; 
432   if (EMCAL() )
433     rv =  EMCAL()->GetGeometry() ;
434   return rv ; 
435
436
437 //____________________________________________________________________________ 
438 void  AliEMCALGetter::Print() 
439 {
440   // Print usefull information about the getter
441     
442   AliRunLoader * rl = AliRunLoader::GetRunLoader(fgEmcalLoader->GetTitle());
443   ::Info("Print", "gAlice file is %s -- version name is %s", (rl->GetFileName()).Data(), rl->GetEventFolder()->GetName() ) ; 
444 }
445
446 //____________________________________________________________________________ 
447 void AliEMCALGetter::ReadPrimaries()  
448 {
449   // Read Primaries from Kinematics.root
450   
451   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
452   
453   // gets kine tree from the root file (Kinematics.root)
454   if ( ! rl->TreeK() )  // load treeK the first time
455     rl->LoadKinematics() ;
456   
457   if (fgDebug) 
458     Info("ReadTreeK", "Found %d particles in event # %d", NPrimaries(), EventNumber() ) ; 
459 }
460
461 //____________________________________________________________________________ 
462 void AliEMCALGetter::FitRaw(Bool_t lowGainFlag, TGraph * gLowGain, TGraph * gHighGain, TF1* signalF, Int_t & amp, Double_t & time)
463 {
464   // Fits the raw signal time distribution 
465
466   const Int_t kNoiseThreshold = 0 ;
467   Double_t timezero1 = 0., timezero2 = 0., timemax = 0. ;
468   Double_t signal = 0., signalmax = 0. ;       
469   Double_t energy = time = 0. ; 
470
471   if (lowGainFlag) {
472     timezero1 = timezero2 = signalmax = timemax = 0. ;
473     signalF->FixParameter(0, EMCAL()->GetRawFormatLowCharge()) ; 
474     signalF->FixParameter(1, EMCAL()->GetRawFormatLowGain()) ; 
475     Int_t index ; 
476     for (index = 0; index < EMCAL()->GetRawFormatTimeBins(); index++) {
477       gLowGain->GetPoint(index, time, signal) ; 
478       if (signal > kNoiseThreshold && timezero1 == 0.) 
479         timezero1 = time ;
480       if (signal <= kNoiseThreshold && timezero1 > 0. && timezero2 == 0.)
481         timezero2 = time ; 
482       if (signal > signalmax) {
483         signalmax = signal ; 
484         timemax   = time ; 
485       }
486     }
487     signalmax /= EMCAL()->RawResponseFunctionMax(EMCAL()->GetRawFormatLowCharge(), 
488                                                 EMCAL()->GetRawFormatLowGain()) ;
489     if ( timezero1 + EMCAL()->GetRawFormatTimePeak() < EMCAL()->GetRawFormatTimeMax() * 0.4 ) { // else its noise 
490       signalF->SetParameter(2, signalmax) ; 
491       signalF->SetParameter(3, timezero1) ;         
492       gLowGain->Fit(signalF, "QRON", "", 0., timezero2); //, "QRON") ; 
493       energy = signalF->GetParameter(2) ; 
494       time   = signalF->GetMaximumX() - EMCAL()->GetRawFormatTimePeak() - EMCAL()->GetRawFormatTimeTrigger() ;
495     }
496   } else {
497     timezero1 = timezero2 = signalmax = timemax = 0. ;
498     signalF->FixParameter(0, EMCAL()->GetRawFormatHighCharge()) ; 
499     signalF->FixParameter(1, EMCAL()->GetRawFormatHighGain()) ; 
500     Int_t index ; 
501     for (index = 0; index < EMCAL()->GetRawFormatTimeBins(); index++) {
502       gHighGain->GetPoint(index, time, signal) ;               
503       if (signal > kNoiseThreshold && timezero1 == 0.) 
504         timezero1 = time ;
505       if (signal <= kNoiseThreshold && timezero1 > 0. && timezero2 == 0.)
506         timezero2 = time ; 
507       if (signal > signalmax) {
508         signalmax = signal ;   
509         timemax   = time ; 
510       }
511     }
512     signalmax /= EMCAL()->RawResponseFunctionMax(EMCAL()->GetRawFormatHighCharge(), 
513                                                 EMCAL()->GetRawFormatHighGain()) ;;
514     if ( timezero1 + EMCAL()->GetRawFormatTimePeak() < EMCAL()->GetRawFormatTimeMax() * 0.4 ) { // else its noise  
515       signalF->SetParameter(2, signalmax) ; 
516       signalF->SetParameter(3, timezero1) ;               
517       gHighGain->Fit(signalF, "QRON", "", 0., timezero2) ; 
518       energy = signalF->GetParameter(2) ; 
519       time   = signalF->GetMaximumX() - EMCAL()->GetRawFormatTimePeak() - EMCAL()->GetRawFormatTimeTrigger() ;
520     }
521   }
522   
523   if (time == 0. && energy == 0.) 
524     amp = 0 ; 
525   else {
526   AliEMCALDigitizer * digitizer = Digitizer() ; 
527   amp = static_cast<Int_t>( (energy - digitizer->GetECApedestal()) / digitizer->GetECAchannel() + 0.5 ) ; 
528   }
529   // dessin
530 //   TCanvas * c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);
531 //   c1->SetFillColor(42);
532 //   c1->SetGrid();
533 //   gLowGain->SetLineColor(2);
534 //   gLowGain->SetLineWidth(4);
535 //   gLowGain->SetMarkerColor(4);
536 //   gLowGain->SetMarkerStyle(21);
537 //   gLowGain->SetTitle("Lowgain");
538 //   gLowGain->GetXaxis()->SetTitle("X title");
539 //   gLowGain->GetYaxis()->SetTitle("Y title");
540 //   gLowGain->Draw("ACP");
541   
542 //   c1->Update();
543 //   c1->GetFrame()->SetFillColor(21);
544 //   c1->GetFrame()->SetBorderSize(12);
545 //   c1->Modified();
546   
547 //   TCanvas * c2 = new TCanvas("c2","A Simple Graph Example",200,10,700,500);
548 //   c2->SetFillColor(42);
549 //   c2->SetGrid();
550 //   gHighGain->SetLineColor(2);
551 //   gHighGain->SetLineWidth(4);
552 //   gHighGain->SetMarkerColor(4);
553 //   gHighGain->SetMarkerStyle(21);
554 //   gHighGain->SetTitle("Highgain");
555 //   gHighGain->GetXaxis()->SetTitle("X title");
556 //   gHighGain->GetYaxis()->SetTitle("Y title");
557 //     gHighGain->Draw("ACP");
558   
559 //   c2->Update();
560 //   c2->GetFrame()->SetFillColor(21);
561 //   c2->GetFrame()->SetBorderSize(12);
562 //   c2->Modified();
563 }
564
565 //____________________________________________________________________________ 
566 Int_t AliEMCALGetter::ReadRaw(Int_t event)
567 {
568   // reads the raw format data, converts it into digits format and store digits in Digits()
569   // container.
570   
571   AliRawReaderFile rawReader(event) ; 
572   AliEMCALRawStream in(&rawReader);
573   
574   Bool_t first = kTRUE ;
575  
576   TF1 * signalF = new TF1("signal", AliEMCAL::RawResponseFunction, 0, EMCAL()->GetRawFormatTimeMax(), 4);
577   signalF->SetParNames("Charge", "Gain", "Amplitude", "TimeZero") ; 
578  
579   
580   Int_t id = -1;
581   Bool_t lowGainFlag = kFALSE ; 
582
583   TClonesArray * digits = Digits() ;
584   digits->Clear() ; 
585   Int_t idigit = 0 ; 
586   Int_t amp = 0 ; 
587   Double_t time = 0. ; 
588
589   TGraph * gLowGain = new TGraph(EMCAL()->GetRawFormatTimeBins()) ; 
590   TGraph * gHighGain= new TGraph(EMCAL()->GetRawFormatTimeBins()) ;  
591
592   while ( in.Next() ) { // EMCAL entries loop 
593     if ( in.IsNewId() ) {
594       if (!first) {
595         FitRaw(lowGainFlag, gLowGain, gHighGain, signalF, amp, time) ; 
596         if (amp > 0) {
597           new((*digits)[idigit]) AliEMCALDigit( -1, -1, id, amp, time) ;        
598           idigit++ ; 
599         }
600         Int_t index ; 
601         for (index = 0; index < EMCAL()->GetRawFormatTimeBins(); index++) {
602           gLowGain->SetPoint(index, index * EMCAL()->GetRawFormatTimeMax() / EMCAL()->GetRawFormatTimeBins(), 0) ;  
603           gHighGain->SetPoint(index, index * EMCAL()->GetRawFormatTimeMax() / EMCAL()->GetRawFormatTimeBins(), 0) ; 
604         } 
605       }  
606       first = kFALSE ; 
607       id = in.GetId() ; 
608       if (in.GetModule() == EMCAL()->GetRawFormatLowGainOffset() ) 
609         lowGainFlag = kTRUE ; 
610       else 
611         lowGainFlag = kFALSE ; 
612     }
613     if (lowGainFlag)
614       gLowGain->SetPoint(in.GetTime(), 
615                          in.GetTime()* EMCAL()->GetRawFormatTimeMax() / EMCAL()->GetRawFormatTimeBins(), 
616                          in.GetSignal()) ;
617     else 
618       gHighGain->SetPoint(in.GetTime(), 
619                           in.GetTime() * EMCAL()->GetRawFormatTimeMax() / EMCAL()->GetRawFormatTimeBins(), 
620                           in.GetSignal() ) ;
621     
622   } // EMCAL entries loop
623   digits->Sort() ; 
624
625   delete signalF ; 
626   delete gLowGain;
627   delete gHighGain ; 
628     
629   return Digits()->GetEntriesFast() ; 
630 }
631   
632   //____________________________________________________________________________ 
633 Int_t AliEMCALGetter::ReadTreeD()
634 {
635   // Read the Digits
636   
637    EmcalLoader()->CleanDigits() ; 
638   // gets TreeD from the root file (EMCAL.Digits.root)
639   //if ( !IsLoaded("D") ) {
640     EmcalLoader()->LoadDigits("UPDATE") ;
641     EmcalLoader()->LoadDigitizer("UPDATE") ;
642     //  SetLoaded("D") ; 
643     //} 
644   return Digits()->GetEntries() ; 
645 }
646
647 //____________________________________________________________________________ 
648 Int_t AliEMCALGetter::ReadTreeH()
649 {
650   // Read the Hits
651   EmcalLoader()->CleanHits() ; 
652   // gets TreeH from the root file (EMCAL.Hit.root)
653   //if ( !IsLoaded("H") ) {
654     EmcalLoader()->LoadHits("READ") ;
655     SetLoaded("H") ; 
656     //}  
657   return Hits()->GetEntries() ; 
658 }
659
660 //____________________________________________________________________________ 
661 Int_t AliEMCALGetter::ReadTreeR()
662 {
663   // Read the RecPoints
664
665    EmcalLoader()->CleanRecPoints() ; 
666   // gets TreeR from the root file (EMCAL.RecPoints.root)
667   //if ( !IsLoaded("R") ) {
668     EmcalLoader()->LoadRecPoints("UPDATE") ;
669     EmcalLoader()->LoadClusterizer("UPDATE") ;
670     //  SetLoaded("R") ; 
671     //}
672
673   return ECARecPoints()->GetEntries() ; 
674 }
675
676 //____________________________________________________________________________ 
677 Int_t AliEMCALGetter::ReadTreeT()
678 {
679   // Read the TrackSegments
680   
681   EmcalLoader()->CleanTracks() ; 
682   // gets TreeT from the root file (EMCAL.TrackSegments.root)
683   //if ( !IsLoaded("T") ) {
684     EmcalLoader()->LoadTracks("UPDATE") ;
685     EmcalLoader()->LoadTrackSegmentMaker("UPDATE") ;
686     //  SetLoaded("T") ; 
687     //}
688
689   return TrackSegments()->GetEntries() ; 
690 }
691 //____________________________________________________________________________ 
692 Int_t AliEMCALGetter::ReadTreeP()
693 {
694   // Read the RecParticles
695   
696   EmcalLoader()->CleanRecParticles() ; 
697   // gets TreeP from the root file (EMCAL.RecParticles.root)
698   //  if ( !IsLoaded("P") ) {
699     EmcalLoader()->LoadRecParticles("UPDATE") ;
700     EmcalLoader()->LoadPID("UPDATE") ;
701     //  SetLoaded("P") ; 
702     // }
703
704   return RecParticles()->GetEntries() ; 
705 }
706 //____________________________________________________________________________ 
707 Int_t AliEMCALGetter::ReadTreeS()
708 {
709   // Read the SDigits
710   
711   //  EmcalLoader()->CleanSDigits() ; 
712   // gets TreeS from the root file (EMCAL.SDigits.root)
713   // if ( !IsLoaded("S") ) {
714     EmcalLoader()->LoadSDigits("READ") ;
715     EmcalLoader()->LoadSDigitizer("READ") ;
716     //  SetLoaded("S") ; 
717     //}
718
719   return SDigits()->GetEntries() ; 
720 }
721
722 //____________________________________________________________________________ 
723 TClonesArray * AliEMCALGetter::SDigits() const  
724 {
725   // asks the Loader to return the Digits container 
726
727   TClonesArray * rv = 0 ; 
728   
729   rv = EmcalLoader()->SDigits() ; 
730   if (!rv) {
731     EmcalLoader()->MakeSDigitsArray() ;
732     rv = EmcalLoader()->SDigits() ; 
733   }
734   return rv ; 
735 }
736
737 //____________________________________________________________________________ 
738 AliEMCALSDigitizer * AliEMCALGetter::SDigitizer() 
739
740   // Return pointer to SDigitizer task
741   AliEMCALSDigitizer * rv ; 
742   rv =  dynamic_cast<AliEMCALSDigitizer *>(EmcalLoader()->SDigitizer()) ;
743   if (!rv) {
744     Event(0, "S") ; 
745     rv =  dynamic_cast<AliEMCALSDigitizer *>(EmcalLoader()->SDigitizer()) ;
746   }
747   return rv ; 
748 }
749
750 //____________________________________________________________________________ 
751 TParticle * AliEMCALGetter::Secondary(const TParticle* p, Int_t index) const
752 {
753   // Return first (index=1) or second (index=2) secondary particle of primary particle p 
754
755   if(index <= 0) 
756     return 0 ;
757   if(index > 2)
758     return 0 ;
759
760   if(p) {
761   Int_t daughterIndex = p->GetDaughter(index-1) ; 
762   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
763   return  rl->GetAliRun()->GetMCApp()->Particle(daughterIndex) ; 
764   }
765   else
766     return 0 ;
767 }
768
769 //____________________________________________________________________________ 
770 void AliEMCALGetter::Track(Int_t itrack) 
771 {
772   // Read the first entry of EMCAL branch in hit tree gAlice->TreeH()
773  
774  AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
775
776   if( !TreeH() ) // load treeH the first time
777     rl->LoadHits() ;
778
779   // first time create the container
780   TClonesArray * hits = Hits() ; 
781   if ( hits ) 
782     hits->Clear() ; 
783
784   TBranch * emcalbranch = dynamic_cast<TBranch*>(TreeH()->GetBranch("EMCAL")) ; 
785   emcalbranch->SetAddress(&hits) ;
786   emcalbranch->GetEntry(itrack) ;
787 }
788
789 //____________________________________________________________________________ 
790 TTree * AliEMCALGetter::TreeD() const 
791 {
792   // return pointer to Digits Tree
793   TTree * rv = 0 ; 
794   rv = EmcalLoader()->TreeD() ; 
795   if ( !rv ) {
796     EmcalLoader()->MakeTree("D");
797     rv = EmcalLoader()->TreeD() ;
798   } 
799   
800   return rv ; 
801 }
802
803 //____________________________________________________________________________ 
804 TTree * AliEMCALGetter::TreeH() const 
805 {
806   // return pointer to Hits Tree
807   TTree * rv = 0 ; 
808   rv = EmcalLoader()->TreeH() ; 
809   if ( !rv ) {
810     EmcalLoader()->MakeTree("H");
811     rv = EmcalLoader()->TreeH() ;
812   } 
813   
814   return rv ; 
815 }
816
817 //____________________________________________________________________________ 
818 TTree * AliEMCALGetter::TreeR() const 
819 {
820   // return pointer to RecPoints Tree
821
822   TTree * rv = 0 ; 
823   rv = EmcalLoader()->TreeR() ; 
824   if ( !rv ) {
825     EmcalLoader()->MakeTree("R");
826     rv = EmcalLoader()->TreeR() ;
827   } 
828   
829   return rv ; 
830 }
831
832 //____________________________________________________________________________ 
833 TTree * AliEMCALGetter::TreeT() const 
834 {  
835   // return pointer to TrackSegments Tree
836   TTree * rv = 0 ; 
837   rv = EmcalLoader()->TreeT() ; 
838   if ( !rv ) {
839     EmcalLoader()->MakeTree("T");
840     rv = EmcalLoader()->TreeT() ;
841   } 
842   
843   return rv ; 
844 }
845 //____________________________________________________________________________ 
846 TTree * AliEMCALGetter::TreeP() const 
847 {
848   // return pointer to RecParticles Tree
849   TTree * rv = 0 ; 
850   rv = EmcalLoader()->TreeP() ; 
851   if ( !rv ) {
852     EmcalLoader()->MakeTree("P");
853     rv = EmcalLoader()->TreeP() ;
854   } 
855   
856   return rv ; 
857 }
858
859 //____________________________________________________________________________ 
860 TTree * AliEMCALGetter::TreeS() const 
861 {
862   // return pointer to SDigits Tree
863   TTree * rv = 0 ; 
864   rv = EmcalLoader()->TreeS() ; 
865   if ( !rv ) {
866     EmcalLoader()->MakeTree("S");
867     rv = EmcalLoader()->TreeS() ;
868   } 
869   
870   return rv ; 
871 }
872
873 //____________________________________________________________________________ 
874 Bool_t AliEMCALGetter::VersionExists(TString & opt) const
875 {
876   // checks if the version with the present name already exists in the same directory
877
878   Bool_t rv = kFALSE ;
879  
880   AliRunLoader * rl = AliRunLoader::GetRunLoader(EmcalLoader()->GetTitle());
881   TString version( rl->GetEventFolder()->GetName() ) ; 
882
883   opt.ToLower() ; 
884   
885   if ( opt == "sdigits") {
886     // add the version name to the root file name
887     TString fileName( EmcalLoader()->GetSDigitsFileName() ) ; 
888     if (version != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name 
889       fileName = fileName.ReplaceAll(".root", "") + "_" + version + ".root" ;
890     if ( !(gSystem->AccessPathName(fileName)) ) { 
891       Warning("VersionExists", "The file %s already exists", fileName.Data()) ;
892       rv = kTRUE ; 
893     }
894     EmcalLoader()->SetSDigitsFileName(fileName) ;
895   }
896
897   if ( opt == "digits") {
898     // add the version name to the root file name
899     TString fileName( EmcalLoader()->GetDigitsFileName() ) ; 
900     if (version != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name 
901       fileName = fileName.ReplaceAll(".root", "") + "_" + version + ".root" ;
902     if ( !(gSystem->AccessPathName(fileName)) ) {
903       Warning("VersionExists", "The file %s already exists", fileName.Data()) ;  
904       rv = kTRUE ; 
905     }
906   }
907
908   return rv ;
909
910 }
911
912 //____________________________________________________________________________ 
913 UShort_t AliEMCALGetter::EventPattern(void) const
914 {
915   // Return the pattern (trigger bit register) of the beam-test event
916 //   if(fBTE)
917 //     return fBTE->GetPattern() ;
918 //   else
919     return 0 ;
920 }
921 //____________________________________________________________________________ 
922 Float_t AliEMCALGetter::BeamEnergy(void) const
923 {
924   // Return the beam energy of the beam-test event
925 //   if(fBTE)
926 //     return fBTE->GetBeamEnergy() ;
927 //   else
928     return 0 ;
929 }