]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDclusterizer.cxx
Fixes in the I/O
[u/mrichter/AliRoot.git] / TRD / AliTRDclusterizer.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.18  2003/03/19 17:16:23  hristov
19 Fixes for the file managment(T.Kuhr)
20
21 Revision 1.17  2003/02/10 11:09:26  cblume
22 Further modifications in OpenOutput and WriteCluster
23
24 Revision 1.16  2003/02/07 09:45:24  cblume
25 Modification in OpenOutput() for many events in single file
26
27 Revision 1.15  2002/11/07 15:52:09  cblume
28 Update of tracking code for tilted pads
29
30 Revision 1.14  2002/10/14 14:57:43  hristov
31 Merging the VirtualMC branch to the main development branch (HEAD)
32
33 Revision 1.11.6.2  2002/10/11 07:26:37  hristov
34 Updating VirtualMC to v3-09-02
35
36 Revision 1.13  2002/09/18 09:20:53  cblume
37 Write the parameter class into the cluster file
38
39 Revision 1.12  2002/06/12 09:54:35  cblume
40 Update of tracking code provided by Sergei
41
42 Revision 1.11  2001/11/27 08:50:33  hristov
43 BranchOld replaced by Branch
44
45 Revision 1.10  2001/11/14 10:50:45  cblume
46 Changes in digits IO. Add merging of summable digits
47
48 Revision 1.9  2001/10/21 18:30:02  hristov
49 Several pointers were set to zero in the default constructors to avoid memory management problems
50
51 Revision 1.8  2001/05/07 08:06:44  cblume
52 Speedup of the code. Create only AliTRDcluster
53
54 Revision 1.7  2001/03/30 14:40:14  cblume
55 Update of the digitization parameter
56
57 Revision 1.6  2000/11/01 14:53:20  cblume
58 Merge with TRD-develop
59
60
61 Revision 1.1.4.5  2000/10/15 23:40:01  cblume
62 Remove AliTRDconst
63
64 Revision 1.1.4.4  2000/10/06 16:49:46  cblume
65 Made Getters const
66
67 Revision 1.1.4.3  2000/10/04 16:34:58  cblume
68 Replace include files by forward declarations
69
70 Revision 1.1.4.2  2000/09/22 14:49:49  cblume
71 Adapted to tracking code
72
73 Revision 1.5  2000/10/02 21:28:19  fca
74 Removal of useless dependecies via forward declarations
75
76 Revision 1.4  2000/06/09 11:10:07  cblume
77 Compiler warnings and coding conventions, next round
78
79 Revision 1.3  2000/06/08 18:32:58  cblume
80 Make code compliant to coding conventions
81
82 Revision 1.2  2000/05/08 16:17:27  cblume
83 Merge TRD-develop
84
85 Revision 1.1.4.1  2000/05/08 15:08:03  cblume
86 Remove the class AliTRDcluster
87
88 Revision 1.4  2000/06/09 11:10:07  cblume
89 Compiler warnings and coding conventions, next round
90
91 Revision 1.3  2000/06/08 18:32:58  cblume
92 Make code compliant to coding conventions
93
94 Revision 1.2  2000/05/08 16:17:27  cblume
95 Merge TRD-develop
96
97 Revision 1.1.4.1  2000/05/08 15:08:03  cblume
98 Remove the class AliTRDcluster
99
100 Revision 1.1  2000/02/28 18:57:58  cblume
101 Add new TRD classes
102
103 */
104
105 ///////////////////////////////////////////////////////////////////////////////
106 //                                                                           //
107 //  TRD cluster finder base class                                            //
108 //                                                                           //
109 ///////////////////////////////////////////////////////////////////////////////
110
111 #include <TROOT.h>
112 #include <TTree.h>
113 #include <TFile.h>
114
115 #include "AliRun.h"
116 #include "AliTRD.h"
117 #include "AliTRDclusterizer.h"
118 #include "AliTRDcluster.h"
119 #include "AliTRDrecPoint.h"
120 #include "AliTRDgeometry.h"
121 #include "AliTRDparameter.h"
122
123 ClassImp(AliTRDclusterizer)
124
125 //_____________________________________________________________________________
126 AliTRDclusterizer::AliTRDclusterizer():TNamed()
127 {
128   //
129   // AliTRDclusterizer default constructor
130   //
131
132   fInputFile   = NULL;
133   fInputFileCreated   = kFALSE;
134   fOutputFile  = NULL;
135   fOutputFileCreated  = kFALSE;
136   fClusterTree = NULL;
137   fTRD         = 0;
138   fEvent       = 0;
139   fVerbose     = 0;
140   fPar         = 0;
141
142 }
143
144 //_____________________________________________________________________________
145 AliTRDclusterizer::AliTRDclusterizer(const Text_t* name, const Text_t* title)
146                   :TNamed(name, title)
147 {
148   //
149   // AliTRDclusterizer default constructor
150   //
151
152   fInputFile   = NULL;
153   fOutputFile  = NULL;
154   fClusterTree = NULL;
155   fEvent       = 0;
156   fVerbose     = 0;
157   fPar         = 0;
158
159 }
160
161 //_____________________________________________________________________________
162 AliTRDclusterizer::AliTRDclusterizer(const AliTRDclusterizer &c)
163 {
164   //
165   // AliTRDclusterizer copy constructor
166   //
167
168   ((AliTRDclusterizer &) c).Copy(*this);
169
170 }
171
172 //_____________________________________________________________________________
173 AliTRDclusterizer::~AliTRDclusterizer()
174 {
175   //
176   // AliTRDclusterizer destructor
177   //
178
179   if (fInputFile && fInputFileCreated) {
180     fInputFile->Close();
181     delete fInputFile;
182   }
183
184   if (fOutputFile && fOutputFileCreated) {
185     fOutputFile->Close();
186     delete fOutputFile;
187   }
188
189 }
190
191 //_____________________________________________________________________________
192 AliTRDclusterizer &AliTRDclusterizer::operator=(const AliTRDclusterizer &c)
193 {
194   //
195   // Assignment operator
196   //
197
198   if (this != &c) ((AliTRDclusterizer &) c).Copy(*this);
199   return *this;
200
201 }
202
203 //_____________________________________________________________________________
204 void AliTRDclusterizer::Copy(TObject &c)
205 {
206   //
207   // Copy function
208   //
209
210   ((AliTRDclusterizer &) c).fInputFile   = NULL;
211   ((AliTRDclusterizer &) c).fOutputFile  = NULL;
212   ((AliTRDclusterizer &) c).fClusterTree = NULL;
213   ((AliTRDclusterizer &) c).fEvent       = 0;  
214   ((AliTRDclusterizer &) c).fVerbose     = fVerbose;  
215   ((AliTRDclusterizer &) c).fPar         = 0;
216
217 }
218
219 //_____________________________________________________________________________
220 Bool_t AliTRDclusterizer::Open(const Char_t *name, Int_t nEvent)
221 {
222   //
223   // Opens the AliROOT file. Output and input are in the same file
224   //
225
226   OpenInput(name,nEvent);
227   OpenOutput(name);
228
229   return kTRUE;
230
231 }
232
233 //_____________________________________________________________________________
234 Bool_t AliTRDclusterizer::Open(const Char_t *inname, const Char_t *outname
235                               , Int_t nEvent)
236 {
237   //
238   // Opens the AliROOT file. Output and input are in different files
239   //
240
241   OpenInput(inname,nEvent);
242   OpenOutput(outname);
243
244   return kTRUE;
245
246 }
247
248 //_____________________________________________________________________________
249 Bool_t AliTRDclusterizer::OpenOutput(const Char_t *name)
250 {
251   //
252   // Open the output file
253   //
254
255   TDirectory *savedir = NULL;
256
257   if (!fInputFile) {
258     printf("<AliTRDclusterizer::OpenOutput> ");
259     printf("No input file defined\n");
260     return kFALSE;
261   }
262
263   if (strcmp(name,fInputFile->GetName()) != 0) {
264     savedir = gDirectory;
265     TFile *file = (TFile *) gROOT->FindObject(name);
266     if (file) {
267       printf("<AliTRDclusterizer::OpenOutput> ");
268       printf("Use same file for output: %s.\n",name);
269       fOutputFile = file;
270       fOutputFile->cd();
271     }
272     else {
273       printf("<AliTRDclusterizer::OpenOutput> ");
274       printf("Open the output file %s.\n",name);
275       fOutputFile = new TFile(name,"RECREATE");
276       fOutputFileCreated = kTRUE;
277     }
278   }
279   else {
280     fOutputFile = fInputFile;
281   }
282
283   // Create a tree for the cluster
284   Char_t treeName[12];  sprintf(treeName,"TreeR%d_TRD",fEvent);
285   fClusterTree = new TTree(treeName,"TRD cluster");
286   TObjArray *ioArray = 0;
287   fClusterTree->Branch("TRDcluster","TObjArray",&ioArray,32000,0);
288
289   if (savedir) {
290     savedir->cd();
291   }
292
293   return kTRUE;
294
295 }
296
297 //_____________________________________________________________________________
298 Bool_t AliTRDclusterizer::OpenInput(const Char_t *name, Int_t nEvent)
299 {
300   //
301   // Opens a ROOT-file with TRD-hits and reads in the digits-tree
302   //
303
304   // Connect the AliRoot file containing Geometry, Kine, and Hits
305   fInputFile = (TFile*) gROOT->GetListOfFiles()->FindObject(name);
306   if (!fInputFile) {
307     printf("AliTRDclusterizer::OpenInput -- ");
308     printf("Open the ALIROOT-file %s.\n",name);
309     fInputFile = new TFile(name,"UPDATE");
310     fInputFileCreated = kTRUE;
311   }
312   else {
313     printf("AliTRDclusterizer::OpenInput -- ");
314     printf("%s is already open.\n",name);
315   }
316
317   // Get AliRun object from file
318   if (gAlice) { 
319     delete gAlice;
320     gAlice = 0;
321   }
322   if (!(gAlice)) gAlice = (AliRun *) fInputFile->Get("gAlice");
323   if (!(gAlice)) {
324     printf("AliTRDclusterizer::OpenInput -- ");
325     printf("Could not find AliRun object.\n");
326     return kFALSE;
327   }
328
329   fEvent = nEvent;
330
331   // Import the Trees for the event nEvent in the file
332   Int_t nparticles = gAlice->GetEvent(fEvent);
333   if (nparticles <= 0) {
334     printf("AliTRDclusterizer::OpenInput -- ");
335     printf("No entries in the trees for event %d.\n",fEvent);
336     return kFALSE;
337   }
338
339   // Get the TRD object
340   fTRD = (AliTRD*) gAlice->GetDetector("TRD"); 
341   if (!fTRD) {
342     printf("AliTRDclusterizer::OpenInput -- ");
343     printf("No TRD detector object found\n");
344     return kFALSE;
345   }
346
347   return kTRUE;
348
349 }
350
351 //_____________________________________________________________________________
352 Bool_t AliTRDclusterizer::WriteClusters(Int_t det)
353 {
354   //
355   // Fills TRDcluster branch in the tree with the clusters 
356   // found in detector = det. For det=-1 writes the tree. 
357   //
358
359   if ((det < -1) || (det >= AliTRDgeometry::Ndet())) {
360     printf("AliTRDclusterizer::WriteClusters -- ");
361     printf("Unexpected detector index %d.\n",det);
362     return kFALSE;
363   }
364  
365   TDirectory *savedir = gDirectory;
366
367   if (fOutputFile) {
368     fOutputFile->cd();
369   }
370
371   TBranch *branch = fClusterTree->GetBranch("TRDcluster");
372   if (!branch) {
373     TObjArray *ioArray = 0;
374     branch = fClusterTree->Branch("TRDcluster","TObjArray",&ioArray,32000,0);
375   }
376
377   if ((det >= 0) && (det < AliTRDgeometry::Ndet())) {
378
379     Int_t nRecPoints = fTRD->RecPoints()->GetEntriesFast();
380     TObjArray *detRecPoints = new TObjArray(400);
381
382     for (Int_t i = 0; i < nRecPoints; i++) {
383       AliTRDcluster *c = (AliTRDcluster *) fTRD->RecPoints()->UncheckedAt(i);
384       if (det == c->GetDetector()) {
385         detRecPoints->AddLast(c);
386       }
387       else {
388         printf("AliTRDclusterizer::WriteClusters --");
389         printf("Attempt to write a cluster with unexpected detector index\n");
390       }
391     }
392
393     branch->SetAddress(&detRecPoints);
394     fClusterTree->Fill();
395
396     return kTRUE;
397
398   }
399
400   if (det == -1) {
401
402     printf("AliTRDclusterizer::WriteClusters -- ");
403     printf("Writing the cluster tree %-18s for event %d.\n"
404           ,fClusterTree->GetName(),fEvent);
405
406     fClusterTree->Write();
407
408     AliTRDgeometry *geo = fTRD->GetGeometry();
409     if (!fOutputFile->Get("TRDgeometry")) {
410       geo->SetName("TRDgeometry");
411       geo->Write();
412     }
413     if (!fOutputFile->Get("TRDparameter")) {
414       fPar->Write();     
415     }    
416
417     return kTRUE;  
418
419   }
420   
421   savedir->cd();
422
423   printf("AliTRDclusterizer::WriteClusters -- ");
424   printf("Unexpected detector index %d.\n",det);
425  
426   return kFALSE;  
427
428 }
429
430
431