(file) Return to thTree.c CVS log (file) (dir) Up to [HallC] / Analyzer / CTP

  1 saw   1.1 #ifdef ROOTTREE
  2           /*-----------------------------------------------------------------------------
  3            * Copyright (c) 1999      Thomas Jefferson National Accelerator Facility
  4            *
  5            * This software was developed under a United States Government license
  6            * described in the NOTICE file included as part of this distribution.
  7            *
  8            * Stephen A. Wood, 12000 Jefferson Ave., Newport News, VA 23606
  9            * Email: saw@jlab.org  Tel: (758) 269-7367  Fax: (757) 269-5235
 10            *-----------------------------------------------------------------------------
 11            * 
 12            * Description:
 13            *  Book ROOT trees.
 14            *	
 15            * Author:  Stephen Wood, CEBAF Hall C
 16            *
 17            * Revision History:
 18            *   $Log: thTree.c,v $
 19 saw   1.2  *   Revision 1.1  2002/07/31 20:07:48  saw
 20            *   Add files for ROOT Trees
 21            *
 22 saw   1.1  *   Revision 1.1  1999/08/25 13:16:07  saw
 23            *   *** empty log message ***
 24            *
 25            */
 26           
 27           #include <stdio.h>
 28           #include <stdlib.h>
 29           #include <string.h>
 30           #include <math.h>
 31           #include "daVar.h"
 32           #include "th.h"
 33           #include "thInternal.h"
 34           #include "thUtils.h"
 35           
 36           extern daVarStatus thTreeRHandler();
 37           
 38           struct thLeafList {		/* Variable and index list */
 39             struct thLeafList *next;
 40             char *name;
 41             int leafsize;			/* Number of bytes in leaf */
 42             char leaftype;		/* Single letter with leaf type */
 43 saw   1.1   daVarStruct *varp; int index;
 44           };
 45           typedef struct thLeafList thLeafList;
 46           
 47           struct thTreeBranchList {
 48             struct thTreeBranchList *next;
 49             char *branchname;		/* Block name without the "block.hist" */
 50             void *evstruct;		/* Event structure to fill with data */
 51             struct thLeafList *leaflistp;
 52           };
 53           typedef struct thTreeBranchList thTreeBranchList;
 54           
 55           struct thTreeOpaque {		/* Opaque structure for histogram definition */
 56             void *file;
 57             /*  void *file Need to point to structure that has file info.  File structures will also
 58             be on a linked list so that we a new file is opened, you can see if it exists.  Wait, we
 59           should just use davars.  Make a new class for files? ? 
 60            */
 61             void *treeptr;		/* Pointer to tree object */
 62             daVarStruct *test; int testindex;
 63             thTreeBranchList *branchlistP;
 64 saw   1.1 };
 65           typedef struct thTreeOpaque thTreeOpaque;
 66           
 67           struct thRBlockList {
 68             struct thRBlockList *next;
 69             char *blockname;		/* Block name without the "block.tree" */
 70             daVarStruct *var;		/* Varptr points to # times called
 71           				   Title is code from file.
 72           				   opaque is pointer to hist speclist */
 73           };
 74           typedef struct thRBlockList thRBlockList;
 75           
 76           thRBlockList *thRBlockListP;	/* Pointer to list of tree blocks */
 77           
 78           thStatus thBookaBranch(thTreeOpaque *tree, char *line, thTreeBranchList **thBranchNext);
 79                /*thStatus thExecuteaHist(thHistSpecList *Hist);*/
 80           thStatus thRemoveTree(char *block_name);
 81           
 82           thStatus thBookTree(daVarStruct *var)
 83           {
 84             char *lines,*eol;
 85 saw   1.1   int line_count;
 86             thTreeBranchList **thBranchNext;
 87             char *blockname;
 88             thTreeOpaque *treedef;
 89             int i;
 90             int isopen;
 91             char *lbuf=0;
 92           
 93             /*  thHistZeroLastId(); */
 94             
 95             printf("In booktrees\n");
 96             /* Get the name without the block.test on it */
 97             blockname = var->name;	/* If name doesn't fit pattern, use whole */
 98             if(strcasestr(var->name,BLOCKSTR)==var->name){
 99               i = strlen(BLOCKSTR) + 1;
100               if(strcasestr((var->name + i),TREESTR)==(var->name + i)){
101                 i += strlen(TREESTR);
102                 if(*(var->name + i) == '.'){
103           	blockname += i + 1;
104                 }
105               }
106 saw   1.1   }
107           
108             printf("Booking tree %s\n",blockname);
109           
110             if(var->opaque) thRemoveTree(blockname);
111           
112             /* We assume for now that thRemoveTree completely removed the opaque definition)
113              */
114             treedef = var->opaque = (thTreeOpaque *) malloc(sizeof(thTreeOpaque));
115             thBranchNext = (thTreeBranchList **) &treedef->branchlistP;
116             
117             lines = var->title;
118             line_count = 0;
119             isopen = 0;
120             while(*lines){
121               char *lcopy;
122           
123               line_count++;
124               eol = strchr(lines,'\n');
125               if(!eol) {
126                 fprintf(stderr,"L %d: Last line of hist block %s has no newline\n"
127 saw   1.1 	      ,line_count,var->name);
128                 break;
129               }
130               if(*(eol+1)=='\0'){		/* This is the last line */
131                 if(strcasestr(lines,ENDSTR) == 0)
132           	fprintf(stderr,"L %d: Last line of tree block %s is not an END\n"
133           		,line_count,var->name);
134                 break;
135               }
136               if(line_count == 1) {
137                 char *fname=0;
138                 if(strcasestr(lines,BEGINSTR) !=0) {
139           	char *p;
140           	if(p = strcasestr(lines,"file=")) {
141           	  p += 5;
142           	  /* If " or ', grab to next matching char */
143           	  /* other interpret as variable.  But interpret as file if variable not found */
144           	  if(*p == QUOTECHAR1 || *p == QUOTECHAR2) {
145           	    char *s; int len;
146           	    s = p+1;
147           	    while(*s && *s != *p && *s !='\n') s++;
148 saw   1.1 	    len = (s - p) - 1;
149           	    fname = malloc(len+1);
150           	    strncpy(fname,p+1,len);
151           	    fname[len] = '\0';
152           	  } else {		/* Probably a variable */
153           	    char *varname=0; char *s; int len; int index;
154           	    daVarStruct *varp;
155           	    s = p;
156           	    while(*s && !isspace(*s) && *s !='\n') s++;
157           	    len = (s-p);
158           	    varname = malloc(len+1);
159           	    strncpy(varname,p,len);
160           	    varname[len] = '\0';
161           	    if(thVarResolve(varname,&varp,&index,1,0)==S_SUCCESS) {
162           	      printf("%s,type=%d, size=%d\n",varp->name,varp->type,varp->size);
163           	      printf("%s\n",(char *) varp->varptr);
164           	      if(varp->type == DAVARSTRING) {
165           		fname = malloc(strlen((char *)varp->varptr)+1);
166           		strcpy(fname,(char *)varp->varptr);
167           	      } else if(varp->type == DAVARFSTRING) {
168           		fname = malloc(varp->size+1);
169 saw   1.1 		strncpy(fname,(char *)varp->varptr,varp->size);
170           		fname[varp->size] = '\0';
171           		p = fname;
172           		while(*p && !isspace(*p)) p++;
173           		*p = '\0';	/* Null terminate at first blank */
174           	      }
175           	    }
176           	    if(!fname) {
177           	      fname = malloc(len+1);
178           	      strncpy(fname,p,len);
179           	      fname[len] = '\0';
180           	    }
181           	  }
182           	}
183                 }
184                 
185                 if(fname) {
186           	/*printf("Opening Root file %s\n",fname);*/
187 saw   1.2 	treedef->file = (void *) _Z12thRoot_TFilePc(fname);
188 saw   1.1 	free(fname);
189                 } else {
190           	/*printf("Opening Root file %s\n","ctp.tree");*/
191 saw   1.2 	treedef->file = (void *) _Z12thRoot_TFilePc("ctp.root");
192 saw   1.1       }
193               
194                 /*printf("Call to TTree(\"%s\",\"title\") goes here\n",blockname);*/
195 saw   1.2       treedef->treeptr = (void *) _Z12thRoot_TTreePc(blockname);
196 saw   1.1 
197                 if(strcasestr(lines,BEGINSTR) != 0){
198           	/*	printf("Is a begin\n");*/
199           	lines = eol + 1;
200           	continue;
201                 } else
202           	fprintf(stderr,"First line of tree block %s is not a BEGIN\n",var->name);
203               }
204               /* Ready to book the line, Add continuation lines later */
205               lcopy = (char *) malloc(eol-lines+1);
206               strncpy(lcopy,lines,(eol-lines));
207               *(lcopy + (eol-lines)) = '\0';
208               if(!thCleanLine(lcopy)){
209                 if(strchr(lcopy,'=')) {	/* Start of a new branch */
210           	if(lbuf) {		/* Do we have a pending branch */
211           	  /*printf("Passing 1 |%s|\n",lbuf);*/
212           	  if(thBookaBranch(treedef,lbuf,thBranchNext)==S_SUCCESS){
213           	    thBranchNext = &((*thBranchNext)->next);
214           	  } else {
215           	    fprintf(stderr,"(%s): Tree booking error in line %d\n",var->name,line_count);
216           	  }
217 saw   1.1 	  free(lbuf);
218           	  lbuf = 0;
219           	}
220                 }
221                 if(lbuf) {		/* Append */
222           	char *lastcomma;
223           	char *firstcomma;
224           	int addcomma=0; 
225           	lastcomma = lbuf + strlen(lbuf) - 1;
226           	while(*lastcomma == ' ') lastcomma--;
227           	if(*lastcomma != ',' && *lastcomma != '=') lastcomma = 0;
228           	firstcomma = lcopy;
229           	while(*firstcomma == ' ') firstcomma++;
230           	if(*firstcomma != ',') firstcomma = 0;
231           	if(firstcomma && lastcomma) {
232           	  *firstcomma = ' ';
233           	} else if (!firstcomma && !lastcomma) {
234           	  addcomma = 1;
235           	}
236           	lbuf = realloc(lbuf,strlen(lbuf) + strlen(lcopy) + 2);
237           	if(addcomma) strcat(lbuf,",");
238 saw   1.1 	strcat(lbuf,lcopy);
239                 } else {			/* Make new lbuf */
240           	lbuf = malloc(strlen(lcopy)+1);
241           	strcpy(lbuf,lcopy);
242                 }
243               }
244               free(lcopy);
245               lines = eol+1;
246             }
247             if(lbuf) {			/* Do the last branch we were building */
248               /*printf("Passing 2 |%s|\n",lbuf);*/
249               if(thBookaBranch(treedef,lbuf,thBranchNext)==S_SUCCESS){
250                 thBranchNext = &((*thBranchNext)->next);
251               } else {
252                 fprintf(stderr,"(%s): Tree booking error in line %d\n",var->name,line_count);
253               }
254               free(lbuf);
255             }
256             /* Update internal table of trees. */
257             {
258               thRBlockList *thisblock,*nextblock,**lastblockp;
259 saw   1.1     nextblock = thRBlockListP;
260               lastblockp = &thRBlockListP;
261               thisblock = thRBlockListP;
262               while(thisblock){
263                 if((strcasecmp(thisblock->var->name,var->name)) == 0){
264           	/* Replacing a block with a new definition */
265           	fprintf(stderr,"Replacing %s with new definition\n",var->name);
266           	if(thisblock->var != var){
267           	  fprintf(stderr,"ERR: Same name, different var pointer\n");
268           	}
269           	break;
270                 }
271                 lastblockp = &thisblock->next;
272                 thisblock = thisblock->next;
273               }
274               if(!thisblock){		/* Create entry for New block */
275                 *lastblockp = thisblock = (thRBlockList *) malloc(sizeof(thRBlockList));
276                 thisblock->var = var;
277                 thisblock->next = (thRBlockList *) NULL;
278                 thisblock->blockname = (char *) malloc(strlen(blockname) + 1);
279                 strcpy(thisblock->blockname,blockname);
280 saw   1.1     }
281             }
282           /*  printf("Returning from booking a tree\n");*/
283             return(S_SUCCESS);
284           }
285           
286           thStatus thBookaBranch(thTreeOpaque *treedef, char *line, thTreeBranchList **thBranchNext)
287                /* Interpret a branch def of the form branch=leaf1,leaf2,leaf3,... */
288                /* For now require the "branch=" part */
289           {
290           
291             /*  char *long_title;*/
292             int n,nleafs;
293             int lenbrancharg;
294             char *brancharg;
295             char *sleafs,*branchname;
296             thTreeBranchList *Branch;
297             thLeafList **LeafNext;
298             thLeafList *thisleaf;
299             daVarStruct *varp;
300             int vind;
301 saw   1.1   char *args[100];
302           
303             /*printf("In thBookaBranch\n");*/
304             if(!(sleafs = strchr(line,'='))) {
305               return(S_FAILURE);
306             }    
307             *sleafs=0;
308             sleafs++;			/* Pointer to list of leaves */
309             nleafs = thCommas(sleafs,args);
310             if(nleafs <=0) {
311               return(S_FAILURE);
312             }
313             Branch = *thBranchNext = (thTreeBranchList *) malloc(sizeof(thTreeBranchList));
314             Branch->next = (thTreeBranchList *) NULL;
315             branchname = thSpaceStrip(line);
316             Branch->branchname = malloc(strlen(branchname)+1);
317             LeafNext = (thLeafList **) &Branch->leaflistp;
318             strcpy(Branch->branchname,branchname);
319             lenbrancharg = 0;
320             for(n=0;n<nleafs;n++) {
321               char *nameptr;
322 saw   1.1     /* Need to look for $name here.  name will be the name given to root */
323               args[n] = thSpaceStrip(args[n]);
324               /*printf("Leaf %s\n",args[n]);*/
325               if(nameptr=strchr(args[n],'$')) *nameptr++=0;
326               if(thVarResolve(args[n],&varp,&vind,0,0) == S_SUCCESS) {
327                 char *p, snum[25];
328                 /*printf("Index=%d\n",vind);*/
329                 thisleaf = *LeafNext = (thLeafList *) malloc(sizeof(thLeafList));
330                 /*printf("thisleaf = %x\n",thisleaf);*/
331                 thisleaf->next = (thLeafList *) NULL;
332                 thisleaf->varp = varp;
333                 thisleaf->index = vind;
334                 /* Pick a good name */
335                 if(nameptr) {
336           	thisleaf->name = (char *) malloc(strlen(nameptr)+1);
337           	strcpy(thisleaf->name,nameptr);
338                 } else {
339           	if(p=strpbrk(args[n],"()[]")) {
340           	  sprintf(snum,"%d",vind+1);
341           	  thisleaf->name = (char *) malloc(strlen(args[n])+strlen(snum)+2);
342           	  strncpy(thisleaf->name,args[n],p-args[n]);
343 saw   1.1 	  thisleaf->name[p-args[n]] = '\0';
344           	  strcat(thisleaf->name,"_");
345           	  strcat(thisleaf->name,snum);
346           	} else {
347           	  thisleaf->name = (char *) malloc(strlen(args[n])+1);
348           	  strcpy(thisleaf->name,args[n]);
349           	}
350                 }
351                 LeafNext = &((*LeafNext)->next);
352                 lenbrancharg += strlen(args[n]) + 3;
353               }	else {
354                 fprintf(stderr,"Bad variable %s\n",args[n]);
355               }
356             }
357             /* Walk down the leaf list and build the Branch call argument */
358             /* What do I do about leaf names with subscripts? */
359             thisleaf = Branch->leaflistp;
360             brancharg = malloc(lenbrancharg+10);
361             brancharg[0] = '\0';
362             while(thisleaf) {
363               /*    printf("thisleaf = %x\n",thisleaf);*/
364 saw   1.1     /*printf("Adding %s to branchlist\n",thisleaf->name);*/
365               strcat(brancharg,thisleaf->name);
366               if(thisleaf->varp->type == DAVARINT) {
367                 strcat(brancharg,"/I");
368                 thisleaf->leafsize=4;
369               } else if(thisleaf->varp->type == DAVARFLOAT) {
370                 strcat(brancharg,"/F");
371                 thisleaf->leafsize=4;
372               } else if(thisleaf->varp->type == DAVARDOUBLE) {
373                 strcat(brancharg,"/D");
374                 thisleaf->leafsize=8;
375               } else {
376                 fprintf(stderr,"Variable %s has unknown type\n");
377               }
378           
379               if(thisleaf->next) {
380                 strcat(brancharg,":");
381               }
382               thisleaf = thisleaf->next;
383             }
384           
385 saw   1.1   /* Reserve enough space as if they were all double */
386             Branch->evstruct = (void *) malloc(lenbrancharg*sizeof(double));
387           
388             /*
389                  * leaflist is the concatenation of all the variable names and types
390                    separated by a colon character :
391                    The variable name and the variable type are separated by a slash (/).
392                    The variable type may be 0,1 or 2 characters. If no type is given,
393                    the type of the variable is assumed to be the same as the previous
394                    variable. If the first variable does not have a type, it is assumed
395                    of type F by default. The list of currently supported types is given below:
396                       - C : a character string terminated by the 0 character
397                       - B : an 8 bit signed integer (Char_t)
398                       - b : an 8 bit unsigned integer (UChar_t)
399                       - S : a 16 bit signed integer (Short_t)
400                       - s : a 16 bit unsigned integer (UShort_t)
401                       - I : a 32 bit signed integer (Int_t)
402                       - i : a 32 bit unsigned integer (UInt_t)
403                       - F : a 32 bit floating point (Float_t)
404                       - D : a 64 bit floating point (Double_t)
405             */
406 saw   1.1 
407             /*printf("Branch=%s  Leafs=%s\n",Branch->branchname,brancharg);*/
408 saw   1.2   _Z13thRoot_BranchP5TTreePcPvS1_(treedef->treeptr,Branch->branchname,(Branch->evstruct),brancharg);
409 saw   1.1 
410             free(brancharg);
411             /*printf("Exiting book a branch\n");*/
412             return(S_SUCCESS);
413           }
414             
415           thStatus thFillTreeV(daVarStruct *var){
416             thTreeOpaque *treedef;
417             thTreeBranchList *thisbranch;
418             thLeafList *thisleaf;
419             void *structp;
420             /*  printf("Executing Tree %s\n",var->name);*/
421           
422             treedef = ((thTreeOpaque *)(var->opaque));
423             thisbranch = treedef->branchlistP;
424             while(thisbranch) {
425               structp = thisbranch->evstruct;
426               /*    printf("Filling branch %s at %x\n",thisbranch->branchname,structp);*/
427               thisleaf = thisbranch->leaflistp;
428               while(thisleaf) {
429                 if(thisleaf->varp->type == DAVARINT) {
430 saw   1.1 	*((DAINT *)(structp))++ = *((DAINT *)thisleaf->varp->varptr
431           				    + thisleaf->index);
432           	/*	printf("   %s=%d\n",thisleaf->name,*((DAINT *)thisleaf->varp->varptr
433           		+ thisleaf->index));*/
434                 } else if(thisleaf->varp->type == DAVARFLOAT) {
435           	*((DAFLOAT *)(structp))++ = *((DAFLOAT *)thisleaf->varp->varptr
436           				    + thisleaf->index);
437           	/*	printf("   %s=%f\n",thisleaf->name,*((DAFLOAT *)thisleaf->varp->varptr
438           		+ thisleaf->index));*/
439                 } else if(thisleaf->varp->type == DAVARDOUBLE) {
440           	*((DADOUBLE *)(structp))++ = *((DADOUBLE *)thisleaf->varp->varptr
441           				    + thisleaf->index);
442           	/*	printf("   %s=%lf\n",thisleaf->name,*((DADOUBLE *)thisleaf->varp->varptr
443           		+ thisleaf->index));*/
444                 }
445                 thisleaf = thisleaf->next;
446               }
447               thisbranch = thisbranch->next;
448             }
449 saw   1.2   _Z11thRoot_FillP5TTree(treedef->treeptr);
450 saw   1.1 
451             (*((DAINT *)var->varptr))++; /* Increment block counter */
452             return(S_SUCCESS);
453           }
454           thStatus thClearTreeV(daVarStruct *var){
455             
456             printf("Clearing Tree %s\n",var->name);
457           
458             (*((DAINT *)var->varptr)) = 0; /* Increment block counter */
459             return(S_SUCCESS);
460           }
461           thStatus thWriteTreeV(daVarStruct *var){
462             
463             printf("Writing Tree %s\n",var->name);
464 saw   1.2   _Z12thRoot_WriteP14thRootFileList(((thTreeOpaque *)(var->opaque))->file);
465 saw   1.1 
466             (*((DAINT *)var->varptr)) = 0; /* Increment block counter */
467             return(S_SUCCESS);
468           }
469           thStatus thCloseTreeV(daVarStruct *var){
470             
471             printf("Closing Tree %s\n",var->name);
472 saw   1.2   _Z12thRoot_CloseP14thRootFileList(((thTreeOpaque *)(var->opaque))->file);
473 saw   1.1 
474             (*((DAINT *)var->varptr)) = 0; /* Increment block counter */
475             return(S_SUCCESS);
476           }
477           
478           thStatus thRemoveTree(char *treename) {
479             printf("Dummy routine to remove tree %s\n",treename);
480             return(S_SUCCESS);
481           }
482           
483           /*
484           int thtreewrite_()
485           {
486             thRoot_Write__Fv();
487           }
488           */
489           
490           #endif

Analyzer/Replay: Mark Jones, Documents: Stephen Wood
Powered by
ViewCVS 0.9.2-cvsgraph-1.4.0