1 #include "AliReaderAOD.h"
2 //______________________________________________________________________________
3 ////////////////////////////////////////////////////////////////////////////////
5 // class AliReaderAOD //
7 // Reader and Writer for AOD format. //
8 // AODs are stored in a tree named by the variable fgkTreeName. //
9 // There is stored 1 or 2 branches. Each of them stores AOD objects //
10 // First branch is named by the variable fgkReconstructedDataBranchName //
11 // ("reconstructed.") and keeps reconstructed data. //
12 // Second branch is called by the variable fgkSimulatedDataBranchName //
13 // ("simulated.") and stores Monte carlo truth. If both branches are present //
14 // AODs are parallel, i.e. nth particle in one branch corresponds to the nth //
15 // particle in the other one. //
17 // Since we accept different formats of particles that are stored in AODs //
18 // reader must take care of that fact: clean buffer if the next file contains //
19 // different particle type. //
21 // Piotr.Skowronski@cern.ch //
23 ////////////////////////////////////////////////////////////////////////////////
25 ClassImp(AliReaderAOD)
34 const TString AliReaderAOD::fgkTreeName("TAOD");
35 const TString AliReaderAOD::fgkReconstructedDataBranchName("reconstructed.");
36 const TString AliReaderAOD::fgkSimulatedDataBranchName("simulated.");
38 AliReaderAOD::AliReaderAOD(const Char_t* aodfilename):
39 fFileName(aodfilename),
49 /********************************************************************/
51 AliReaderAOD::~AliReaderAOD()
54 if (fEventSim == fSimBuffer )
65 /********************************************************************/
67 void AliReaderAOD::Rewind()
77 /********************************************************************/
78 Int_t AliReaderAOD::ReadNext()
82 Info("ReadNext","Entered");
83 do //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
87 Int_t openfailed = OpenFile(fCurrentDir);//rl is opened here
90 //Error("ReadNext","Error Occured while opening directory number %d",fCurrentDir);
96 //Tree must exist because OpenFile would reuturn error in the other case
97 if ( fCurrentEvent >= fTree->GetEntries() )
113 Info("ReadNext","Getting event %d",fCurrentEvent);
114 fTree->GetEvent(fCurrentEvent);
115 Info("ReadNext","Getting event %d Done",fCurrentEvent);
118 if (fReadRec && fReadSim)
120 retval = ReadRecAndSim();
124 if (fReadRec) retval = ReadRec();
125 if (fReadSim) retval = ReadSim();
131 //something wrong has happend during reading this event, take next
136 return retval;//success -> read one event
138 }while(fCurrentDir < GetNumberOfDirs());//end of loop over directories specified in fDirs Obj Array
140 return 1; //no more directories to read
144 /********************************************************************/
146 Int_t AliReaderAOD::ReadRecAndSim()
148 //Reads raconstructed and simulated data
150 Info("ReadRecAndSim","Found %d reconstructed tracks and %d simulated particles",
151 fRecBuffer->GetNumberOfParticles(),fSimBuffer->GetNumberOfParticles());
153 if (fCuts->GetEntriesFast() == 0x0)
154 {//if there is no cuts we return pointer to the buffer
155 if (fEventRec != fRecBuffer)
160 fEventRec = fRecBuffer;//fEventRec is the pointer that the user gets when he asks about an event
161 fEventSim = fSimBuffer;
164 {//if there are cuts specified
165 if ( (fEventRec == 0x0) || (fEventRec == fRecBuffer) )
166 {//we need to create a new event, if it is not existing or it is the same as branch buffer
167 fEventRec = new AliAOD();
168 fEventSim = new AliAOD();
170 fEventRec->SetParticleClass( fRecBuffer->GetParticleClass() );
171 fEventSim->SetParticleClass( fSimBuffer->GetParticleClass() );
174 {//or simply reset it in case it already exists
179 Int_t npart = fRecBuffer->GetNumberOfParticles();
181 if (npart != fSimBuffer->GetNumberOfParticles())
183 Error("ReadRecAndSim","There is different number of simulated and reconstructed particles!",
184 fSimBuffer->GetNumberOfParticles(),npart);
187 for (Int_t i = 0; i < npart; i++)
189 AliVAODParticle* prec = fRecBuffer->GetParticle(i);
190 AliVAODParticle* psim = fSimBuffer->GetParticle(i);
194 Error("ReadRecAndSim","Reconstructed Particle is NULL !!!");
199 Error("ReadRecAndSim","Simulated Particle is NULL !!!");
203 if (Rejected(prec)) continue;//we make cuts only on reconstructed data
205 fEventRec->AddParticle(prec);
206 fEventSim->AddParticle( fSimBuffer->GetParticle(i));
210 Info("ReadRecAndSim","Read %d reconstructed tracks and %d simulated particles",
211 fEventRec->GetNumberOfParticles(),fEventSim->GetNumberOfParticles());
213 fTrackCounter->Fill(fEventRec->GetNumberOfParticles());
217 /********************************************************************/
219 Int_t AliReaderAOD::ReadRec()
221 //Reads reconstructed data only
223 Info("ReadRec","Found %d reconstructed tracks",fRecBuffer->GetNumberOfParticles());
225 if (fCuts->GetEntriesFast() == 0x0)
226 {//if there is no cuts we return pointer to the buffer
227 if (fEventRec != fRecBuffer)
231 fEventRec = fRecBuffer;//fEventRec is the pointer that the user gets when he asks about an event
234 {//if there are cuts specified
235 if ( (fEventRec == 0x0) || (fEventRec == fRecBuffer) )
236 {//we need to create a new event, if it is not existing or it is the same as branch buffer
237 fEventRec = new AliAOD();
239 fEventRec->SetParticleClass( fRecBuffer->GetParticleClass() );
242 {//or simply reset it in case it already exists
246 Int_t npart = fRecBuffer->GetNumberOfParticles();
247 for (Int_t i = 0; i < npart; i++)
249 AliVAODParticle* prec = fRecBuffer->GetParticle(i);
250 if (Rejected(prec)) continue;//we make cuts only on simulated data
252 fEventRec->AddParticle(prec);
256 Info("ReadRec","Read %d reconstructed tracks",fEventRec->GetNumberOfParticles());
257 fTrackCounter->Fill(fEventRec->GetNumberOfParticles());
261 /********************************************************************/
263 Int_t AliReaderAOD::ReadSim()
265 //Reads simulated data only
267 Info("ReadSim","Found %d simulated particles",fSimBuffer->GetNumberOfParticles());
269 if (fCuts->GetEntriesFast() == 0x0)
270 {//if there is no cuts we return pointer to the buffer
271 if (fEventSim != fSimBuffer)
275 fEventSim = fSimBuffer;
278 {//if there are cuts specified
279 if ( (fEventSim == 0x0) || (fEventSim == fSimBuffer) )
280 {//we need to create a new event, if it is not existing or it is the same as branch buffer
281 fEventSim = new AliAOD();
283 fEventSim->SetParticleClass( fSimBuffer->GetParticleClass() );
286 {//or simply reset it in case it already exists
290 Int_t npart = fSimBuffer->GetNumberOfParticles();
291 for (Int_t i = 0; i < npart; i++)
293 AliVAODParticle* prec = fSimBuffer->GetParticle(i);
294 if (Rejected(prec)) continue;//we make cuts only on simulated data
295 fEventSim->AddParticle(prec);
299 Info("ReadSim","Read %d simulated particles",fEventSim->GetNumberOfParticles());
300 fTrackCounter->Fill(fEventSim->GetNumberOfParticles());
305 /********************************************************************/
307 Int_t AliReaderAOD::OpenFile(Int_t n)
309 //opens fFile with tree
311 // Info("ReadNext","Opening File %d",n);
312 const TString dirname = GetDirName(n);
315 if (AliVAODParticle::GetDebug() > 2 )
317 Info("OpenFile","Got empty string as a directory name.");
322 TString filename = dirname +"/"+ fFileName;
323 fFile = TFile::Open(filename.Data());
326 Error("OpenFile","Can't open fFile %s",filename.Data());
329 if (!fFile->IsOpen())
331 Error("OpenFile","Can't open fFile %s",filename.Data());
337 Info("ReadNext","File %s Is Opened, Getting the TREE",filename.Data());
339 fTree = dynamic_cast<TTree*>(fFile->Get(fgkTreeName));
342 if (AliVAODParticle::GetDebug() > 2 )
344 Info("ReadNext","Can not find TTree object named %s",fgkTreeName.Data());
351 // Info("ReadNext","Got TREE, Setting branch addresses");
355 TBranch* branch = fTree->GetBranch(fgkReconstructedDataBranchName);
358 Error("OpenFile","Can not find branch %s in file %s",
359 fgkReconstructedDataBranchName.Data(),filename.Data());
367 fTree->SetBranchAddress(fgkReconstructedDataBranchName,&fRecBuffer);
373 TBranch* branch = fTree->GetBranch(fgkSimulatedDataBranchName);
376 Error("OpenFile","Can not find branch %s in file %s",
377 fgkSimulatedDataBranchName.Data(),filename.Data());
385 fTree->SetBranchAddress(fgkSimulatedDataBranchName,&fSimBuffer);
387 // Info("ReadNext","Got TREE, Addresses are set.");
388 // Info("ReadNext","Quitting the method.");
393 /********************************************************************/
395 Int_t AliReaderAOD::WriteAOD(AliReader* reader, const char* outfilename, const char* pclassname, Bool_t /*multcheck*/)
397 //reads tracks from runs and writes them to file
398 ::Info("AliReaderAOD::Write","________________________________________________________");
399 ::Info("AliReaderAOD::Write","________________________________________________________");
400 ::Info("AliReaderAOD::Write","________________________________________________________");
404 ::Error("AliReaderAOD::Write","Input Reader is NULL");
407 TFile *outfile = TFile::Open(outfilename,"recreate");
410 ::Error("AliReaderAOD::Write","Can not open output file %s",outfilename);
414 TTree *tree = new TTree(fgkTreeName,"Tree with tracks");
416 TBranch *recbranch = 0x0, *simbranch = 0x0;
418 AliAOD* eventrec = new AliAOD();//must be created before Branch is called. Otherwise clones array is not splitted
419 AliAOD* eventsim = new AliAOD();//AOD together with fParticles clones array knowing exact type of particles
421 eventrec->SetParticleClassName(pclassname);
422 eventsim->SetParticleClassName(pclassname);
424 AliAOD* recbuffer = eventrec;
425 AliAOD* simbuffer = eventsim;
427 if (reader->ReadsRec()) recbranch = tree->Branch(fgkReconstructedDataBranchName,"AliAOD",&recbuffer,32000,99);
428 if (reader->ReadsSim()) simbranch = tree->Branch(fgkSimulatedDataBranchName,"AliAOD",&simbuffer,32000,99);
431 while (reader->Next() == kFALSE)
434 if (reader->ReadsRec())
435 {//here we can get AOD that has different particle type
436 AliAOD* event = reader->GetEventRec();
437 if ( eventrec->GetParticleClass() != event->GetParticleClass() )
438 {//if class type is not what what we whant we copy particles
439 eventrec->CopyData(event);
440 recbuffer = eventrec;
443 {//else just pointer to event from input reader is passed
448 if (reader->ReadsSim())
450 AliAOD* event = reader->GetEventSim();
451 if ( eventsim->GetParticleClass() != event->GetParticleClass() )
452 {//if class type is not what what we whant we copy particles
453 eventsim->CopyData(event);
454 simbuffer = eventrec;
457 {//else just pointer to event from input reader is passed
464 ::Info("AliReaderAOD::Write","Written %d events",tree->GetEntries());