1 saw 1.1 //
2 // thRootStuff.cpp
3 // C++ wrapper routines to interface between CTP and Root libraries
4 //
5 // $Log: thRootStuff.cpp,v $
|
6 saw 1.3 // Revision 1.2 2004/07/07 18:16:55 saw
7 // use extern "C" to export names needed in thTree.c
8 //
|
9 saw 1.2 // Revision 1.1 2002/07/31 20:07:48 saw
10 // Add files for ROOT Trees
11 //
|
12 saw 1.1 // Revision 1.1 1999/08/25 13:16:07 saw
13 // *** empty log message ***
14 //
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <TROOT.h>
19 #include <TFile.h>
20 #include <TH1.h>
21 #include <TH2.h>
22 #include <TProfile.h>
23 #include <TRandom.h>
24 #include <TTree.h>
25
26 TROOT CTP("CTP","CTP Histograms and trees");
27
28 TTree *tree;
29 TFile *hfile;
30
31 struct thRootFileList {
32 char *filename;
33 saw 1.1 TFile *tfile;
34 int count;
35 struct thRootFileList *next;
36 };
37 typedef struct thRootFileList thRootFileList;
38
39 thRootFileList *rootfilelistp=0;
40
|
41 saw 1.2 extern "C" void *thRoot_TFile(char *filename);
42
43 extern "C" void *thRoot_TTree(char *treename);
44
45 extern "C" void thRoot_Branch(TTree *tree, char *branchname, void *structp, char *brancharg);
46
47 extern "C" void thRoot_Fill(TTree *tree);
48
49 extern "C" void thRoot_Write(thRootFileList *file);
50
51 extern "C" void thRoot_Close(thRootFileList *file);
52
|
53 saw 1.1 void *thRoot_TFile(char *filename)
54 {
55 thRootFileList *thisfile,**lastp;
56 thisfile = rootfilelistp;
57 lastp = &rootfilelistp;
58 while(thisfile) {
59 if(strcmp(thisfile->filename,filename)==0) {
60 thisfile->count++;
61 return((void *) thisfile);
62 }
63 lastp = &(thisfile->next);
64 thisfile = thisfile->next;
65 }
66 /* Need to check if file has been opened */
67 printf("Tfile(\"%s\",\"RECREATE\",\"CTP ROOT file with trees\")\n",filename);
68 *lastp = (thRootFileList *) malloc(sizeof(thRootFileList));
69 thisfile = *lastp;
70 thisfile->tfile = new TFile(filename,"RECREATE","CTP ROOT file with trees");
71 thisfile->count = 1;
72 thisfile->filename = (char *)malloc(strlen(filename)+1);
|
73 saw 1.3 thisfile->next = (thRootFileList *) 0;
|
74 saw 1.1 strcpy(thisfile->filename,filename);
75 return((void *) thisfile);
76 }
77
78 void *thRoot_TTree(char *treename)
79 {
80 TTree *tree;
81
82 /* Perhaps Check if a tree exists by this name?? */
83 printf("new TTree(\"%s\",\"CTP ROOT tree\")\n",treename);
84 tree = new TTree(treename,"CTP ROOT tree");
85 return((void *)tree);
86 }
87
88 void thRoot_Branch(TTree *tree, char *branchname, void *structp, char *brancharg)
89 {
90 tree->Branch(branchname,structp,brancharg);
91 }
92
93 void thRoot_Fill(TTree *tree)
94 {
95 saw 1.1 tree->Fill();
96 }
97
98 void thRoot_Write(thRootFileList *file)
99 {
100 (file->tfile)->Flush();
101 }
102
103 void thRoot_Close(thRootFileList *file)
104 {
105 TFile *hfile;
106 if(--file->count <= 0){
107 printf("Closing\n");
108 hfile = file->tfile;
109 hfile->Write();
110 hfile->Close();
111 } else {
112 printf("Not Closing\n");
113 }
114 }
|
115 saw 1.2
|