/[PAMELA software]/gp2root/gp2root.cpp
ViewVC logotype

Contents of /gp2root/gp2root.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Sun Oct 22 10:25:58 2006 UTC (18 years, 1 month ago) by pamela
Branch: MAIN
CVS Tags: v0r10, HEAD
Changes since 1.2: +18 -772 lines
Some gp2root.cpp clean up

1 // Author: Rene Brun 20/09/96
2 /////////////////////////////////////////////////////////////////////////
3 // Program to convert an HBOOK file into a ROOT file
4 // Author: Rene Brun
5 //
6 // This program is invoked via:
7 // h2root hbook_file_name root_file_name compress tolower
8 // if the second parameter root_file_name is missing the name will be
9 // automatically generated from the hbook file name. Example:
10 // h2root test.hbook
11 // is identical to
12 // h2root test.hbook test.root
13 // if compress is missing (or = 1)the ROOT file will be compressed
14 // if compress = 0 the ROOT file will not be compressed.
15 // if tolower is missing (or = 1) ntuple column names are converted to lower case
16 // but the first character is converted to upper case.
17 // if tolower = 2 same as tolower=1 except that the first character is also
18 // convertex to lower case
19 /////////////////////////////////////////////////////////////////////////
20
21 #include <stdlib.h>
22 #include <string>
23 #include <ctype.h>
24 #include <sstream>
25 #include <iostream>
26
27 #include "Riostream.h"
28 #include "TFile.h"
29 #include "TDirectory.h"
30 #include "TTree.h"
31 #include "TLeafI.h"
32 #include "TH1.h"
33 #include "TH2.h"
34 #include "TProfile.h"
35 #include "TGraph.h"
36 #include "TMath.h"
37 //#include "../gpevent.h"
38 #include "pBlockPointer.h"
39 #include "pEvent.h"
40
41 int Error; //to be removed soon
42
43 // Define the names of the Fortran common blocks for the different OSs
44
45 #ifndef WIN32
46 #define PAWC_SIZE 5000000
47 # define pawc pawc_
48 # define quest quest_
49 # define hcbits hcbits_
50 # define hcbook hcbook_
51 # define rzcl rzcl_
52 int pawc[PAWC_SIZE];
53 int quest[100];
54 int hcbits[37];
55 int hcbook[51];
56 int rzcl[11];
57 #else
58 // on windows /pawc/ must have the same length as in libPacklib.a !!
59 #define PAWC_SIZE 2000000
60 # define pawc PAWC
61 # define quest QUEST
62 # define hcbits HCBITS
63 # define hcbook HCBOOK
64 # define rzcl RZCL
65 extern "C" int pawc[PAWC_SIZE];
66 extern "C" int quest[100];
67 extern "C" int hcbits[37];
68 extern "C" int hcbook[51];
69 extern "C" int rzcl[11];
70 #endif
71
72 int *iq, *lq;
73 float *q;
74 char idname[128];
75 int nentries;
76 char chtitl[128];
77 int ncx,ncy,nwt,idb;
78 int lcont, lcid, lcdir;
79 float xmin,xmax,ymin,ymax;
80 const Int_t kMIN1 = 7;
81 const Int_t kMAX1 = 8;
82
83 #if defined __linux
84 //On linux Fortran wants this, so we give to it!
85 int xargv=0;
86 int xargc=0;
87 void MAIN__() {}
88 #endif
89
90 // Define the names of the Fortran subroutine and functions for the different OSs
91
92 #ifndef WIN32
93 # define hlimit hlimit_
94 # define hropen hropen_
95 # define hrin hrin_
96 # define hnoent hnoent_
97 # define hgive hgive_
98 # define hgiven hgiven_
99 # define hprntu hprntu_
100 # define hgnpar hgnpar_
101 # define hgnf hgnf_
102 # define hgnt hgnt_
103 # define hgntb hgntb_
104 # define rzink rzink_
105 # define hdcofl hdcofl_
106 # define hmaxim hmaxim_
107 # define hminim hminim_
108 # define hdelet hdelet_
109 # define hntvar2 hntvar2_
110 # define hbname hbname_
111 # define hbnamc hbnamc_
112 # define hbnam hbnam_
113 # define hi hi_
114 # define hie hie_
115 # define hif hif_
116 # define hij hij_
117 # define hix hix_
118 # define hijxy hijxy_
119 # define hije hije_
120 # define hcdir hcdir_
121 # define zitoh zitoh_
122 # define uhtoc uhtoc_
123
124 # define type_of_call
125 # define DEFCHAR const char*
126 # define PASSCHAR(string) string
127 #else
128 # define hlimit HLIMIT
129 # define hropen HROPEN
130 # define hrin HRIN
131 # define hnoent HNOENT
132 # define hgive HGIVE
133 # define hgiven HGIVEN
134 # define hprntu HPRNTU
135 # define hgnpar HGNPAR
136 # define hgnf HGNF
137 # define hgnt HGNT
138 # define rzink RZINK
139 # define hdcofl HDCOFL
140 # define hmaxim HMAXIM
141 # define hminim HMINIM
142 # define hdelet HDELET
143 # define hntvar2 HNTVAR2
144 # define hbname HBNAME
145 # define hbnamc HBNAMC
146 # define hbnam HBNAM
147 # define hi HI
148 # define hie HIE
149 # define hif HIF
150 # define hij HIJ
151 # define hix HIX
152 # define hijxy HIJXY
153 # define hije HIJE
154 # define hcdir HCDIR
155 # define zitoh ZITOH
156 # define uhtoc UHTOC
157 # define type_of_call _stdcall
158 # define DEFCHAR const char*, const int
159 # define PASSCHAR(string) string, strlen(string)
160 #endif
161
162 extern "C" void type_of_call hlimit(const int&);
163 #ifndef WIN32
164 extern "C" void type_of_call hropen(const int&,DEFCHAR,DEFCHAR,DEFCHAR,
165 const int&,const int&,const int,const int,const int);
166 #else
167 extern "C" void type_of_call hropen(const int&,DEFCHAR,DEFCHAR,DEFCHAR,
168 const int&,const int&);
169 #endif
170
171 extern "C" void type_of_call hrin(const int&,const int&,const int&);
172 extern "C" void type_of_call hnoent(const int&,const int&);
173 #ifndef WIN32
174 extern "C" void type_of_call hgive(const int&,DEFCHAR,const int&,const float&,const float&,
175 const int&,const float&,const float&,const int&,const int&,const int);
176 #else
177 extern "C" void type_of_call hgive(const int&,DEFCHAR,const int&,const float&,const float&,
178 const int&,const float&,const float&,const int&,const int&);
179 #endif
180
181 #ifndef WIN32
182 extern "C" void type_of_call hgiven(const int&,DEFCHAR,const int&,DEFCHAR,
183 const float&,const float&,const int,const int);
184 #else
185 extern "C" void type_of_call hgiven(const int&,DEFCHAR,const int&,DEFCHAR,
186 const float&,const float&);
187 #endif
188
189 #ifndef WIN32
190 extern "C" void type_of_call hntvar2(const int&,const int&,DEFCHAR,DEFCHAR,DEFCHAR,int&,int&,int&,int&,int&,const int,const int, const int);
191 #else
192 extern "C" void type_of_call hntvar2(const int&,const int&,DEFCHAR,DEFCHAR,DEFCHAR,int&,int&,int&,int&,int&);
193 #endif
194
195 #ifndef WIN32
196 extern "C" void type_of_call hbnam(const int&,DEFCHAR,const int&,DEFCHAR,const int&,const int, const int);
197 #else
198 extern "C" void type_of_call hbnam(const int&,DEFCHAR,const int&,DEFCHAR,const int&);
199 #endif
200
201 extern "C" void type_of_call hprntu(const int&);
202 extern "C" void type_of_call hgnpar(const int&,const char *,const int);
203 extern "C" void type_of_call hgnf(const int&,const int&,const float&,const int&);
204 extern "C" void type_of_call hgnt(const int&,const int&,const int&);
205 extern "C" void type_of_call hgntb(const int&,DEFCHAR,const int&,const int&);
206 extern "C" void type_of_call rzink(const int&,const int&,const char *,const int);
207 extern "C" void type_of_call hdcofl();
208 extern "C" void type_of_call hmaxim(const int&,const float&);
209 extern "C" void type_of_call hminim(const int&,const float&);
210 extern "C" void type_of_call hdelet(const int&);
211 extern "C" float type_of_call hi(const int&,const int&);
212 extern "C" float type_of_call hie(const int&,const int&);
213 extern "C" float type_of_call hif(const int&,const int&);
214 extern "C" float type_of_call hij(const int&,const int&,const int&);
215 extern "C" void type_of_call hix(const int&,const int&,const float&);
216 extern "C" void type_of_call hijxy(const int&,const int&,const int&,const float&,const float&);
217 extern "C" float type_of_call hije(const int&,const int&,const int&);
218 #ifndef WIN32
219 extern "C" void type_of_call hcdir(DEFCHAR,DEFCHAR ,const int,const int);
220 #else
221 extern "C" void type_of_call hcdir(DEFCHAR,DEFCHAR);
222 #endif
223
224 extern "C" void type_of_call zitoh(const int&,const int&,const int&);
225 #ifndef WIN32
226 extern "C" void type_of_call uhtoc(const int&,const int&,DEFCHAR,int&,const int);
227 #else
228 extern "C" void type_of_call uhtoc(const int&,const int&,DEFCHAR,int&);
229 #endif
230
231 extern void convert_directory(const char*);
232 extern void convert_1d(Int_t id);
233 extern void convert_2d(Int_t id);
234 extern void convert_profile(Int_t id);
235 extern void convert_cwn(Int_t id);
236 extern void convert_rwn(Int_t id);
237 extern void convert_psam(const char*, TTree *, pEvent *);
238
239 Int_t golower = 1;
240 Int_t bufsize = 64000;
241 Int_t optcwn = 1;
242
243
244
245 int main(int argc, char **argv)
246 {
247 if (argc < 2) {
248 printf("******Error in invoking h2root\n");
249 printf("===> h2root file.hbook file.root [compress] [tolower] [lrecl] [bufsize] [optcwn] \n");
250 printf(" if file.root is not given it will be = file.root\n");
251 printf(" compress = 1 by default (use 0 for no compression)\n");
252 printf(" tolower = 1 by default (use 0 to keep case of column names)\n");
253 printf(" lrecl =0 by default (must be specified if >8092)\n");
254 printf(" bufsize = 8000 by default (branch buffer size)\n");
255 printf(" for cwn ntuple only: optcwn = 1 (default) 1-byte int -> char, 2-byte int -> short, (use 0 to keep 4-byte int) \n");
256 return 1;
257 }
258 lq = &pawc[9];
259 iq = &pawc[17];
260 void *qq = iq;
261 q = (float*)qq;
262 char *file_in=argv[1];
263 char *file_out;
264 Int_t compress = 1;
265 int ier=0, record_size=0;
266 if (argc > 7) {
267 optcwn = atoi(argv[7]);
268 }
269 if (argc > 6) {
270 bufsize = atoi(argv[6]);
271 }
272 if (argc > 5) {
273 record_size = atoi(argv[5]);
274 }
275 if (argc > 4) {
276 golower = atoi(argv[4]);
277 }
278 if (argc > 3) {
279 compress = atoi(argv[3]);
280 }
281 if (argc > 2) {
282 file_out=argv[2];
283 } else {
284 file_out= new char[2000];
285 strcpy(file_out,file_in);
286 char *dot = strrchr(file_out,'.');
287 if (dot) strcpy(dot+1,"root");
288 else strcat(file_out,".root");
289 }
290
291 #if defined(_HIUX_SOURCE) && !defined(__GNUC__)
292 hf_fint((char *)NULL);
293 #endif
294
295
296 int pawc_size = PAWC_SIZE;
297 hlimit(pawc_size);
298
299 int lun = 10;
300 #ifndef WIN32
301 hropen(lun,PASSCHAR("example"),PASSCHAR(file_in),PASSCHAR("p"),record_size,ier,7,strlen(file_in),1);
302 #else
303 hropen(lun,PASSCHAR("example"),PASSCHAR(file_in),PASSCHAR("p"),record_size,ier);
304 #endif
305
306 if (ier) printf (" Error on hropen was %d \n", ier);
307 if (quest[0]) {
308 printf("Error cannot open input file: %s\n",file_in);
309 return 1;
310 }
311
312 char root_file_title[2000];
313 sprintf(root_file_title,"HBOOK file: %s converted to ROOT",file_in);
314 TFile* hfile=new TFile(file_out,"RECREATE",root_file_title,compress);
315
316 // Create global GPAMELA event
317
318 pEvent *myevent=new pEvent();
319 // Create ROOT tree and assign a Branch to the event
320 TTree* gptree=new TTree("gptree","GPAMELA tree");
321 gptree->Branch("pEvent_br","pEvent",&myevent,16000,0);
322
323 if (!hfile) {
324 printf("Error: can't open output file: %s \n",file_out);
325 return 1;
326 }
327
328
329 convert_psam("//example",gptree,myevent);
330 // convert_directory("//example");
331
332 hfile->Write();
333 hfile->ls();
334 hfile->Close();
335 delete hfile;
336 return(0);
337 }
338
339 void convert_psam(const char *dir, TTree *gptree, pEvent* myevent){
340 int nsub,itype,isize,ielem,ierr;
341 const int kNchar=9;
342 printf(" Converting directory %s\n",dir);
343 Int_t id=20;
344 Int_t nvar;
345 float rmin[1000], rmax[1000];
346 int i999 = 999;
347 char * chtag_out;
348
349 Int_t bufpos = 0;
350 Int_t isachar = 0;
351 Int_t isabool = 0;
352 char fullname[1024];
353 char name[512];
354 char block[512];
355 char oldblock[512];
356 Int_t nbits = 0;
357 strcpy(oldblock,"OLDBLOCK");
358 Int_t oldischar = -1;
359 string blockname;
360 char *bigbuf = new char[2500000];
361 // Long_t add= (Long_t)&bigbuf[0];
362 char *t=bigbuf;
363
364
365
366 std::istringstream *test;
367
368 hrin(id,i999,0);
369 if (quest[0]) {
370 printf("Error cannot read ID = %d\n",id);
371 return;
372 // break;
373 }
374 if (id > 0) sprintf(idname,"h%d",id);
375 else sprintf(idname,"h_%d",-id);
376 hnoent(id,nentries);
377 printf(" Converting CWN with ID= %d, nentries = %d\n",id,nentries);
378 nvar=0;
379
380 hgiven(id,chtitl,nvar,PASSCHAR(""),rmin[0],rmax[0],80,0);
381 chtag_out = new char[nvar*kNchar+1];
382 hgiven(id,chtitl,nvar,chtag_out,rmin[0],rmax[0],80,kNchar);
383
384 // my_pointer m;
385
386 pBlockPointer m;
387 for( int i=0;i<nvar;++i) {
388 hntvar2(id,i+1,PASSCHAR(name),PASSCHAR(fullname),
389 PASSCHAR(block),nsub,itype,isize,nbits,ielem,
390 512,1024,512);
391 // std::cout << strlen(block) << " i: " << i << std::endl;
392 test= new std::istringstream(block);
393 *test >> blockname;
394 m.CountByte(blockname)+=(ielem*isize);
395 m.CountVar(blockname)++;
396 m.SetMult(blockname)=ielem;
397 // std::cout << blockname << std::endl;
398 delete test;
399 }
400 // std::cout << " Block found : " << m.Blocks() << " Size :" << m.TotBufSize()
401 // << std::endl;
402 m.Print();
403 for(pBlockMap::const_iterator p=(m.GetBlockMap())->begin();
404 p!=(m.GetBlockMap())->end(); ++p){
405 std::cout << p->name << ": "<< p->nbyte << '\n';
406 if(p->name!="GENERAL" && p->name!="CALI"
407 && p->name!="TRD" && p->name!= "SPE")
408 myevent->AddDetector(p->name);
409 }
410 myevent->Print();
411 t=m.CreatePbuf();
412 hbnam(id,PASSCHAR(" "),(int) *t,PASSCHAR("$CLEAR"),0,1,6);
413
414 // Now is time to register all the blocks to the hbook routine
415 Int_t ischar=0;
416 for(pBlockMap::const_iterator p=(m.GetBlockMap())->begin();
417 p!=(m.GetBlockMap())->end(); ++p){
418
419 if(p->name!="GENERAL" && p->name!="CALI" && p->name!="CAL"
420 && p->name!="TRD" && p->name!= "SPE"){
421 std::cout << p->name.c_str() << ": "<< p->nbyte << '\n';
422 hbnam(id,PASSCHAR(p->name.c_str()),
423 (Long_t) m.BlockAddress(p->name.c_str()),
424 PASSCHAR("$SET"),ischar,
425 strlen( p->name.c_str() ),4);
426 }
427 }
428 char * junk;
429 for(int i=0;i<nentries;++i){
430 hgnt(id,i+1,ierr);
431 junk=m.BuffAddress();
432 junk=m.BuffReorder();
433 std::cout << junk << std::dec <<
434 ", " << ierr << std::endl;
435 for(pBlockMap::const_iterator p=(m.GetBlockMap())->begin();
436 p!=(m.GetBlockMap())->end(); ++p){
437
438 if(p->name!="GENERAL" && p->name!="CALI" && p->name!="CAL"
439 && p->name!="TRD" && p->name!= "SPE" ){
440 std::cout << p->name.c_str() << ": "<< p->nbyte << '\n';
441 int nhit= *(int *) m.BlockAddressR(p->name.c_str());
442 char * cc= m.BlockAddressR(p->name.c_str());
443 cc=cc+4;
444 int nb= (m.BlockVar(p->name.c_str())-1)*4;
445 std::cout << " nhit : "<< nhit << endl;
446 for (int i=0; i<nhit;++i) {
447 myevent->AddHit(p->name.c_str(),cc);
448 cc+=nb;
449 }
450 }
451 gptree->Fill();
452 }
453 }
454 myevent->Print();
455 }
456 //____________________________________________________________________________

  ViewVC Help
Powered by ViewVC 1.1.23