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

Contents of /gp2root/gp2root.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Sun Oct 22 08:26:36 2006 UTC (18 years, 1 month ago) by cafagna
Branch: MAIN
CVS Tags: v0r9
Changes since 1.1: +108 -108 lines
Stable version, lot of changes, ROOT support included, persistency not yet activated

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*);
238
239 Int_t golower = 1;
240 Int_t bufsize = 64000;
241 Int_t optcwn = 1;
242
243
244 // struct gdata{
245 // int irun;
246 // int iev;
247 // int ipa;
248 // float x0;
249 // float y0;
250 // float z0;
251 // float theta;
252 // float phi;
253 // float p0;
254 // };
255
256 // union gcdata{
257 // gdata data;
258 // char c[36];
259 // };
260
261
262 // struct general{
263
264 // gcdata dat;
265
266
267 // void SetIt(char * buf){
268 // for(int i=0;i<36;++i) dat.c[i]=*(buf+i);
269 // // irun=*(int *)buf++;
270 // // iev =(int) *buf++;
271 // // ipa =(int) *buf++;
272 // // x0 =(float) *buf++;
273 // // y0 =(float) *buf++;
274 // // z0 =(float) *buf++;
275 // // theta =(float) *buf++;
276 // // phi =(float) *buf++;
277 // // p0 =(float) *buf++;
278
279 // }
280
281 // void Print(){
282 // std::cout << dat.data.irun << ", " <<
283 // dat.data.iev << ", " <<
284 // dat.data.ipa << ", " <<
285 // dat.data.x0 << ", " <<
286 // dat.data.y0 << ", " <<
287 // dat.data.z0 << ", " <<
288 // dat.data.theta << ", " <<
289 // dat.data.phi << ", " <<
290 // dat.data.p0 << std::endl;
291
292 // }
293 // };
294
295
296 // struct cascwn {
297 // int nthcas;
298 // int iparcas[50];
299 // int icas[50];
300 // float x0[50];
301 // float y0[50];
302 // float z0[50];
303 // float x1[50];
304 // float y1[50];
305 // float z1[50];
306 // float erel[50];
307 // float time[50];
308 // float path[50];
309 // float p0[50];
310
311 // };
312
313 // struct structcas{
314
315 // cascwn *cas; //= new cascwn;
316
317 // void SetIt(char * buf){
318 // cas = (cascwn *) buf;
319 // }
320
321 // void Print(){
322 // std::cout << cas->nthcas << ", " <<
323 // // iparcas[0] << ", " <<
324 // // x0[0] << ", " <<
325 // // y0[0] << ", " <<
326 // // z0[0] << ", " <<
327 // // erel[0] << ", " <<
328 // // p0[0] <<
329 // std::endl;
330
331 // }
332
333 // gpcasdat GetCas(int &i){
334 // gpcasdat temp;
335 // temp.pathcas=(double *)&cas->path[i];
336 // temp.icas=&cas->icas[i];
337 // temp.dat = new gpdat();
338 // temp.dat->ipart=&cas->iparcas[i];
339 // temp.dat->xyzin = new coord();
340 // temp.dat->xyzout = new coord();
341 // temp.dat->xyzin->x=(double )cas->x0[i];
342 // temp.dat->xyzin->y=(double )cas->y0[i];
343 // temp.dat->xyzin->z=(double )cas->z0[i];
344 // temp.dat->xyzout->x=(double )cas->x1[i];
345 // temp.dat->xyzout->y=(double )cas->y1[i];
346 // temp.dat->xyzout->z=(double )cas->z1[i];
347 // temp.dat->erel=(double *)&cas->erel[i];
348 // temp.dat->time=(double *)&cas->time[i];
349 // temp.dat->p=(double *)&cas->p0[i];
350 // return temp;
351 // }
352 // };
353
354
355 int main(int argc, char **argv)
356 {
357 if (argc < 2) {
358 printf("******Error in invoking h2root\n");
359 printf("===> h2root file.hbook file.root [compress] [tolower] [lrecl] [bufsize] [optcwn] \n");
360 printf(" if file.root is not given it will be = file.root\n");
361 printf(" compress = 1 by default (use 0 for no compression)\n");
362 printf(" tolower = 1 by default (use 0 to keep case of column names)\n");
363 printf(" lrecl =0 by default (must be specified if >8092)\n");
364 printf(" bufsize = 8000 by default (branch buffer size)\n");
365 printf(" for cwn ntuple only: optcwn = 1 (default) 1-byte int -> char, 2-byte int -> short, (use 0 to keep 4-byte int) \n");
366 return 1;
367 }
368 lq = &pawc[9];
369 iq = &pawc[17];
370 void *qq = iq;
371 q = (float*)qq;
372 char *file_in=argv[1];
373 char *file_out;
374 Int_t compress = 1;
375 int ier=0, record_size=0;
376 if (argc > 7) {
377 optcwn = atoi(argv[7]);
378 }
379 if (argc > 6) {
380 bufsize = atoi(argv[6]);
381 }
382 if (argc > 5) {
383 record_size = atoi(argv[5]);
384 }
385 if (argc > 4) {
386 golower = atoi(argv[4]);
387 }
388 if (argc > 3) {
389 compress = atoi(argv[3]);
390 }
391 if (argc > 2) {
392 file_out=argv[2];
393 } else {
394 file_out= new char[2000];
395 strcpy(file_out,file_in);
396 char *dot = strrchr(file_out,'.');
397 if (dot) strcpy(dot+1,"root");
398 else strcat(file_out,".root");
399 }
400
401 #if defined(_HIUX_SOURCE) && !defined(__GNUC__)
402 hf_fint((char *)NULL);
403 #endif
404
405
406 int pawc_size = PAWC_SIZE;
407 hlimit(pawc_size);
408
409 int lun = 10;
410 #ifndef WIN32
411 hropen(lun,PASSCHAR("example"),PASSCHAR(file_in),PASSCHAR("p"),record_size,ier,7,strlen(file_in),1);
412 #else
413 hropen(lun,PASSCHAR("example"),PASSCHAR(file_in),PASSCHAR("p"),record_size,ier);
414 #endif
415
416 if (ier) printf (" Error on hropen was %d \n", ier);
417 if (quest[0]) {
418 printf("Error cannot open input file: %s\n",file_in);
419 return 1;
420 }
421
422 char root_file_title[2000];
423 sprintf(root_file_title,"HBOOK file: %s converted to ROOT",file_in);
424 TFile* hfile= TFile::Open(file_out,"RECREATE",root_file_title,compress);
425
426 if (!hfile) {
427 printf("Error: can't open output file: %s \n",file_out);
428 return 1;
429 }
430
431
432 convert_psam("//example");
433 // convert_directory("//example");
434
435 hfile->Write();
436 hfile->ls();
437 hfile->Close();
438 delete hfile;
439 return(0);
440 }
441
442 void convert_psam(const char *dir){
443 int nsub,itype,isize,ielem,ierr;
444 const int kNchar=9;
445 printf(" Converting directory %s\n",dir);
446 Int_t id=20;
447 Int_t nvar;
448 float rmin[1000], rmax[1000];
449 int i999 = 999;
450 char * chtag_out;
451
452 Int_t bufpos = 0;
453 Int_t isachar = 0;
454 Int_t isabool = 0;
455 char fullname[1024];
456 char name[512];
457 char block[512];
458 char oldblock[512];
459 Int_t nbits = 0;
460 strcpy(oldblock,"OLDBLOCK");
461 Int_t oldischar = -1;
462 string blockname;
463 char *bigbuf = new char[2500000];
464 // Long_t add= (Long_t)&bigbuf[0];
465 char *t=bigbuf;
466
467
468 // gpevent myevent;
469 pEvent myevent;
470
471 std::istringstream *test;
472
473 hrin(id,i999,0);
474 if (quest[0]) {
475 printf("Error cannot read ID = %d\n",id);
476 return;
477 // break;
478 }
479 if (id > 0) sprintf(idname,"h%d",id);
480 else sprintf(idname,"h_%d",-id);
481 hnoent(id,nentries);
482 printf(" Converting CWN with ID= %d, nentries = %d\n",id,nentries);
483 nvar=0;
484
485 hgiven(id,chtitl,nvar,PASSCHAR(""),rmin[0],rmax[0],80,0);
486 chtag_out = new char[nvar*kNchar+1];
487 hgiven(id,chtitl,nvar,chtag_out,rmin[0],rmax[0],80,kNchar);
488
489 // my_pointer m;
490
491 pBlockPointer m;
492 for( int i=0;i<nvar;++i) {
493 hntvar2(id,i+1,PASSCHAR(name),PASSCHAR(fullname),
494 PASSCHAR(block),nsub,itype,isize,nbits,ielem,
495 512,1024,512);
496 // std::cout << strlen(block) << " i: " << i << std::endl;
497 test= new std::istringstream(block);
498 *test >> blockname;
499 m.CountByte(blockname)+=(ielem*isize);
500 m.CountVar(blockname)++;
501 m.SetMult(blockname)=ielem;
502 // std::cout << blockname << std::endl;
503 delete test;
504 }
505 // std::cout << " Block found : " << m.Blocks() << " Size :" << m.TotBufSize()
506 // << std::endl;
507 m.Print();
508 for(pBlockMap::const_iterator p=(m.GetBlockMap())->begin();
509 p!=(m.GetBlockMap())->end(); ++p){
510 std::cout << p->name << ": "<< p->nbyte << '\n';
511 if(p->name!="GENERAL" && p->name!="CALI"
512 && p->name!="TRD" && p->name!= "SPE")
513 myevent.AddDetector(p->name);
514 }
515 myevent.Print();
516 // general mygen;
517 // structcas mycas;
518 // t=(char *) &mygen;
519 // hbnam(id,PASSCHAR(" "),(int) *t,PASSCHAR("$CLEAR"),0,1,6);
520 // t=(char *) &mycas;
521 // hbnam(id,PASSCHAR(" "),(int) *t,PASSCHAR("$CLEAR"),0,1,6);
522 // Long_t add= (Long_t)&bigbuf[0];
523
524 // Generate the buffer and pass it to the hbook routine
525 t=m.CreatePbuf();
526 hbnam(id,PASSCHAR(" "),(int) *t,PASSCHAR("$CLEAR"),0,1,6);
527
528 // Now is time to register all the blocks to the hbook routine
529 Int_t ischar=0;
530 for(pBlockMap::const_iterator p=(m.GetBlockMap())->begin();
531 p!=(m.GetBlockMap())->end(); ++p){
532
533 if(p->name!="GENERAL" && p->name!="CALI" && p->name!="CAL"
534 && p->name!="TRD" && p->name!= "SPE"){
535 std::cout << p->name.c_str() << ": "<< p->nbyte << '\n';
536 hbnam(id,PASSCHAR(p->name.c_str()),
537 (Long_t) m.BlockAddress(p->name.c_str()),
538 PASSCHAR("$SET"),ischar,
539 strlen( p->name.c_str() ),4);
540 }
541 }
542 // Long_t add= (Long_t)&mygen;
543 // Int_t lblock = strlen("GENERAL");
544
545 // hbnam(id,PASSCHAR("GENERAL"),add,PASSCHAR("$SET"),ischar,lblock,4);
546 // add= (Long_t)&mycas;
547 // lblock = strlen("CAS");
548 // hbnam(id,PASSCHAR("CAS"),add,PASSCHAR("$SET"),ischar,lblock,4);
549
550 // hgntb(id,PASSCHAR("GENERAL"),0,ierr);
551 // hbnam(id,PASSCHAR("GENERAL"),(Long_t) m.BuffAddress(),
552 // PASSCHAR("$SET"),ischar,lblock,4);
553 // lblock = strlen("CAS");
554 // hbnam(id,PASSCHAR("CAS"),(Long_t) m.BuffAddress(),
555 // PASSCHAR("$SET"),ischar,lblock,4);
556
557 // hbnam(id,PASSCHAR("GENERAL"),(Long_t) m.BlockAddress("GENERAL"),
558 // PASSCHAR("$SET"),ischar,lblock,4);
559 // lblock = strlen("CAS");
560 // hbnam(id,PASSCHAR("CAS"),(Long_t) m.BlockAddress("CAS"),
561 // PASSCHAR("$SET"),ischar,lblock,4);
562
563 // gpcasdat ttt;
564 // gpcas *tc;
565 char * junk;
566 for(int i=0;i<nentries;++i){
567 hgnt(id,i+1,ierr);
568 junk=m.BuffAddress();
569 junk=m.BuffReorder();
570 // t=(char *) &mygen;
571 // for(int i=0; i<36; ++i) *t++=*junk++;
572 std::cout << junk << std::dec <<
573 ", " << ierr << std::endl;
574 for(pBlockMap::const_iterator p=(m.GetBlockMap())->begin();
575 p!=(m.GetBlockMap())->end(); ++p){
576
577 if(p->name!="GENERAL" && p->name!="CALI" && p->name!="CAL"
578 && p->name!="TRD" && p->name!= "SPE" ){
579 std::cout << p->name.c_str() << ": "<< p->nbyte << '\n';
580 int nhit= *(int *) m.BlockAddressR(p->name.c_str());
581 char * cc= m.BlockAddressR(p->name.c_str());
582 cc=cc+4;
583 int nb= (m.BlockVar(p->name.c_str())-1)*4;
584 std::cout << " nhit : "<< nhit << endl;
585 for (int i=0; i<nhit;++i) {
586 myevent.AddHit(p->name.c_str(),cc);
587 cc+=nb;
588 }
589 // hbnam(id,PASSCHAR(p->name.c_str()),
590 // (Long_t) m.BlockAddress(p->name.c_str()),
591 // PASSCHAR("$SET"),ischar,
592 // strlen( p->name.c_str() ),4);
593 }
594 }
595 // mygen.SetIt(m.BlockAddress("GENERAL"));
596 // mycas.SetIt(m.BlockAddress("CAS"));
597 // mygen.Print();
598 // mycas.Print();
599
600 // if(mycas.nthcas) {
601 // for(int j=0;j<mycas.nthcas;++j) {
602 // ttt=mycas.GetCas(j);
603 // myevent.AddCasHit(&ttt);
604 // // tc=new gpcas(&ttt);
605 // // tc->Print();
606 // // delete tc;
607 // }
608 // }
609 }
610 myevent.Print();
611 }
612 //____________________________________________________________________________
613 void convert_directory(const char *dir)
614 {
615
616 printf(" Converting directory %s\n",dir);
617 Int_t id;
618 // Int_t nastycase=0;
619 // Int_t nastyprint=0;
620 // Int_t idold = 0;
621 for (Int_t key=1;key<1000000;key++) {
622 int z0 = 0;
623 rzink(key,z0,"S",1);
624 if (quest[0]) break;
625 if (quest[13] & 8) {
626 continue;
627 // if (!nastyprint) {
628 // printf("Found nasty Hbook case!! You had an Hbook error message\n");
629 // printf(" when creating the file (too many records)\n");
630 // printf(" Hbook file should have been created with a bigger LRECL\n");
631 // printf(" ROOT will try to recover\n");
632 // nastyprint = 1;
633 // }
634 // nastycase = 1;
635 }
636 id = quest[20];
637 // if (id == idold && nastycase) continue;
638 // nastycase = 0;
639 // idold = id;
640 int i999 = 999;
641 hrin(id,i999,0);
642 if (quest[0]) {
643 printf("Error cannot read ID = %d\n",id);
644 break;
645 }
646 hdcofl();
647 lcid = hcbook[10];
648 lcont = lq[lcid-1];
649 if (hcbits[3]) {
650 if (iq[lcid-2] == 2) convert_rwn(id);
651 else convert_cwn(id);
652 hdelet(id);
653 continue;
654 }
655 if (hcbits[0] && hcbits[7]) {
656 convert_profile(id);
657 hdelet(id);
658 continue;
659 }
660 if (hcbits[0]) {
661 convert_1d(id);
662 hdelet(id);
663 continue;
664 }
665 if (hcbits[1] || hcbits[2]) {
666 convert_2d(id);
667 hdelet(id);
668 }
669 }
670
671 // converting subdirectories of this directory
672 const Int_t kKLS = 26;
673 const Int_t kKNSD = 23;
674 lcdir = rzcl[2];
675 Int_t ls = iq[lcdir+kKLS];
676 Int_t ndir = iq[lcdir+kKNSD];
677 Int_t nch=16;
678 Int_t ihdir[4];
679 Int_t ncw = 4;
680 TDirectory *cursav = gDirectory;
681 Int_t i;
682 char chdir[17];
683 char hbookdir[17];
684 for (Int_t k=0;k<ndir;k++) {
685 lcdir = rzcl[2];
686 zitoh(iq[lcdir+ls+7*k],ihdir[0],ncw);
687 for (i=0;i<17;i++) chdir[i] = 0;
688 #ifndef WIN32
689 uhtoc(ihdir[0],ncw,chdir,nch ,16);
690 #else
691 uhtoc(ihdir[0],ncw,chdir,16,nch);
692 #endif
693 strcpy(hbookdir,chdir);
694 for (i=16;i>0;i--) {
695 if (chdir[i] == 0) continue;
696 if (chdir[i] != ' ') break;
697 chdir[i] = 0;
698 }
699 #ifndef WIN32
700 hcdir(PASSCHAR(hbookdir),PASSCHAR(" "),16,1);
701 #else
702 hcdir(PASSCHAR(hbookdir),PASSCHAR(" "));
703 #endif
704 TDirectory *newdir = new TDirectory(chdir,chdir);
705 newdir->cd();
706 convert_directory(chdir);
707 #ifndef WIN32
708 hcdir(PASSCHAR("\\"),PASSCHAR(" "),1,1);
709 #else
710 hcdir(PASSCHAR("\\"),PASSCHAR(" "));
711 #endif
712 newdir->Write();
713 cursav->cd();
714 }
715 }
716
717 //____________________________________________________________________________
718 void convert_1d(Int_t id)
719 {
720 if (id > 0) sprintf(idname,"h%d",id);
721 else sprintf(idname,"h_%d",-id);
722 hnoent(id,nentries);
723 #ifndef WIN32
724 hgive(id,chtitl,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb,80);
725 #else
726 hgive(id,chtitl,80,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb);
727 #endif
728 chtitl[4*nwt] = 0;
729 TH1F *h1;
730 Int_t i;
731 if (hcbits[5]) {
732 Int_t lbins = lq[lcid-2];
733 Double_t *xbins = new Double_t[ncx+1];
734 for (i=0;i<=ncx;i++) xbins[i] = q[lbins+i+1];
735 h1 = new TH1F(idname,chtitl,ncx,xbins);
736 delete [] xbins;
737 } else {
738 h1 = new TH1F(idname,chtitl,ncx,xmin,xmax);
739 }
740 if (hcbits[8]) h1->Sumw2();
741 TGraph *gr = 0;
742 if (hcbits[11]) {
743 gr = new TGraph(ncx);
744 h1->GetListOfFunctions()->Add(gr);
745 }
746
747 Float_t x;
748 for (i=0;i<=ncx+1;i++) {
749 x = h1->GetBinCenter(i);
750 h1->Fill(x,hi(id,i));
751 if (hcbits[8]) h1->SetBinError(i,hie(id,i));
752 if (gr && i>0 && i<=ncx) gr->SetPoint(i,x,hif(id,i));
753 }
754 Float_t ymin, ymax;
755 if (hcbits[19]) {
756 ymax = q[lcid+kMAX1];
757 h1->SetMaximum(ymax);
758 }
759 if (hcbits[20]) {
760 ymin = q[lcid+kMIN1];
761 h1->SetMinimum(ymin);
762 }
763 h1->SetEntries(nentries);
764 h1->Write();
765 delete h1;
766 }
767
768 //____________________________________________________________________________
769 void convert_2d(Int_t id)
770 {
771 if (id > 0) sprintf(idname,"h%d",id);
772 else sprintf(idname,"h_%d",-id);
773 hnoent(id,nentries);
774 #ifndef WIN32
775 hgive(id,chtitl,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb,80);
776 #else
777 hgive(id,chtitl,80,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb);
778 #endif
779 chtitl[4*nwt] = 0;
780 TH2F *h2 = new TH2F(idname,chtitl,ncx,xmin,xmax,ncy,ymin,ymax);
781 Float_t offsetx = 0.5*(xmax-xmin)/ncx;
782 Float_t offsety = 0.5*(ymax-ymin)/ncy;
783 Int_t lw = lq[lcont];
784 if (lw) h2->Sumw2();
785
786 Float_t x,y;
787 for (Int_t j=0;j<=ncy+1;j++) {
788 for (Int_t i=0;i<=ncx+1;i++) {
789 hijxy(id,i,j,x,y);
790 h2->Fill(x+offsetx,y+offsety,hij(id,i,j));
791 if (lw) {
792 Double_t err2 = hije(id,i,j);
793 h2->SetCellError(i,j,err2);
794 }
795 }
796 }
797 h2->SetEntries(nentries);
798 h2->Write();
799 delete h2;
800 }
801
802 //____________________________________________________________________________
803 void convert_profile(Int_t id)
804 {
805 // the following structure is used in Hbook
806 // lcid points to the profile in array iq
807 // lcont = lq(lcid-1)
808 // lw = lq(lcont)
809 // ln = lq(lw)
810 // if option S jbyt(iq(lw),1,2) = 1
811 // if option I jbyt(iq(lw),1,2) = 2
812
813 if (id > 0) sprintf(idname,"h%d",id);
814 else sprintf(idname,"h_%d",-id);
815 hnoent(id,nentries);
816 Int_t lw = lq[lcont];
817 Int_t ln = lq[lw];
818 #ifndef WIN32
819 hgive(id,chtitl,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb,80);
820 #else
821 hgive(id,chtitl,80,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb);
822 #endif
823 Float_t offsetx = 0.5*(xmax-xmin)/ncx;
824 chtitl[4*nwt] = 0;
825 const char *option= " ";
826 if (iq[lw] == 1) option = "S";
827 if (iq[lw] == 2) option = "I";
828 TProfile *p = new TProfile(idname,chtitl,ncx,xmin,xmax,ymin,ymax,option);
829
830 const Int_t kCON1 = 9;
831 Int_t i;
832 Float_t x;
833 Float_t y = 0.5*(ymin+ymax);
834 for (i=1;i<=ncx;i++) {
835 Int_t n = Int_t(q[ln+i]);
836 hix(id,i,x);
837 for (Int_t j=0;j<n;j++) {
838 p->Fill(x+offsetx,y);
839 }
840 //p->SetBinEntries(i,n); //this one should be much faster (to be tested)
841 Float_t content = q[lcont+kCON1+i];
842 Float_t error = TMath::Sqrt(q[lw+i]);
843 p->SetBinContent(i,content);
844 p->SetBinError(i,error);
845 }
846 p->SetEntries(nentries);
847 p->Write();
848 delete p;
849 }
850
851 //____________________________________________________________________________
852 void convert_rwn(Int_t id)
853 {
854 const int kNchar=9;
855 int nvar;
856 int ier=0;
857 int i,j;
858 char *chtag_out;
859 float *x;
860 float rmin[1000], rmax[1000];
861
862 if (id > 0) sprintf(idname,"h%d",id);
863 else sprintf(idname,"h_%d",-id);
864 hnoent(id,nentries);
865 printf(" Converting RWN with ID= %d, nentries = %d\n",id,nentries);
866 nvar=0;
867 #ifndef WIN32
868 hgiven(id,chtitl,nvar,PASSCHAR(""),rmin[0],rmax[0],80,0);
869 #else
870 hgiven(id,chtitl,80,nvar,PASSCHAR(""),rmin[0],rmax[0]);
871 #endif
872
873 chtag_out = new char[nvar*kNchar+1];
874 x = new float[nvar];
875
876 chtag_out[nvar*kNchar]=0;
877 for (i=0;i<80;i++)chtitl[i]=0;
878 #ifndef WIN32
879 hgiven(id,chtitl,nvar,chtag_out,rmin[0],rmax[0],80,kNchar);
880 #else
881 hgiven(id,chtitl,80,nvar,chtag_out,kNchar,rmin[0],rmax[0]);
882 #endif
883 hgnpar(id,"?",1);
884 char *name = chtag_out;
885 for (i=80;i>0;i--) {if (chtitl[i] == ' ') chtitl[i] = 0; }
886 TTree *tree = new TTree(idname,chtitl);
887 Int_t first,last;
888 for(i=0; i<nvar;i++) {
889 name[kNchar-1] = 0;
890 first = last = 0;
891 // suppress traling blanks
892 for (j=kNchar-2;j>0;j--) {
893 if(golower) name[j] = tolower(name[j]);
894 if (name[j] == ' ' && last == 0) name[j] = 0;
895 else last = j;
896 }
897 if (golower == 2) name[0] = tolower(name[0]);
898
899 // suppress heading blanks
900 for (j=0;j<kNchar;j++) {
901 if (name[j] != ' ') break;
902 first = j+1;
903 }
904 tree->Branch(&name[first],&x[i],&name[first],bufsize);
905 name += kNchar;
906 }
907 for(i=1;i<=nentries;i++) {
908 hgnf(id,i,x[0],ier);
909 tree->Fill();
910 }
911 tree->Write();
912 delete tree;
913 delete [] x;
914 }
915
916 //____________________________________________________________________________
917 void convert_cwn(Int_t id)
918 {
919 const int kNchar=9;
920 int nvar;
921 int ier=0;
922 int i,j;
923 int nsub,itype,isize,ielem;
924 char *chtag_out;
925 float *x;
926 float rmin[1000], rmax[1000];
927
928 if (id > 0) sprintf(idname,"h%d",id);
929 else sprintf(idname,"h_%d",-id);
930 hnoent(id,nentries);
931 printf(" Converting CWN with ID= %d, nentries = %d\n",id,nentries);
932 nvar=0;
933 #ifndef WIN32
934 hgiven(id,chtitl,nvar,PASSCHAR(""),rmin[0],rmax[0],80,0);
935 #else
936 hgiven(id,chtitl,80,nvar,PASSCHAR(""),rmin[0],rmax[0]);
937 #endif
938
939
940 chtag_out = new char[nvar*kNchar+1];
941 Int_t *charflag = new Int_t[nvar];
942 Int_t *lenchar = new Int_t[nvar];
943 Int_t *boolflag = new Int_t[nvar];
944 Int_t *lenbool = new Int_t[nvar];
945 UChar_t *boolarr = new UChar_t[10000];
946 x = new float[nvar];
947 char *bigbuf = new char[2500000];
948
949 chtag_out[nvar*kNchar]=0;
950 for (i=0;i<80;i++)chtitl[i]=0;
951 #ifndef WIN32
952 hgiven(id,chtitl,nvar,chtag_out,rmin[0],rmax[0],80,kNchar);
953 #else
954 hgiven(id,chtitl,80,nvar,chtag_out,kNchar,rmin[0],rmax[0]);
955 #endif
956 #ifndef WIN32
957 hbnam(id,PASSCHAR(" "),bigbuf[0],PASSCHAR("$CLEAR"),0,1,6);
958 #else
959 hbnam(id,PASSCHAR(" "),bigbuf[0],PASSCHAR("$CLEAR"),0);
960 #endif
961
962 Int_t bufpos = 0;
963 Int_t isachar = 0;
964 Int_t isabool = 0;
965 char fullname[1024];
966 char name[512];
967 char block[512];
968 char oldblock[512];
969 Int_t nbits = 0;
970 strcpy(oldblock,"OLDBLOCK");
971 Int_t oldischar = -1;
972 for (i=80;i>0;i--) {if (chtitl[i] == ' ') chtitl[i] = 0; }
973 TTree *tree = new TTree(idname,chtitl);
974 for(i=0; i<nvar;i++) {
975 memset(name,' ',sizeof(name));
976 name[sizeof(name)-1] = 0;
977 memset(block,' ',sizeof(block));
978 block[sizeof(block)-1] = 0;
979 memset(fullname,' ',sizeof(fullname));
980 fullname[sizeof(fullname)-1]=0;
981 #ifndef WIN32
982 hntvar2(id,i+1,PASSCHAR(name),PASSCHAR(fullname),PASSCHAR(block),nsub,itype,isize,nbits,ielem,512,1024,512);
983 #else
984 hntvar2(id,i+1,PASSCHAR(name),PASSCHAR(fullname),PASSCHAR(block),nsub,itype,isize,nbits,ielem);
985 #endif
986
987 for (j=510;j>0;j--) {
988 if(golower) name[j] = tolower(name[j]);
989 if (name[j] == ' ') name[j] = 0;
990 }
991 if (golower == 2) name[0] = tolower(name[0]);
992
993 for (j=1022;j>0;j--) {
994 if(golower && fullname[j-1] != '[') fullname[j] = tolower(fullname[j]);
995 // convert also character after [, if golower == 2
996 if (golower == 2) fullname[j] = tolower(fullname[j]);
997 if (fullname[j] == ' ') fullname[j] = 0;
998 }
999 // convert also first character, if golower == 2
1000 if (golower == 2) fullname[0] = tolower(fullname[0]);
1001 for (j=510;j>0;j--) {
1002 if (block[j] == ' ') block[j] = 0;
1003 else break;
1004 }
1005 if (itype == 1) {
1006 if( isize == 4 ) strcat(fullname,"/F");
1007 else if( isize == 8) strcat(fullname,"/D");
1008 }
1009
1010
1011 // add support for 1-byte (Char_t) and 2-byte (Short_t) integers
1012 Int_t nBytesUsed = 4; // default for integers
1013
1014 if( itype == 2 )
1015 {
1016 if( optcwn == 1 )
1017 {
1018 if( nbits > 16 )
1019 {
1020 strcat(fullname,"/I");
1021 }
1022 else
1023 {
1024 if( nbits > 8 )
1025 {
1026 strcat(fullname,"/S");
1027 nBytesUsed = 2;
1028 }
1029 else
1030 {
1031 strcat(fullname,"/B");
1032 nBytesUsed = 1;
1033 }
1034 }
1035 }
1036 else
1037 {
1038 strcat(fullname,"/I");
1039 }
1040 }
1041
1042 // add support for 1-byte (UChar_t) and 2-byte (UShort_t) integers
1043 if ( itype == 3 )
1044 {
1045 if( optcwn == 1 )
1046 {
1047 if( nbits > 16)
1048 {
1049 strcat(fullname,"/i");
1050 }
1051 else
1052 {
1053 if( nbits > 8 )
1054 {
1055 strcat(fullname,"/s");
1056 nBytesUsed = 2;
1057 }
1058 else
1059 {
1060 strcat(fullname,"/b");
1061 nBytesUsed = 1;
1062 }
1063 }
1064 }
1065 else
1066 {
1067 strcat(fullname,"/i");
1068 }
1069 }
1070
1071
1072
1073
1074 // if (itype == 4) strcat(fullname,"/i");
1075 if (itype == 4) strcat(fullname,"/b");
1076 if (itype == 5) strcat(fullname,"/C");
1077 printf("Creating branch:%s, block:%s, fullname:%s, nsub=%d, itype=%d, isize=%d, ielem=%d\n",name,block,fullname,nsub,itype,isize,ielem);
1078 Int_t ischar;
1079 if (itype == 5) ischar = 1;
1080 else ischar = 0;
1081 if (ischar != oldischar || strcmp(oldblock,block) != 0) {
1082 strcpy(oldblock,block);
1083 oldischar = ischar;
1084 Int_t lblock = strlen(block);
1085 Long_t add= (Long_t)&bigbuf[bufpos];
1086 #ifndef WIN32
1087 hbnam(id,PASSCHAR(block),add,PASSCHAR("$SET"),ischar,lblock,4);
1088 #else
1089 hbnam(id,PASSCHAR(block),add,PASSCHAR("$SET"),ischar);
1090 #endif
1091
1092 }
1093 TBranch *branch = tree->Branch(name,(void*)&bigbuf[bufpos],fullname,bufsize);
1094 boolflag[i] = -10;
1095 charflag[i] = 0;
1096 if (itype == 4) {isabool++; boolflag[i] = bufpos; lenbool[i] = ielem;}
1097 bufpos += isize*ielem;
1098 if (ischar) {isachar++; charflag[i] = bufpos-1; lenchar[i] = isize*ielem;}
1099 TObjArray *ll= branch->GetListOfLeaves();
1100 TLeaf *leaf = (TLeaf*)ll->UncheckedAt(0);
1101 if (!leaf) continue;
1102 TLeafI *leafcount = (TLeafI*)leaf->GetLeafCount();
1103 if (leafcount) {
1104 if (leafcount->GetMaximum() <= 0) leafcount->SetMaximum(ielem);
1105 }
1106 }
1107 Int_t cf,l;
1108 for(i=1;i<=nentries;i++) {
1109 hgnt(id,i,ier);
1110 if (isabool) { // if column is boolean
1111 for (j=0;j<nvar;j++) {
1112 cf = boolflag[j];
1113 if (cf >-1) {
1114 for (l=0;l<lenbool[j];l++) {
1115 #ifdef R__BYTESWAP
1116 boolarr[l] = (UChar_t)bigbuf[cf+4*l];
1117 #else
1118 boolarr[l] = (UChar_t)bigbuf[cf+4*l+3];
1119 #endif
1120 }
1121 memcpy(&bigbuf[cf],boolarr,lenbool[j]);
1122 }
1123 }
1124 }
1125 if (isachar) { // if column is character, set terminator
1126 for (j=0;j<nvar;j++) {
1127 cf = charflag[j];
1128 if (cf) {
1129 bigbuf[cf] = '\0';
1130 if (bigbuf[cf-1] != ' ') continue;
1131 bigbuf[cf-1] = '\0';
1132 if (bigbuf[cf-2] != ' ') continue;
1133 bigbuf[cf-2] = '\0';
1134 }
1135 }
1136 }
1137
1138 // if optimizing cwn ntuple then look up bufpos and adjust integers to be shorts or chars
1139 if( optcwn == 1 )
1140 {
1141 bufpos = 0;
1142 for(int k=0; k<nvar;k++)
1143 {
1144 #ifndef WIN32
1145 hntvar2(id,k+1,PASSCHAR(name),PASSCHAR(fullname),PASSCHAR(block),nsub,itype,isize,nbits,ielem,32,64,32);
1146 #else
1147 hntvar2(id,k+1,PASSCHAR(name),PASSCHAR(fullname),PASSCHAR(block),nsub,itype,isize,nbits,ielem);
1148 #endif
1149
1150 Int_t nBytesUsed = 4; // default for integers
1151
1152 if ( itype == 2 || itype == 3)
1153 {
1154 if( nbits > 16)
1155 {
1156 // do nothing for integers of 4 byte
1157 }
1158 else
1159 {
1160 if( nbits > 8 )
1161 {
1162 nBytesUsed = 2;
1163 }
1164 else
1165 {
1166 nBytesUsed = 1;
1167 }
1168 }
1169 }
1170
1171 if(nBytesUsed == 1)
1172 {
1173
1174 for(Int_t index = 0; index < ielem; index++)
1175 {
1176 // shift all chars with data to be one after another
1177 bigbuf[bufpos + index*nBytesUsed ] = bigbuf[bufpos + index * isize];
1178 }
1179 }
1180 else
1181 {
1182 if(nBytesUsed == 2)
1183 {
1184
1185 for(Int_t index = 0; index < ielem; index++)
1186 {
1187 // shift all shorts ( 2 chars) with data to be one after another
1188 bigbuf[bufpos + index*nBytesUsed ] = bigbuf[bufpos + index * isize];
1189 bigbuf[bufpos + index*nBytesUsed+1 ] = bigbuf[bufpos + index * isize+1];
1190 }
1191 }
1192 }
1193 bufpos += isize*ielem;
1194 }
1195 }
1196
1197 tree->Fill();
1198 }
1199 tree->Print();
1200 tree->Write();
1201 delete tree;
1202 delete [] x;
1203 delete [] bigbuf;
1204 delete [] charflag;
1205 delete [] lenchar;
1206 delete [] boolflag;
1207 delete [] lenbool;
1208 delete [] boolarr;
1209 delete [] chtag_out;
1210 }

  ViewVC Help
Powered by ViewVC 1.1.23