StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RootWrapper.cxx
1 #include <cstdlib>
2 #include <cstdio>
3 #include <ctime>
4 
5 #include "TROOT.h"
6 #include "TFile.h"
7 #include "TDirectory.h"
8 #include "TTree.h"
9 #include "TBranch.h"
10 
11 
12 #include "EEfeeDataBlock.h"
13 #include "EEfeeRawEvent.h"
14 #include "EEmcEventHeader.h"
15 
16 
17 // A simple wrapper for Fortran xsort program
18 // works for Linux and possibly other Unices
19 // Author Piotr A. Zolnierczuk / IUCF
20 // 2002-2003
21 
22 
23 //#define DEBUG 0
24 static const int MaxCommentLen=1024;
25 
26 static TFile *file = NULL ;
27 static TTree *tree = NULL ;
28 
29 static TBranch *beve = NULL ;
30 static TBranch *bhead = NULL ;
31 
32 static EEfeeDataBlock *b = NULL ;
33 static EEfeeRawEvent *eve = NULL ;
34 static EEmcEventHeader *ehead = NULL ;
35 
36 static int evnum = 0;
37 static int nAutoSave =10000;
38 static char *filename = NULL;
39 
40 
41 extern "C" void
42 eemcfeerootopen_(long& run, long& runtime, char *chfile, int &nAuto, int len)
43 {
44  char *comment = new char[MaxCommentLen];
45  char *basefile = new char[MaxCommentLen];
46  const char *rootdir = getenv("MINIROOTDIR");
47  if ( rootdir == NULL ) rootdir=".";
48  filename = new char[(len<MaxCommentLen)?MaxCommentLen:len]; // ???
49 
50  // a hack for now
51  if(strstr(chfile,"[BSND ALL]")!=NULL) { // we have an online case
52  fprintf(stderr,"rootopen: on-line data <[BSND ALL]>\n");
53  sprintf(basefile,"run%03ld",run);
54  } else {
55  memcpy(basefile,chfile,len);
56  char *isp = strchr(basefile,' '); *isp = 0x00; // locate first space
57  fprintf(stderr,"rootopen: off-line data <%s>\n",basefile);
58  char *idt = strchr(basefile,'.'); *idt = 0x00; // locate first dot
59  }
60  sprintf(filename,"%s/%s.ez.root",rootdir,basefile);
61  std::time_t utime = runtime;
62  sprintf(comment,"run:%05ld, time:%s ",run,ctime(&utime));
63 
64  file = new TFile(filename,"RECREATE");
65  tree = new TTree("ezstar","A tree with FEE events");
66  eve = new EEfeeRawEvent();
67  ehead = new EEmcEventHeader();
68  b = new EEfeeDataBlock();
69 
70  bhead = tree->Branch("head","EEmcEventHeader",&ehead,10000,99);
71  beve = tree->Branch("eemc" ,"EEfeeRawEvent",&eve,10000,99);
72 
73  ehead->clear();
74  ehead->setRunNumber(run);
75  ehead->setProcessingTime(time(0));
76  ehead->setTimeStamp(runtime);
77  ehead->setComment(comment);
78  evnum=1; // reset event counter
79 
80  nAutoSave=nAuto;
81 
82  fprintf(stderr,"rootopen: file=%s\n",filename);
83  fprintf(stderr,"rootopen: comment=%s\n",comment);
84  fprintf(stderr,"rootopen: nAutoSave=%d\n",nAutoSave);
85  fflush(stderr);
86  if(basefile) delete [] basefile;
87  if(comment ) delete [] comment;
88  return;
89 }
90 
91 
92 extern "C" void
93 eemcfeerootfill_(unsigned short& evtype, unsigned short& evtoken, unsigned short& size , unsigned short *e, int &eveID, int *ierr)
94 {
95  UShort_t *head = new UShort_t[EEfeeDataBlock::DefaultMaxHead];
96  static int nTotErr=0;
97 
98  *ierr=0;
99  b->clear();
100  eve->clear();
101  eve->setID(++evnum);
102 
103  if(evnum%10000==0)
104  fprintf(stderr,"EVENT %6dk %6d 0x%04hx\r",evnum/10000,size,evtype);
105 
106 #ifdef DEBUG
107  fprintf(stderr,"\nEVENT %08d %06d 0x%04hx\n",evnum,size,evtype);
108 #endif
109 
110 #ifdef DEBUG
111  int i=0;
112  while(i<size) {
113  for(int k=0; i<size && k<2 ; k++,i++) fprintf(stderr,"[%03d] 0x%04hx ",i,e[i]);
114  fprintf(stderr,"\n");
115  for(int k=0; i<size && k<4 ; k++,i++) fprintf(stderr,"[%03d/%02d]=0x%04hx ",i,k,e[i]);
116  fprintf(stderr,"\n");
117  for(int k=0; i<size && k<128; k++,i++);
118  }
119  fprintf(stderr,"i=%d\n",i);
120 #endif
121 
122  for(unsigned short *p = e; (p-e)<size; ) {
123  b->clear();
124  *p++; // skip marker
125  unsigned int wordcnt = *p++;
126  *p++; // 0 resword1
127  *p++; // 1 resword2
128  unsigned short token = *p++; // 2
129  unsigned short cratrig = *p++; // 3
130  if(token!=evtoken) {
131  nTotErr++;
132  if(nTotErr%10000==0) {
133  fprintf(stderr,"eemcfeerootfill: *** token mismatch, \n so far %d-th data blocks in %d events\n",nTotErr, eveID);
134  fprintf(stderr," (event token=%hd crate token=%hd)\n",evtoken,token);
135  }
136  }
137  if(wordcnt>4) wordcnt -= 4;
138  head[EEfeeDataBlock::ERRFLG] = evtype; // use free slot
139  head[EEfeeDataBlock::WRDCNT] = wordcnt;
140  head[EEfeeDataBlock::TOKEN] = token;
141  head[EEfeeDataBlock::CRATE] = cratrig;
142  b->setHead(head);
143  b->setDataArray(p,wordcnt);
144 
145 #if DEBUG
146  b->print(0);
147 #endif
148  if(cratrig) eve->addFeeDataBlock(b);
149  p += wordcnt;
150  }
151 
152 
153  ehead->setProcessingTime(time(0));
154  ehead->setToken(evtoken);
155  ehead->setEventNumber(eveID);
156  tree->Fill();
157 
158  if(evnum%nAutoSave==0) {
159  tree->AutoSave();
160  file->SaveSelf();
161  printf("====> ezTree AutoSave done, nEve=%d\n",evnum);
162  }
163 
164 
165  *ierr=0;
166  return;
167 }
168 
169 
170 
171 extern "C" void
172 eemcfeerootclose_()
173 {
174  if(file) {
175  file->Write();
176  delete file;
177  file=NULL;
178  fprintf(stderr,"eemcfeerootclose: file %s written\n",filename);
179  }
180  fprintf(stderr,"eemcfeerootclose: OK (total events=%8d)\n",evnum);
181  if(filename) delete [] filename;
182  return;
183 }