ROOT logo
//*-- Author : Manuel Sanchez
//*-- Modified : 08/10/2003 by Romain Holzmann
//*-- Modified : 24/02/2003 by Ilse Koenig
//*-- Modified : 23/05/2002 by Manuel Sanchez
//*-- Modified : 01/03/2000 by Manuel Sanchez
//*-- Modified : 5/05/98 by Manuel Sanchez
//*-- Copyright : GENP (Univ. Santiago de Compostela)

//_HADES_CLASS_DESCRIPTION
////////////////////////////////////////////////////////////////////////////
// HRootSource
//
// This data source can read data from a ROOT file generated by
// HYDRA, this includes both the reconstruction and simulation software.
//
// The data source takes care of instantiating the appropriate classes even
// if they are non-standard. That is, if you have an input file with
// HMdcHitSim instead of HMdcHit; HRootSource will take care of using
// HMdcHitSim during the analysis.
//
////////////////////////////////////////////////////////////////////////////
#include "hrootsource.h"
#include "hades.h"
#include "hrecevent.h"
#include "heventheader.h"
#include "hpartialevent.h"
#include "hcategory.h"
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <unistd.h>
#include "TKey.h"

using namespace std;

ClassImp(HRootSource)

HRootSource::HRootSource(Bool_t fPers, Bool_t fMerg)
{
    // for fPers==kFALSE, input cats are suppressed in output
    // for fMerg==kTRUE, partial events in input tree are merged into current
    // event, if it exists already at init().
    fInput           = 0;
    fEventInFile     = 0;
    fCursor          = 0;
    fCurrentRunId    = INT_MIN;
    fCurrentRefId    = -1;
    fGlobalRefId     = -1;
    fDirectory       = "./";
    fPersistency     = fPers;
    fMerge           = fMerg;
    fEventList       = 0;
    fLastRunId       = -1;
    overwriteVersion = kFALSE;
    replaceVersion   = 0;
}

HRootSource::~HRootSource(void)
{
    //Class destructor, it clears the data source.
    Clear();
}

Text_t const *HRootSource::getCurrentFileName(void)
{
    if(fInput){
	TFile* file = fInput->GetCurrentFile();
	if(file){
	    return file->GetName();
	} else {
	    return "";
	}
    }
    return "";
}

Bool_t HRootSource::init(void)
{
    // Initializes the data source connecting the parts of the input tree in
    // the input ROOT file to the event structure used for the analysis. If
    // no event structure has been set by the user, HRootSource::init() will
    // set one read from the input file. If it exists, it will be used as such
    // (fMerge==kFALSE) or merged with the one in the input file (fMerge==kTRUE).
    Bool_t r = kTRUE;

    if (fEventInFile != 0)
    {
	//If there is an active event structure
	HRecEvent* fCurrentEvent = (HRecEvent*)gHades->getCurrentEvent();
	if (fCurrentEvent != 0) {
	    if (fMerge) {  // merge the 2 event structures

		Warning("init","Merging with predefined event structure");
		((HRecEvent*)fEventInFile)->merge(fCurrentEvent);

	    } else {
		Warning("init","Using predefined event structure");
	    }

	} else  { gHades->setEvent(fEventInFile); }

	if ( fInput!=0)
	{
	    Char_t sl=*(strchr(fInput->GetTitle(),'.')+1);
	    switch (sl) {
	    case '0' : fSplitLevel = 0;
	    break;
	    case '1' : fSplitLevel = 1;
	    break;
	    case '2' : fSplitLevel = 2;
	    break;
	    default : fSplitLevel  = 0;
	    }
	    fInput->SetBranchStatus("*",kFALSE);
	    gHades->activateTree(fInput);
	    if (fEventList) {
		fEntries = fEventList->GetN();
	    } else {
		fEntries=fInput->GetEntries();
	    }
	    if (fCursor >= fEntries) {
		Error("init","Entry not existing %i",fCursor);
		return kFALSE; //Entry not existing
	    }
	    if (fEventList) {
		fInput->GetEvent(fEventList->GetEntry(fCursor));
	    } else {
		fInput->GetEvent(fCursor);
	    }
	    fCurrentRunId = gHades->getCurrentEvent()->getHeader()->getEventRunNumber();
	    fLastRunId = fCurrentRunId;

	    if(overwriteVersion)
	    { // fix not set version in simulation
		gHades->getCurrentEvent()->getHeader()->setVersion(replaceVersion);
	    }

	    if (fRefIds.find(fCurrentRunId) != fRefIds.end()) {
		fCurrentRefId = fRefIds[fCurrentRunId];
	    } else {
		fCurrentRefId = fGlobalRefId;
	    }
	    if (fPersistency == kFALSE) { // set all input categories non-persistent

		for(Int_t i = 0; i < 16; i ++) { // loop over partial events
		    HPartialEvent* fPar = ((HRecEvent*)fEventInFile)->getPartialEvent(i<<kBitCategorySize);
		    if (fPar) { fPar->setPersistency(kFALSE); }
		}
	    }
	    setCursorToPreviousEvent();
	    r = kTRUE;
	} else {
	    Warning("init","Not input");
	    Clear();
	    r = kFALSE;
	}
    } else {
	r = kFALSE;
    }
    return r;
}

EDsState HRootSource::getNextEvent(Bool_t doUnpack)
{
    //Retrieves next event in the input file.
    Int_t bread = 0;
    if (fInput)
    {
	if (fSplitLevel < 2) (*fEventAddr)->clearAll(fSplitLevel);
	if (fCursor < fEntries)
	{
	    if (fEventList) {
		bread = fInput->GetEvent(fEventList->GetEntry(fCursor));
	    } else {
		bread = fInput->GetEvent(fCursor);
	    }
	    if (bread == 0) { return kDsEndData; }
	    fCursor ++;
	    fCurrentRunId = gHades->getCurrentEvent()->getHeader()->getEventRunNumber();
	    if(overwriteVersion)
	    {  // fix not set version in simulation
		gHades->getCurrentEvent()->getHeader()->setVersion(replaceVersion);
	    }
	    if (fCurrentRunId != fLastRunId)
	    {
		if (fRefIds.find(fCurrentRunId) != fRefIds.end()) {
		    fCurrentRefId = fRefIds[fCurrentRunId];
		} else {
		    fCurrentRefId = fGlobalRefId;
		}
		fLastRunId = fCurrentRunId;
		return kDsEndFile;
	    }
	} else { return kDsEndData; }
    } else { return kDsError; }
    return kDsOk;
}
void HRootSource::setCursorToPreviousEvent()
{
    // Rewinds the file cursour read by 1 event
    // The cursor does not point to next event as usual
    // but to the current event. This feature is needed
    // for the event embedding if a event is skipped
    // by the first data source or any task. The same
    // event (from second data source) will be read for
    // the next event from first data source.
    if(fCursor > 0) { fCursor --; }
}

Bool_t HRootSource::getEvent(Int_t eventN) {
    //Retrieves event in position eventN in the input file, copying the
    //information to the event structure.
    if (fInput) {
	if (fEventList) {
	    if (fInput->GetEvent(fEventList->GetEntry(eventN)) > 0)
	    {
		return kTRUE;
	    } else {
		if (fInput->GetEvent(eventN)>0){ return kTRUE; }
		else                           { return kFALSE; }
	    }
	}
    }
    return kFALSE;
}

void HRootSource::Clear(void)
{
    //Closes the input file.
    if (fInput) {
	delete fInput;
	fInput = 0;
    }
}

void HRootSource::setDirectory(const Text_t dirName[])
{
    //Sets the directory where to read files from.
    fDirectory = dirName;
    if (fDirectory[fDirectory.Length()-1] != '/') { fDirectory += "/"; }
}

Bool_t HRootSource::fileExists(const TString &name) {
    //Checks for the existence on one file.
    return (access(name.Data(),F_OK) == 0) ? kTRUE : kFALSE;
}

TString HRootSource::getFileName(const Text_t *file)
{
    TString fname;
    if (file[0] == '/') { //Absolute or relative path??
	fname = file;
    } else {
	fname = fDirectory;
	fname += file;
    }
    return fname;
}

Bool_t HRootSource::addFile(const Text_t *file)
{
    Text_t treeName[] = "T";
    TString fname = getFileName(file);

    if (fileExists(fname))
    {
	if (!fInput)
	{ //If chain doesn't already exist
	    TFile *fileTemp;
	    TKey *key = 0;
	    TString title;

	    fileTemp = getFile(fname.Data()); //Obtain tree title
	    //new TFile(fname.Data());
	    key               = fileTemp->GetKey(treeName);  //No need to delete this pointer
	    fEventInFile      = (HEvent *)fileTemp->Get("Event");
	    fCurrentRunId     = fEventInFile->getHeader()->getEventRunNumber();
	    if (fRefIds.find(fCurrentRunId) != fRefIds.end()) {
		fCurrentRefId = fRefIds[fCurrentRunId];
	    } else {
		fCurrentRefId = fGlobalRefId;
	    }
	    title = key->GetTitle();
	    fileTemp->Close();
	    delete fileTemp;
	    fInput = new TChain(treeName,title.Data());
	}
	fInput->Add(fname.Data());

    } else {
	Warning("addFile","File %s not found",fname.Data());
	return kFALSE;
    }
    return kTRUE;
}

Bool_t HRootSource::setInput(const Text_t *fileName,const Text_t *treeName)
{
    //Sets the input file and tree. Opening the corresponding files, it also
    //loads in memory the input event description so the disableCategory()
    //method can be used.
    if (strcmp(treeName,"T") != 0) { return kFALSE; }
    else                           { return addFile(fileName); }
}

Bool_t HRootSource::disableCategory(Cat_t aCat)
{
    //Disables the category aCat so it is not read even if it is stored in
    //the input file. This method shouldn't be called after init()
    //Returns kTRUE if the aCat was stored in the input file and has succesfully
    //been disabled, otherwise the return value is kFALSE.
    if (!fEventInFile) { return kFALSE; }
    else               { return fEventInFile->removeCategory(aCat); }
}

Bool_t HRootSource::disablePartialEvent(Cat_t aCat)
{
    if (!fEventInFile) { return kFALSE; }
    else               { return ((HRecEvent*)fEventInFile)->removePartialEvent(aCat); }
}

Int_t HRootSource::getSplitLevel(void)
{
    //Returns the split level of the input tree.
    return fSplitLevel;
}

void HRootSource::deactivateBranch(const Text_t *branchName)
{
    //Deactivates a branch so it is not read.
    //This method is deprecated, use disableCategory() instead.
    if (fInput) {
	fInput->SetBranchStatus(branchName,kFALSE);
    }
}

TTree *HRootSource::getTree(void)
{
    //Returns the input tree.
    return fInput;
}

EDsState HRootSource::skipEvents(Int_t nEv)
{
    enum EDsState state = kDsOk;

    if (nEv > 0) {
	Int_t newCursor = fCursor + nEv;

	if (newCursor < fEntries) {
	    fCursor = newCursor - 1;
	    state = getNextEvent();
	} else { state = kDsEndData; }
    }

    return state;
}

TFile * HRootSource::getFile(TString name)
{
    return TFile::Open(name.Data(),"READ");
}


 hrootsource.cc:1
 hrootsource.cc:2
 hrootsource.cc:3
 hrootsource.cc:4
 hrootsource.cc:5
 hrootsource.cc:6
 hrootsource.cc:7
 hrootsource.cc:8
 hrootsource.cc:9
 hrootsource.cc:10
 hrootsource.cc:11
 hrootsource.cc:12
 hrootsource.cc:13
 hrootsource.cc:14
 hrootsource.cc:15
 hrootsource.cc:16
 hrootsource.cc:17
 hrootsource.cc:18
 hrootsource.cc:19
 hrootsource.cc:20
 hrootsource.cc:21
 hrootsource.cc:22
 hrootsource.cc:23
 hrootsource.cc:24
 hrootsource.cc:25
 hrootsource.cc:26
 hrootsource.cc:27
 hrootsource.cc:28
 hrootsource.cc:29
 hrootsource.cc:30
 hrootsource.cc:31
 hrootsource.cc:32
 hrootsource.cc:33
 hrootsource.cc:34
 hrootsource.cc:35
 hrootsource.cc:36
 hrootsource.cc:37
 hrootsource.cc:38
 hrootsource.cc:39
 hrootsource.cc:40
 hrootsource.cc:41
 hrootsource.cc:42
 hrootsource.cc:43
 hrootsource.cc:44
 hrootsource.cc:45
 hrootsource.cc:46
 hrootsource.cc:47
 hrootsource.cc:48
 hrootsource.cc:49
 hrootsource.cc:50
 hrootsource.cc:51
 hrootsource.cc:52
 hrootsource.cc:53
 hrootsource.cc:54
 hrootsource.cc:55
 hrootsource.cc:56
 hrootsource.cc:57
 hrootsource.cc:58
 hrootsource.cc:59
 hrootsource.cc:60
 hrootsource.cc:61
 hrootsource.cc:62
 hrootsource.cc:63
 hrootsource.cc:64
 hrootsource.cc:65
 hrootsource.cc:66
 hrootsource.cc:67
 hrootsource.cc:68
 hrootsource.cc:69
 hrootsource.cc:70
 hrootsource.cc:71
 hrootsource.cc:72
 hrootsource.cc:73
 hrootsource.cc:74
 hrootsource.cc:75
 hrootsource.cc:76
 hrootsource.cc:77
 hrootsource.cc:78
 hrootsource.cc:79
 hrootsource.cc:80
 hrootsource.cc:81
 hrootsource.cc:82
 hrootsource.cc:83
 hrootsource.cc:84
 hrootsource.cc:85
 hrootsource.cc:86
 hrootsource.cc:87
 hrootsource.cc:88
 hrootsource.cc:89
 hrootsource.cc:90
 hrootsource.cc:91
 hrootsource.cc:92
 hrootsource.cc:93
 hrootsource.cc:94
 hrootsource.cc:95
 hrootsource.cc:96
 hrootsource.cc:97
 hrootsource.cc:98
 hrootsource.cc:99
 hrootsource.cc:100
 hrootsource.cc:101
 hrootsource.cc:102
 hrootsource.cc:103
 hrootsource.cc:104
 hrootsource.cc:105
 hrootsource.cc:106
 hrootsource.cc:107
 hrootsource.cc:108
 hrootsource.cc:109
 hrootsource.cc:110
 hrootsource.cc:111
 hrootsource.cc:112
 hrootsource.cc:113
 hrootsource.cc:114
 hrootsource.cc:115
 hrootsource.cc:116
 hrootsource.cc:117
 hrootsource.cc:118
 hrootsource.cc:119
 hrootsource.cc:120
 hrootsource.cc:121
 hrootsource.cc:122
 hrootsource.cc:123
 hrootsource.cc:124
 hrootsource.cc:125
 hrootsource.cc:126
 hrootsource.cc:127
 hrootsource.cc:128
 hrootsource.cc:129
 hrootsource.cc:130
 hrootsource.cc:131
 hrootsource.cc:132
 hrootsource.cc:133
 hrootsource.cc:134
 hrootsource.cc:135
 hrootsource.cc:136
 hrootsource.cc:137
 hrootsource.cc:138
 hrootsource.cc:139
 hrootsource.cc:140
 hrootsource.cc:141
 hrootsource.cc:142
 hrootsource.cc:143
 hrootsource.cc:144
 hrootsource.cc:145
 hrootsource.cc:146
 hrootsource.cc:147
 hrootsource.cc:148
 hrootsource.cc:149
 hrootsource.cc:150
 hrootsource.cc:151
 hrootsource.cc:152
 hrootsource.cc:153
 hrootsource.cc:154
 hrootsource.cc:155
 hrootsource.cc:156
 hrootsource.cc:157
 hrootsource.cc:158
 hrootsource.cc:159
 hrootsource.cc:160
 hrootsource.cc:161
 hrootsource.cc:162
 hrootsource.cc:163
 hrootsource.cc:164
 hrootsource.cc:165
 hrootsource.cc:166
 hrootsource.cc:167
 hrootsource.cc:168
 hrootsource.cc:169
 hrootsource.cc:170
 hrootsource.cc:171
 hrootsource.cc:172
 hrootsource.cc:173
 hrootsource.cc:174
 hrootsource.cc:175
 hrootsource.cc:176
 hrootsource.cc:177
 hrootsource.cc:178
 hrootsource.cc:179
 hrootsource.cc:180
 hrootsource.cc:181
 hrootsource.cc:182
 hrootsource.cc:183
 hrootsource.cc:184
 hrootsource.cc:185
 hrootsource.cc:186
 hrootsource.cc:187
 hrootsource.cc:188
 hrootsource.cc:189
 hrootsource.cc:190
 hrootsource.cc:191
 hrootsource.cc:192
 hrootsource.cc:193
 hrootsource.cc:194
 hrootsource.cc:195
 hrootsource.cc:196
 hrootsource.cc:197
 hrootsource.cc:198
 hrootsource.cc:199
 hrootsource.cc:200
 hrootsource.cc:201
 hrootsource.cc:202
 hrootsource.cc:203
 hrootsource.cc:204
 hrootsource.cc:205
 hrootsource.cc:206
 hrootsource.cc:207
 hrootsource.cc:208
 hrootsource.cc:209
 hrootsource.cc:210
 hrootsource.cc:211
 hrootsource.cc:212
 hrootsource.cc:213
 hrootsource.cc:214
 hrootsource.cc:215
 hrootsource.cc:216
 hrootsource.cc:217
 hrootsource.cc:218
 hrootsource.cc:219
 hrootsource.cc:220
 hrootsource.cc:221
 hrootsource.cc:222
 hrootsource.cc:223
 hrootsource.cc:224
 hrootsource.cc:225
 hrootsource.cc:226
 hrootsource.cc:227
 hrootsource.cc:228
 hrootsource.cc:229
 hrootsource.cc:230
 hrootsource.cc:231
 hrootsource.cc:232
 hrootsource.cc:233
 hrootsource.cc:234
 hrootsource.cc:235
 hrootsource.cc:236
 hrootsource.cc:237
 hrootsource.cc:238
 hrootsource.cc:239
 hrootsource.cc:240
 hrootsource.cc:241
 hrootsource.cc:242
 hrootsource.cc:243
 hrootsource.cc:244
 hrootsource.cc:245
 hrootsource.cc:246
 hrootsource.cc:247
 hrootsource.cc:248
 hrootsource.cc:249
 hrootsource.cc:250
 hrootsource.cc:251
 hrootsource.cc:252
 hrootsource.cc:253
 hrootsource.cc:254
 hrootsource.cc:255
 hrootsource.cc:256
 hrootsource.cc:257
 hrootsource.cc:258
 hrootsource.cc:259
 hrootsource.cc:260
 hrootsource.cc:261
 hrootsource.cc:262
 hrootsource.cc:263
 hrootsource.cc:264
 hrootsource.cc:265
 hrootsource.cc:266
 hrootsource.cc:267
 hrootsource.cc:268
 hrootsource.cc:269
 hrootsource.cc:270
 hrootsource.cc:271
 hrootsource.cc:272
 hrootsource.cc:273
 hrootsource.cc:274
 hrootsource.cc:275
 hrootsource.cc:276
 hrootsource.cc:277
 hrootsource.cc:278
 hrootsource.cc:279
 hrootsource.cc:280
 hrootsource.cc:281
 hrootsource.cc:282
 hrootsource.cc:283
 hrootsource.cc:284
 hrootsource.cc:285
 hrootsource.cc:286
 hrootsource.cc:287
 hrootsource.cc:288
 hrootsource.cc:289
 hrootsource.cc:290
 hrootsource.cc:291
 hrootsource.cc:292
 hrootsource.cc:293
 hrootsource.cc:294
 hrootsource.cc:295
 hrootsource.cc:296
 hrootsource.cc:297
 hrootsource.cc:298
 hrootsource.cc:299
 hrootsource.cc:300
 hrootsource.cc:301
 hrootsource.cc:302
 hrootsource.cc:303
 hrootsource.cc:304
 hrootsource.cc:305
 hrootsource.cc:306
 hrootsource.cc:307
 hrootsource.cc:308
 hrootsource.cc:309
 hrootsource.cc:310
 hrootsource.cc:311
 hrootsource.cc:312
 hrootsource.cc:313
 hrootsource.cc:314
 hrootsource.cc:315
 hrootsource.cc:316
 hrootsource.cc:317
 hrootsource.cc:318
 hrootsource.cc:319
 hrootsource.cc:320
 hrootsource.cc:321
 hrootsource.cc:322
 hrootsource.cc:323
 hrootsource.cc:324
 hrootsource.cc:325
 hrootsource.cc:326
 hrootsource.cc:327
 hrootsource.cc:328
 hrootsource.cc:329
 hrootsource.cc:330
 hrootsource.cc:331
 hrootsource.cc:332
 hrootsource.cc:333
 hrootsource.cc:334
 hrootsource.cc:335
 hrootsource.cc:336
 hrootsource.cc:337
 hrootsource.cc:338
 hrootsource.cc:339
 hrootsource.cc:340
 hrootsource.cc:341
 hrootsource.cc:342
 hrootsource.cc:343
 hrootsource.cc:344
 hrootsource.cc:345
 hrootsource.cc:346
 hrootsource.cc:347
 hrootsource.cc:348
 hrootsource.cc:349
 hrootsource.cc:350
 hrootsource.cc:351
 hrootsource.cc:352
 hrootsource.cc:353
 hrootsource.cc:354
 hrootsource.cc:355
 hrootsource.cc:356
 hrootsource.cc:357
 hrootsource.cc:358
 hrootsource.cc:359
 hrootsource.cc:360
 hrootsource.cc:361
 hrootsource.cc:362
 hrootsource.cc:363
 hrootsource.cc:364