1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //______________________________________________________________________________
19 ////////////////////////////////////////////////////////////////////////////////
21 // class AliReaderAOD //
23 // Reader and Writer for AOD format. //
24 // AODs are stored in a tree named by the variable fgkTreeName. //
25 // There is stored 1 or 2 branches. Each of them stores AOD objects //
26 // First branch is named by the variable fgkReconstructedDataBranchName //
27 // ("reconstructed.") and keeps reconstructed data. //
28 // Second branch is called by the variable fgkSimulatedDataBranchName //
29 // ("simulated.") and stores Monte carlo truth. If both branches are present //
30 // AODs are parallel, i.e. nth particle in one branch corresponds to the nth //
31 // particle in the other one. //
33 // Since we accept different formats of particles that are stored in AODs //
34 // reader must take care of that fact: clean buffer if the next file contains //
35 // different particle type. //
37 // Piotr.Skowronski@cern.ch //
39 ////////////////////////////////////////////////////////////////////////////////
49 #include "AliReaderAOD.h"
51 const TString AliReaderAOD::fgkTreeName("TAOD");
52 const TString AliReaderAOD::fgkReconstructedDataBranchName("reconstructed.");
53 const TString AliReaderAOD::fgkSimulatedDataBranchName("simulated.");
55 ClassImp(AliReaderAOD)
57 AliReaderAOD::AliReaderAOD(const Char_t* aodfilename):
58 fFileName(aodfilename),
68 /********************************************************************/
70 AliReaderAOD::~AliReaderAOD()
73 if (fEventSim == fSimBuffer )
84 /********************************************************************/
86 void AliReaderAOD::Rewind()
96 /********************************************************************/
97 Int_t AliReaderAOD::ReadNext()
101 Info("ReadNext","Entered");
102 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
106 Int_t openfailed = OpenFile(fCurrentDir);//rl is opened here
109 //Error("ReadNext","Error Occured while opening directory number %d",fCurrentDir);
115 //Tree must exist because OpenFile would reuturn error in the other case
116 if ( fCurrentEvent >= fTree->GetEntries() )
132 Info("ReadNext","Getting event %d",fCurrentEvent);
133 fTree->GetEvent(fCurrentEvent);
134 Info("ReadNext","Getting event %d Done",fCurrentEvent);
137 if (fReadRec && fReadSim)
139 retval = ReadRecAndSim();
143 if (fReadRec) retval = ReadRec();
144 if (fReadSim) retval = ReadSim();
150 //something wrong has happend during reading this event, take next
155 return retval;//success -> read one event
157 }while(fCurrentDir < GetNumberOfDirs());//end of loop over directories specified in fDirs Obj Array
159 return 1; //no more directories to read
163 /********************************************************************/
165 Int_t AliReaderAOD::ReadRecAndSim()
167 //Reads raconstructed and simulated data
169 Info("ReadRecAndSim","Found %d reconstructed tracks and %d simulated particles",
170 fRecBuffer->GetNumberOfParticles(),fSimBuffer->GetNumberOfParticles());
172 if (fCuts->GetEntriesFast() == 0x0)
173 {//if there is no cuts we return pointer to the buffer
174 if (fEventRec != fRecBuffer)
179 fEventRec = fRecBuffer;//fEventRec is the pointer that the user gets when he asks about an event
180 fEventSim = fSimBuffer;
183 {//if there are cuts specified
184 if ( (fEventRec == 0x0) || (fEventRec == fRecBuffer) )
185 {//we need to create a new event, if it is not existing or it is the same as branch buffer
186 fEventRec = new AliAOD();
187 fEventSim = new AliAOD();
189 fEventRec->SetParticleClass( fRecBuffer->GetParticleClass() );
190 fEventSim->SetParticleClass( fSimBuffer->GetParticleClass() );
193 {//or simply reset it in case it already exists
198 Int_t npart = fRecBuffer->GetNumberOfParticles();
200 if (npart != fSimBuffer->GetNumberOfParticles())
202 Error("ReadRecAndSim","There is different number of simulated and reconstructed particles!",
203 fSimBuffer->GetNumberOfParticles(),npart);
206 for (Int_t i = 0; i < npart; i++)
208 AliVAODParticle* prec = fRecBuffer->GetParticle(i);
209 AliVAODParticle* psim = fSimBuffer->GetParticle(i);
213 Error("ReadRecAndSim","Reconstructed Particle is NULL !!!");
218 Error("ReadRecAndSim","Simulated Particle is NULL !!!");
222 if (Rejected(prec)) continue;//we make cuts only on reconstructed data
224 fEventRec->AddParticle(prec);
225 fEventSim->AddParticle( fSimBuffer->GetParticle(i));
229 Info("ReadRecAndSim","Read %d reconstructed tracks and %d simulated particles",
230 fEventRec->GetNumberOfParticles(),fEventSim->GetNumberOfParticles());
232 fTrackCounter->Fill(fEventRec->GetNumberOfParticles());
236 /********************************************************************/
238 Int_t AliReaderAOD::ReadRec()
240 //Reads reconstructed data only
242 Info("ReadRec","Found %d reconstructed tracks",fRecBuffer->GetNumberOfParticles());
244 if (fCuts->GetEntriesFast() == 0x0)
245 {//if there is no cuts we return pointer to the buffer
246 if (fEventRec != fRecBuffer)
250 fEventRec = fRecBuffer;//fEventRec is the pointer that the user gets when he asks about an event
253 {//if there are cuts specified
254 if ( (fEventRec == 0x0) || (fEventRec == fRecBuffer) )
255 {//we need to create a new event, if it is not existing or it is the same as branch buffer
256 fEventRec = new AliAOD();
258 fEventRec->SetParticleClass( fRecBuffer->GetParticleClass() );
261 {//or simply reset it in case it already exists
265 Int_t npart = fRecBuffer->GetNumberOfParticles();
266 for (Int_t i = 0; i < npart; i++)
268 AliVAODParticle* prec = fRecBuffer->GetParticle(i);
269 if (Rejected(prec)) continue;//we make cuts only on simulated data
271 fEventRec->AddParticle(prec);
275 Info("ReadRec","Read %d reconstructed tracks",fEventRec->GetNumberOfParticles());
276 fTrackCounter->Fill(fEventRec->GetNumberOfParticles());
280 /********************************************************************/
282 Int_t AliReaderAOD::ReadSim()
284 //Reads simulated data only
286 Info("ReadSim","Found %d simulated particles",fSimBuffer->GetNumberOfParticles());
288 if (fCuts->GetEntriesFast() == 0x0)
289 {//if there is no cuts we return pointer to the buffer
290 if (fEventSim != fSimBuffer)
294 fEventSim = fSimBuffer;
297 {//if there are cuts specified
298 if ( (fEventSim == 0x0) || (fEventSim == fSimBuffer) )
299 {//we need to create a new event, if it is not existing or it is the same as branch buffer
300 fEventSim = new AliAOD();
302 fEventSim->SetParticleClass( fSimBuffer->GetParticleClass() );
305 {//or simply reset it in case it already exists
309 Int_t npart = fSimBuffer->GetNumberOfParticles();
310 for (Int_t i = 0; i < npart; i++)
312 AliVAODParticle* prec = fSimBuffer->GetParticle(i);
313 if (Rejected(prec)) continue;//we make cuts only on simulated data
314 fEventSim->AddParticle(prec);
318 Info("ReadSim","Read %d simulated particles",fEventSim->GetNumberOfParticles());
319 fTrackCounter->Fill(fEventSim->GetNumberOfParticles());
324 /********************************************************************/
326 Int_t AliReaderAOD::OpenFile(Int_t n)
328 //opens fFile with tree
330 // Info("ReadNext","Opening File %d",n);
331 const TString dirname = GetDirName(n);
334 AliDebug(3,"Got empty string as a directory name.");
338 TString filename = dirname +"/"+ fFileName;
339 fFile = TFile::Open(filename.Data());
342 Error("OpenFile","Can't open fFile %s",filename.Data());
345 if (!fFile->IsOpen())
347 Error("OpenFile","Can't open fFile %s",filename.Data());
353 Info("ReadNext","File %s Is Opened, Getting the TREE",filename.Data());
355 fTree = dynamic_cast<TTree*>(fFile->Get(fgkTreeName));
358 AliDebug(3,Form("Can not find TTree object named %s",fgkTreeName.Data()));
364 // Info("ReadNext","Got TREE, Setting branch addresses");
368 TBranch* branch = fTree->GetBranch(fgkReconstructedDataBranchName);
371 Error("OpenFile","Can not find branch %s in file %s",
372 fgkReconstructedDataBranchName.Data(),filename.Data());
380 fTree->SetBranchAddress(fgkReconstructedDataBranchName,&fRecBuffer);
386 TBranch* branch = fTree->GetBranch(fgkSimulatedDataBranchName);
389 Error("OpenFile","Can not find branch %s in file %s",
390 fgkSimulatedDataBranchName.Data(),filename.Data());
398 fTree->SetBranchAddress(fgkSimulatedDataBranchName,&fSimBuffer);
400 // Info("ReadNext","Got TREE, Addresses are set.");
401 // Info("ReadNext","Quitting the method.");
406 /********************************************************************/
408 Int_t AliReaderAOD::WriteAOD(AliReader* reader, const char* outfilename, const char* pclassname, Bool_t /*multcheck*/)
410 //reads tracks from runs and writes them to file
411 ::Info("AliReaderAOD::Write","________________________________________________________");
412 ::Info("AliReaderAOD::Write","________________________________________________________");
413 ::Info("AliReaderAOD::Write","________________________________________________________");
417 ::Error("AliReaderAOD::Write","Input Reader is NULL");
420 TFile *outfile = TFile::Open(outfilename,"recreate");
423 ::Error("AliReaderAOD::Write","Can not open output file %s",outfilename);
427 TTree *tree = new TTree(fgkTreeName,"Tree with tracks");
429 TBranch *recbranch = 0x0, *simbranch = 0x0;
431 AliAOD* eventrec = new AliAOD();//must be created before Branch is called. Otherwise clones array is not splitted
432 AliAOD* eventsim = new AliAOD();//AOD together with fParticles clones array knowing exact type of particles
434 eventrec->SetParticleClassName(pclassname);
435 eventsim->SetParticleClassName(pclassname);
437 AliAOD* recbuffer = eventrec;
438 AliAOD* simbuffer = eventsim;
440 if (reader->ReadsRec()) recbranch = tree->Branch(fgkReconstructedDataBranchName,"AliAOD",&recbuffer,32000,99);
441 if (reader->ReadsSim()) simbranch = tree->Branch(fgkSimulatedDataBranchName,"AliAOD",&simbuffer,32000,99);
444 while (reader->Next() == kFALSE)
447 if (reader->ReadsRec())
448 {//here we can get AOD that has different particle type
449 AliAOD* event = reader->GetEventRec();
450 if ( eventrec->GetParticleClass() != event->GetParticleClass() )
451 {//if class type is not what what we whant we copy particles
452 eventrec->CopyData(event);
453 recbuffer = eventrec;
456 {//else just pointer to event from input reader is passed
461 if (reader->ReadsSim())
463 AliAOD* event = reader->GetEventSim();
464 if ( eventsim->GetParticleClass() != event->GetParticleClass() )
465 {//if class type is not what what we whant we copy particles
466 eventsim->CopyData(event);
467 simbuffer = eventrec;
470 {//else just pointer to event from input reader is passed
477 ::Info("AliReaderAOD::Write","Written %d events",tree->GetEntries());