ROOT logo
/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/* $Id$ */

#include <TROOT.h>
#include <TFile.h>
#include <TObjString.h>
#include <TTree.h>

#include "AliLog.h"
#include "AliLoader.h"
#include "AliRun.h"
#include "AliStream.h"

////////////////////////////////////////////////////////////////////////
//
// AliStream.cxx
//
// - store file names associated with a given stream
// - open and close files
// - return serial event number of the next event in the stream
// and the TFile pointer for a proper file
//  Author: Jiri Chudoba (CERN), 2001
//
////////////////////////////////////////////////////////////////////////

ClassImp(AliStream)

AliStream::AliStream():
  fLastEventSerialNr(-1),
  fLastEventNr(0),
  fCurrentFileIndex(-1),
  fEvents(0),
  fMode(0),
  fFileNames(0x0),
  fEventFolderName(0)
{
  // root requires default ctor, where no new objects can be created
  // do not use this ctor, it is supplied only for root needs
}
//_______________________________________________________________________

AliStream::AliStream(const char* foldername,Option_t *option):
  fLastEventSerialNr(-1),
  fLastEventNr(0),
  fCurrentFileIndex(-1),
  fEvents(0),
  fMode(option),
  fFileNames(new TObjArray(1)),
  fEventFolderName(foldername)
{
// ctor
}
//_______________________________________________________________________

AliStream::AliStream(const AliStream &as):
  TNamed(as),
  fLastEventSerialNr(-1),
  fLastEventNr(0),
  fCurrentFileIndex(-1),
  fEvents(0),
  fMode(0),
  fFileNames(0x0),
  fEventFolderName(" ")
{
  //
  // Copy ctor
  //
  as.Copy(*this);
}
//_______________________________________________________________________

AliStream::~AliStream()
{
// default dtor
  delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
  if (fFileNames) delete fFileNames;
}
//_______________________________________________________________________

void AliStream::Copy(TObject &) const
{
  //
  // Copy function
  //
  AliFatal("Not implemented!");
}
//_______________________________________________________________________

void AliStream::AddFile(const char *fileName)
{
// stores the name of the file
  TObjString *name = new TObjString(fileName);
  fFileNames->Add(name);
}
//_______________________________________________________________________

Bool_t AliStream::NextEventInStream()
{
// returns kFALSE if no more events
// returns kTRUE and the serial nr of the next event
// fCurrentFile points to the file containing offered event

// no files given:
  if (fFileNames->GetLast() < 0) return kFALSE;
  
  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
  if (currentloader == 0x0) 
   {
    AliDebug(1, Form(
         "Can not get RL from folder named %s. Attempting to open next file",
         fEventFolderName.Data()));
    Int_t res = OpenNextFile();
    if ( res == 0) return kFALSE;
    currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
   }
  
  if (fLastEventSerialNr+1 >= fEvents) 
   {
    if (!OpenNextFile()) return kFALSE;
   }
  AliDebug(1, Form("Trying to get event %d",fLastEventSerialNr+1));
  currentloader->GetEvent(++fLastEventSerialNr);
  return kTRUE;
}
//_______________________________________________________________________

void AliStream::ChangeMode(Option_t* option)
{
  // set the mode to READ or UPDATE, reopen file with the new mode
  // only change from UPDATE to READ have sense in the current scheme,
  // other changes are possible but not usefull

  fMode = option;
  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
  if (currentloader) {
    delete currentloader;
    fCurrentFileIndex--;
    OpenNextFile();
  }
}
//_______________________________________________________________________

Bool_t AliStream::OpenNextFile()
{
  //
  // Opens next file in the list
  //
  if (++fCurrentFileIndex > fFileNames->GetLast()) {
    AliInfo("No more files in the stream") ;
    return kFALSE;
  }

  const char* filename =   static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();

// check if the file was already opened by some other code
  TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
  if (f) f->Close();

  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
  
  if (currentloader) 
   {
     delete currentloader;
   }
  
  currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
  

  if (currentloader == 0x0) 
   {
// cannot open file specified on input. Do not skip it silently.
    AliError("Cannot open session ");
    return kFALSE;
   }
   
// find nr of events in the given file  
  
  if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
   {
    Int_t res = currentloader->LoadHeader();
    if (res)
     {
       AliError("Problems with loading header");
       return kFALSE;
     }
    fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
   }
  else
    {
     //if it is new, create or recreate there is no chance to find header in file
      fEvents = 0;
    }
   
  fLastEventSerialNr = -1;
  return kTRUE;
}
//_______________________________________________________________________

Bool_t AliStream::ImportgAlice()
{
  //
  // Imports gAlice object from file
  //
  if (fFileNames->GetLast() < 0) return kFALSE;
  
  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
  if (!currentloader) 
   {
    if (!OpenNextFile()) return kFALSE;
    currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
   }
  currentloader->LoadgAlice();
  gAlice = currentloader->GetAliRun();
  if (!gAlice)  return kFALSE;
  return kTRUE;
}

//_______________________________________________________________________
TString AliStream::GetFileName(Int_t order) const
{
  // returns name of the order-th file
  // returns empty string if such file does not exist
  // first file in the input stream is 0
  TString fileName("");
  if (order > fFileNames->GetLast()) return fileName;
  TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
  if (fileNameStored) fileName = fileNameStored->GetString();
  return fileName;
}

 AliStream.cxx:1
 AliStream.cxx:2
 AliStream.cxx:3
 AliStream.cxx:4
 AliStream.cxx:5
 AliStream.cxx:6
 AliStream.cxx:7
 AliStream.cxx:8
 AliStream.cxx:9
 AliStream.cxx:10
 AliStream.cxx:11
 AliStream.cxx:12
 AliStream.cxx:13
 AliStream.cxx:14
 AliStream.cxx:15
 AliStream.cxx:16
 AliStream.cxx:17
 AliStream.cxx:18
 AliStream.cxx:19
 AliStream.cxx:20
 AliStream.cxx:21
 AliStream.cxx:22
 AliStream.cxx:23
 AliStream.cxx:24
 AliStream.cxx:25
 AliStream.cxx:26
 AliStream.cxx:27
 AliStream.cxx:28
 AliStream.cxx:29
 AliStream.cxx:30
 AliStream.cxx:31
 AliStream.cxx:32
 AliStream.cxx:33
 AliStream.cxx:34
 AliStream.cxx:35
 AliStream.cxx:36
 AliStream.cxx:37
 AliStream.cxx:38
 AliStream.cxx:39
 AliStream.cxx:40
 AliStream.cxx:41
 AliStream.cxx:42
 AliStream.cxx:43
 AliStream.cxx:44
 AliStream.cxx:45
 AliStream.cxx:46
 AliStream.cxx:47
 AliStream.cxx:48
 AliStream.cxx:49
 AliStream.cxx:50
 AliStream.cxx:51
 AliStream.cxx:52
 AliStream.cxx:53
 AliStream.cxx:54
 AliStream.cxx:55
 AliStream.cxx:56
 AliStream.cxx:57
 AliStream.cxx:58
 AliStream.cxx:59
 AliStream.cxx:60
 AliStream.cxx:61
 AliStream.cxx:62
 AliStream.cxx:63
 AliStream.cxx:64
 AliStream.cxx:65
 AliStream.cxx:66
 AliStream.cxx:67
 AliStream.cxx:68
 AliStream.cxx:69
 AliStream.cxx:70
 AliStream.cxx:71
 AliStream.cxx:72
 AliStream.cxx:73
 AliStream.cxx:74
 AliStream.cxx:75
 AliStream.cxx:76
 AliStream.cxx:77
 AliStream.cxx:78
 AliStream.cxx:79
 AliStream.cxx:80
 AliStream.cxx:81
 AliStream.cxx:82
 AliStream.cxx:83
 AliStream.cxx:84
 AliStream.cxx:85
 AliStream.cxx:86
 AliStream.cxx:87
 AliStream.cxx:88
 AliStream.cxx:89
 AliStream.cxx:90
 AliStream.cxx:91
 AliStream.cxx:92
 AliStream.cxx:93
 AliStream.cxx:94
 AliStream.cxx:95
 AliStream.cxx:96
 AliStream.cxx:97
 AliStream.cxx:98
 AliStream.cxx:99
 AliStream.cxx:100
 AliStream.cxx:101
 AliStream.cxx:102
 AliStream.cxx:103
 AliStream.cxx:104
 AliStream.cxx:105
 AliStream.cxx:106
 AliStream.cxx:107
 AliStream.cxx:108
 AliStream.cxx:109
 AliStream.cxx:110
 AliStream.cxx:111
 AliStream.cxx:112
 AliStream.cxx:113
 AliStream.cxx:114
 AliStream.cxx:115
 AliStream.cxx:116
 AliStream.cxx:117
 AliStream.cxx:118
 AliStream.cxx:119
 AliStream.cxx:120
 AliStream.cxx:121
 AliStream.cxx:122
 AliStream.cxx:123
 AliStream.cxx:124
 AliStream.cxx:125
 AliStream.cxx:126
 AliStream.cxx:127
 AliStream.cxx:128
 AliStream.cxx:129
 AliStream.cxx:130
 AliStream.cxx:131
 AliStream.cxx:132
 AliStream.cxx:133
 AliStream.cxx:134
 AliStream.cxx:135
 AliStream.cxx:136
 AliStream.cxx:137
 AliStream.cxx:138
 AliStream.cxx:139
 AliStream.cxx:140
 AliStream.cxx:141
 AliStream.cxx:142
 AliStream.cxx:143
 AliStream.cxx:144
 AliStream.cxx:145
 AliStream.cxx:146
 AliStream.cxx:147
 AliStream.cxx:148
 AliStream.cxx:149
 AliStream.cxx:150
 AliStream.cxx:151
 AliStream.cxx:152
 AliStream.cxx:153
 AliStream.cxx:154
 AliStream.cxx:155
 AliStream.cxx:156
 AliStream.cxx:157
 AliStream.cxx:158
 AliStream.cxx:159
 AliStream.cxx:160
 AliStream.cxx:161
 AliStream.cxx:162
 AliStream.cxx:163
 AliStream.cxx:164
 AliStream.cxx:165
 AliStream.cxx:166
 AliStream.cxx:167
 AliStream.cxx:168
 AliStream.cxx:169
 AliStream.cxx:170
 AliStream.cxx:171
 AliStream.cxx:172
 AliStream.cxx:173
 AliStream.cxx:174
 AliStream.cxx:175
 AliStream.cxx:176
 AliStream.cxx:177
 AliStream.cxx:178
 AliStream.cxx:179
 AliStream.cxx:180
 AliStream.cxx:181
 AliStream.cxx:182
 AliStream.cxx:183
 AliStream.cxx:184
 AliStream.cxx:185
 AliStream.cxx:186
 AliStream.cxx:187
 AliStream.cxx:188
 AliStream.cxx:189
 AliStream.cxx:190
 AliStream.cxx:191
 AliStream.cxx:192
 AliStream.cxx:193
 AliStream.cxx:194
 AliStream.cxx:195
 AliStream.cxx:196
 AliStream.cxx:197
 AliStream.cxx:198
 AliStream.cxx:199
 AliStream.cxx:200
 AliStream.cxx:201
 AliStream.cxx:202
 AliStream.cxx:203
 AliStream.cxx:204
 AliStream.cxx:205
 AliStream.cxx:206
 AliStream.cxx:207
 AliStream.cxx:208
 AliStream.cxx:209
 AliStream.cxx:210
 AliStream.cxx:211
 AliStream.cxx:212
 AliStream.cxx:213
 AliStream.cxx:214
 AliStream.cxx:215
 AliStream.cxx:216
 AliStream.cxx:217
 AliStream.cxx:218
 AliStream.cxx:219
 AliStream.cxx:220
 AliStream.cxx:221
 AliStream.cxx:222
 AliStream.cxx:223
 AliStream.cxx:224
 AliStream.cxx:225
 AliStream.cxx:226
 AliStream.cxx:227
 AliStream.cxx:228
 AliStream.cxx:229
 AliStream.cxx:230
 AliStream.cxx:231
 AliStream.cxx:232
 AliStream.cxx:233
 AliStream.cxx:234
 AliStream.cxx:235
 AliStream.cxx:236
 AliStream.cxx:237
 AliStream.cxx:238
 AliStream.cxx:239
 AliStream.cxx:240
 AliStream.cxx:241
 AliStream.cxx:242
 AliStream.cxx:243
 AliStream.cxx:244