/[PAMELA software]/calo/flight/FUTILITIES/macros/FCaloFUNCTIONS.cxx
ViewVC logotype

Annotation of /calo/flight/FUTILITIES/macros/FCaloFUNCTIONS.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Mon Sep 22 20:18:45 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.2: +54 -26 lines
Added -m32 flag for cross compilation on 64bit machines

1 mocchiut 1.1 //
2     // Calorimeter useful functions and subroutines - Emiliano Mocchiutti
3     //
4     // FCaloFUNCTIONS.cxx version 1.00 (2006-03-03)
5     //
6     // Programs in this file are called by other programs and cannot be run by hand.
7     //
8     // Changelog:
9     //
10     // 1.00 - 1.01 (2006-03-03): read unique YODA file and compile correctly with ROOT classes etc. Small changes in the fetchpreviousfile/whatnamewith routines.
11     //
12     // 0.00 - 1.00 (2006-03-03) Clone of CaloFunctions.h
13     //
14     #include <iostream>
15     #include <sstream>
16     #include <fstream>
17     #include <TSystem.h>
18     #include <TH1.h>
19     #include <TF1.h>
20     #include <TROOT.h>
21     #include <TStyle.h>
22     #include <TClassEdit.h>
23     #include <TObject.h>
24     #include <TList.h>
25     #include <TSystem.h>
26     #include <TSystemDirectory.h>
27     #include <TString.h>
28     #include <TTree.h>
29     #include <TChain.h>
30     #include <TFile.h>
31     #include <TClass.h>
32     #include <TCanvas.h>
33     #include <TKey.h>
34     #include <TClassTable.h>
35     #include <TThread.h>
36 mocchiut 1.3 #include <TMath.h>
37 mocchiut 1.1 //
38     #include <PamelaRun.h>
39     #include <physics/calorimeter/CalorimeterEvent.h>
40     #include <CalibCalPedEvent.h>
41     //
42     #include <fcalostructs.h>
43     #include <FCaloFUNCTIONSfun.h>
44     #include <caloclassesfun.h>
45     //
46     using namespace std;
47    
48     void stringcopy(TString& s1, const TString& s2, Int_t from=0, Int_t to=0){
49     if ( to == 0 ){
50     Int_t t2length = s2.Length();
51     s1 = "";
52     to = t2length;
53     };
54     for (Int_t i = from; i<to; i++){
55     s1.Append(s2[i],1);
56     };
57     }
58    
59     void stringappend(TString& s1, const TString& s2){
60     Int_t t2length = s2.Length();
61     for (Int_t i = 0; i<t2length; i++){
62     s1.Append(s2[i],1);
63     };
64     }
65    
66     void CaloCompressData(Calib & calib, Evento & evento, Int_t l, Int_t m, Int_t pre){
67     Float_t minstrip = 100000.;
68     Float_t rms = 0.;
69     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
70     if ( calib.calgood[l][m][e] == 0. && evento.dexyc[l][m][e]-calib.calped[l][m][e] < minstrip && evento.dexyc[l][m][e] > 0.) {
71     minstrip = evento.dexyc[l][m][e]-calib.calped[l][m][e];
72     rms = calib.calthr[l][m][pre];
73     };
74     };
75     //
76     // compression
77     //
78     if ( minstrip < evento.base[l][m][pre] && minstrip != 100000.){
79     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
80     if ( evento.dexyc[l][m][e]-calib.calped[l][m][e] <= (minstrip+rms) ) evento.dexyc[l][m][e] = 0.;
81     };
82     };
83     }
84    
85     void CaloFindBase(Calib & calib, Evento & evento, Int_t l, Int_t m, Int_t pre){
86     Float_t ominstrip = 100000.;
87     Float_t orms = 0.;
88     Float_t minstrip = 100000.;
89     Float_t rms = 0.;
90     evento.base[l][m][pre] = 0.;
91     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
92     if ( calib.calgood[l][m][e] == 0. && evento.dexyc[l][m][e]-calib.calped[l][m][e] < minstrip && evento.dexyc[l][m][e] > 0.) {
93     ominstrip = minstrip;
94     minstrip = evento.dexyc[l][m][e]-calib.calped[l][m][e];
95     orms = rms;
96     rms = calib.calthr[l][m][pre];
97     };
98     if ( calib.calgood[l][m][e] == 0. && evento.dexyc[l][m][e]-calib.calped[l][m][e] < ominstrip && evento.dexyc[l][m][e]-calib.calped[l][m][e] >minstrip ) {
99     ominstrip = evento.dexyc[l][m][e]-calib.calped[l][m][e];
100     orms = calib.calthr[l][m][pre];
101     };
102     };
103     if ( minstrip != 100000. ) {
104     if ( ominstrip != 10000.) {
105     minstrip = ominstrip;
106     rms = orms;
107     };
108     Float_t strip6s = 0.;
109     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
110     if ( (evento.dexyc[l][m][e]-calib.calped[l][m][e]) >= minstrip && (evento.dexyc[l][m][e]-calib.calped[l][m][e]) <= (minstrip+rms) ) {
111     strip6s += 1.;
112     evento.base[l][m][pre] += (evento.dexyc[l][m][e] - calib.calped[l][m][e]);
113     };
114     //
115     // compression
116     //
117     if ( evento.dexyc[l][m][e]-calib.calped[l][m][e] <= (minstrip+rms) ) evento.dexyc[l][m][e] = 0.;
118     };
119     if ( strip6s >= 9. ){
120     Double_t arro = evento.base[l][m][pre]/strip6s;
121     Float_t deci = 1000.*((float)arro - float(int(arro)));
122     if ( deci < 500. ) {
123     arro = double(int(arro));
124     } else {
125     arro = 1. + double(int(arro));
126     };
127     evento.base[l][m][pre] = arro;
128     } else {
129     evento.base[l][m][pre] = 31000.;
130     };
131     } else {
132     evento.base[l][m][pre] = 31000.;
133     };
134     }
135    
136     void CaloFindBaseRaw(Calib & calib, Evento & evento, Int_t l, Int_t m, Int_t pre){
137     Float_t minstrip = 100000.;
138     Float_t rms = 0.;
139     evento.base[l][m][pre] = 0.;
140     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
141     if ( calib.calgood[l][m][e] == 0. && evento.dexy[l][m][e]-calib.calped[l][m][e] < minstrip && evento.dexy[l][m][e] > 0.) {
142     minstrip = evento.dexy[l][m][e]-calib.calped[l][m][e];
143     rms = calib.calthr[l][m][pre];
144     };
145     };
146     if ( minstrip != 100000. ) {
147     Float_t strip6s = 0.;
148     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
149     if ( (evento.dexy[l][m][e]-calib.calped[l][m][e]) >= minstrip && (evento.dexy[l][m][e]-calib.calped[l][m][e]) <= (minstrip+rms) ) {
150     strip6s += 1.;
151     evento.base[l][m][pre] += (evento.dexy[l][m][e] - calib.calped[l][m][e]);
152     };
153     //
154     // compression
155     //
156     if ( abs((int)(evento.dexy[l][m][e]-calib.calped[l][m][e])) <= (minstrip+rms) ) {
157     evento.dexyc[l][m][e] = 0.;
158     } else {
159     evento.dexyc[l][m][e] = evento.dexy[l][m][e];
160     };
161     };
162     if ( strip6s >= 9. ){
163     Double_t arro = evento.base[l][m][pre]/strip6s;
164     Float_t deci = 1000.*((float)arro - float(int(arro)));
165     if ( deci < 500. ) {
166     arro = double(int(arro));
167     } else {
168     arro = 1. + double(int(arro));
169     };
170     evento.base[l][m][pre] = arro;
171     } else {
172     evento.base[l][m][pre] = 31000.;
173     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
174     evento.dexyc[l][m][e] = evento.dexy[l][m][e];
175     };
176     };
177     } else {
178     evento.base[l][m][pre] = 31000.;
179     };
180     }
181    
182     void CaloFindBaseRawNC(Calib & calib, Evento & evento, Int_t l, Int_t m, Int_t pre){
183     Float_t minstrip = 100000.;
184     Float_t rms = 0.;
185     evento.base[l][m][pre] = 0.;
186     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
187     if ( calib.calgood[l][m][e] == 0. && evento.dexy[l][m][e]-calib.calped[l][m][e] < minstrip && evento.dexy[l][m][e] > 0.) {
188     minstrip = evento.dexy[l][m][e]-calib.calped[l][m][e];
189     rms = calib.calthr[l][m][pre];
190     };
191     };
192     if ( minstrip != 100000. ) {
193     Float_t strip6s = 0.;
194     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
195     if ( (evento.dexy[l][m][e]-calib.calped[l][m][e]) >= minstrip && (evento.dexy[l][m][e]-calib.calped[l][m][e]) <= (minstrip+rms) ) {
196     strip6s += 1.;
197     evento.base[l][m][pre] += (evento.dexy[l][m][e] - calib.calped[l][m][e]);
198     };
199     //
200     // compression
201     //
202     evento.dexyc[l][m][e] = evento.dexy[l][m][e];
203     };
204     if ( strip6s >= 9. ){
205     Double_t arro = evento.base[l][m][pre]/strip6s;
206     Float_t deci = 1000.*((float)arro - float(int(arro)));
207     if ( deci < 500. ) {
208     arro = double(int(arro));
209     } else {
210     arro = 1. + double(int(arro));
211     };
212     evento.base[l][m][pre] = arro;
213     } else {
214     evento.base[l][m][pre] = 31000.;
215     for (Int_t e = pre*16; e < (pre+1)*16 ; e++){
216     evento.dexyc[l][m][e] = evento.dexy[l][m][e];
217     };
218     };
219     } else {
220     evento.base[l][m][pre] = 31000.;
221     };
222     }
223    
224     Bool_t existfile(TString filename){
225     ifstream myfile;
226     myfile.open(filename.Data());
227     if ( !myfile ){
228     // printf("\n\nERROR: not able to open file: %s \n",filename.Data());
229     return(false);
230     };
231     myfile.close();
232     return(true);
233     }
234    
235     int CaloPede(TString filename, Int_t s, Int_t atime, Calib & calib){
236     if ( !existfile(filename) ) return(3);
237     //
238     TFile *File = new TFile(filename.Data());
239     TTree *tr = (TTree*)File->Get("CalibCalPed");
240     //
241     pamela::CalibCalPedEvent *ce = 0;
242     pamela::PscuHeader *cph = 0;
243     pamela::EventHeader *ceh = 0;
244     tr->SetBranchAddress("Header", &ceh);
245     tr->SetBranchAddress("CalibCalPed", &ce);
246     //
247     Long64_t ncalibs = tr->GetEntries();
248     for (Int_t ci = 0; ci < ncalibs ; ci++){
249     tr->GetEntry(ci);
250     cph = ceh->GetPscuHeader();
251     if ( atime == cph->GetOrbitalTime()){
252     calib.iev = ce->iev;
253     if (ce->cstwerr[s] != 0 && ce->cperror[s] == 0 ) {
254     for ( Int_t d=0 ; d<11 ;d++ ){
255     Int_t pre = -1;
256     for ( Int_t j=0; j<96 ;j++){
257     if ( j%16 == 0 ) pre++;
258     if ( s == 2 ){
259     calib.calped[0][2*d+1][j] = ce->calped[3][d][j];
260     calib.cstwerr[3] = ce->cstwerr[3];
261     calib.cperror[3] = ce->cperror[3];
262     calib.calgood[0][2*d+1][j] = ce->calgood[3][d][j];
263     calib.calthr[0][2*d+1][pre] = ce->calthr[3][d][pre];
264     calib.calrms[0][2*d+1][j] = ce->calrms[3][d][j];
265     calib.calbase[0][2*d+1][pre] = ce->calbase[3][d][pre];
266     calib.calvar[0][2*d+1][pre] = ce->calvar[3][d][pre];
267     };
268     if ( s == 3 ){
269     calib.calped[0][2*d][j] = ce->calped[1][d][j];
270     calib.cstwerr[1] = ce->cstwerr[1];
271     calib.cperror[1] = ce->cperror[1];
272     calib.calgood[0][2*d][j] = ce->calgood[1][d][j];
273     calib.calthr[0][2*d][pre] = ce->calthr[1][d][pre];
274     calib.calrms[0][2*d][j] = ce->calrms[1][d][j];
275     calib.calbase[0][2*d][pre] = ce->calbase[1][d][pre];
276     calib.calvar[0][2*d][pre] = ce->calvar[1][d][pre];
277     };
278     if ( s == 0 ){
279     calib.calped[1][2*d][j] = ce->calped[0][d][j];
280     calib.cstwerr[0] = ce->cstwerr[0];
281     calib.cperror[0] = ce->cperror[0];
282     calib.calgood[1][2*d][j] = ce->calgood[0][d][j];
283     calib.calthr[1][2*d][pre] = ce->calthr[0][d][pre];
284     calib.calrms[1][2*d][j] = ce->calrms[0][d][j];
285     calib.calbase[1][2*d][pre] = ce->calbase[0][d][pre];
286     calib.calvar[1][2*d][pre] = ce->calvar[0][d][pre];
287     };
288     if ( s == 1 ){
289     calib.calped[1][2*d+1][j] = ce->calped[2][d][j];
290     calib.cstwerr[2] = ce->cstwerr[2];
291     calib.cperror[2] = ce->cperror[2];
292     calib.calgood[1][2*d+1][j] = ce->calgood[2][d][j];
293     calib.calthr[1][2*d+1][pre] = ce->calthr[2][d][pre];
294     calib.calrms[1][2*d+1][j] = ce->calrms[2][d][j];
295     calib.calbase[1][2*d+1][pre] = ce->calbase[2][d][pre];
296     calib.calvar[1][2*d+1][pre] = ce->calvar[2][d][pre];
297     };
298     };
299     };
300     };
301     };
302     };
303     tr->Delete();
304     return(0);
305     }
306    
307     TString getFilename(const TString filename){
308 mocchiut 1.3 //
309     const string fil = gSystem->BaseName(filename.Data());
310     Int_t posiz = fil.find(".root");
311     //
312     TString file2;
313     if ( posiz == -1 ){
314     file2 = gSystem->BaseName(filename.Data());
315     } else {
316     Int_t posiz2 = 0;
317     stringcopy(file2,gSystem->BaseName(filename.Data()),posiz2,posiz);
318     TString pdat(".pam");
319     stringappend(file2,pdat);
320     };
321     return file2;
322 mocchiut 1.1 }
323 mocchiut 1.3 // TString getFilename(const TString filename){
324     // const string fil = (const char*)filename;
325     // Int_t posiz = fil.find("dw_");
326     // if ( posiz == -1 ) posiz = fil.find("DW_");
327     // if ( posiz == -1 ) return 0;
328     // Int_t posiz2 = posiz+13;
329     // TString file2;
330     // stringcopy(file2,filename,posiz,posiz2);
331     // TString pdat(".dat");
332     // stringappend(file2,pdat);
333     // return file2;
334     // }
335 mocchiut 1.1
336     Int_t fetchpreviousfile(TString ffile, Int_t s){
337     const char *file = ffile;
338     stringstream fil;
339     fil.str("");
340     fil << file ;
341     Int_t posiz = fil.str().find("dw_");
342     Int_t upper = 0;
343     if ( posiz == -1 ) {
344     posiz = fil.str().find("DW_");
345     upper = 1;
346     };
347     if ( posiz == -1 ) return(1);
348     //
349     TString nomefile = getFilename(ffile);
350     const char *ufnome = nomefile;
351     stringstream nyear;
352     stringstream nmonth;
353     stringstream nday;
354     stringstream nfno;
355     TString year;
356     TString month;
357     TString day;
358     TString fno;
359     TString yno;
360     stringcopy(year, ufnome, 3, 5);
361     stringcopy(month, ufnome, 5, 7);
362     stringcopy(day, ufnome, 7, 9);
363     stringcopy(fno, ufnome, 10, 13);
364     stringcopy(yno, ffile.Data(), posiz+14, posiz+16);
365     const char *cyear = year;
366     const char *cmonth = month;
367     const char *cday = day;
368     const char *cfno = fno;
369     const char *cyno = yno;
370     Int_t jy = atoi(cyear);
371     Int_t jm = atoi(cmonth);
372     Int_t jd = atoi(cday);
373     Int_t jn = atoi(cfno);
374     Int_t jynst = atoi(cyno);
375     Int_t jyn = 0;
376     Int_t inter = 0;
377     char *zy;
378     char *zm;
379     char *zd;
380     char *zn = "";
381     char *zyn;
382     Int_t goodthis = 0;
383     Int_t onegood = 0;
384     Long64_t ncalibs;
385     if ( jn > 1 ) {
386     jn--;
387     } else {
388     jn = 999;
389     if ( jd > 1 ) {
390     jd--;
391     } else {
392     if ( jm > 1 ) {
393     jm--;
394     } else {
395     jm = 12;
396     if ( jy > 3 ) {
397     jy--;
398     } else {
399     return(1);
400     };
401     };
402     if ( jm==4 || jm==6 || jm==9 || jm==11 ){
403     jd = 30;
404     } else {
405     jd = 31;
406     };
407     if ( jm==2 ) jd = 29;
408     };
409     };
410     Int_t gday = 0;
411     Int_t retvar = 1;
412     stringstream nyd;
413     while ( jy>3 ) {
414     while ( jm>0 ){
415     while ( jd > 0 ){
416     while ( jn > 0 ){
417     retvar--;
418     nyear.str("");
419     nyear << jy;
420     if ( jy<10 ){
421     zy = "0";
422     } else {
423     zy = "";
424     };
425     nmonth.str("");
426     nmonth << jm;
427     if ( jm<10 ){
428     zm = "0";
429     } else {
430     zm = "";
431     };
432     nday.str("");
433     nday << jd;
434     if ( jd<10 ){
435     zd = "0";
436     } else {
437     zd = "";
438     };
439     nfno.str("");
440     nfno << jn;
441     if ( jn>99 ) zn = "";
442     if ( jn<100 && jn>9 ) zn = "0";
443     if ( jn<10 ) zn = "00";
444     jyn = jynst;
445     goodthis = 0;
446     onegood = 0;
447     while ( jyn < 100 ){
448     begwhile:
449     nyd.str("");
450     nyd << jyn;
451     if ( jyn<10 ){
452     zyn = "0";
453     } else {
454     zyn = "";
455     };
456     stringstream nfile;
457     nfile.str("");
458     if ( upper ){
459     nfile << "DW_" << zy;
460     } else {
461     nfile << "dw_" << zy;
462     };
463     nfile << nyear.str().c_str();
464     nfile << zm;
465     nfile << nmonth.str().c_str();
466     nfile << zd;
467     nfile << nday.str().c_str() << "_";
468     nfile << zn;
469     nfile << nfno.str().c_str();
470     nfile << zyn;
471     nfile << nyd.str().c_str();
472     nfile << ".root"; // addded
473     //
474     TString fgfile = "";
475     stringcopy(fgfile,ffile,0,posiz);
476     stringappend(fgfile,nfile.str().c_str());
477     //
478     TFile *File = 0;
479     TTree *tr = 0;
480     pamela::CalibCalPedEvent *ce = 0;
481     pamela::PscuHeader *cph = 0;
482     pamela::EventHeader *ceh = 0;
483     if ( !existfile(fgfile) ){
484     if ( jyn > jynst && onegood ) {
485     goodthis = 1;
486     jyn--;
487     goto begwhile;
488     };
489     if ( jyn == jynst ) {
490     onegood = 0;
491     jyn = 100;
492     goto salta;
493     };
494     if ( jyn > jynst && !onegood ) {
495     onegood = 0;
496     jyn = 100;
497     goto salta;
498     };
499     goto salta;
500     } else {
501     onegood = 1;
502     if ( !goodthis ) {
503     goto salta;
504     };
505     };
506     File = new TFile(fgfile.Data());
507     tr = (TTree*)File->Get("CalibCalPed");
508     //Takes the tree of the header file
509     tr->SetBranchAddress("Header", &ceh);
510     tr->SetBranchAddress("CalibCalPed", &ce);
511     ncalibs = tr->GetEntries();
512     if ( ncalibs == 0 ) {
513     jyn = 100;
514     onegood = 0;
515     goodthis = 0;
516     File->Close();
517     goto salta;
518     };
519     inter = 0;
520     for (Int_t c = 0; c < ncalibs; c++){
521     tr->GetEntry(c);
522     cph = ceh->GetPscuHeader();
523     if ( ce->cstwerr[s] != 0 && ce->cperror[s] == 0 ) {
524     inter++;
525     };
526     };
527     if ( inter ){
528     jyn = 100;
529     File->Close();
530     return(retvar);
531     };
532     File->Close();
533     salta:
534     jyn++;
535     };
536     jn--;
537     };
538     jn = 999;
539     jd--;
540     gday++;
541     if ( gday>1 ) return(1);
542     };
543     jm--;
544     if ( jm==4 || jm==6 || jm==9 || jm==11 ){
545     jd = 30;
546     } else {
547     jd = 31;
548     };
549     if ( jm==2 ) jd = 29;
550     };
551     jm = 12;
552     jy--;
553     };
554     return(retvar);
555     }
556    
557     TString whatnamewith(TString ffile, Int_t retvar){
558     Int_t debug = 0;
559     const char *file = ffile;
560     stringstream fil;
561     fil.str("");
562     fil << file ;
563     if ( debug ) printf("ffile %s retvar %i \n",file,retvar);
564     Int_t posiz = fil.str().find("dw_");
565     Int_t upper = 0;
566     if ( posiz == -1 ) {
567     posiz = fil.str().find("DW_");
568     upper = 1;
569     };
570     if ( posiz == -1 ) return("wrong");
571     //
572     TString nomefile = getFilename(ffile);
573     const char *ufnome = nomefile;
574     stringstream nyear;
575     stringstream nmonth;
576     stringstream nday;
577     stringstream nfno;
578     TString year;
579     TString month;
580     TString day;
581     TString fno;
582     TString yno;
583     stringcopy(year, ufnome, 3, 5);
584     stringcopy(month, ufnome, 5, 7);
585     stringcopy(day, ufnome, 7, 9);
586     stringcopy(fno, ufnome, 10, 13);
587     stringcopy(yno, ffile.Data(), posiz+14, posiz+16);
588     const char *cyear = year;
589     const char *cmonth = month;
590     const char *cday = day;
591     const char *cfno = fno;
592     const char *cyno = yno;
593     Int_t jy = atoi(cyear);
594     Int_t jm = atoi(cmonth);
595     Int_t jd = atoi(cday);
596     Int_t jn = atoi(cfno);
597     Int_t jynst = atoi(cyno);
598     Int_t jyn = 0;
599     Int_t goodthis = 0;
600     Int_t onegood = 0;
601     char *zy;
602     char *zm;
603     char *zd;
604     char *zn = "";
605     char *zyn;
606     Long64_t ncalibs;
607     TString fgfile;
608     if ( jn > 1 ) {
609     jn--;
610     } else {
611     jn = 999;
612     if ( jd > 1 ) {
613     jd--;
614     } else {
615     if ( jm > 1 ) {
616     jm--;
617     } else {
618     jm = 12;
619     if ( jy > 3 ) {
620     jy--;
621     } else {
622     return("wrong");
623     };
624     };
625     if ( jm==4 || jm==6 || jm==9 || jm==11 ){
626     jd = 30;
627     } else {
628     jd = 31;
629     };
630     if ( jm==2 ) jd = 29;
631     };
632     };
633     Int_t retval = 1;
634     Int_t gday = 0;
635     stringstream nyd;
636     while ( jy>3 ) {
637     while ( jm>0 ){
638     while ( jd > 0 ){
639     while ( jn > 0 ){
640     retval--;
641     if ( retval < retvar ) return("wrong");
642     nyear.str("");
643     nyear << jy;
644     if ( jy<10 ){
645     zy = "0";
646     } else {
647     zy = "";
648     };
649     nmonth.str("");
650     nmonth << jm;
651     if ( jm<10 ){
652     zm = "0";
653     } else {
654     zm = "";
655     };
656     nday.str("");
657     nday << jd;
658     if ( jd<10 ){
659     zd = "0";
660     } else {
661     zd = "";
662     };
663     nfno.str("");
664     nfno << jn;
665     if ( jn>99 ) zn = "";
666     if ( jn<100 && jn>9 ) zn = "0";
667     if ( jn<10 ) zn = "00";
668     jyn = jynst;
669     goodthis = 0;
670     onegood = 0;
671     while ( jyn < 100 ){
672     begwhile:
673     nyd.str("");
674     nyd << jyn;
675     if ( jyn<10 ){
676     zyn = "0";
677     } else {
678     zyn = "";
679     };
680     stringstream nfile;
681     nfile.str("");
682     if ( upper ){
683     nfile << "DW_" << zy;
684     } else {
685     nfile << "dw_" << zy;
686     };
687     nfile << nyear.str().c_str();
688     nfile << zm;
689     nfile << nmonth.str().c_str();
690     nfile << zd;
691     nfile << nday.str().c_str() << "_";
692     nfile << zn;
693     nfile << nfno.str().c_str();
694     nfile << zyn;
695     nfile << nyd.str().c_str();
696     nfile << ".root"; // added
697     //
698     fgfile = "";
699     stringcopy(fgfile,ffile,0,posiz);
700     stringappend(fgfile,nfile.str().c_str());
701     const char *tosee = fgfile;
702     if ( debug ) printf("file: %s retval %i retvar %i \n",tosee,retval,retvar);
703     TFile *File = 0;
704     TTree *tr = 0;
705     //
706     if ( !existfile(fgfile) ){
707     if ( jyn > jynst && onegood ) {
708     goodthis = 1;
709     jyn--;
710     goto begwhile;
711     };
712     if ( jyn == jynst ) {
713     onegood = 0;
714     jyn = 100;
715     goto salta;
716     };
717     if ( jyn > jynst && !onegood ) {
718     onegood = 0;
719     jyn = 100;
720     goto salta;
721     };
722     goto salta;
723     } else {
724     onegood = 1;
725     if ( !goodthis ){
726     goto salta;
727     };
728     };
729     File = new TFile(fgfile.Data());
730     tr = (TTree*)File->Get("CalibCalPed");
731     ncalibs = tr->GetEntries();
732     if ( ncalibs == 0 ) {
733     jyn = 100;
734     onegood = 0;
735     goodthis = 0;
736     File->Close();
737     goto salta;
738     };
739     File->Close();
740     if ( retval == retvar ) return(fgfile);
741     salta:
742     jyn++;
743     };
744     jn--;
745     };
746     jn = 999;
747     jd--;
748     gday++;
749     if ( gday>7 ) return("wrong");
750     };
751     jm--;
752     if ( jm==4 || jm==6 || jm==9 || jm==11 ){
753     jd = 30;
754     } else {
755     jd = 31;
756     };
757     if ( jm==2 ) jd = 29;
758     };
759     jm = 12;
760     jy--;
761     };
762     return("wrong");
763     }
764    
765     char *getLEVname(TString filename, TString detc, TString numb, TString frame="root"){
766     // char *file;
767     stringstream file;
768     file.str("");
769     const char *num = numb;
770     const char *det = detc;
771     const char *fra = frame;
772     string fil = (const char *)filename;
773     Int_t posiz = fil.find("dw_");
774     if ( posiz == -1 ) posiz = fil.find("DW_");
775 mocchiut 1.2 if ( posiz == -1 ) return("unkwnown.root");
776 mocchiut 1.1 Int_t spos = posiz;
777     Int_t epos = posiz+15;
778     TString tmptempf;
779     stringcopy(tmptempf,filename,spos,epos);
780     const char *tempf = tmptempf;
781     file << tempf << ".Physics.Level";
782     file << num << ".";
783     file << det << ".Event.";
784     file << fra;
785     const char *rfile = file.str().c_str();
786     return((char*)rfile);
787     };
788    
789     char *getfileLEVname(TString filename, TString detc, TString numb, TString frame="root"){
790     // char *file;
791     stringstream file;
792     const char *num = numb;
793     const char *det = detc;
794     const char *fra = frame;
795     string fil = (const char *)filename;
796     Int_t posiz = fil.find("dw_");
797     if ( posiz == -1 ) posiz = fil.find("DW_");
798 mocchiut 1.2 if ( posiz == -1 ) return("unknown.root");
799 mocchiut 1.1 Int_t spos = posiz;
800     Int_t epos = posiz+13;
801     TString tmptempf;
802     stringcopy(tmptempf,filename,spos,epos);
803     const char *tempf = tmptempf;
804     file.str("");
805     file << tempf << "00.Physics.Level";
806     // file << "00.Physics.Level";
807     file << num << ".";
808     file << det << ".Event.";
809     file << fra;
810     const char *rfile = file.str().c_str();
811     return((char*)rfile);
812     };
813    
814     int OLDCaloFindCalibs(TString &filename, Calib & calib){
815     for (Int_t s = 0; s < 4; s++){
816     for (Int_t d = 1; d<50; d++){
817     calib.ttime[s][d] = 0;
818     if ( d < 49 ) calib.time[s][d] = 0;
819     };
820     };
821     if ( !existfile(filename) ) return(1);
822     //
823     TFile *File = new TFile(filename.Data());
824     TTree *tr = (TTree*)File->Get("CalibCalPed");
825     pamela::CalibCalPedEvent *ce = 0;
826     pamela::PscuHeader *cph = 0;
827     pamela::EventHeader *ceh = 0;
828     tr->SetBranchAddress("Header", &ceh);
829     tr->SetBranchAddress("CalibCalPed", &ce);
830     Long64_t ncalibs = tr->GetEntries();
831     Int_t inter;
832     for (Int_t s = 0; s < 4; s++){
833     for (Int_t d = 1; d<50; d++){
834     calib.ttime[s][d] = 0;
835     if ( d < 49 ) calib.time[s][d] = 0;
836     };
837     };
838     for (Int_t s = 0; s < 4; s++){
839     inter = 0;
840     for (Int_t c = 0; c < ncalibs; c++){
841     tr->GetEntry(c);
842     cph = ceh->GetPscuHeader();
843     calib.ttime[s][inter] = 0;
844     if ( ce->cstwerr[s] != 0 && ce->cperror[s] == 0 ) {
845     calib.ttime[s][inter] = cph->GetOrbitalTime();
846     inter++;
847     } else {
848     if ( ce->cstwerr[s] != 0 && ce->cperror[s] != 0 ) {
849     printf(" ERROR: entry %i stwerr %X perror %f \n",c,ce->cstwerr[s],ce->cperror[s]);
850     };
851     };
852     };
853     if ( inter == 0 ){
854     printf(" ERROR: no suitable calibration for section %i in this file!\n",s);
855     };
856     for (Int_t d = 1; d<50; d++){
857     if ( calib.ttime[s][d] != 0 ) {
858     calib.time[s][d-1] = calib.ttime[s][d-1] + (int)((calib.ttime[s][d] - calib.ttime[s][d-1])/2.);
859     } else {
860     if ( d == 1 ) {
861     calib.time[s][d-1] = 0;
862     };
863     };
864     };
865     };
866     File->Close();
867     return(0);
868     }
869    
870     int CaloFindCalibs(TString &filename, TString &calibfile, Int_t &wused, Calib & calib){
871     //
872     // First of all check if the file has a monotone growing OBT time function.
873     //
874     Int_t debug = 0;
875     Int_t obtjump = 0;
876     Int_t evjump[50];
877     Int_t OBT = 0;
878     Int_t OOBT = 0;
879     Int_t OBT1 = 0;
880     Int_t LOBT = 0;
881     pamela::PscuHeader *ph = 0;
882     pamela::EventHeader *eh = 0;
883     //
884     if ( !existfile(filename) ) return(1);
885     TFile *File = new TFile(filename.Data());
886     TTree *ctr = (TTree*)File->Get("Physics");
887     ctr->SetBranchStatus("*",0);
888     ctr->SetBranchStatus("Header*",1);
889     ctr->SetBranchStatus("Pscu*",1);
890     Long64_t nevents = ctr->GetEntries();
891     if ( nevents == 0 ) {
892     printf(" The file is empty! \n ");
893     return(1);
894     };
895     ctr->SetBranchAddress("Header", &eh);
896     for (Int_t i = 0; i < nevents; i++){
897     ctr->GetEntry(i);
898     ph = eh->GetPscuHeader();
899     OBT = ph->GetOrbitalTime();
900     if ( !i ) OBT1 = OBT;
901     if ( i == nevents-1 ) LOBT = OBT;
902     if ( OOBT > OBT ) {
903     evjump[obtjump] = i;
904     obtjump++;
905     };
906     OOBT = OBT;
907     };
908     printf("\n Obtjump = %i - FIRST OBT %i - LAST OBT %i \n\n",obtjump,OBT1,LOBT);
909     //
910     TTree *tr;
911     pamela::CalibCalPedEvent *ce = 0;
912     pamela::PscuHeader *cph = 0;
913     pamela::EventHeader *ceh = 0;
914     Long64_t ncalibs;
915     //
916     for (Int_t s = 0; s < 4; s++){
917     for (Int_t d = 1; d<50; d++){
918     calib.ttime[s][d] = 0;
919     if ( d < 49 ) calib.time[s][d] = 0;
920     };
921     };
922     for (Int_t s = 0; s < 4; s++){
923     //
924     Int_t firstlook = 0;
925     repeatsearch:
926     //
927     Int_t inter = 0;
928     //
929     tr = (TTree*)File->Get("CalibCalPed");
930     tr->SetBranchAddress("Header", &ceh);
931     tr->SetBranchAddress("CalibCalPed", &ce);
932     if ( tr->GetEntries() == 0 ){
933     printf(" No calibration in this file! \n");
934     if ( !firstlook ){
935     wused = 2;
936     filename = calibfile;
937     firstlook = 1;
938     goto repeatsearch;
939     } else {
940     wused = 0;
941     goto jte;
942     };
943     };
944     wused = 1;
945     //
946     ncalibs = tr->GetEntries();
947     calib.obtjump = 0;
948     inter = 0;
949     for (Int_t c = 0; c < ncalibs; c++){
950     tr->GetEntry(c);
951     cph = ceh->GetPscuHeader();
952     calib.ttime[s][inter] = 0;
953     if ( ce->cstwerr[s] != 0 && ce->cperror[s] == 0 ) {
954     calib.ttime[s][inter] = cph->GetOrbitalTime();
955     inter++;
956     } else {
957     if ( ce->cstwerr[s] != 0 && ce->cperror[s] != 0 ) {
958     printf(" ERROR: entry %i stwerr %X perror %f \n",c,ce->cstwerr[s],ce->cperror[s]);
959     };
960     };
961     };
962     jte:
963     if ( inter == 0 ){
964     printf(" WARNING: no suitable calibration for section %i in this file!\n",s);
965     printf(" I WILL SEARCH IN PREVIOUS FILES\n");
966     if ( !firstlook ){
967     wused = 2;
968     filename = calibfile;
969     firstlook = 1;
970     goto repeatsearch;
971     };
972     };
973     if ( inter > 50 ){
974     printf(" WARNING: cannot handle more than 50 calibrations for file!\n");
975     printf(" I WILL SEARCH IN PREVIOUS FILES\n");
976     inter = 0;
977     if ( !firstlook ){
978     wused = 2;
979     filename = calibfile;
980     firstlook = 1;
981     goto repeatsearch;
982     };
983     };
984     if ( obtjump ){
985     calib.obtjump = 1;
986     printf(" ERROR: jump in OBT! CPU restarded? \n");
987     printf(" WARNING: I will use only the first calibration (if any)\n");
988     };
989     //
990     Int_t lastcal = 0;
991     Int_t of = 0;
992     if ( OBT1 < calib.ttime[s][0] || !inter ) {
993     //
994     // here we must look for a calibration in the previous file...
995     //
996     TString ffile = "";
997     stringcopy(ffile,filename,0,0);
998     Int_t fcodep = fetchpreviousfile(ffile,s);
999     if ( fcodep == 1 ) {
1000     if ( inter ){
1001     of = -3;
1002     printf(" WARNING: Problems to find a good calibration for section %i \n",s);
1003     } else {
1004     of = -2;
1005     printf(" ERROR: I was not able to find any good calibration for section %i \n",s);
1006     };
1007     } else {
1008     stringcopy(ffile,filename,0,0);
1009     TString pfile = whatnamewith(ffile,fcodep);
1010     struct Calib tcal;
1011     OLDCaloFindCalibs(pfile,tcal);
1012     for ( Int_t d = 0; d<51; d++ ){
1013     if ( tcal.ttime[s][d] == 0 ){
1014     lastcal = d-1;
1015     break;
1016     };
1017     };
1018     if ( !inter ){
1019     calib.ttime[s][0] = tcal.ttime[s][lastcal];
1020     calib.time[s][0] = OBT1;
1021     calib.time[s][1] = LOBT;
1022     calib.fcode[s][0] = fcodep;
1023     if ( debug ) printf("boh! %i \n",fcodep);
1024     if ( debug ) getchar();
1025     of = -1;
1026     } else {
1027     if ( obtjump ){
1028     if ( debug ) printf("boh2! %i \n",fcodep);
1029     if ( debug ) getchar();
1030     inter = 1;
1031     calib.time[s][0] = OBT1;
1032     calib.time[s][1] = LOBT;
1033     calib.fcode[s][0] = 10;
1034     of = -5;
1035     for (Int_t d = 1; d<50; d++){
1036     calib.ttime[s][d] = 0;
1037     if ( d>1 && d<49 ) calib.time[s][d] = 0;
1038     };
1039     } else {
1040     if ( lastcal >= 0 && tcal.ttime[s][lastcal]<OBT1 && (OBT1-tcal.ttime[s][lastcal])<7200000 && (OBT1-tcal.ttime[s][lastcal])>0 ){
1041     for (Int_t i = inter+1; i>0; i--){
1042     calib.ttime[s][i] = calib.ttime[s][i-1];
1043     };
1044     calib.time[s][0] = OBT1;
1045     calib.ttime[s][0] = tcal.ttime[s][lastcal];
1046     calib.fcode[s][0] = fcodep;
1047     of = 2;
1048     } else {
1049     calib.time[s][0] = OBT1;
1050     calib.fcode[s][0] = 10;
1051     of = 1;
1052     };
1053     };
1054     };
1055     };
1056     } else {
1057     of = 0;
1058     };
1059     if ( debug ) printf("boh3! %i \n",of);
1060     Int_t of1 = 0;
1061     if ( inter && of != 0 && of != 2 && of != -2 && of != -5) {
1062     for (Int_t i = inter+1; i>0; i--){
1063     calib.ttime[s][i] = calib.ttime[s][i-1];
1064     };
1065     of1 = 0;
1066     };
1067     if ( of == -2 ) of = -1;
1068     if ( of == -3 ) {
1069     of = 1;
1070     calib.fcode[s][0] = 10;
1071     };
1072     //
1073     if ( of != -1 && of != -5 ){
1074     if ( of == 2 ) of = 1;
1075     for (Int_t d = 0; d<inter+1; d++){
1076     calib.time[s][d+of] = calib.ttime[s][d+of];
1077     calib.fcode[s][d+of] = 10;
1078     if ( d == inter ){
1079     if ( debug ) printf("boh4! %i \n",of);
1080     calib.time[s][d+of1+of] = LOBT;
1081     calib.fcode[s][d+of] = 0;
1082     };
1083     };
1084     };
1085     // if ( calheadFile ) calheadFile->Close();
1086     // if ( calcalibFile ) calcalibFile->Close();
1087     //if ( File ) File->Close();
1088     };
1089     return(0);
1090     }
1091    
1092     void Calo1stcalib(TString filename, Calib & calib, Int_t b[4]){
1093     Float_t estrip[2][22][96];
1094     //
1095     // this is the value of the mip for each strip. To be changed when we will have the real values
1096     //
1097     for (Int_t s=0; s<4;s++){
1098     for (Int_t d = 0; d<50; d++){
1099     calib.ttime[s][d] = 0 ;
1100     if ( d < 49 ) calib.time[s][d] = 0 ;
1101     };
1102     };
1103     for (Int_t m = 0; m < 2 ; m++ ){
1104     for (Int_t k = 0; k < 22; k++ ){
1105     for (Int_t l = 0; l < 96; l++ ){
1106     calib.calped[m][k][l] = 0. ;
1107     estrip[m][k][l] = 0.;
1108     };
1109     };
1110     }
1111     //
1112     // first of all find the calibrations in the file
1113     //
1114     OLDCaloFindCalibs(filename, calib);
1115     //
1116     // print on the screen the results:
1117     //
1118     printf(" ---------------------------------------------------------- \n \n");
1119     Int_t calibex = 0;
1120     for (Int_t s=0; s<4;s++){
1121     Int_t stop = 0;
1122     for (Int_t d = 0; d<48; d++){
1123     if ( calib.ttime[s][d] != 0 ) calibex++;
1124     if ( calib.time[s][0] != 0 ){
1125     if ( d == 0 ) printf(" Section %i from time 0 to time %i use calibration at time %i \n",s,calib.time[s][d],calib.ttime[s][d]);
1126     if ( calib.time[s][d+1] != 0 ) {
1127     printf(" Section %i from time %i to time %i use calibration at time %i \n",s,calib.time[s][d],calib.time[s][d+1],calib.ttime[s][d+1]);
1128     } else {
1129     if ( !stop ){
1130     printf(" Section %i from time %i use calibration at time %i \n",s,calib.time[s][d],calib.ttime[s][d+1]);
1131     stop = 1;
1132     };
1133     };
1134     } else {
1135     if ( calib.ttime[s][d] != 0 ) printf(" Section %i from time 0 use calibration at time %i \n",s,calib.ttime[s][d]);
1136     };
1137     };
1138     printf("\n");
1139     };
1140     printf(" ---------------------------------------------------------- \n");
1141     if ( calibex < 1 ) {
1142     printf("No full calibration data in this file, sorry!\n");
1143     } else {
1144     //
1145     // calibrate before starting
1146     //
1147     for (Int_t s = 0; s < 4; s++){
1148     b[s]=0;
1149     CaloPede(filename,s,calib.ttime[s][b[s]],calib);
1150     };
1151     };
1152     }
1153    
1154    
1155     void ColorMIP(Float_t mip, int& colo){
1156     // printf("mip = %f \n",mip);
1157     if ( colo > 0 ){
1158     colo = 10;
1159     if ( mip > 0.7 ) colo = 38;
1160     if ( mip > 2. ) colo = 4;
1161     if ( mip > 10. ) colo = 3;
1162     if ( mip > 100. ) colo = 2;
1163     if ( mip > 500. ) colo = 6;
1164     } else {
1165     colo = 10;
1166     if ( mip > 0.7 ) colo = 17;
1167     if ( mip > 2. ) colo = 15;
1168     if ( mip > 10. ) colo = 14;
1169     if ( mip > 100. ) colo = 13;
1170     if ( mip > 500. ) colo = 12;
1171     };
1172     }
1173    
1174     void ColorTOFMIP(Float_t mip, int& colo){
1175     // printf("mip = %f \n",mip);
1176     if ( colo > 0 ){
1177     colo = 10;
1178     if ( mip > 0. ) colo = 38;
1179     if ( mip > 2. ) colo = 4;
1180     if ( mip > 10. ) colo = 3;
1181     if ( mip > 100. ) colo = 2;
1182     if ( mip > 500. ) colo = 6;
1183     } else {
1184     colo = 10;
1185     if ( mip > 0. ) colo = 17;
1186     if ( mip > 2. ) colo = 15;
1187     if ( mip > 10. ) colo = 14;
1188     if ( mip > 100. ) colo = 13;
1189     if ( mip > 500. ) colo = 12;
1190     };
1191     }
1192    
1193     void *processevents(void *ptr){
1194     char *tellme = (char *)ptr;
1195     cin.getline(tellme,256);
1196     delete TThread::Self();
1197     return(NULL);
1198     }
1199    
1200     Int_t WhatToDo(int& i, int& doflag, int& jumpto, Long64_t nevents, Float_t lastevno, Float_t firstevno, const char *file, TString outDir, TString figty, TCanvas &figure) {
1201     char *bw;
1202     if ( jumpto == -1 ){
1203     bw = "bw";
1204     } else {
1205     bw = "";
1206     };
1207     jumpto = 0;
1208     char tellme[256];
1209     stringstream input;
1210     char tellme2[256];
1211     stringstream input2;
1212     char tellme3[256];
1213     stringstream input3;
1214     stringstream out;
1215     out.str("x");
1216     input2.str("zzzzzzzzzzzzzz");
1217     input3.str("z");
1218     input << out.str().c_str();
1219     stringstream stc;
1220     stringstream stc2;
1221     while ( !strcmp(input.str().c_str(),out.str().c_str()) ) {
1222     printf("\nPress <enter> to continue, b<enter> to go backward, j<enter> to jump to a\nparticular event number, p<enter> to save the figure, q<enter> to quit: \n");
1223     //
1224     input.str("");
1225     if ( !gROOT->GetListOfClasses()->FindObject("TRint") ){
1226     TThread *thread = new TThread((TThread::VoidFunc_t)processevents,(void *)tellme);
1227     thread->Run();
1228     while ( thread->GetState() == TThread::kRunningState ){
1229     gSystem->ProcessEvents();
1230     };
1231     thread->Kill();
1232     } else {
1233     cin.getline(tellme,256);
1234     };
1235     input << tellme;
1236     //
1237     out << "y";
1238     //
1239     stc.str("b");
1240     //
1241     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
1242     if ( i > 0 ) {
1243     printf("WARNING: going backward!\n\n");
1244     doflag = 2;
1245     } else {
1246     printf("This is the first event, you can't go backward! \n");
1247     out.str("");
1248     out << input.str().c_str();
1249     };
1250     };
1251     //
1252     stc.str("j");
1253     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
1254     printf("\n Do you want to jump to a progressive number [p] or to an event number [e]? ");
1255     cin.getline(tellme3,256);
1256     input3.str("");
1257     input3 << tellme3;
1258     stc.str("p");
1259     if ( !strcmp(input3.str().c_str(),stc.str().c_str()) ) {
1260     printf("\n Enter the progressive number you want to jump to: ");
1261     cin.getline(tellme2,256);
1262     input2.str("");
1263     input2 << tellme2;
1264     Int_t j;
1265     j = atoi(input2.str().c_str());
1266     if ( j < 1 || j > nevents+1 ) {
1267     printf("\n You can choose between 1 and %i \n",(int)nevents+1);
1268     out.str("");
1269     out << input.str().c_str();
1270     } else {
1271     printf("\n Jumping to progressive number %i\n\n",j);
1272     i = j-2;
1273     };
1274     };
1275     stc.str("e");
1276     if ( !strcmp(input3.str().c_str(),stc.str().c_str()) ) {
1277     printf("\n Enter the event number you want to jump to: ");
1278     cin.getline(tellme2,256);
1279     input2.str("");
1280     input2 << tellme2;
1281     Int_t j;
1282     j = atoi(input2.str().c_str());
1283     if ( j < firstevno || j > lastevno ) {
1284     printf("\n You can choose between %i and %i \n",(int)firstevno,(int)lastevno);
1285     out.str("");
1286     out << input.str().c_str();
1287     } else {
1288     printf("\n Jumping to event number %i\n\n",j);
1289     jumpto = j;
1290     i = -1;
1291     };
1292     };
1293     stc.str("e");
1294     stc2.str("p");
1295     if ( strcmp(input3.str().c_str(),stc.str().c_str()) && strcmp(input3.str().c_str(),stc2.str().c_str()) ) {
1296     printf(" You must type or \"p\" or \"e\"\n");
1297     out.str("");
1298     out << input.str().c_str();
1299     };
1300     };
1301     //
1302     stc.str("q");
1303     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
1304     printf("Exiting...\n");
1305     return(1);
1306     };
1307     //
1308     stc.str("p");
1309     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
1310     printf("Enter a file extension recognized by ROOT (ps, eps, gif,...):\n");
1311     cin.getline(tellme2,256);
1312     input2.str("");
1313     input2 << tellme2;
1314     out.str("");
1315     out << input.str().c_str();
1316     //
1317     TString filename = file;
1318     const string fil = (const char*)filename;
1319     Int_t posiz = fil.find("dw_");
1320     if ( posiz == -1 ) posiz = fil.find("DW_");
1321     Int_t posiz2 = posiz+13;
1322     TString file2;
1323     stringcopy(file2,filename,posiz,posiz2);
1324     const char *figrec = file2;
1325     const char *outdir = outDir;
1326     stringstream figsave;
1327     const char *ty = figty;
1328     figsave.str("");
1329     figsave << outdir << "/";
1330     figsave << ty << "_";
1331     figsave << (i+1) << "_";
1332     figsave << figrec;
1333     figsave << bw << ".";
1334     figsave << input2.str().c_str();
1335     figure.SaveAs(figsave.str().c_str());
1336     printf("\n");
1337     };
1338     };
1339     return(0);
1340     }
1341    
1342     void PrintFigure(TString filename, TString outDir, TString figty, TString saveas, TCanvas& figure) {
1343 mocchiut 1.3 //
1344     const string fil = gSystem->BaseName(filename.Data());
1345     Int_t posiz = fil.find(".root");
1346     //
1347     TString file2;
1348     if ( posiz == -1 ){
1349     file2 = gSystem->BaseName(filename.Data());
1350     } else {
1351     Int_t posiz2 = 0;
1352     stringcopy(file2,gSystem->BaseName(filename.Data()),posiz2,posiz);
1353     };
1354     // const string fil = (const char*)filename;
1355     // Int_t posiz = fil.find("dw_");
1356     // if ( posiz == -1 ) posiz = fil.find("DW_");
1357     // Int_t posiz2 = posiz+13;
1358     // TString file2;
1359     // stringcopy(file2,filename,posiz,posiz2);
1360     //
1361     const char *figrec = file2;
1362     stringstream figsave;
1363     figsave.str("");
1364     figsave << outDir.Data() << "/";
1365     figsave << figrec << "_";
1366     figsave << figty.Data() << ".";
1367     figsave << saveas.Data();
1368     figure.SaveAs(figsave.str().c_str());
1369     return;
1370 mocchiut 1.1 }
1371    
1372     Double_t langaufun(Double_t *x, Double_t *par) {
1373     // Numeric constants
1374     Double_t invsq2pi = 0.3989422804014; // (2 pi)^(-1/2)
1375     Double_t mpshift = -0.22278298; // Landau maximum location
1376     // Control constants
1377     Double_t np = 100.0; // number of convolution steps
1378     Double_t sc = 3.; // convolution extends to +-sc Gaussian sigmas
1379     // Variables
1380     Double_t xx;
1381     Double_t mpc;
1382     Double_t fland;
1383     Double_t sum = 0.0;
1384     Double_t xlow,xupp;
1385     Double_t step;
1386     Double_t i;
1387     // MP shift correction
1388     mpc = par[1] - mpshift * par[0];
1389     // Range of convolution integral
1390     xlow = x[0] - sc * par[3];
1391     xupp = x[0] + sc * par[3];
1392     step = (xupp-xlow) / np;
1393     // Convolution integral of Landau and Gaussian by sum
1394     for(i=1.0; i<=np/2; i++) {
1395     xx = xlow + (i-.5) * step;
1396     fland = TMath::Landau(xx,mpc,par[0]) / par[0];
1397     sum += fland * TMath::Gaus(x[0],xx,par[3]);
1398     xx = xupp - (i-.5) * step;
1399     fland = TMath::Landau(xx,mpc,par[0]) / par[0];
1400     sum += fland * TMath::Gaus(x[0],xx,par[3]);
1401     };
1402     return (par[2] * step * sum * invsq2pi / par[3]);
1403     }
1404    
1405    
1406    
1407     TF1 *langaufit(TH1F *his, Double_t *fitrange, Double_t *startvalues, Double_t *parlimitslo, Double_t *parlimitshi, Double_t *fitparams, Double_t *fiterrors, Double_t *ChiSqr, Int_t *NDF, TString drw="QR0"){
1408     // Once again, here are the Landau * Gaussian parameters:
1409     // par[0]=Width (scale) parameter of Landau density
1410     // par[1]=Most Probable (MP, location) parameter of Landau density
1411     // par[2]=Total area (integral -inf to inf, normalization constant)
1412     // par[3]=Width (sigma) of convoluted Gaussian function
1413     //
1414     // Variables for langaufit call:
1415     // his histogram to fit
1416     // fitrange[2] lo and hi boundaries of fit range
1417     // startvalues[4] reasonable start values for the fit
1418     // parlimitslo[4] lower parameter limits
1419     // parlimitshi[4] upper parameter limits
1420     // fitparams[4] returns the final fit parameters
1421     // fiterrors[4] returns the final fit errors
1422     // ChiSqr returns the chi square
1423     // NDF returns ndf
1424     Int_t i;
1425     Char_t FunName[100];
1426     sprintf(FunName,"Fitfcn_%s",his->GetName());
1427     //
1428     TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName);
1429     if (ffitold) delete ffitold;
1430     //
1431     TF1 *ffit = new TF1(FunName,langaufun,fitrange[0],fitrange[1],4);
1432     ffit->SetParameters(startvalues);
1433     ffit->SetParNames("Width","MP","Area","GSigma");
1434     //
1435     for (i=0; i<4; i++) {
1436     ffit->SetParLimits(i, parlimitslo[i], parlimitshi[i]);
1437     };
1438     his->Fit(FunName,drw); // fit within specified range, use ParLimits, do not plot
1439     //
1440     ffit->GetParameters(fitparams); // obtain fit parameters
1441     for (i=0; i<4; i++) {
1442     fiterrors[i] = ffit->GetParError(i); // obtain fit parameter errors
1443     }
1444     ChiSqr[0] = ffit->GetChisquare(); // obtain chi^2
1445     NDF[0] = ffit->GetNDF(); // obtain ndf
1446     //
1447     return (ffit); // return fit function
1448     }
1449    
1450    
1451     //Double_t langaupro(Double_t *params, Double_t &maxx) {
1452     Double_t langaupro(Double_t *params) {
1453     // Seaches for the location (x value) at the maximum of the
1454     // Landau-Gaussian convolute and its full width at half-maximum.
1455     //
1456     Double_t p,x;
1457     Double_t step;
1458     Double_t l,lold;
1459     Int_t i = 0;
1460     Int_t MAXCALLS = 5000;
1461     // Search for maximum
1462     p = params[1] - 0.1 * params[0];
1463     step = 0.05 * params[0];
1464     lold = -2.0;
1465     l = -1.0;
1466     // while ( (l != lold) && (i < MAXCALLS) ) {
1467     while ( fabs(l-lold)>10e-12 && (i < MAXCALLS) ) {
1468     i++;
1469     lold = l;
1470     x = p + step;
1471     l = langaufun(&x,params);
1472     if ( l < lold ) step = -step/10.;
1473     p += step;
1474     // printf(" i %i: l %.16g lold %.16g x %.16g p %.16g step %.16g \n",i,l,lold,x,p,step);
1475     // printf(" l %.16g \n",l);
1476     };
1477     // printf(" i %i: l %.16g lold %.16g x %.16g p %.16g step %.16g \n",i,l,lold,x,p,step);
1478     //printf(" l %.16g \n",l);
1479     if (i == MAXCALLS) return (-1.);
1480     //
1481     return(x);
1482     }
1483    
1484    
1485     void delay(int selftrig, int &del){
1486     int i;
1487     i = 0;
1488     del = 300;
1489     //
1490     while ((i < 16) && ((selftrig & (0x8000>>i)) == 0)) {
1491     del += 50;
1492     i++;
1493     };
1494     if (i > 15) del = 1100;
1495     }
1496    
1497     Double_t fitraw(Double_t *x, Double_t *par){
1498     //
1499     Double_t fitval = par[3] + par[0]*TMath::Exp(-par[2]*(x[0]-par[1]));
1500     //
1501     return fitval;
1502     }
1503    
1504    
1505    
1506     int checkmerging(Chmerge & chmerge){
1507     for (Int_t i = chmerge.fromflag; i<1000; i++){
1508     if ( chmerge.pscuOBT == 0 && chmerge.pscucount == 0 ) return(1);
1509     if ( chmerge.pscuOBT == chmerge.lastOBT[i] && chmerge.pscucount == chmerge.lastcount[i] ){
1510     chmerge.fromflag = i;
1511     printf("\n WARNING! %i event number %i at OBT %i already processed, skipped!\n",i,chmerge.pscucount,chmerge.pscuOBT);
1512     return(1);
1513     };
1514     };
1515     return(0);
1516     }
1517    
1518     int fillmerging(Chmerge & chmerge){
1519     chmerge.lastOBT[chmerge.scanflag] = chmerge.pscuOBT;
1520     chmerge.lastcount[chmerge.scanflag] = chmerge.pscucount;
1521     chmerge.scanflag++;
1522     if ( chmerge.scanflag > 999 ) return(0);
1523     chmerge.lastOBT[chmerge.scanflag] = 0;
1524     chmerge.lastcount[chmerge.scanflag] = 0;
1525     return(0);
1526     }
1527    
1528     //short int checkifpretyodaversion(TString filename){
1529     // string fil = (const char *)filename;
1530     // Int_t posiz = fil.find("dw_");
1531     // if ( posiz == -1 ) posiz = fil.find("DW_");
1532     // if ( posiz == -1 ) return (-1);
1533     // TString year;
1534     // stringcopy(year,filename,posiz+3,posiz+5);
1535     // const char *ye = year;
1536     // Int_t y = atoi(ye);
1537     // TString month;
1538     // stringcopy(month,filename,posiz+5,posiz+7);
1539     // const char *mo = month;
1540     // Int_t m = atoi(mo);
1541     // TString day;
1542     // stringcopy(day,filename,posiz+7,posiz+9);
1543     // const char *da = day;
1544     // Int_t d = atoi(da);
1545     // TString fno;
1546     // stringcopy(fno,filename,posiz+10,posiz+13);
1547     // const char *fn = fno;
1548     // Int_t f = atoi(fn);
1549     // if ( y >= 5 && m >= 5 && d < 15 ) return(1);
1550     // if ( y == 5 && m == 5 && d == 15 && f < 7 ) return(1);
1551     // return(0);
1552     //}
1553    
1554    

  ViewVC Help
Powered by ViewVC 1.1.23