Updates to AddTask macro from Chris
[u/mrichter/AliRoot.git] / MUON / AliMUONSDigitizerV2.cxx
CommitLineData
60d3a1d3 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
7ca4655f 18#include <TClonesArray.h>
9da52a4f 19#include <TParticle.h>
c93255fe 20#include <TTree.h>
7ca4655f 21
60d3a1d3 22#include "AliMUONSDigitizerV2.h"
23
60d3a1d3 24#include "AliMUON.h"
25#include "AliMUONChamber.h"
445096c0 26#include "AliMUONVDigit.h"
60d3a1d3 27#include "AliMUONHit.h"
5ee595ac 28#include "AliMUONVDigitStore.h"
445096c0 29#include "AliMUONVHitStore.h"
7d7d22a6 30#include "AliMUONResponseTrigger.h"
2a5f75ae 31#include "AliMUONConstants.h"
7d7d22a6 32
a55f49a0 33#include "AliMpCDB.h"
34#include "AliMpDEManager.h"
35
36#include "AliLog.h"
37#include "AliCDBManager.h"
38#include "AliLoader.h"
39#include "AliRun.h"
40#include "AliRunLoader.h"
e41d0a3c 41
42#include "AliHeader.h"
43#include "AliGenCocktailEventHeader.h"
a55f49a0 44
3d1463c8 45//-----------------------------------------------------------------------------
ae6eeca4 46/// The sdigitizer performs the transformation from hits (energy deposits by
47/// the transport code) to sdigits (equivalent of charges on pad).
48///
49/// It does so by converting the energy deposit into a charge and then spreading
50/// this charge over several pads, according to the response function (a
51/// Mathieson distribution, basically).
52///
53/// See also AliMUONResponseV0, which is doing the real job (in DisIntegrate
54/// method), while this sdigitizer is just "steering" the process.
55///
56/// Please note that we do *not* merge sdigits after creation, which means
57/// that after sdigitization, a given pad can have several sdigits. This
58/// merging is taken care of later on by the digitizer(V3).
3d1463c8 59//-----------------------------------------------------------------------------
ae6eeca4 60
60d3a1d3 61ClassImp(AliMUONSDigitizerV2)
62
2a5f75ae 63Float_t AliMUONSDigitizerV2::fgkMaxIntTime = 10.0;
64Float_t AliMUONSDigitizerV2::fgkMaxPosTimeDif = 1.22E-6;
65Float_t AliMUONSDigitizerV2::fgkMaxNegTimeDif = -3.5E-6;
66Float_t AliMUONSDigitizerV2::fgkMinTimeDif = 25E-9;
67
60d3a1d3 68//_____________________________________________________________________________
69AliMUONSDigitizerV2::AliMUONSDigitizerV2()
f21fc003 70: TNamed("AliMUONSDigitizerV2","From Hits to SDigits for MUON")
60d3a1d3 71{
71a2d3aa 72 ///
73 /// ctor.
74 ///
a55f49a0 75
76 // Load mapping
77 if ( ! AliMpCDB::LoadMpSegmentation() ) {
88544f7e 78 AliFatal("Could not access mapping from OCDB !");
79 }
60d3a1d3 80}
81
82//_____________________________________________________________________________
83AliMUONSDigitizerV2::~AliMUONSDigitizerV2()
84{
71a2d3aa 85 ///
86 /// dtor.
87 ///
60d3a1d3 88}
89
90//_____________________________________________________________________________
91void
f21fc003 92AliMUONSDigitizerV2::Digitize(Option_t*)
60d3a1d3 93{
71a2d3aa 94 ///
95 /// Go from hits to sdigits.
96 ///
97 /// In the code below, apart from the loop itself (which look complicated
98 /// but is really only a loop on each hit in the input file) the main
99 /// work is done in AliMUONResponse::DisIntegrate method, which converts
100 /// a single hit in (possibly) several sdigits.
101 ///
60d3a1d3 102
103 AliDebug(1,"");
104
33c3c91a 105 AliRunLoader* runLoader = AliRunLoader::Instance();
445096c0 106 AliLoader* loader = runLoader->GetDetectorLoader("MUON");
f951b91b 107
445096c0 108 loader->LoadHits("READ");
60d3a1d3 109
60d3a1d3 110 AliMUON* muon = static_cast<AliMUON*>(gAlice->GetModule("MUON"));
7d7d22a6 111
ae6eeca4 112 Int_t nofEvents(runLoader->GetNumberOfEvents());
445096c0 113
5ee595ac 114 TString classname = muon->DigitStoreClassName();
445096c0 115
5ee595ac 116 AliMUONVDigitStore* sDigitStore = AliMUONVDigitStore::Create(classname.Data());
117
118 if (!sDigitStore)
119 {
120 AliFatal(Form("Could not create digitstore of class %s",classname.Data()));
121 }
122
68cf1410 123 AliDebug(1,Form("Will use digitStore of type %s",sDigitStore->ClassName()));
f951b91b 124
2a5f75ae 125 // average arrival time to chambers, for pileup studies
f951b91b 126
60d3a1d3 127 for ( Int_t iEvent = 0; iEvent < nofEvents; ++iEvent )
128 {
2c6e3e30 129 // Loop over events.
60d3a1d3 130 TObjArray tdlist;
131 tdlist.SetOwner(kTRUE);
132
133 AliDebug(1,Form("iEvent=%d",iEvent));
134 runLoader->GetEvent(iEvent);
f951b91b 135
9da52a4f 136 // for pile up studies
9ee1d6ff 137 float t0=fgkMaxIntTime; int aa=0;
e41d0a3c 138 AliHeader* header = runLoader->GetHeader();
139 AliGenCocktailEventHeader* cocktailHeader =
f951b91b 140 dynamic_cast<AliGenCocktailEventHeader*>(header->GenEventHeader());
e41d0a3c 141 if (cocktailHeader) {
142 AliGenCocktailEventHeader* genEventHeader = (AliGenCocktailEventHeader*) (header->GenEventHeader());
143 TList* headers = genEventHeader->GetHeaders();
144 TIter nextH(headers);
145 AliGenEventHeader *entry;
146 while((entry = (AliGenEventHeader*)nextH())) {
f951b91b 147 float t = entry->InteractionTime();
148 if (TMath::Abs(t)<TMath::Abs(t0)) t0 = t;
149 aa++;
e41d0a3c 150 }
151 } else {
152 AliGenEventHeader* evtHeader =
f951b91b 153 (AliGenEventHeader*)(header->GenEventHeader());
154 if (evtHeader)
155 {
156 float t = evtHeader->InteractionTime();
157 if (TMath::Abs(t)<TMath::Abs(t0)) t0 = t;
158 aa++;
159 }
160 else
161 {
162 // some generators may not offer a header, if which
163 // case we cannot get the interaction time, so we assume zero
164 t0 = 0.;
165 }
9da52a4f 166 }
f951b91b 167
445096c0 168 loader->MakeSDigitsContainer();
f951b91b 169
445096c0 170 TTree* treeS = loader->TreeS();
f951b91b 171
60d3a1d3 172 if ( !treeS )
173 {
445096c0 174 AliFatal("");
60d3a1d3 175 }
f951b91b 176
445096c0 177 sDigitStore->Connect(*treeS);
178
179 TTree* treeH = loader->TreeH();
f951b91b 180
445096c0 181 AliMUONVHitStore* hitStore = AliMUONVHitStore::Create(*treeH);
182 hitStore->Connect(*treeH);
60d3a1d3 183
60d3a1d3 184 Long64_t nofTracks = treeH->GetEntries();
445096c0 185
60d3a1d3 186 for ( Long64_t iTrack = 0; iTrack < nofTracks; ++iTrack )
187 {
2c6e3e30 188 // Loop over the tracks of this event.
60d3a1d3 189 treeH->GetEvent(iTrack);
f951b91b 190
445096c0 191 AliMUONHit* hit;
192 TIter next(hitStore->CreateIterator());
193 Int_t ihit(0);
194
195 while ( ( hit = static_cast<AliMUONHit*>(next()) ) )
60d3a1d3 196 {
f951b91b 197 Int_t chamberId = hit->Chamber()-1;
198 Float_t age = hit->Age()-t0;
199
60d3a1d3 200 AliMUONChamber& chamber = muon->Chamber(chamberId);
201 AliMUONResponse* response = chamber.ResponseModel();
2c6e3e30 202
203 // This is the heart of this method : the dis-integration
204 TList digits;
f951b91b 205 if (aa>1){ // if there are pileup events
206 Float_t chamberTime = AliMUONConstants::AverageChamberT(chamberId);
207 Float_t timeDif=age-chamberTime;
208 if (timeDif>fgkMaxPosTimeDif || timeDif<fgkMaxNegTimeDif) {
209 continue;
210 }
211 if(TMath::Abs(timeDif)>fgkMinTimeDif){
212 response->DisIntegrate(*hit,digits,timeDif);
213 }
214 else{
215 response->DisIntegrate(*hit,digits,0.);
216 }
217 }
218 else{
219 response->DisIntegrate(*hit,digits,0.);
220 }
2c6e3e30 221
bf0d3528 222 TIter nextd(&digits);
445096c0 223 AliMUONVDigit* d;
bf0d3528 224 while ( ( d = (AliMUONVDigit*)nextd() ) )
60d3a1d3 225 {
2c6e3e30 226 // Update some sdigit information that could not be known
227 // by the DisIntegrate method
60d3a1d3 228 d->SetHit(ihit);
f951b91b 229 d->SetTime(age);
2e2d0c44 230 d->AddTrack(hit->GetTrack(),d->Charge());
60d3a1d3 231 tdlist.Add(d);
232 }
445096c0 233 ++ihit;
60d3a1d3 234 }
445096c0 235 hitStore->Clear();
2c6e3e30 236 } // end of loop on tracks within an event
237
445096c0 238 TIter next(&tdlist);
239 AliMUONVDigit* d;
240
241 while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
60d3a1d3 242 {
05315e71 243 d->ChargeInFC(kTRUE);
244
245 AliMUONVDigit* added = sDigitStore->Add(*d,AliMUONVDigitStore::kMerge);
246 if (!added)
60d3a1d3 247 {
05315e71 248 AliError("Could not add digit to digitStore");
60d3a1d3 249 }
250 }
f951b91b 251
445096c0 252 treeS->Fill();
253
254 loader->WriteSDigits("OVERWRITE");
255
256 sDigitStore->Clear();
257
258 loader->UnloadSDigits();
259
260 delete hitStore;
2c6e3e30 261
60d3a1d3 262 } // loop on events
263
445096c0 264 loader->UnloadHits();
265
266 delete sDigitStore;
f951b91b 267
60d3a1d3 268}