9 #include "StEvent/StEventTypes.h"
11 #include "StEventUtilities/StuRefMult.hh"
12 #include "StEventUtilities/StuProbabilityPidAlgorithm.h"
14 #include "StarClassLibrary/StPhysicalHelixD.hh"
15 #include "StarClassLibrary/StTimer.hh"
17 #include "StIOMaker/StIOMaker.h"
20 #include "StStrangeMuDstMaker/StStrangeEvMuDst.hh"
21 #include "StStrangeMuDstMaker/StV0MuDst.hh"
22 #include "StStrangeMuDstMaker/StV0Mc.hh"
23 #include "StStrangeMuDstMaker/StXiMuDst.hh"
24 #include "StStrangeMuDstMaker/StXiMc.hh"
25 #include "StStrangeMuDstMaker/StKinkMuDst.hh"
26 #include "StStrangeMuDstMaker/StKinkMc.hh"
28 #include "StMuDSTMaker/COMMON/StMuException.hh"
29 #include "StMuDSTMaker/COMMON/StMuEvent.h"
30 #include "StMuDSTMaker/COMMON/StMuTrack.h"
31 #include "StMuDSTMaker/COMMON/StMuDebug.h"
32 #include "StMuDSTMaker/COMMON/StMuCut.h"
33 #include "StMuDSTMaker/COMMON/StMuDst.h"
35 #include "StHbtMuDstReader.h"
36 #include "Infrastructure/StHbtEvent.hh"
37 #include "StHbtMaker/Base/StHbtEventCut.h"
43 #include "TStreamerInfo.h"
44 #include "TClonesArray.h"
48 #if !(ST_NO_NAMESPACES)
49 using namespace units;
57 mStEvent(0), mStStrangeMuDstMaker(0), mIOMaker(0),
58 mIoMode((ioMode)mode), mIoNameMode((ioNameMode)nameMode),
59 mDirName(dirName), mFileName(fileName), mFilter(filter), mMaxFiles(maxFiles),
60 mTrackType(primary), mReadTracks(1),
61 mReadV0s(1), mReadXis(1), mReadKinks(1), mFinish(0),
62 mSplit(99), mCompress(9), mBufferSize(65536*4), mHbtEvent(0)
68 for (
int i=0; i<__NARRAYS__; i++) {
73 for (
int i=0; i<__NSTRANGEARRAYS__; i++) {
75 mStrangeArrays[i]= clonesArray(strangeArrays[i],StMuArrays::strangeArrayTypes[i],StMuArrays::strangeArraySizes[i],StMuArrays::strangeArrayCounters[i]);
78 mStMuDst->
set(arrays,strangeArrays);
80 if (mIoMode==ioRead) openRead();
88 StHbtMuDstReader::~StHbtMuDstReader(){
94 void StHbtMuDstReader::clear(){
99 for (
int i=0; i<__NARRAYS__; i++) {
102 for (
int i=0; i<__NSTRANGEARRAYS__; i++) {
103 clear(mStrangeArrays[i],StMuArrays::strangeArrayCounters[i]);
111 void StHbtMuDstReader::clear(TClonesArray* t,
int& counter){
112 if (t) t->Clear(
""); counter=0;
117 TClonesArray* StHbtMuDstReader::clonesArray(TClonesArray* p,
const char* type,
int size,
int& counter) {
121 p =
new TClonesArray(type, size);
124 if (!p)
throw StMuExceptionNullPointer(
"could not create TClonesArray",__PRETTYF__);
130 int StHbtMuDstReader::Init(){
140 void StHbtMuDstReader::Clear(){
147 StHbtEvent* StHbtMuDstReader::ReturnHbtEvent(){
153 if (mIoMode == ioRead) read();
155 catch(StMuExceptionEOF e) {
165 DEBUGVALUE1(timer.elapsedTime());
168 mHbtEvent =
new StHbtEvent(mStMuDst, mTrackType);
171 if (!(mEventCut->Pass(mHbtEvent))){
182 void StHbtMuDstReader::fill(){
186 DEBUGMESSAGE1(
"no StEvent");
192 if (mProbabilityPidAlgorithm)
delete mProbabilityPidAlgorithm;
209 void StHbtMuDstReader::write(){
221 string ioMakerFileName;
223 ioMakerFileName = string(mIOMaker->GetFile());
226 ioMakerFileName = mDirName+mFileName;
228 DEBUGVALUE1(ioMakerFileName.c_str());
231 string theFileName = buildFileName(mDirName,basename(ioMakerFileName),
".MuDst.root");
232 if (theFileName != mCurrentFileName) {
234 openWrite(theFileName);
235 mCurrentFileName = theFileName;
238 DEBUGMESSAGE2(
"now fill tree");
240 DEBUGMESSAGE2(
"tree filled");
247 void StHbtMuDstReader::Finish() {
249 for (
int i=0; i<10; i++) {
250 cout <<
"why are you calling the Finish() again ???????" << endl;
251 cout <<
"are you the stupid chain destructor ???????????" << endl;
255 if (mIoMode== ioWrite ) closeWrite();
256 if (mIoMode== ioRead ) closeRead();
264 void StHbtMuDstReader::openRead() {
265 DEBUGVALUE1(mDirName.c_str());
266 DEBUGVALUE1(mFileName.c_str());
267 DEBUGVALUE1(mFilter.c_str());
269 makeChain(mDirName.c_str(), mFilter.c_str(),mMaxFiles);
272 for (
int i=0; i<__NARRAYS__; i++) {
273 mChain->SetBranchAddress(StMuArrays::arrayNames[i],&mArrays[i]);
277 for (
int i=0; i<__NSTRANGEARRAYS__; i++) {
278 mChain->SetBranchAddress(StMuArrays::strangeArrayNames[i],&mStrangeArrays[i]);
281 (
void*)mChain->GetBranch(
"");
282 mTTree = mChain->GetTree();
284 mStMuDst->
set(mArrays,mStrangeArrays);
289 void StHbtMuDstReader::read(){
291 if ( !(mEventCounter<mChain->GetEntries()) )
throw StMuExceptionEOF(
"end of input",__PRETTYF__);
292 mChain->GetEntry(mEventCounter);
293 DEBUGVALUE2(mChain->GetCurrentFile()->GetName());
301 void StHbtMuDstReader::closeRead(){
306 void StHbtMuDstReader::openWrite(
string fileName) {
307 DEBUGVALUE1(fileName.c_str());
309 DEBUGMESSAGE2(
"now create file");
310 mCurrentFile =
new TFile(fileName.c_str(),
"RECREATE",
"StMuDst");
312 if (!mCurrentFile)
throw StMuExceptionNullPointer(
"no file openend",__PRETTYF__);
314 mCurrentFile->SetCompressionLevel(mCompress);
317 DEBUGMESSAGE2(
"now create trees and branches");
320 int bufsize = mBufferSize;
321 if (mSplit) bufsize /= 4;
324 mTTree =
new TTree(
"MuDst",
"StMuDst",mSplit);
325 if (!mTTree)
throw StMuExceptionNullPointer(
"can not create tree",__PRETTYF__);
326 mTTree->SetAutoSave(1000000);
327 DEBUGMESSAGE(
"arrays");
328 for (
int i=0; i<__NARRAYS__; i++) {
330 branch = mTTree->Branch(StMuArrays::arrayNames[i],&mArrays[i], bufsize, mSplit);
334 DEBUGMESSAGE(
"strange arrays");
335 for (
int i=0; i<__NSTRANGEARRAYS__; i++) {
337 branch = mTTree->Branch(StMuArrays::strangeArrayNames[i],&mStrangeArrays[i], bufsize, mSplit);
340 mCurrentFileName = fileName;
345 void StHbtMuDstReader::closeWrite(){
346 if (mTTree) mTTree->AutoSave();
347 if (mCurrentFile) mCurrentFile->Close();
359 fillL3AlgorithmInfo(ev);
360 fillDetectorStates(ev);
367 fillTracks(ev,mTrackFilter);
374 fillL3Tracks(ev, mL3TrackFilter);
381 fillStrange(mStStrangeMuDstMaker);
396 if (!ev)
throw StMuExceptionNullPointer(
"no StEvent",__PRETTYF__);
399 if (!cut || cut->
pass(ev)) {
401 addType(mArrays[muEvent],ev,typeOfEvent);
404 DEBUGVALUE2(timer.elapsedTime());
409 void StHbtMuDstReader::fillL3AlgorithmInfo(
StEvent* ev) {
411 if ( !ev->l3Trigger() )
return;
412 if ( !ev->l3Trigger()->l3EventSummary())
return;
417 int n = l3->numberOfAlgorithms();
418 for (
int i=0; i<n; i++) {
419 if (l3->algorithms()[i]->accept())
420 addType( mArrays[muAccept], *l3->algorithms()[i] );
422 addType( mArrays[muReject], *l3->algorithms()[i] );
425 DEBUGVALUE2(timer.elapsedTime());
435 StSPtrVecTrackNode& nodes= ev->trackNodes();
436 DEBUGVALUE2(nodes.size());
437 for (StSPtrVecTrackNodeConstIterator iter=nodes.begin(); iter!=nodes.end(); iter++) {
438 addTrackNode(ev, *iter, cut, mArrays[muGlobal], mArrays[muPrimary], mArrays[muOther],
false);
441 DEBUGVALUE2(timer.elapsedTime());
448 if (!ev->l3Trigger())
return;
452 StSPtrVecTrackNode& nodes= ev->l3Trigger()->trackNodes();
453 DEBUGVALUE2(nodes.size());
454 for (StSPtrVecTrackNodeConstIterator iter=nodes.begin(); iter!=nodes.end(); iter++) {
455 addTrackNode(ev, *iter, cut, mArrays[muL3], 0, 0,
true );
458 DEBUGVALUE2(timer.elapsedTime());
463 void StHbtMuDstReader::fillDetectorStates(
StEvent* ev) {
470 addType( mArrays[muState], ev->detectorState((StDetectorId)i) );
473 DEBUGVALUE2(timer.elapsedTime());
479 TClonesArray* gTCA, TClonesArray* pTCA, TClonesArray* oTCA,
bool l3) {
484 int index2Global =-1;
486 tr= node->track(global);
487 if (tr ) index2Global = addTrack(gTCA, ev, tr, cut, -1, l3);
492 tr = node->track(primary);
493 if (tr) index = addTrack(pTCA, ev, tr, cut, index2Global, l3);
497 size_t nEntries = node->entries();
498 for (
size_t j=0; j<nEntries; j++) {
500 if (tr && (tr->type()!=global) && (tr->type()!=primary) ) {
501 index = addTrack(oTCA, ev, tr, cut, index2Global, l3);
509 int StHbtMuDstReader::addTrack(TClonesArray* tca,
const StEvent* event,
const StTrack*
track,
StMuCut* cut,
int index2Global,
bool l3) {
513 int index2RichSpectra=-1;
515 int counter = tca->GetEntries();
517 if (cut && !cut->
pass(track))
throw StMuExceptionBadValue(
"failed track cut",__PRETTYF__);
522 index2RichSpectra = addType( mArrays[muRich], *rich );
524 new((*tca)[counter])
StMuTrack(event, track, 0, index2Global, index2RichSpectra, l3);
537 const StPtrVecTrackPidTraits& traits = track->pidTraits(kRichId);
538 for (StPtrVecTrackPidTraitsConstIterator traitIter=traits.begin();traitIter!=traits.end();++traitIter) {
540 if (pid)
return pid->getRichSpectra();
550 if (!maker)
throw StMuExceptionNullPointer(
"no StrangeMuDstMaker",__PRETTYF__);
561 addType(maker->GetEvClonesArray(), mStrangeArrays[0],ev);
562 addType(maker->GetEvMcArray(), mStrangeArrays[1],ev);
564 addType(maker->GetV0ClonesArray(), mStrangeArrays[2],v0);
565 addType(maker->GetV0McArray(), mStrangeArrays[3],v0Mc);
566 addType(maker->GetV0AssocArray(), mStrangeArrays[4],assoc);
568 addType(maker->GetXiClonesArray(), mStrangeArrays[5],xi);
569 addType(maker->GetXiMcArray(), mStrangeArrays[6],xiMc);
570 addType(maker->GetXiAssocArray(), mStrangeArrays[7],assoc);
572 addType(maker->GetKinkClonesArray(),mStrangeArrays[8],kink);
573 addType(maker->GetKinkMcArray(), mStrangeArrays[9],kinkMc);
574 addType(maker->GetKinkAssocArray(), mStrangeArrays[10],assoc);
581 void StHbtMuDstReader::addType(TClonesArray* tcaFrom, TClonesArray* tcaTo , T t) {
582 if (tcaFrom && tcaTo) {
583 int n = tcaFrom->GetEntries();
584 int counter = tcaTo->GetEntries();
585 for (
int i=0; i<n;i++) {
586 new((*tcaTo)[counter++]) T( (T&)*tcaFrom->UncheckedAt(i) );
594 int StHbtMuDstReader::addType(TClonesArray* tcaTo , T t) {
597 counter = tcaTo->GetEntries();
598 new((*tcaTo)[counter]) T( t );
605 template <
class T,
class U>
606 int StHbtMuDstReader::addType(TClonesArray* tcaTo , U u, T t) {
609 counter = tcaTo->GetEntries();
611 new((*tcaTo)[counter]) T(u);
618 string StHbtMuDstReader::buildFileName(
string dir,
string fileName,
string extention){
620 fileName = dir +
"/" + fileName + extention;
626 string StHbtMuDstReader::basename(
string s){
628 while ( name.find(
"/") != string::npos ) {
629 string::size_type pos = name.find(
"/");
630 name.erase(0, pos+1 );
632 string::size_type pos = name.find(
".event.root");
633 if (pos != string::npos) name.erase(pos,pos+11);
639 void StHbtMuDstReader::makeChain(
const char* dir,
const char* filter,
int maxFiles) {
641 mChain =
new TChain(
"MuDst");
646 if(strncmp(dir+strlen(dir)-4,
".lis",4)==0){
651 while ((!tF.eof()) && (tNFile<=maxFiles)){
652 cout <<
"Add file " << tFileName << endl;
653 mChain->Add(tFileName);
660 const char* fileName(0);
661 void *pDir = gSystem->OpenDirectory(dir);
662 while((fileName = gSystem->GetDirEntry(pDir))){
663 if(strcmp(fileName,
".")==0 || strcmp(fileName,
"..")==0)
continue;
664 if(strcmp(fileName,
".event.root")==0 || strcmp(fileName,
"..")==0)
continue;
666 if(strstr(fileName,filter) && strstr(fileName,
".MuDst.root") ){
667 char* fullFile = gSystem->ConcatFileName(dir,fileName);
669 cout << fileCount <<
" " << fullFile << endl;
670 mChain->Add(fullFile);
673 if(++fileCount >= maxFiles)
break;
677 DEBUGVALUE2(fileCount);
680 void StHbtMuDstReader::setProbabilityPidFile(
const char* file) {
681 if (mProbabilityPidAlgorithm)
682 mProbabilityPidAlgorithm->readParametersFromFile(file);
static void set(StMuDstMaker *maker)
set the pointers to the TClonesArrays
static const char * arrayTypes[__NALLARRAYS__]
< names of the classes, the TClonesArrays are arrays of this type
bool pass(const StEvent *)
called by user code, returns true if argument passes cuts, else false
static int arrayCounters[__NALLARRAYS__]
< number of entries in current event, currently not used
static void setProbabilityPidCentrality(Double_t cent)
Sets the centrality for calculating Aihong's pid.
static void fixTrackIndices(TClonesArray *primary, TClonesArray *global)
checks and if necessary corrects the indecies of elements pointing to each other (e.g., a primary track's index to the corresponding global track)
static void setProbabilityPidAlgorithm(StuProbabilityPidAlgorithm *)
Sets the StuProbabilityPidAlgorithm. Important in order to calculate Aihong's pids.
StHbtMuDstReader(int mode, int nameMode, const char *dirName="./", const char *fileName="test.event.root", const char *filter=".", int maxfiles=10)
static int arraySizes[__NALLARRAYS__]
< maximum sizes of the TClonesArrays