Fix for Savannah bug 63624 (M.Masera)
[u/mrichter/AliRoot.git] / STEER / AliModule.cxx
CommitLineData
4c039060 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
acd84897 16/* $Id$ */
4c039060 17
8494b010 18///////////////////////////////////////////////////////////////////////////////
19// //
20// Base class for ALICE modules. Both sensitive modules (Modules) and //
21// non-sensitive ones are described by this base class. This class //
22// supports the hit and digit trees produced by the simulation and also //
23// the objects produced by the reconstruction. //
24// //
25// This class is also responsible for building the geometry of the //
26// Modules. //
27// //
28//Begin_Html
29/*
1439f98e 30<img src="picts/AliModuleClass.gif">
8494b010 31*/
32//End_Html
33// //
34///////////////////////////////////////////////////////////////////////////////
88cb7938 35
116cbefd 36#include <TObjArray.h>
2cad796f 37#include <TClonesArray.h>
38#include <TTree.h>
116cbefd 39#include <TSystem.h>
2cad796f 40#include <TDirectory.h>
5d12ce38 41#include <TVirtualMC.h>
4a9de4af 42#include <TGeoManager.h>
43#include <TString.h>
94de3818 44
594d8990 45#include "AliLog.h"
88cb7938 46#include "AliConfig.h"
47#include "AliLoader.h"
48#include "AliMagF.h"
8494b010 49#include "AliModule.h"
50#include "AliRun.h"
2cad796f 51#include "AliTrackReference.h"
5d12ce38 52#include "AliMC.h"
00b459eb 53#include "AliRawDataHeader.h"
54
362c9d61 55#include "AliDAQ.h"
8494b010 56
57ClassImp(AliModule)
58
00ee7999 59Float_t AliModule::fgDensityFactor = 1.0;
60
e2afb3b6 61//_______________________________________________________________________
62AliModule::AliModule():
e2afb3b6 63 fIdtmed(0),
64 fIdmate(0),
65 fLoMedium(0),
66 fHiMedium(0),
67 fActive(0),
2cad796f 68 fEnable(1),
2cad796f 69 fMaxIterTrackRef(0),
6d7874d8 70 fCurrentIterTrackRef(0),
71 fRunLoader(0)
8494b010 72{
73 //
74 // Default constructor for the AliModule class
75 //
8494b010 76}
77
e2afb3b6 78//_______________________________________________________________________
79AliModule::AliModule(const char* name,const char *title):
80 TNamed(name,title),
e2afb3b6 81 fIdtmed(new TArrayI(100)),
82 fIdmate(new TArrayI(100)),
83 fLoMedium(65536),
84 fHiMedium(0),
85 fActive(0),
2cad796f 86 fEnable(1),
2cad796f 87 fMaxIterTrackRef(0),
6d7874d8 88 fCurrentIterTrackRef(0),
89 fRunLoader(0)
8494b010 90{
91 //
92 // Normal constructor invoked by all Modules.
93 // Create the list for Module specific histograms
94 // Add this Module to the global list of Modules in Run.
95 //
8494b010 96 // Get the Module numeric ID
2e42b4d4 97
8494b010 98 Int_t id = gAlice->GetModuleID(name);
02ca2762 99 if (id>=0) {
100 // Module already added !
594d8990 101 AliWarning(Form("Module: %s already present at %d",name,id));
8494b010 102 return;
103 }
104 //
105 // Add this Module to the list of Modules
88cb7938 106
107 gAlice->AddModule(this);
108
e939a978 109 //PH SetMarkerColor(3);
8494b010 110 //
e2afb3b6 111 // Clear space for tracking media and material indexes
112
8494b010 113 for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
8494b010 114}
115
e2afb3b6 116//_______________________________________________________________________
8494b010 117AliModule::~AliModule()
118{
119 //
120 // Destructor
121 //
e2afb3b6 122
bbcea4df 123 // Remove this Module from the list of Modules
a9cc22a2 124 if (gAlice) {
125 TObjArray * modules = gAlice->Modules();
126 if (modules) modules->Remove(this);
127 }
7dd2cbe4 128
8494b010 129 // Delete TArray objects
130 delete fIdtmed;
131 delete fIdmate;
88cb7938 132
7dd2cbe4 133}
8494b010 134
e2afb3b6 135//_______________________________________________________________________
8494b010 136void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
e2afb3b6 137 Float_t z, Float_t dens, Float_t radl,
138 Float_t absl, Float_t *buf, Int_t nwbuf) const
8494b010 139{
140 //
141 // Store the parameters for a material
142 //
143 // imat the material index will be stored in (*fIdmate)[imat]
144 // name material name
145 // a atomic mass
146 // z atomic number
147 // dens density
148 // radl radiation length
149 // absl absorbtion length
150 // buf adress of an array user words
151 // nwbuf number of user words
152 //
153 Int_t kmat;
4a9de4af 154 //Build the string uniquename as "DET_materialname"
155 TString uniquename = GetName();
156 uniquename.Append("_");
157 uniquename.Append(name);
158 //if geometry loaded from file only fill fIdmate, else create material too
159 if(gAlice->IsRootGeometry()){
160 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
161 kmat = mat->GetUniqueID();
162 (*fIdmate)[imat]=kmat;
163 }else{
00ee7999 164 if (fgDensityFactor != 1.0)
165 AliWarning(Form("Material density multiplied by %.2f!", fgDensityFactor));
166 gMC->Material(kmat, uniquename.Data(), a, z, dens * fgDensityFactor, radl, absl, buf, nwbuf);
4a9de4af 167 (*fIdmate)[imat]=kmat;
168 }
8494b010 169}
b60e0f5e 170
e2afb3b6 171//_______________________________________________________________________
0afa509f 172void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
e2afb3b6 173 Float_t &z, Float_t &dens, Float_t &radl,
116cbefd 174 Float_t &absl) const
0afa509f 175{
176 //
177 // Store the parameters for a material
178 //
179 // imat the material index will be stored in (*fIdmate)[imat]
180 // name material name
181 // a atomic mass
182 // z atomic number
183 // dens density
184 // radl radiation length
185 // absl absorbtion length
186 // buf adress of an array user words
187 // nwbuf number of user words
188 //
189
190 Float_t buf[10];
191 Int_t nwbuf, kmat;
192 kmat=(*fIdmate)[imat];
193 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
194}
b60e0f5e 195
8494b010 196
e2afb3b6 197//_______________________________________________________________________
8494b010 198void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
e2afb3b6 199 Float_t *z, Float_t dens, Int_t nlmat,
200 Float_t *wmat) const
8494b010 201{
202 //
203 // Defines mixture or compound imat as composed by
204 // nlmat materials defined by arrays a, z and wmat
205 //
206 // If nlmat > 0 wmat contains the proportion by
207 // weights of each basic material in the mixture
208 //
209 // If nlmat < 0 wmat contains the number of atoms
210 // of eack kind in the molecule of the compound
211 // In this case, wmat is changed on output to the relative weigths.
212 //
213 // imat the material index will be stored in (*fIdmate)[imat]
214 // name material name
215 // a array of atomic masses
216 // z array of atomic numbers
217 // dens density
218 // nlmat number of components
219 // wmat array of concentrations
220 //
221 Int_t kmat;
4a9de4af 222 //Build the string uniquename as "DET_mixturename"
223 TString uniquename = GetName();
224 uniquename.Append("_");
225 uniquename.Append(name);
226 //if geometry loaded from file only fill fIdmate, else create mixture too
227 if(gAlice->IsRootGeometry()){
228 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
229 kmat = mat->GetUniqueID();
230 (*fIdmate)[imat]=kmat;
231 }else{
00ee7999 232 if (fgDensityFactor != 1.0)
233 AliWarning(Form("Material density multiplied by %.2f!", fgDensityFactor));
234 gMC->Mixture(kmat, uniquename.Data(), a, z, dens * fgDensityFactor, nlmat, wmat);
4a9de4af 235 (*fIdmate)[imat]=kmat;
236 }
8494b010 237}
b60e0f5e 238
e2afb3b6 239//_______________________________________________________________________
8494b010 240void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
e2afb3b6 241 Int_t isvol, Int_t ifield, Float_t fieldm,
242 Float_t tmaxfd, Float_t stemax, Float_t deemax,
243 Float_t epsil, Float_t stmin, Float_t *ubuf,
244 Int_t nbuf) const
8494b010 245{
246 //
247 // Store the parameters of a tracking medium
248 //
b13db077 249 // numed the medium number is stored into (*fIdtmed)[numed]
8494b010 250 // name medium name
251 // nmat the material number is stored into (*fIdmate)[nmat]
252 // isvol sensitive volume if isvol!=0
253 // ifield magnetic field flag (see below)
254 // fieldm maximum magnetic field
255 // tmaxfd maximum deflection angle due to magnetic field
256 // stemax maximum step allowed
257 // deemax maximum fractional energy loss in one step
258 // epsil tracking precision in cm
259 // stmin minimum step due to continuous processes
260 //
261 // ifield = 0 no magnetic field
262 // = -1 user decision in guswim
263 // = 1 tracking performed with Runge Kutta
264 // = 2 tracking performed with helix
265 // = 3 constant magnetic field along z
266 //
267 Int_t kmed;
4a9de4af 268 //Build the string uniquename as "DET_mediumname"
269 TString uniquename = GetName();
270 uniquename.Append("_");
271 uniquename.Append(name);
272 //if geometry loaded from file only fill fIdtmed, else create medium too
273 if(gAlice->IsRootGeometry()){
274 TGeoMedium *med = gGeoManager->GetMedium(uniquename.Data());
275 kmed = med->GetId();
276 (*fIdtmed)[numed]=kmed;
277 }else{
278 gMC->Medium(kmed, uniquename.Data(), (*fIdmate)[nmat], isvol, ifield,
279 fieldm, tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
280 (*fIdtmed)[numed]=kmed;
281 }
8494b010 282}
b60e0f5e 283
e2afb3b6 284//_______________________________________________________________________
8494b010 285void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
e2afb3b6 286 Float_t theta2, Float_t phi2, Float_t theta3,
287 Float_t phi3) const
8494b010 288{
289 //
290 // Define a rotation matrix. Angles are in degrees.
291 //
292 // nmat on output contains the number assigned to the rotation matrix
293 // theta1 polar angle for axis I
294 // phi1 azimuthal angle for axis I
295 // theta2 polar angle for axis II
296 // phi2 azimuthal angle for axis II
297 // theta3 polar angle for axis III
298 // phi3 azimuthal angle for axis III
299 //
cfce8870 300 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
8494b010 301}
302
e2afb3b6 303//_______________________________________________________________________
65fb704d 304Float_t AliModule::ZMin() const
305{
306 return -500;
307}
308
e2afb3b6 309//_______________________________________________________________________
65fb704d 310Float_t AliModule::ZMax() const
311{
312 return 500;
313}
314
e2afb3b6 315//_______________________________________________________________________
4787b401 316void AliModule::AddAlignableVolumes() const
317{
318 //
40d37d9d 319 if (IsActive())
320 AliWarning(Form(" %s still has to implement the AddAlignableVolumes method!",GetName()));
4787b401 321}
322
323//_______________________________________________________________________
88cb7938 324
df75403f 325AliLoader* AliModule::MakeLoader(const char* /*topfoldername*/)
326{
327 return 0x0;
328}
8494b010 329
2cad796f 330
88cb7938 331//_____________________________________________________________________________
9ac3aec9 332AliTrackReference* AliModule::AddTrackReference(Int_t label, Int_t id){
2cad796f 333 //
334 // add a trackrefernce to the list
9ac3aec9 335 return (gAlice->GetMCApp()->AddTrackReference(label, id));
2cad796f 336}
337
88cb7938 338//_____________________________________________________________________________
339TTree* AliModule::TreeTR()
340{
1bb2a43c 341 //
342 // Return TR tree pointer
343 //
6d7874d8 344 if ( fRunLoader == 0x0)
88cb7938 345 {
594d8990 346 AliError("Can not get the run loader");
88cb7938 347 return 0x0;
348 }
349
6d7874d8 350 TTree* tree = fRunLoader->TreeTR();
88cb7938 351 return tree;
352}
0421c3d1 353
354
355//_____________________________________________________________________________
356void AliModule::Digits2Raw()
357{
358// This is a dummy version that just copies the digits file contents
359// to a raw data file.
360
594d8990 361 AliWarning(Form("Dummy version called for %s", GetName()));
0421c3d1 362
362c9d61 363 Int_t nDDLs = AliDAQ::NumberOfDdls(GetName());
0421c3d1 364
365 if (!GetLoader()) return;
366 fstream digitsFile(GetLoader()->GetDigitsFileName(), ios::in);
367 if (!digitsFile) return;
368
369 digitsFile.seekg(0, ios::end);
370 UInt_t size = digitsFile.tellg();
3c166bf6 371 UInt_t ddlSize = 4 * (size / (4*nDDLs));
0421c3d1 372 Char_t* buffer = new Char_t[ddlSize+1];
373
374 for (Int_t iDDL = 0; iDDL < nDDLs; iDDL++) {
375 char fileName[20];
362c9d61 376 strcpy(fileName,AliDAQ::DdlFileName(GetName(),iDDL));
0421c3d1 377 fstream rawFile(fileName, ios::out);
378 if (!rawFile) return;
379
380 AliRawDataHeader header;
381 header.fSize = ddlSize + sizeof(header);
382 rawFile.write((char*) &header, sizeof(header));
383
384 digitsFile.read(buffer, ddlSize);
385 rawFile.write(buffer, ddlSize);
386 rawFile.close();
387 }
388
389 digitsFile.close();
390 delete[] buffer;
391}