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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (download) (vendor branch)
Wed Mar 22 15:07:47 2006 UTC (18 years, 8 months ago) by mocchiut
Branch: FUTILITIES
CVS Tags: start, v1r01
Changes since 1.1: +0 -0 lines
Flight Utilities calorimeter package 1st release

1 mocchiut 1.1 //
2     //- Emiliano Mocchiutti
3     //
4     // FCaloADC2MIP.c version 1.01 (2006-03-20)
5     //
6     // The only input needed is
7     //
8     // Changelog:
9     //
10     // 1.00 - 1.01 (2006-03-20): First flight version, read single yoda file.
11     //
12     // 0.00 - 1.00 (2006-03-20): Clone of CaloADC2MIP v4r01.
13     //
14     //
15     #include <fstream>
16     #include <sstream>
17     #include <string>
18     #include <iostream>
19     #include <TTree.h>
20     #include <TClassEdit.h>
21     #include <TObject.h>
22     #include <TList.h>
23     #include <TSystem.h>
24     #include <TSystemDirectory.h>
25     #include <TString.h>
26     #include <TFile.h>
27     #include <TClass.h>
28     #include <TCanvas.h>
29     #include <TH1.h>
30     #include <TH1F.h>
31     #include <TH2D.h>
32     #include <TLatex.h>
33     #include <TPad.h>
34     #include <TPaveLabel.h>
35     #include <TChain.h>
36     #include <TGraph.h>
37     #include <TMultiGraph.h>
38     #include <TLine.h>
39     #include <TStyle.h>
40     #include <TF1.h>
41     //
42     #include <fcalostructs.h>
43     //
44     extern const char *pathtocalibration();
45     extern TString getFilename(const TString);
46     extern TString whatnamewith(TString, Int_t);
47     extern void stringcopy(TString&, const TString&, Int_t, Int_t);
48     extern int CaloPede(TString, Int_t, Int_t, Calib &);
49     extern void CaloFindBaseRaw(Calib &, Evento &, Int_t, Int_t, Int_t);
50     extern void CaloFindBaseRawNC(Calib &, Evento &, Int_t, Int_t, Int_t);
51     extern void CaloCompressData(Calib &, Evento &, Int_t, Int_t, Int_t);
52     extern int CaloFindCalibs(TString &, TString &, Int_t &, Calib &);
53     extern bool existfile(TString);
54     TF1 *langaufit(TH1F *, Double_t *, Double_t *, Double_t *, Double_t *, Double_t *, Double_t *, Double_t *, Int_t *, TString);
55     Double_t langaupro(Double_t *);
56     //
57     //
58     #include <PamelaRun.h>
59     #include <physics/calorimeter/CalorimeterEvent.h>
60     #include <physics/trigger/TriggerEvent.h>
61     #include <CalibCalPedEvent.h>
62     //
63     #include <caloclassesfun.h>
64     #include <FCaloADC2MIPfun.h>
65     using namespace std;
66    
67     void FCaloADC2MIP(TString Filename, TString calcalibfile = "", TString OutDir="", TString flist=""){
68     const char *pam_calib = pathtocalibration();
69     if ( !strcmp(OutDir.Data(),"") ){
70     OutDir = pam_calib;
71     };
72     //
73     TString calcalib;
74     struct Evento evento;
75     struct Calib calib;
76     Int_t debug = 0;
77     Int_t b[4];
78     Int_t etime,evno;
79     Float_t sdexy[2][22][96],sdexyc[2][22][96];
80     Float_t edelta[2][22][96];
81     Float_t estrip[2][22][96];
82     Float_t estripnull[2][22][96];
83     Float_t delta;
84     delta = 1.;
85     evento.emin = 0.7;
86     Int_t UPYES = 0;
87     Int_t se =5;
88     Int_t pre;
89     Int_t rdone=0;
90     Int_t done;
91     bool isCOMP = 0;
92     bool isFULL = 0;
93     bool isRAW = 0;
94     TH1F *pfmip[2][22][96];
95     TF1 *pfitsnr[2][22][96];
96     stringstream Fmip;
97     for (Int_t i=0; i<2;i++){
98     for (Int_t j=0; j<22;j++){
99     for (Int_t m=0; m<96;m++){
100     Fmip.str("");
101     Fmip << "Fmip " << i;
102     Fmip << " " << j;
103     Fmip << " " << m;
104     pfmip[i][j][m] = new TH1F(Fmip.str().c_str(),"",56,0.,55.);
105     pfitsnr[i][j][m] = new TF1;
106     };
107     };
108     };
109     //
110     // the file which contains the calorimeter ADC to MIP conversion values is file2 = path to the script dir / CaloADC2MIP.root
111     //
112     stringstream file2;
113     file2.str("");
114     file2 << OutDir.Data() << "/CaloADC2MIP.root";
115     //
116     // this the figures and fit (4224 x 2 objects!)
117     //
118     stringstream file3;
119     file3.str("");
120     file3 << OutDir.Data() << "/CaloADC2MIPf.root";
121     //
122     stringstream file4;
123     file4.str("");
124     file4 << OutDir.Data() << "/CaloADC2MIPf";
125     //
126     stringstream file5;
127     file5.str("");
128     file5 << OutDir.Data() << "/CaloADC2MIPdata.root";
129     //
130     Int_t nfile = 0;
131     Int_t fileup = 0;
132     if ( flist != "" ){
133     printf("\n You have given an input file list \n\n Files: \n");
134     ifstream in;
135     in.open(flist, ios::in);
136     char ctime[20];
137     while (1) {
138     in >> ctime;
139     if (!in.good()) break;
140     printf(" File number %i = %s \n",nfile+1,ctime);
141     nfile++;
142     };
143     in.close();
144     if ( !nfile ) {
145     printf("\n\n The list is empty, exiting... \n\n");
146     return;
147     };
148     } else {
149     nfile = 1;
150     };
151     printf("\n");
152     //
153     // First check if the calibration file exists:
154     //
155     Int_t EFILE = 0;
156     printf(" Check the existence of CaloADC2MIP.root file: \n\n");
157     if ( !existfile(file2.str().c_str()) ){
158     printf(" %s :no such file or directory \n",file2.str().c_str());
159     printf("\n OK, I will create it!\n\n");
160     EFILE = 0;
161     } else {
162     printf(" The file already exists! Check if an update is needed \n\n");
163     EFILE = 1;
164     };
165     //
166     // the check for an update is done once even if we have a list.
167     //
168     TTree *tree;
169     CalorimeterCalibration *calo = new CalorimeterCalibration();
170     if ( EFILE == 1) {
171     //
172     // The file exists, open it (READ ONLY) and check if any update is needed:
173     //
174     TFile *hfile = 0;
175     hfile = new TFile(file2.str().c_str(),"READ","Calorimeter CALIBRATION data");
176     tree = (TTree*)hfile->Get("CaloADC");
177     calo = new CalorimeterCalibration();
178     tree->SetBranchAddress("Event", &calo);
179     //
180     Long64_t nevents = tree->GetEntries();
181     printf("\n Number of updates: %i\n",(int)nevents);
182     tree->GetEntry(nevents-1);
183     //
184     // Read the status variable of the last event: if it is 0 then we need an update, if it is one print out informations and exit
185     //
186     if ( calo->status == 0 ) {
187     printf("\n An update is needed: \n");
188     Int_t upstrip = 0;
189     for (Int_t i = 0; i<2; i++){
190     for (Int_t j = 0; j<22; j++){
191     for (Int_t l = 0; l<96; l++){
192     if ( calo->mask[i][j][l] == 0. ) upstrip++;
193     };
194     };
195     };
196     printf(" %i strip out of 4224 have no statistic enough.\n\n",upstrip);
197     } else {
198     printf(" No update is needed! \n The data files used to obtain the ADC to MIP conversion are: \n");
199     for (Int_t i = 0; i<nevents; i++){
200     tree->GetEntry(i);
201     const char *fout = calo->fname;
202     printf(" - %s \n",fout);
203     };
204     printf("\n\n");
205     return;
206     };
207     hfile->Close();
208     };
209     //
210     const char *filename2 = Filename;
211     //
212     TString lfile;
213     TString sfile;
214     //
215     Int_t e = 0;
216     ifstream in;
217     in.open(flist, ios::in);
218     char ctime[20];
219     TString file2f;
220     TString pfile;
221     const char *cali;
222     const char *ffile;
223     Int_t calibex = 0;
224     //
225     TString filename;
226     const char *nome;
227     TFile *hfile = 0;
228     calo = new CalorimeterCalibration();
229     Long64_t nevents;
230     Int_t used = 0;
231     const char *compare;
232     Int_t hf3 = 0;
233     Int_t ufile = 0;
234     TFile *headerFile = 0;
235     file2f = "";
236     pfile = "";
237     stringstream fmippa;
238     const char *porca;
239     Int_t S4;
240     Int_t upnn;
241     Int_t notgood = 0;
242     Int_t notgood1 = 0;
243     stringstream files;
244     TFile *hfileb = 0;
245     //
246     Int_t pl1, pl2, strmin, strmax;
247     Int_t spre=0;
248     Int_t surstrip = 0;
249     Float_t estrippa = 0.;
250     Int_t surmam = 0;
251     Int_t surman = 0;
252     Int_t surmatrix[3][3];
253     //
254     TString fififile;
255     const char *file;
256     //
257     stringstream nofi;
258     nofi.str("");
259     TFile *hfile3 = 0;
260     //
261     while ( e < nfile ){
262     if ( flist != "" ){
263     in >> ctime;
264     nofi.str("");
265     nofi << filename2 << "/";
266     nofi << ctime;
267     lfile = TString(nofi.str().c_str());
268     fififile = getFilename(lfile);
269     file = fififile;
270     nofi.str("");
271     nofi << file;
272     sfile = TString(nofi.str().c_str());
273     } else {
274     nofi.str("");
275     nofi << filename2;
276     lfile = TString(nofi.str().c_str());
277     fififile = getFilename(lfile);
278     file = fififile;
279     nofi.str("");
280     nofi << file;
281     sfile = TString(nofi.str().c_str());
282     };
283     //
284     filename = lfile;
285     nome = filename;
286     printf("\n Processing file number %i: %s \n\n",e,nome);
287     nome = sfile;
288     //
289     // this must be done for each file.
290     //
291     const char *calname;
292     calname = "";
293     Int_t wused = 0;
294     TTree *otr;
295     pamela::calorimeter::CalorimeterEvent *de = 0;
296     pamela::trigger::TriggerEvent *trigger = 0;
297     pamela::PscuHeader *ph = 0;
298     pamela::EventHeader *eh = 0;
299     //
300     if ( EFILE == 1 ){
301     hfile = new TFile(file2.str().c_str(),"UPDATE","Calorimeter CALIBRATION data");
302     tree = (TTree*)hfile->Get("CaloADC");
303     calo = new CalorimeterCalibration();
304     tree->SetBranchAddress("Event", &calo);
305     //
306     // Check if the input file has already been used
307     //
308     printf(" Check if file %s has already been used... \n",nome);
309     nevents = tree->GetEntries();
310     used = 0;
311     for (Int_t i = 0; i<nevents; i++){
312     tree->GetEntry(i);
313     compare= calo->fname;
314     if ( !strcmp(compare,nome) ) {
315     used = 1;
316     break;
317     };
318     };
319     if ( used ) {
320     printf(" The file %s has already been used!\n You cannot run twice this program with that file!\n\n",nome);
321     hfile->Close();
322     goto jumpfile;
323     } else {
324     printf(" Good, the file has not been used yet. \n\n");
325     nevents = tree->GetEntries() -1;
326     tree->GetEntry(nevents);
327     };
328     } else {
329     //
330     // If the file doesn't exist create it
331     //
332     calo = new CalorimeterCalibration();
333     hfile = new TFile(file2.str().c_str(),"NEW","Calorimeter CALIBRATION data");
334     if ( !existfile(file2.str().c_str()) ){
335     printf(" %s :no such file or directory \n",filename.Data());
336     printf(" ERROR: Cannot create file \n");
337     return;
338     };
339     // if ( hfile->IsZombie() ){
340     // printf(" ERROR: Cannot create file \n");
341     // return;
342     //};
343     tree = new TTree("CaloADC","Calorimeter calibration data");
344     tree->Branch("Event","CalorimeterCalibration",&calo);
345     };
346     //
347     //
348     //
349     hf3 = 0;
350     if ( e == 0 ){
351     if ( !existfile(file3.str().c_str()) ){
352     ufile = 0;
353     } else {
354     printf(" Update figures file! \n\n");
355     ufile = 1;
356     };
357     };
358     if ( (EFILE == 1 || ufile == 1) && e == 0 ){
359     printf(" Reading the old MIP figures to be updated...\n ");
360     hfile3 = new TFile(file3.str().c_str());
361     hf3 = 1;
362     //
363     stringstream fmino;
364     fmino.str("");
365     for (Int_t l = 0; l < 2; l++){
366     for (Int_t m = 0; m < 22; m++){
367     for (Int_t n =0 ; n < 96; n++){
368     fmino.str("");
369     fmino << "fmip " << l;
370     fmino << " " << m;
371     fmino << " " << n;
372     gDirectory->Delete(fmino.str().c_str());
373     TH1F *temp = (TH1F*)hfile3->Get(fmino.str().c_str());
374     pfmip[l][m][n] = (TH1F*)temp->Clone();
375     };
376     };
377     };
378     printf(" ...done!\n");
379     };
380     EFILE = 1;
381     //
382     // Check if the input calibration file contains any calibration data
383     //
384     done = 0;
385     printf(" Check for calorimeter calibration: \n");
386     calcalib = filename;
387     findcalibs: printf(" Calibration file: %s \n",calname);
388     calname = calcalib;
389     for (Int_t s=0; s<4;s++){
390     for (Int_t d = 0; d<50; d++){
391     calib.ttime[s][d] = 0 ;
392     if ( d < 49 ) calib.time[s][d] = 0 ;
393     };
394     };
395     for (Int_t m = 0; m < 2 ; m++ ){
396     for (Int_t k = 0; k < 22; k++ ){
397     for (Int_t l = 0; l < 96; l++ ){
398     calib.calped[m][k][l] = 0. ;
399     evento.dexy[m][k][l] = 0. ;
400     evento.dexyc[m][k][l] = 0. ;
401     edelta[m][k][l] = 0.;
402     estrip[m][k][l] = 0.;
403     estripnull[m][k][l] = 0.;
404     };
405     };
406     }
407     wused = 0;
408     CaloFindCalibs(calcalib, calcalib, wused, calib);
409     //
410     // print on the screen the results:
411     //
412     ffile = filename;
413     printf(" ------ %s ------- \n \n",ffile);
414     calibex = 0;
415     for (Int_t s=0; s<4;s++){
416     printf(" ** SECTION %i **\n",s);
417     for (Int_t d = 0; d<51; d++){
418     if ( calib.ttime[s][d] != 0 ) {
419     calibex++;
420     if ( calib.fcode[s][d] != 10 ){
421     file2f = "";
422     stringcopy(file2f,calcalib,0,0);
423     pfile = (TString)whatnamewith(file2f,calib.fcode[s][d]);
424     } else {
425     pfile = (TString)calcalib;
426     };
427     ffile = pfile;
428     printf(" - from time %i to time %i use calibration at\n time %i, file: %s \n",calib.time[s][d],calib.time[s][d+1],calib.ttime[s][d],ffile);
429     };
430     };
431     printf("\n");
432     };
433     printf(" ----------------------------------------------------------------------- \n \n");
434     //
435     if ( calibex < 1 ) {
436     printf("No calibration data in this file!\n");
437     calcalib = calcalibfile;
438     if ( !done && calcalibfile != "" ) {
439     cali = calcalibfile;
440     printf(" Search in the calibration file %s \n",cali);
441     done = 1;
442     goto findcalibs;
443     } else {
444     printf(" Cannot find any calibration for this file! \n");
445     goto jumpfile;
446     };
447     };
448     if ( calibex < 4 ) {
449     printf(" WARNING! No full calibration data in this file! \n\n");
450     };
451     //
452     // upgrade data...
453     //
454     if ( !existfile(filename) ){
455     printf(" %s :no such file or directory \n",filename.Data());
456     goto jumpfile;
457     };
458     headerFile = new TFile(filename.Data());
459     otr = (TTree*)headerFile->Get("Physics");
460     otr->SetBranchAddress("Header", &eh);
461     otr->SetBranchAddress("Calorimeter", &de);
462     otr->SetBranchAddress("Trigger", &trigger);
463     //
464     nevents = otr->GetEntries();
465     //
466     // calibrate before starting
467     //
468     for (Int_t s = 0; s < 4; s++){
469     b[s]=0;
470     if ( calib.fcode[s][b[s]] != 10 ){
471     stringcopy(file2f,calcalib,0,0);
472     pfile = (TString)whatnamewith(file2f,calib.fcode[s][b[s]]);
473     } else {
474     pfile = (TString)calcalib;
475     };
476     porca = pfile;
477     if ( debug ) printf(" porca miseria %s \n",porca);
478     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
479     };
480     //
481     // run over all the events
482     //
483     printf("\n Processed events: \n\n");
484     //
485     for (Int_t i = 0; i < nevents; i++){
486     evno = i;
487     if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);
488     // printf(" %i \n",i);
489     //
490     // read from the header of the event the absolute time at which it was recorded
491     //
492     otr->GetEntry(i);
493     //
494     S4 = trigger->patterntrig[1] & (1<<0);
495     //
496     if ( !S4 || i == 0 ) {
497     ph = eh->GetPscuHeader();
498     etime = ph->GetOrbitalTime();
499     //
500     // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration
501     //
502     if ( !calib.obtjump ) {
503     for (Int_t s = 0; s < 4; s++){
504     if ( calib.ttime[s][b[s]] ){
505     while ( etime > calib.time[s][b[s]+1] ){
506     printf(" CALORIMETER: \n" );
507     printf(" - Section %i, event at time %i while old calibration time limit at %i. Use new calibration at time %i -\n",s,etime,calib.time[s][b[s]+1],calib.ttime[s][b[s]+1]);
508     printf(" END CALORIMETER. \n\n" );
509     b[s]++;
510     if ( calib.fcode[s][b[s]] != 10 ){
511     stringcopy(file2f,calcalib,0,0);
512     pfile = (TString)whatnamewith(file2f,calib.fcode[s][b[s]]);
513     } else {
514     pfile = (TString)calcalib;
515     };
516     porca = pfile;
517     if ( debug ) printf(" porca miseria %s \n",porca);
518     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
519     };
520     };
521     };
522     };
523     //
524     // do whatever you want with data
525     //
526     evento.iev = de->iev;
527     //
528     // run over views and planes
529     //
530     for (Int_t l = 0; l < 2; l++){
531     for (Int_t m = 0; m < 22; m++){
532     //
533     // determine the section number
534     //
535     if (l == 0 && m%2 == 0) se = 3;
536     if (l == 0 && m%2 != 0) se = 2;
537     if (l == 1 && m%2 == 0) se = 0;
538     if (l == 1 && m%2 != 0) se = 1;
539     //
540     // determine what kind of event we are going to analyze
541     //
542     isCOMP = 0;
543     isFULL = 0;
544     isRAW = 0;
545     if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;
546     if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;
547     if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;
548     //
549     // save the prevoius energy deposit and calibration in sbase, sdexy, sdexyc
550     //
551     pre = -1;
552     if ( isRAW ){
553     for (Int_t nn = 0; nn < 96; nn++){
554     if ( nn%16 == 0 ) pre++;
555     evento.base[l][m][pre] = calib.calbase[l][m][pre];
556     sdexy[l][m][nn] = evento.dexy[l][m][nn];
557     evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;
558     sdexyc[l][m][nn] = evento.dexyc[l][m][nn];
559     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
560     };
561     };
562     //
563     // run over strips
564     //
565     pre = -1;
566     for (Int_t n =0 ; n < 96; n++){
567     if ( n%16 == 0 ) {
568     pre++;
569     rdone = 0;
570     done = 0;
571     };
572     if ( calo->mask[l][m][n] == 0. && ( isRAW || ( de->dexyc[l][m][n] > 0. && de->dexyc[l][m][n] < 32000.)) ){
573     //
574     // baseline check and calculation
575     //
576     if ( isRAW ) {
577     if ( !rdone ){
578     CaloFindBaseRaw(calib,evento,l,m,pre);
579     rdone = 1;
580     };
581     } else {
582     if ( !done ){
583     evento.base[l][m][pre] = de->base[l][m][pre] ;
584     evento.dexyc[l][m][n] = de->dexyc[l][m][n] ;
585     };
586     };
587     //
588     // no suitable new baseline, use old ones and compress data!
589     //
590     if ( !done && (evento.base[l][m][pre] > 30000. || evento.base[l][m][pre] == 0.) ){
591     evento.base[l][m][pre] = calib.sbase[l][m][pre];
592     upnn = n+16;
593     if ( upnn > 96 ) upnn = 96;
594     for ( Int_t nn = n; nn<upnn; nn++ ){
595     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
596     };
597     CaloCompressData(calib,evento,l,m,pre);
598     done = 1;
599     };
600     //
601     // CALIBRATION ALGORITHM
602     //
603     pl1 = -1;
604     pl2 = -1;
605     pl1 = m - 1;
606     pl2 = m + 1;
607     if ( pl1 < 0 ) {
608     pl1 = pl2;
609     pl2 = m + 2;
610     };
611     if ( pl2 > 21 ) {
612     pl2 = pl1;
613     pl1 = m - 2;
614     };
615     strmin = n - 1;
616     if ( strmin < 0 ) strmin = 0;
617     strmax = n + 1;
618     if ( strmax > 95 ) strmax = 95;
619     surstrip = 0;
620     surmam = 0;
621     surman = 0;
622     for (Int_t im = 0; im<3 ;im++){
623     for (Int_t jm = 0; jm<3 ;jm++){
624     surmatrix[im][jm] = 0;
625     };
626     };
627     for (Int_t spl = pl1; spl<pl2+1; spl++){
628     if ( spl != m ){
629     surmam = spl - pl1;
630     for (Int_t sst = strmin; sst <= strmax ; sst++){
631     surman = sst - strmin;
632     if ( sst < 16 ) spre = 0;
633     if ( 15 < sst && sst < 32 ) spre = 1;
634     if ( 31 < sst && sst < 48 ) spre = 2;
635     if ( 47 < sst && sst < 64 ) spre = 3;
636     if ( 63 < sst && sst < 80 ) spre = 4;
637     if ( 79 < sst ) spre = 5;
638     if ( isRAW ){
639     estrippa = ( evento.dexy[l][spl][sst] - calib.calped[l][spl][sst] - calib.sbase[l][spl][spre])/26. ;
640     } else {
641     estrippa = ( evento.dexyc[l][spl][sst] - calib.calped[l][spl][sst] - calib.sbase[l][spl][spre])/26. ;
642     };
643     if ( estrippa > 0.7 && estrippa < 10. && calib.calgood[l][spl][sst] == 0 ){
644     surstrip++;
645     surmatrix[surmam][surman]++;
646     //printf("surstrip = %i estrip = %f \n",surstrip,estrip);
647     };
648     };
649     };
650     };
651     if ( (surmatrix[0][0] && surmatrix[2][0]) ) surstrip = 0;
652     if ( (surmatrix[0][2] && surmatrix[2][2]) ) surstrip = 0;
653     //
654     //
655     estrip[l][m][n] = ( evento.dexyc[l][m][n] - calib.calped[l][m][n] - evento.base[l][m][pre]) ;
656     if ( estrip[l][m][n] >= 0. && estrip[l][m][n] <= 56. && surstrip >=2 ){
657     pfmip[l][m][n]->Fill(estrip[l][m][n]);
658     UPYES = 1;
659     };
660     calib.sbase[l][m][pre] = evento.base[l][m][pre];
661     };
662     };
663     };
664     };
665     };
666     };
667     //
668     // save filename
669     //
670     calo->fname = sfile;
671     tree->Fill();
672     hfile->Write();
673     fileup++;
674     hfile->Close();
675     //
676     // create a backup copy!
677     //
678    
679     if ( nfile > 1 && ( e%10 == 0 || e == nfile-1 ) && e > 0 ){
680     files.str("");
681     files << file4.str().c_str() << (e+1);
682     files << ".bak";
683     printf(" Save the backup file with figures:\n %s \n ",files.str().c_str());
684     hfileb = new TFile(files.str().c_str(),"RECREATE","Calorimeter CALIBRATION figures");
685     if ( !existfile(files.str().c_str()) ){
686     printf(" ERROR: Cannot create file!!! \n");
687     goto oltre;
688     };
689     TH1F *bfmip[2][22][96];
690     stringstream bmippa;
691     bmippa.str("");
692     for (Int_t l = 0; l < 2; l++){
693     for (Int_t m = 0; m < 22; m++){
694     for (Int_t n =0 ; n < 96; n++){
695     bmippa.str("");
696     bmippa << "bmip " << l;
697     bmippa << " " << m;
698     bmippa << " " << n;
699     bfmip[l][m][n] = new TH1F(bmippa.str().c_str(),"",56,0.,55.);
700     bfmip[l][m][n] = (TH1F*)pfmip[l][m][n]->Clone(bmippa.str().c_str());
701     bfmip[l][m][n]->Write();
702     };
703     };
704     };
705     hfileb->Close();
706     };
707     //
708     //
709     //
710     headerFile->Close();
711     // caloFile->Close();
712     //triggerFile->Close();
713     goto oltre;
714     jumpfile: printf(" File not processed! \n ");
715     hfile->Close();
716     oltre:
717     // gObjectTable->Print();
718     e++;
719     };
720     //
721     if ( !UPYES ) goto end;
722     printf("\n OK, now I will fit the MIP distribution for each strip \n\n");
723     //
724     hfile = new TFile(file2.str().c_str(),"UPDATE","Calorimeter CALIBRATION data");
725     calo = new CalorimeterCalibration();
726     tree = (TTree*)hfile->Get("CaloADC");
727     tree->SetBranchAddress("Event", &calo);
728     nevents = tree->GetEntries();
729     if ( (nevents-1-fileup) > 0 ){
730     tree->GetEntry(nevents-1-fileup);
731     };
732     //
733     // Fit all 4224 distributions and save parameters, fit, figures (all basically).
734     //
735     notgood = 0;
736     notgood1 = 0;
737     //
738     //
739     for (Int_t l = 0; l < 2; l++){
740     for (Int_t m = 0; m < 22; m++){
741     for (Int_t n =0 ; n < 96; n++){
742     Double_t SNRPeak = 0.;
743     if ( calo->mask[l][m][n] == 0. ){
744     //
745     // Setting fit range and start values
746     //
747     Double_t fr[2];
748     Double_t sv[4], pllo[4], plhi[4], fp[4], fpe[4];
749     if ( SNRPeak > 16. && SNRPeak < 35. ){
750     fr[0] = (Float_t)SNRPeak - 7.;
751     sv[0]= fp[0];
752     sv[1]= fp[1];
753     sv[2]= fp[2];
754     sv[3]= fp[3];
755     } else {
756     fr[0] = 19.;
757     sv[0] = 2.8;
758     sv[1] = 21.0;
759     sv[2] = 1000.0;
760     sv[3] = 0.1;
761     };
762     Int_t doitagain = 0;
763     fitting: printf("Fitting strip %i %i %i \n",l,m,n);
764     fr[1] = 60.;
765     pllo[0]=0.5; pllo[1]=5.0; pllo[2]=1.0; pllo[3]=0.2;
766     plhi[0]=8.0; plhi[1]=50.0; plhi[2]=1000000.0; plhi[3]=8.0;
767     Double_t chisqr;
768     Int_t ndf;
769     //
770     // fit the figure pfmip and put fit results in pfitsnr
771     //
772     pfitsnr[l][m][n] = langaufit(pfmip[l][m][n],fr,sv,pllo,plhi,fp,fpe,&chisqr,&ndf,"QR0");
773     //
774     // search for the maximum of the distribution, it will be the conversion factor between ADC channels and the MIP
775     //
776     Double_t SNRPeak = langaupro(fp);
777     printf("\n Conversion factor: %f \n\n",SNRPeak);
778     //
779     //
780     //
781     if ( ( SNRPeak < 0. || (SNRPeak < 16. || SNRPeak > 35.) || chisqr > 100.) && doitagain < 3 ){
782     printf("\n The fit is not good, I will try again, step %i \n\n",doitagain);
783     doitagain++;
784     if ( chisqr > 100. ) {
785     fr[0] = fr[0] + 5.;
786     sv[0] = fp[0];
787     sv[1] = fp[1];
788     sv[2] = fp[2];
789     sv[3] = fp[3];
790     goto fitting;
791     };
792     if ( doitagain == 2 ) {
793     fr[0] = 19.;
794     sv[0] = 2.8;
795     sv[1] = 21.0;
796     sv[2] = 1000.0;
797     sv[3] = 0.1;
798     goto fitting;
799     };
800     if ( doitagain == 1 ) {
801     fr[0] = 22.;
802     sv[0] = 2.8;
803     sv[1] = 25.0;
804     sv[2] = 1000.0;
805     sv[3] = 0.1;
806     goto fitting;
807     };
808     if ( doitagain == 3 ) {
809     fr[0] = 12.;
810     sv[0] = 2.8;
811     sv[1] = 15.0;
812     sv[2] = 1000.0;
813     sv[3] = 0.1;
814     goto fitting;
815     };
816     };
817    
818     //
819     // save data
820     //
821     calo->mip[l][m][n] = (float)SNRPeak;
822     calo->ermip[l][m][n] = (float)fpe[1];
823     for (Int_t a = 0; a < 4 ; a++){
824     calo->fp[a][l][m][n] = (float)fp[a];
825     calo->fpe[a][l][m][n] = (float)fpe[a];
826     };
827     calo->ndf[l][m][n] = (float)ndf;
828     calo->chi2[l][m][n] = (float)chisqr;
829     if ( calo->fp[1][l][m][n] > 16. && calo->fp[1][l][m][n] < 35. ) notgood1++;
830     //
831     if ( ndf!=0 && chisqr/ndf < 2. && fpe[1] < 0.15 && fp[1] > 15. && fp[1] < 40. && SNRPeak > 0. ) {
832     printf(" THIS IS A GOOD FIT, SAVED! \n\n");
833     calo->mask[l][m][n] = 1.;
834     } else {
835     notgood++;
836     calo->mask[l][m][n] = 0.;
837     };
838     printf("Fitting done, strip %i %i %i \n\n\n",l,m,n);
839     //
840     // draw the figure and fit on the screen
841     //
842     //c1->Clear();
843     //c1->cd();
844     //pfmip[l][m][n]->DrawCopy();
845     //pfitsnr[l][m][n]->DrawCopy("lsame");
846     //c1->Modified();
847     //c1->Update();
848     };
849     };
850     };
851     };
852     //
853     calo->status = 0;
854     if ( !notgood ) calo->status = 1;
855     //
856     // save fit data
857     //
858     tree->Fill();
859     hfile->Write();
860     hfile->Close();
861     //
862     // Save histograms and fit figures
863     //
864     TFile *hfile2;
865     hfile2 = new TFile(file3.str().c_str(),"RECREATE","Calorimeter CALIBRATION figures");
866     if ( !existfile(file3.str().c_str()) ){
867     printf(" ERROR: Cannot create file \n");
868     return;
869     };
870     TH1F *sfmip[2][22][96];
871     TF1 *sfitsnr[2][22][96];
872     fmippa.str("");
873     for (Int_t l = 0; l < 2; l++){
874     for (Int_t m = 0; m < 22; m++){
875     for (Int_t n =0 ; n < 96; n++){
876     fmippa.str("");
877     fmippa << "fmip " << l;
878     fmippa << " " << m;
879     fmippa << " " << n;
880     sfmip[l][m][n] = new TH1F(fmippa.str().c_str(),"",56,0.,55.);
881     sfmip[l][m][n] = (TH1F*)pfmip[l][m][n]->Clone(fmippa.str().c_str());
882     fmippa.str("");
883     fmippa << "fit " << l;
884     fmippa << " " << m;
885     fmippa << " " << n;
886     sfitsnr[l][m][n] = (TF1*)pfitsnr[l][m][n]->Clone(fmippa.str().c_str());
887     sfmip[l][m][n]->Write();
888     sfitsnr[l][m][n]->Write();
889     };
890     };
891     };
892     hfile2->Close();
893     if ( hf3 ) hfile3->Close();
894     //
895     if ( notgood ) {
896     printf("\n An update will be necessary:\n %i strip with too low statistic (%i of them gave a result anyway)\n\n",notgood,notgood1);
897     };
898     //
899     end: printf("\n");
900     //
901     }
902    
903     void FCaloRAWADC2MIPPLOT(TString Filename, TString calcalibfile = "", TString OutDir="", TString flist=""){
904     const char *pam_calib = pathtocalibration();
905     if ( !strcmp(OutDir.Data(),"") ){
906     OutDir = pam_calib;
907     };
908     //
909     TString calcalib;
910     struct Evento evento;
911     struct Calib calib;
912     Int_t debug = 0;
913     Int_t b[4];
914     Int_t etime,evno;
915     Float_t sdexy[2][22][96],sdexyc[2][22][96];
916     Float_t edelta[2][22][96];
917     Float_t estrip[2][22][96];
918     Float_t estripnull[2][22][96];
919     Float_t delta;
920     CalorimeterCalibration *calo = new CalorimeterCalibration();
921     delta = 1.;
922     evento.emin = 0.7;
923     Int_t se =5;
924     Int_t pre;
925     Int_t rdone = 0;
926     Int_t done;
927     bool isCOMP = 0;
928     bool isFULL = 0;
929     bool isRAW = 0;
930     TH1F *pfmip[2][22][96];
931     TF1 *pfitsnr[2][22][96];
932     stringstream Fmip;
933     Fmip.str("");
934     for (Int_t i=0; i<2;i++){
935     for (Int_t j=0; j<22;j++){
936     for (Int_t m=0; m<96;m++){
937     Fmip.str("");
938     Fmip << "Fmip " << i;
939     Fmip << " " << j;
940     Fmip << " " << m;
941     pfmip[i][j][m] = new TH1F(Fmip.str().c_str(),"",70,-15.,55.);
942     pfitsnr[i][j][m] = new TF1;
943     };
944     };
945     };
946     //
947     // the file which contains the calorimeter ADC to MIP conversion values is file2 = path to the script dir / CaloADC2MIP.root
948     //
949     stringstream file2;
950     file2.str("");
951     file2 << OutDir.Data() << "/CaloADC2MIP.raw";
952     //
953     // this the figures and fit (4224 x 2 objects!)
954     //
955     stringstream file3;
956     file3 << OutDir.Data() << "/CaloADC2MIPfr.raw";
957     //
958     stringstream file4;
959     file4.str("");
960     file4 << OutDir.Data() << "/CaloADC2MIPfr";
961     //
962     stringstream file5;
963     file5.str("");
964     file5 << OutDir.Data() << "/CaloADC2MIPdata.raw";
965     //
966     Int_t nfile = 0;
967     Int_t fileup = 0;
968     if ( flist != "" ){
969     printf("\n You have given an input file list \n\n Files: \n");
970     ifstream in;
971     in.open(flist, ios::in);
972     char ctime[20];
973     while (1) {
974     in >> ctime;
975     if (!in.good()) break;
976     printf(" File number %i = %s \n",nfile+1,ctime);
977     nfile++;
978     };
979     in.close();
980     if ( !nfile ) {
981     printf("\n\n The list is empty, exiting... \n\n");
982     return;
983     };
984     } else {
985     nfile = 1;
986     };
987     printf("\n");
988     //
989     // First check if the calibration file exists:
990     //
991     Int_t EFILE = 0;
992     printf(" Check the existence of CaloADC2MIP.raw file: \n\n");
993    
994     if ( !existfile(file2.str().c_str()) ){
995     printf(" %s :no such file or directory \n",file2.str().c_str());
996     printf("\n OK, I will create it!\n\n");
997     EFILE = 0;
998     } else {
999     printf(" The file already exists! Check if an update is needed \n\n");
1000     EFILE = 1;
1001     };
1002     //
1003     // the check for an update is done once even if we have a list.
1004     //
1005     TTree *tree = 0;
1006     if ( EFILE == 1) {
1007     //
1008     // The file exists, open it (READ ONLY) and check if any update is needed:
1009     //
1010     TFile *hfile = 0;
1011     hfile = new TFile(file2.str().c_str(),"READ","Calorimeter CALIBRATION data");
1012     calo = new CalorimeterCalibration();
1013     tree = (TTree*)hfile->Get("CaloADC");
1014     tree->SetBranchAddress("Event", &calo);
1015     //
1016     Long64_t nevents = tree->GetEntries();
1017     printf("\n Number of updates: %i\n",(int)nevents);
1018     tree->GetEntry(nevents-1);
1019     //
1020     // Read the status variable of the last event: if it is 0 then we need an update, if it is one print out informations and exit
1021     //
1022     if ( calo->status == 0 ) {
1023     printf("\n An update is needed: \n");
1024     Int_t upstrip = 0;
1025     for (Int_t i = 0; i<2; i++){
1026     for (Int_t j = 0; j<22; j++){
1027     for (Int_t l = 0; l<96; l++){
1028     if ( calo->mask[i][j][l] == 0. ) upstrip++;
1029     };
1030     };
1031     };
1032     printf(" %i strip out of 4224 have no statistic enough.\n\n",upstrip);
1033     } else {
1034     printf(" No update is needed! \n The data files used to obtain the ADC to MIP conversion are: \n");
1035     for (Int_t i = 0; i<nevents; i++){
1036     tree->GetEntry(i);
1037     const char *fnout = calo->fname;
1038     printf(" - %s \n",fnout);
1039     };
1040     printf("\n\n");
1041     return;
1042     };
1043     hfile->Close();
1044     };
1045     //
1046     const char *filename2 = Filename;
1047     //
1048     TString lfile;
1049     TString sfile;
1050     //
1051     Int_t e = 0;
1052     ifstream in;
1053     in.open(flist, ios::in);
1054     char ctime[20];
1055     TString pfile;
1056     const char *cali;
1057     const char *ffile;
1058     Int_t calibex = 0;
1059     //
1060     TString filename;
1061     const char *nome = 0;
1062     TFile *hfile = 0;
1063     // CalorimeterCalibration *calo = 0;
1064     Long64_t nevents;
1065     Int_t used = 0;
1066     const char *compare = 0;
1067     Int_t hf3 = 0;
1068     TFile *headerFile = 0;
1069     TString file2f = "";
1070     pfile = "";
1071     const char *porca = 0;
1072     Int_t S4;
1073     Int_t upnn;
1074     stringstream files;
1075     TFile *hfileb = 0;
1076     //
1077     Int_t pl1, pl2, strmin, strmax;
1078     Int_t spre=0;
1079     Int_t surstrip = 0;
1080     Float_t estrippa = 0.;
1081     //
1082     TFile *hfile3 = 0;
1083     TString fififile;
1084     const char *file;
1085     //
1086     calo = new CalorimeterCalibration();
1087     stringstream finome;
1088     finome.str("");
1089     while ( e < nfile ){
1090     if ( flist != "" ){
1091     in >> ctime;
1092     finome.str("");
1093     finome << filename2 << "/";
1094     finome << ctime;
1095     lfile = TString(finome.str().c_str());
1096     fififile = getFilename(lfile);
1097     file = fififile;
1098     finome.str("");
1099     finome << file;
1100     sfile = TString(finome.str().c_str());
1101     } else {
1102     finome.str("");
1103     finome << filename2;
1104     lfile = TString(finome.str().c_str());
1105     fififile = getFilename(lfile);
1106     file = fififile;
1107     finome.str("");
1108     finome << file;
1109     sfile = TString(finome.str().c_str());
1110     };
1111     //
1112     filename = lfile;
1113     nome = filename;
1114     printf("\n Processing file number %i: %s \n\n",e,nome);
1115     nome = sfile;
1116     //
1117     // this must be done for each file.
1118     //
1119     TTree *otr;
1120     pamela::calorimeter::CalorimeterEvent *de = 0;
1121     pamela::trigger::TriggerEvent *trigger = 0;
1122     pamela::PscuHeader *ph = 0;
1123     pamela::EventHeader *eh = 0;
1124     const char *calname;
1125     Int_t wused = 0;
1126     //
1127     // TTree *tree;
1128     if ( EFILE == 1 ){
1129     hfile = new TFile(file2.str().c_str(),"UPDATE","Calorimeter CALIBRATION data");
1130     tree = (TTree*)hfile->Get("CaloADC");
1131     tree->SetBranchAddress("Event", &calo);
1132     //
1133     // Check if the input file has already been used
1134     //
1135     printf(" Check if file %s has already been used... \n",nome);
1136     nevents = tree->GetEntries();
1137     used = 0;
1138     for (Int_t i = 0; i<nevents; i++){
1139     tree->GetEntry(i);
1140     compare= calo->fname;
1141     if ( !strcmp(compare,nome) ) {
1142     used = 1;
1143     break;
1144     };
1145     };
1146     if ( used ) {
1147     printf(" The file %s has already been used!\n You cannot run twice this program with that file!\n\n",nome);
1148     hfile->Close();
1149     goto jumpfile;
1150     } else {
1151     printf(" Good, the file has not been used yet. \n\n");
1152     nevents = tree->GetEntries() -1;
1153     tree->GetEntry(nevents);
1154     };
1155     } else {
1156     //
1157     // If the file doesn't exist create it
1158     //
1159     calo = new CalorimeterCalibration();
1160     hfile = new TFile(file2.str().c_str(),"NEW","Calorimeter CALIBRATION data");
1161     if ( !existfile(file2.str().c_str()) ){
1162     printf(" %s :no such file or directory \n",filename.Data());
1163     printf(" ERROR: Cannot create file \n");
1164     return;
1165     };
1166     tree = new TTree("CaloADC","Calorimeter calibration data");
1167     tree->Branch("Event","CalorimeterCalibration",&calo);
1168     };
1169     //
1170     //
1171     //
1172     hf3 = 0;
1173     if ( EFILE == 1 && e == 0 ){
1174     printf(" Reading the old MIP figures to be updated...\n ");
1175     hfile3 = new TFile(file3.str().c_str());
1176     hf3 = 1;
1177     //
1178     stringstream fmippa;
1179     fmippa.str("");
1180     for (Int_t l = 0; l < 2; l++){
1181     for (Int_t m = 0; m < 22; m++){
1182     for (Int_t n =0 ; n < 96; n++){
1183     fmippa.str("");
1184     fmippa << "fmip " << l;
1185     fmippa << " " << m;
1186     fmippa << " " << n;
1187     TH1F *temp = (TH1F*)hfile3->Get(fmippa.str().c_str());
1188     pfmip[l][m][n] = (TH1F*)temp->Clone();
1189     };
1190     };
1191     };
1192     printf(" ...done!\n");
1193     };
1194     EFILE = 1;
1195     //
1196     // Check if the input calibration file contains any calibration data
1197     //
1198     done = 0;
1199     printf(" Check for calorimeter calibration: \n");
1200     calcalib = filename;
1201     findcalibs: calname = calcalib;
1202     printf(" Calibration file: %s \n",calname);
1203     for (Int_t s=0; s<4;s++){
1204     for (Int_t d = 0; d<50; d++){
1205     calib.ttime[s][d] = 0 ;
1206     if ( d < 49 ) calib.time[s][d] = 0 ;
1207     };
1208     };
1209     for (Int_t m = 0; m < 2 ; m++ ){
1210     for (Int_t k = 0; k < 22; k++ ){
1211     for (Int_t l = 0; l < 96; l++ ){
1212     calib.calped[m][k][l] = 0. ;
1213     evento.dexy[m][k][l] = 0. ;
1214     evento.dexyc[m][k][l] = 0. ;
1215     edelta[m][k][l] = 0.;
1216     estrip[m][k][l] = 0.;
1217     estripnull[m][k][l] = 0.;
1218     };
1219     };
1220     }
1221     wused = 0;
1222     CaloFindCalibs(calcalib, calcalib, wused, calib);
1223     //
1224     // print on the screen the results:
1225     //
1226     ffile = filename;
1227     printf(" ------ %s ------- \n \n",ffile);
1228     calibex = 0;
1229     for (Int_t s=0; s<4;s++){
1230     printf(" ** SECTION %i **\n",s);
1231     for (Int_t d = 0; d<51; d++){
1232     if ( calib.ttime[s][d] != 0 ) {
1233     calibex++;
1234     if ( calib.fcode[s][d] != 10 ){
1235     file2f = "";
1236     stringcopy(file2f,calcalib,0,0);
1237     pfile = (TString)whatnamewith(file2f,calib.fcode[s][d]);
1238     } else {
1239     pfile = (TString)calcalib;
1240     };
1241     ffile = pfile;
1242     printf(" - from time %i to time %i use calibration at\n time %i, file: %s \n",calib.time[s][d],calib.time[s][d+1],calib.ttime[s][d],ffile);
1243     };
1244     };
1245     printf("\n");
1246     };
1247     printf(" ----------------------------------------------------------------------- \n \n");
1248     //
1249     if ( calibex < 1 ) {
1250     printf("No calibration data in this file!\n");
1251     calcalib = calcalibfile;
1252     if ( !done && calcalibfile != "" ) {
1253     cali = calcalibfile;
1254     printf(" Search in the calibration file %s \n",cali);
1255     done = 1;
1256     goto findcalibs;
1257     } else {
1258     printf(" Cannot find any calibration for this file! \n");
1259     goto jumpfile;
1260     };
1261     };
1262     if ( calibex < 4 ) {
1263     printf(" WARNING! No full calibration data in this file! \n\n");
1264     };
1265     //
1266     // upgrade data...
1267     //
1268     if ( !existfile(filename) ){
1269     printf(" %s :no such file or directory \n",filename.Data());
1270     goto jumpfile;
1271     };
1272     headerFile = new TFile(filename.Data());
1273     otr = (TTree*)headerFile->Get("Physics");
1274     otr->SetBranchAddress("Header", &eh);
1275     otr->SetBranchAddress("Calorimeter", &de);
1276     otr->SetBranchAddress("Trigger", &trigger);
1277     //
1278     //
1279     nevents = otr->GetEntries();
1280     //
1281     // calibrate before starting
1282     //
1283     for (Int_t s = 0; s < 4; s++){
1284     b[s]=0;
1285     if ( calib.fcode[s][b[s]] != 10 ){
1286     stringcopy(file2f,calcalib,0,0);
1287     pfile = (TString)whatnamewith(file2f,calib.fcode[s][b[s]]);
1288     } else {
1289     pfile = (TString)calcalib;
1290     };
1291     porca = pfile;
1292     if ( debug ) printf(" porca miseria %s \n",porca);
1293     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
1294     };
1295     //
1296     // run over all the events
1297     //
1298     printf("\n Processed events: \n\n");
1299     //
1300     for (Int_t i = 0; i < nevents; i++){
1301     evno = i;
1302     if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);
1303     //
1304     // read from the header of the event the absolute time at which it was recorded
1305     //
1306     otr->GetEntry(i);
1307     //
1308     S4 = trigger->patterntrig[1] & (1<<0);
1309     //
1310     if ( !S4 || i == 0 ) {
1311     ph = eh->GetPscuHeader();
1312     etime = ph->GetOrbitalTime();
1313     //
1314     // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration
1315     //
1316     if ( !calib.obtjump ) {
1317     for (Int_t s = 0; s < 4; s++){
1318     if ( calib.ttime[s][b[s]] ){
1319     while ( etime > calib.time[s][b[s]+1] ){
1320     printf(" CALORIMETER: \n" );
1321     printf(" - Section %i, event at time %i while old calibration time limit at %i. Use new calibration at time %i -\n",s,etime,calib.time[s][b[s]+1],calib.ttime[s][b[s]+1]);
1322     printf(" END CALORIMETER. \n\n" );
1323     b[s]++;
1324     if ( calib.fcode[s][b[s]] != 10 ){
1325     stringcopy(file2f,calcalib,0,0);
1326     pfile = (TString)whatnamewith(file2f,calib.fcode[s][b[s]]);
1327     } else {
1328     pfile = (TString)calcalib;
1329     };
1330     porca = pfile;
1331     if ( debug ) printf(" porca miseria %s \n",porca);
1332     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
1333     };
1334     };
1335     };
1336     };
1337     //
1338     // do whatever you want with data
1339     //
1340     evento.iev = de->iev;
1341     //
1342     // run over views and planes
1343     //
1344     for (Int_t l = 0; l < 2; l++){
1345     for (Int_t m = 0; m < 22; m++){
1346     //
1347     // determine the section number
1348     //
1349     if (l == 0 && m%2 == 0) se = 3;
1350     if (l == 0 && m%2 != 0) se = 2;
1351     if (l == 1 && m%2 == 0) se = 0;
1352     if (l == 1 && m%2 != 0) se = 1;
1353     //
1354     // determine what kind of event we are going to analyze
1355     //
1356     isCOMP = 0;
1357     isFULL = 0;
1358     isRAW = 0;
1359     if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;
1360     if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;
1361     if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;
1362     //
1363     // save the prevoius energy deposit and calibration in sbase, sdexy, sdexyc
1364     //
1365     pre = -1;
1366     if ( isRAW ){
1367     for (Int_t nn = 0; nn < 96; nn++){
1368     if ( nn%16 == 0 ) pre++;
1369     evento.base[l][m][pre] = calib.calbase[l][m][pre];
1370     sdexy[l][m][nn] = evento.dexy[l][m][nn];
1371     evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;
1372     sdexyc[l][m][nn] = evento.dexyc[l][m][nn];
1373     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
1374     };
1375     //
1376     // run over strips
1377     //
1378     pre = -1;
1379     for (Int_t n =0 ; n < 96; n++){
1380     if ( n%16 == 0 ) {
1381     pre++;
1382     rdone = 0;
1383     done = 0;
1384     };
1385     if ( calo->mask[l][m][n] == 0. && ( isRAW || ( de->dexyc[l][m][n] > 0. && de->dexyc[l][m][n] < 32000.)) ){
1386     //
1387     // baseline check and calculation
1388     //
1389     if ( !rdone ){
1390     CaloFindBaseRawNC(calib,evento,l,m,pre);
1391     rdone = 1;
1392     };
1393     //
1394     // no suitable new baseline, use old ones and compress data!
1395     //
1396     if ( !done && (evento.base[l][m][pre] > 30000. || evento.base[l][m][pre] == 0.) ){
1397     evento.base[l][m][pre] = calib.sbase[l][m][pre];
1398     upnn = n+16;
1399     if ( upnn > 96 ) upnn = 96;
1400     for ( Int_t nn = n; nn<upnn; nn++ ){
1401     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
1402     };
1403     done = 1;
1404     };
1405     //
1406     // CALIBRATION ALGORITHM
1407     //
1408     pl1 = -1;
1409     pl2 = -1;
1410     pl1 = m - 1;
1411     pl2 = m + 1;
1412     if ( pl1 < 0 ) {
1413     pl1 = pl2;
1414     pl2 = m + 2;
1415     };
1416     if ( pl2 > 21 ) {
1417     pl2 = pl1;
1418     pl1 = m - 2;
1419     };
1420     strmin = n - 2;
1421     if ( strmin < 0 ) strmin = 0;
1422     strmax = n + 2;
1423     if ( strmax > 95 ) strmax = 95;
1424     surstrip = 0;
1425     for (Int_t spl = pl1; spl<pl2+1; spl++){
1426     if ( spl != m ){
1427     for (Int_t sst = strmin; sst<strmax+1; sst++){
1428     if ( sst < 16 ) spre = 0;
1429     if ( 15 < sst && sst < 32 ) spre = 2;
1430     if ( 31 < sst && sst < 48 ) spre = 3;
1431     if ( 47 < sst && sst < 64 ) spre = 4;
1432     if ( 63 < sst && sst < 80 ) spre = 5;
1433     if ( 79 < sst ) spre = 6;
1434     if ( isRAW ){
1435     estrippa = ( evento.dexy[l][spl][sst] - calib.calped[l][spl][sst] - calib.sbase[l][spl][spre])/26. ;
1436     } else {
1437     estrippa = ( evento.dexyc[l][spl][sst] - calib.calped[l][spl][sst] - calib.sbase[l][spl][spre])/26. ;
1438     };
1439     if ( estrippa > 0.7 && estrippa < 10. && calib.calgood[l][spl][sst] == 0 ){
1440     surstrip++;
1441     //printf("surstrip = %i estrip = %f \n",surstrip,estrip);
1442     };
1443     };
1444     };
1445     };
1446     //
1447     //
1448     estrip[l][m][n] = ( evento.dexyc[l][m][n] - calib.calped[l][m][n] - evento.base[l][m][pre]) ;
1449     pfmip[l][m][n]->Fill(estrip[l][m][n]);
1450     calib.sbase[l][m][pre] = evento.base[l][m][pre];
1451     };
1452     };
1453     };
1454     };
1455     };
1456     };
1457     };
1458     //
1459     // save filename
1460     //
1461     calo->fname = sfile;
1462     tree->Fill();
1463     hfile->Write();
1464     fileup++;
1465     hfile->Close();
1466     //
1467     // create a backup copy!
1468     //
1469     if ( nfile > 1 && ( e%10 == 0 || e == nfile-1 ) && e > 0 ){
1470     files.str("");
1471     files << file4.str().c_str() << (e+1);
1472     files << ".bak";
1473     printf(" Save the backup file with figures:\n %s \n ",files.str().c_str());
1474     hfileb = new TFile(files.str().c_str(),"RECREATE","Calorimeter CALIBRATION figures");
1475     if ( !existfile(files.str().c_str()) ){
1476     printf(" ERROR: Cannot create file!!! \n");
1477     goto oltre;
1478     };
1479     TH1F *bfmip[2][22][96];
1480     stringstream bmippa;
1481     for (Int_t l = 0; l < 2; l++){
1482     for (Int_t m = 0; m < 22; m++){
1483     for (Int_t n =0 ; n < 96; n++){
1484     bmippa.str("");
1485     bmippa << "bmip " << l;
1486     bmippa << " " << m;
1487     bmippa << " " << n;
1488     bfmip[l][m][n] = new TH1F(bmippa.str().c_str(),"",70,-15.,55.);
1489     bfmip[l][m][n] = (TH1F*)pfmip[l][m][n]->Clone(bmippa.str().c_str());
1490     bfmip[l][m][n]->Write();
1491     };
1492     };
1493     };
1494     hfileb->Close();
1495     };
1496     //
1497     //
1498     //
1499     headerFile->Close();
1500     goto oltre;
1501     jumpfile: printf(" File not processed! \n ");
1502     hfile->Close();
1503     oltre:
1504     // gObjectTable->Print();
1505     e++;
1506     };
1507     //
1508     // Save histograms and fit figures
1509     //
1510     TFile *hfile2;
1511     hfile2 = new TFile(file3.str().c_str(),"RECREATE","Calorimeter CALIBRATION figures");
1512     if ( !existfile(file3.str().c_str()) ){
1513     printf(" ERROR: Cannot create file!!! \n");
1514     return;
1515     };
1516     TH1F *sfmip[2][22][96];
1517     stringstream fmippa;
1518     for (Int_t l = 0; l < 2; l++){
1519     for (Int_t m = 0; m < 22; m++){
1520     for (Int_t n =0 ; n < 96; n++){
1521     fmippa.str("");
1522     fmippa << "fmip " << l;
1523     fmippa << " " << m;
1524     fmippa << " " << n;
1525     sfmip[l][m][n] = new TH1F(fmippa.str().c_str(),"",72,-15.,55.);
1526     sfmip[l][m][n] = (TH1F*)pfmip[l][m][n]->Clone(fmippa.str().c_str());
1527     sfmip[l][m][n]->Write();
1528     };
1529     };
1530     };
1531     hfile2->Close();
1532     if ( hf3 ) hfile3->Close();
1533     }
1534    
1535     void FCaloRAWADC2MIPDATA(TString Filename, TString calcalibfile = "", TString OutDir="", TString flist=""){
1536     const char *pam_calib = pathtocalibration();
1537     if ( !strcmp(OutDir.Data(),"") ){
1538     OutDir = pam_calib;
1539     };
1540     TString calcalib;
1541     struct Evento evento;
1542     struct Calib calib;
1543     Int_t debug = 0;
1544     Int_t b[4];
1545     Int_t etime,evno;
1546     Float_t sdexy[2][22][96],sdexyc[2][22][96];
1547     Float_t edelta[2][22][96];
1548     Float_t estrip[2][22][96];
1549     Float_t estripnull[2][22][96];
1550     Float_t delta;
1551     delta = 1.;
1552     evento.emin = 0.7;
1553     Int_t se =5;
1554     Int_t pre;
1555     Int_t rdone = 0;
1556     Int_t done;
1557     bool isCOMP = 0;
1558     bool isFULL = 0;
1559     bool isRAW = 0;
1560     //
1561     // the file which contains the calorimeter ADC to MIP conversion values is file2 = path to the script dir / CaloADC2MIP.root
1562     //
1563     stringstream file5;
1564     file5.str("");
1565     file5 << OutDir.Data() << "/CaloADC2MIPdata.raw";
1566     //
1567     Int_t nfile = 0;
1568     Int_t fileup = 0;
1569     if ( flist != "" ){
1570     printf("\n You have given an input file list \n\n Files: \n");
1571     ifstream in;
1572     in.open(flist, ios::in);
1573     char ctime[20];
1574     while (1) {
1575     in >> ctime;
1576     if (!in.good()) break;
1577     printf(" File number %i = %s \n",nfile+1,ctime);
1578     nfile++;
1579     };
1580     in.close();
1581     if ( !nfile ) {
1582     printf("\n\n The list is empty, exiting... \n\n");
1583     return;
1584     };
1585     } else {
1586     nfile = 1;
1587     };
1588     printf("\n");
1589     //
1590     // First check if the calibration file exists:
1591     //
1592     //
1593     const char *filename2 = Filename;
1594     //
1595     TString lfile;
1596     TString sfile;
1597     //
1598     Int_t e = 0;
1599     ifstream in;
1600     in.open(flist, ios::in);
1601     char ctime[20];
1602     TString file2f;
1603     TString pfile;
1604     const char *cali;
1605     const char *ffile;
1606     Int_t calibex = 0;
1607     //
1608     TString filename;
1609     const char *nome = 0;
1610     Long64_t nevents;
1611     TFile *headerFile = 0;
1612     file2f = "";
1613     pfile = "";
1614     const char *porca = 0;
1615     Int_t upnn;
1616     //
1617     //
1618     TFile *hfl1 = 0;
1619     hfl1 = new TFile(file5.str().c_str(),"RECREATE","Calorimeter LEVEL1 data");
1620     //
1621     // class CalorimeterLevel1
1622     //
1623     CalorimeterADCRAW *clev1 = new CalorimeterADCRAW();
1624     TTree *trl1 = 0;
1625     trl1 = new TTree("CaloADCRAW","PAMELA Level1 calorimeter data");
1626     trl1->Branch("Rawdata","CalorimeterADCRAW",&clev1);
1627     //
1628     Int_t glbc = 0;
1629     //
1630     TString fififile;
1631     const char *file;
1632     //
1633     stringstream lfillo;
1634     while ( e < nfile ){
1635     if ( flist != "" ){
1636     in >> ctime;
1637     lfillo.str("");
1638     lfillo << filename2 << "/";
1639     lfillo << ctime;
1640     lfile = TString(lfillo.str().c_str());
1641     fififile = getFilename(lfile);
1642     file = fififile;
1643     lfillo.str("");
1644     lfillo << file;
1645     sfile = TString(lfillo.str().c_str());
1646     } else {
1647     lfillo.str("");
1648     lfillo << filename2;
1649     lfile = TString(lfillo.str().c_str());
1650     fififile = getFilename(lfile);
1651     file = fififile;
1652     lfillo.str("");
1653     lfillo << file;
1654     sfile = TString(lfillo.str().c_str());
1655     };
1656     //
1657     filename = lfile;
1658     nome = filename;
1659     printf("\n Processing file number %i: %s \n\n",e,nome);
1660     nome = sfile;
1661     //
1662     // Check if the input calibration file contains any calibration data
1663     //
1664     TTree *otr;
1665     pamela::calorimeter::CalorimeterEvent *de = 0;
1666     pamela::PscuHeader *ph = 0;
1667     pamela::EventHeader *eh = 0;
1668     //
1669     done = 0;
1670     printf(" Check for calorimeter calibration: \n");
1671     calcalib = filename;
1672     findcalibs: const char *calname = calcalib;
1673     printf(" Calibration file: %s \n",calname);
1674     for (Int_t s=0; s<4;s++){
1675     for (Int_t d = 0; d<50; d++){
1676     calib.ttime[s][d] = 0 ;
1677     if ( d < 49 ) calib.time[s][d] = 0 ;
1678     };
1679     };
1680     for (Int_t m = 0; m < 2 ; m++ ){
1681     for (Int_t k = 0; k < 22; k++ ){
1682     for (Int_t l = 0; l < 96; l++ ){
1683     calib.calped[m][k][l] = 0. ;
1684     evento.dexy[m][k][l] = 0. ;
1685     evento.dexyc[m][k][l] = 0. ;
1686     edelta[m][k][l] = 0.;
1687     estrip[m][k][l] = 0.;
1688     estripnull[m][k][l] = 0.;
1689     };
1690     };
1691     }
1692     Int_t wused = 0;
1693     CaloFindCalibs(filename, calcalibfile, wused, calib);
1694     if ( wused == 1 ) calcalibfile = filename;
1695     //
1696     // print on the screen the results:
1697     //
1698     ffile = filename;
1699     printf(" ------ %s ------- \n \n",ffile);
1700     calibex = 0;
1701     for (Int_t s=0; s<4;s++){
1702     printf(" ** SECTION %i **\n",s);
1703     for (Int_t d = 0; d<51; d++){
1704     if ( calib.ttime[s][d] != 0 ) {
1705     calibex++;
1706     if ( calib.fcode[s][d] != 10 ){
1707     file2f = "";
1708     stringcopy(file2f,calcalib,0,0);
1709     pfile = (TString)whatnamewith(file2f,calib.fcode[s][d]);
1710     } else {
1711     pfile = (TString)calcalib;
1712     };
1713     ffile = pfile;
1714     printf(" - from time %i to time %i use calibration at\n time %i, file: %s \n",calib.time[s][d],calib.time[s][d+1],calib.ttime[s][d],ffile);
1715     };
1716     };
1717     printf("\n");
1718     };
1719     printf(" ----------------------------------------------------------------------- \n \n");
1720     //
1721     if ( calibex < 1 ) {
1722     printf("No calibration data in this file!\n");
1723     calcalib = calcalibfile;
1724     if ( !done && calcalibfile != "" ) {
1725     cali = calcalibfile;
1726     printf(" Search in the calibration file %s \n",cali);
1727     done = 1;
1728     goto findcalibs;
1729     } else {
1730     printf(" Cannot find any calibration for this file! \n");
1731     goto jumpfile;
1732     };
1733     };
1734     if ( calibex < 4 ) {
1735     printf(" WARNING! No full calibration data in this file! \n\n");
1736     };
1737     //
1738     // upgrade data...
1739     //
1740     if ( !existfile(filename) ){
1741     printf(" %s :no such file or directory \n",filename.Data());
1742     goto jumpfile;
1743     };
1744     headerFile = new TFile(filename.Data());
1745     otr = (TTree*)headerFile->Get("Physics");
1746     otr->SetBranchAddress("Header", &eh);
1747     otr->SetBranchAddress("Calorimeter", &de);
1748     //
1749     nevents = otr->GetEntries();
1750     //
1751     // calibrate before starting
1752     //
1753     for (Int_t s = 0; s < 4; s++){
1754     b[s]=0;
1755     if ( calib.fcode[s][b[s]] != 10 ){
1756     stringcopy(file2f,calcalib,0,0);
1757     pfile = (TString)whatnamewith(file2f,calib.fcode[s][b[s]]);
1758     } else {
1759     pfile = (TString)calcalib;
1760     };
1761     porca = pfile;
1762     if ( debug ) printf(" porca miseria %s \n",porca);
1763     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
1764     };
1765     //
1766     // run over all the events
1767     //
1768     printf("\n Processed events: \n\n");
1769     //
1770     Int_t updata;
1771     for (Int_t i = 0; i < nevents; i++){
1772     evno = i;
1773     if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);
1774     //
1775     // read from the header of the event the absolute time at which it was recorded
1776     //
1777     otr->GetEntry(i);
1778     //
1779     //
1780     ph = eh->GetPscuHeader();
1781     etime = ph->GetOrbitalTime();
1782     //
1783     // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration
1784     //
1785     if ( !calib.obtjump ) {
1786     for (Int_t s = 0; s < 4; s++){
1787     if ( calib.ttime[s][b[s]] ){
1788     while ( etime > calib.time[s][b[s]+1] ){
1789     printf(" CALORIMETER: \n" );
1790     printf(" - Section %i, event at time %i while old calibration time limit at %i. Use new calibration at time %i -\n",s,etime,calib.time[s][b[s]+1],calib.ttime[s][b[s]+1]);
1791     printf(" END CALORIMETER. \n\n" );
1792     b[s]++;
1793     if ( calib.fcode[s][b[s]] != 10 ){
1794     stringcopy(file2f,calcalib,0,0);
1795     pfile = (TString)whatnamewith(file2f,calib.fcode[s][b[s]]);
1796     } else {
1797     pfile = (TString)calcalib;
1798     };
1799     porca = pfile;
1800     if ( debug ) printf(" porca miseria %s \n",porca);
1801     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
1802     };
1803     };
1804     };
1805     };
1806     //
1807     // do whatever you want with data
1808     //
1809     evento.iev = de->iev;
1810     //
1811     // run over views and planes
1812     //
1813     updata = 0;
1814     glbc++;
1815     for (Int_t l = 0; l < 2; l++){
1816     for (Int_t m = 0; m < 22; m++){
1817     //
1818     // determine the section number
1819     //
1820     if (l == 0 && m%2 == 0) se = 3;
1821     if (l == 0 && m%2 != 0) se = 2;
1822     if (l == 1 && m%2 == 0) se = 0;
1823     if (l == 1 && m%2 != 0) se = 1;
1824     //
1825     // determine what kind of event we are going to analyze
1826     //
1827     isCOMP = 0;
1828     isFULL = 0;
1829     isRAW = 0;
1830     if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;
1831     if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;
1832     if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;
1833     //
1834     // save the prevoius energy deposit and calibration in sbase, sdexy, sdexyc
1835     //
1836     pre = -1;
1837     if ( isRAW || isFULL ){
1838     updata = 1;
1839     for (Int_t nn = 0; nn < 96; nn++){
1840     if ( nn%16 == 0 ) pre++;
1841     evento.base[l][m][pre] = calib.calbase[l][m][pre];
1842     sdexy[l][m][nn] = evento.dexy[l][m][nn];
1843     evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;
1844     sdexyc[l][m][nn] = evento.dexyc[l][m][nn];
1845     // !!!
1846     evento.dexyc[l][m][nn] = de->dexy[l][m][nn] ;
1847     // !!!
1848     };
1849     //
1850     // run over strips
1851     //
1852     pre = -1;
1853     for (Int_t n =0 ; n < 96; n++){
1854     if ( n%16 == 0 ) {
1855     pre++;
1856     rdone = 0;
1857     done = 0;
1858     };
1859     if ( isRAW || ( de->dexyc[l][m][n] > 0. && de->dexyc[l][m][n] < 32000.) || isFULL ){
1860     if ( isFULL && !rdone ) rdone = 1;
1861     //
1862     // baseline check and calculation
1863     //
1864     if ( !rdone ){
1865     CaloFindBaseRawNC(calib,evento,l,m,pre);
1866     rdone = 1;
1867     };
1868     //
1869     // no suitable new baseline, use old ones and compress data!
1870     //
1871     if ( !done && (evento.base[l][m][pre] > 30000. || evento.base[l][m][pre] == 0.) ){
1872     evento.base[l][m][pre] = calib.sbase[l][m][pre];
1873     upnn = n+16;
1874     if ( upnn > 96 ) upnn = 96;
1875     for ( Int_t nn = n; nn<upnn; nn++ ){
1876     evento.dexyc[l][m][nn] = de->dexy[l][m][nn] ;
1877     };
1878     done = 1;
1879     };
1880     //
1881     // CALIBRATION ALGORITHM
1882     //
1883     estrip[l][m][n] = ( evento.dexyc[l][m][n] - calib.calped[l][m][n] - evento.base[l][m][pre]) ;
1884     calib.sbase[l][m][pre] = evento.base[l][m][pre];
1885     clev1->diffbas[l][m][pre] = evento.base[l][m][pre];
1886     clev1->estrip[l][m][n] = estrip[l][m][n];
1887     };
1888     };
1889     };
1890     };
1891     };
1892     // if ( updata ){
1893     clev1->evno = glbc;
1894     clev1->etime = etime;
1895     trl1->Fill();
1896     for (Int_t l =0 ; l < 2; l++){
1897     for (Int_t m =0 ; m < 22; m++){
1898     pre = -1;
1899     for (Int_t n =0 ; n < 96; n++){
1900     // memcpy(estrip, estripnull, sizeof(estrip));
1901     if ( n%16 == 0 ) pre++;
1902     estrip[l][m][n] = 0.;
1903     evento.base[l][m][pre] = 0.;
1904     clev1->diffbas[l][m][pre] = evento.base[l][m][pre];
1905     clev1->estrip[l][m][n] = estrip[l][m][n];
1906     };
1907     };
1908     };
1909     //gObjectTable->Print();
1910     //};
1911     };
1912     //
1913     // save filename
1914     //
1915     fileup++;
1916     //
1917     headerFile->Close();
1918     goto oltre;
1919     jumpfile: printf(" File not processed! \n ");
1920     oltre:
1921     e++;
1922     };
1923     hfl1->Write();
1924     hfl1->Close();
1925     }
1926    
1927     void FCalo4224STATUS(TString filename = "", TString style = ""){
1928     const char *pam_calib = pathtocalibration();
1929     //
1930     // this routine shows the calorimeter calibration status with a figure. You can choose style between "error" (or "") to show the error on the
1931     // peak maximum determined and "status" to show which strips have converged to a certain value.
1932     //
1933     if ( filename == "" ){
1934     stringstream filenome;
1935     filenome.str("");
1936     filenome << pam_calib << "/CaloADC2MIP.root";
1937     filename = (TString)filenome.str().c_str();
1938     };
1939     if ( style == "" ) style = "error";
1940     const char *file2 = filename;
1941     TFile *hfile;
1942     hfile = new TFile(file2,"READ","Calorimeter CALIBRATION data");
1943     CalorimeterCalibration *calo = 0;
1944     TTree *tree;
1945     tree = (TTree*)hfile->Get("CaloADC");
1946     tree->SetBranchAddress("Event", &calo);
1947     //
1948     Long64_t nevents = tree->GetEntries();
1949     tree->GetEntry(nevents-1);
1950     //
1951     // Book the histograms:
1952     //
1953     //
1954     TH2F *Xview = new TH2F("x-view event ","",96,-0.5,95.5,22,-0.5,21.5);
1955     TH2F *Yview = new TH2F("y-view event ","",96,-0.5,95.5,22,-0.5,21.5);
1956     //
1957     // figures:
1958     //
1959     TCanvas *figura2 = new TCanvas("Calorimeter:_strip_RMS", "Calorimeter:_strip_RMS", 750, 650);
1960     figura2->SetFillColor(10);
1961     figura2->Range(0,0,100,100);
1962     Int_t bgcolor = 10;
1963     TPad *pd1 = new TPad("pd1","This is pad1",0.02,0.05,0.88,0.49,bgcolor);
1964     TPad *pd2 = new TPad("pd2","This is pad2",0.02,0.51,0.88,0.95,bgcolor);
1965     TPad *palette = new TPad("palette","This is palette",0.90,0.05,0.98,0.90,bgcolor);
1966     figura2->cd();
1967     TLatex *t=new TLatex();
1968     t->SetTextFont(32);
1969     t->SetTextColor(1);
1970     t->SetTextSize(0.03);
1971     t->SetTextAlign(12);
1972     t->DrawLatex(90.,92.5,"error");
1973     pd1->Range(0,0,100,100);
1974     pd2->Range(0,0,100,100);
1975     palette->Range(0,0,101,100);
1976     pd1->SetTicks();
1977     pd2->SetTicks();
1978     pd1->Draw();
1979     pd2->Draw();
1980     palette->Draw();
1981     palette->cd();
1982     const char *style2 = style;
1983     if ( !strcmp(style2,"error") ){
1984     // palette
1985     TPaveLabel *box1 = new TPaveLabel(1,2,99,14,"no fit","");
1986     box1->SetTextFont(32);
1987     box1->SetTextColor(1);
1988     box1->SetTextSize(0.25);
1989     box1->SetFillColor(10);
1990     box1->Draw();
1991     TPaveLabel *box2 = new TPaveLabel(1,16,99,28,"< 0.15","");
1992     box2->SetTextFont(32);
1993     box2->SetTextColor(1);
1994     box2->SetTextSize(0.25);
1995     box2->SetFillColor(38);
1996     box2->Draw();
1997     TPaveLabel *box3 = new TPaveLabel(1,30,99,42,"0.15 - 0.25","");
1998     box3->SetTextFont(32);
1999     box3->SetTextColor(1);
2000     box3->SetTextSize(0.2);
2001     box3->SetFillColor(4);
2002     box3->Draw();
2003     TPaveLabel *box4 = new TPaveLabel(1,44,99,56,"0.25 - 0.55","");
2004     box4->SetTextFont(32);
2005     box4->SetTextColor(1);
2006     box4->SetTextSize(0.2);
2007     box4->SetFillColor(3);
2008     box4->Draw();
2009     TPaveLabel *box5 = new TPaveLabel(1,58,99,70,"0.55 - 1","");
2010     box5->SetTextFont(32);
2011     box5->SetTextColor(1);
2012     box5->SetTextSize(0.25);
2013     box5->SetFillColor(2);
2014     box5->Draw();
2015     TPaveLabel *box6 = new TPaveLabel(1,72,99,84,"1 - 5","");
2016     box6->SetTextFont(32);
2017     box6->SetTextColor(1);
2018     box6->SetTextSize(0.25);
2019     box6->SetFillColor(6);
2020     box6->Draw();
2021     TPaveLabel *box7 = new TPaveLabel(1,86,99,98,"> 5","");
2022     box7->SetTextFont(32);
2023     box7->SetTextColor(10);
2024     box7->SetTextSize(0.25);
2025     box7->SetFillColor(1);
2026     box7->Draw();
2027     } else {
2028     TPaveLabel *box1 = new TPaveLabel(1,2,99,14,"not done","");
2029     box1->SetTextFont(32);
2030     box1->SetTextColor(1);
2031     box1->SetTextSize(0.25);
2032     box1->SetFillColor(10);
2033     box1->Draw();
2034     TPaveLabel *box2 = new TPaveLabel(1,16,99,28,"done","");
2035     box2->SetTextFont(32);
2036     box2->SetTextColor(1);
2037     box2->SetTextSize(0.25);
2038     box2->SetFillColor(38);
2039     box2->Draw();
2040     };
2041     //
2042     pd1->cd();
2043     gStyle->SetOptStat(0);
2044     Xview->SetXTitle("strip");
2045     Xview->SetYTitle("X - plane");
2046     Xview->GetYaxis()->SetTitleOffset(0.5);
2047     Xview->SetFillColor(bgcolor);
2048     Xview->Fill(1.,1.,1.);
2049     Xview->Draw("box");
2050     pd1->Update();
2051     pd2->cd();
2052     gStyle->SetOptStat(0);
2053     Yview->SetXTitle("strip");
2054     Yview->SetYTitle("Y - plane");
2055     Yview->GetYaxis()->SetTitleOffset(0.5);
2056     Yview->SetFillColor(bgcolor);
2057     Yview->Fill(1.,1.,1.);
2058     Yview->Draw("box");
2059     pd2->Update();
2060     //
2061     // run over views and planes
2062     //
2063     stringstream xview;
2064     stringstream yview;
2065     for (Int_t m = 0; m < 22; m++){
2066     for (Int_t l = 0; l < 2; l++){
2067     for (Int_t n = 0; n < 96; n++){
2068     xview.str("");
2069     xview << "x-view event " << n;
2070     xview << " " << m;
2071     xview << " " << l;
2072     yview.str("");
2073     yview << "y-view event " << n;
2074     yview << " " << m;
2075     yview << " " << l;
2076     gDirectory->Delete(xview.str().c_str());
2077     gDirectory->Delete(yview.str().c_str());
2078     TH2F *Xview = new TH2F(xview.str().c_str(),"",96,-0.5,95.5,22,-0.5,21.5);
2079     TH2F *Yview = new TH2F(yview.str().c_str(),"",96,-0.5,95.5,22,-0.5,21.5);
2080     if ( !strcmp(style2,"error") ){
2081     if ( calo->fpe[1][l][m][n] < 0.15 ){
2082     Xview->SetFillColor(38);
2083     Yview->SetFillColor(38);
2084     };
2085     if ( calo->fpe[1][l][m][n] >= 0.15 ){
2086     Xview->SetFillColor(4);
2087     Yview->SetFillColor(4);
2088     };
2089     if ( calo->fpe[1][l][m][n] >= 0.25){
2090     Xview->SetFillColor(3);
2091     Yview->SetFillColor(3);
2092     };
2093     if ( calo->fpe[1][l][m][n] >= 0.55 ){
2094     Xview->SetFillColor(2);
2095     Yview->SetFillColor(2);
2096     };
2097     if ( calo->fpe[1][l][m][n] >= 1. ){
2098     Xview->SetFillColor(6);
2099     Yview->SetFillColor(6);
2100     };
2101     if ( calo->fpe[1][l][m][n] >= 5. ){
2102     Xview->SetFillColor(1);
2103     Yview->SetFillColor(1);
2104     };
2105     if ( calo->mip[l][m][n] < 15. || calo->mip[l][m][n] > 40. ){
2106     Xview->SetFillColor(10);
2107     Yview->SetFillColor(10);
2108     };
2109     } else {
2110     if ( calo->mask[l][m][n] == 1 ){
2111     Xview->SetFillColor(38);
2112     Yview->SetFillColor(38);
2113     } else {
2114     Xview->SetFillColor(10);
2115     Yview->SetFillColor(10);
2116     };
2117     };
2118     if ( l == 0 ) {
2119     Xview->Fill(n,m,1.);
2120     pd1->cd();
2121     Xview->Draw("box same");
2122     };
2123     if ( l == 1 ) {
2124     Yview->Fill(n,m,1.);
2125     pd2->cd();
2126     Yview->Draw("box same");
2127     };
2128     };
2129     };
2130     };
2131     figura2->cd();
2132     gStyle->SetOptDate(1);
2133     //
2134     t=new TLatex();
2135     t->SetTextFont(32);
2136     t->SetTextColor(8);
2137     t->SetTextAlign(12);
2138     t->SetTextSize(0.02);
2139     figura2->Update();
2140     pd1->Update();
2141     pd2->Update();
2142     //
2143     }
2144    
2145     void FCalo4224FIT(TString filename = "", TString OutDir="", TString filevalue = "", TString type=""){
2146     const char *pam_calib = pathtocalibration();
2147     if ( !strcmp(OutDir.Data(),"") ){
2148     OutDir=pam_calib;
2149     };
2150     //
2151     // this routine shows the 4224 figures with fit from the main program
2152     //
2153     const char* startingdir = gSystem->WorkingDirectory();
2154     stringstream filenome;
2155     filenome.str("");
2156     if ( filename == "" ){
2157     filenome << OutDir.Data() << "/CaloADC2MIPf.root";
2158     filename = (TString)filenome.str().c_str();
2159     filenome.str("");
2160     };
2161     if ( filevalue == "" ){
2162     filenome.str("");
2163     filenome << OutDir.Data() << "/CaloADC2MIP.root";
2164     } else {
2165     filenome << filevalue.Data();
2166     };
2167     // EM
2168     const char *file2;
2169     file2 = filenome.str().c_str();
2170     TFile *hfile = 0;
2171     hfile = new TFile(file2,"UPDATE","Calorimeter CALIBRATION data");
2172     CalorimeterCalibration *calo = 0;
2173     TTree *tree;
2174     tree = (TTree*)hfile->Get("CaloADC");
2175     tree->SetBranchAddress("Event", &calo);
2176     Long64_t nevents = tree->GetEntries();
2177     tree->GetEntry(nevents-1);
2178     // EM end
2179     //
2180     const char *file = filename;
2181     stringstream file3;
2182     file3.str("");
2183     // file3 << OutDir.Data() << "/";
2184     file3 << file;
2185     TFile hfile3(file3.str().c_str());
2186     Int_t l = 0;
2187     TCanvas *c1;
2188     Int_t ty = 1;
2189     if ( type != "" ) ty = 0;
2190     c1 = new TCanvas("canvas","canvas",800,800);
2191     c1->Draw();
2192     gStyle->SetOptFit(111);
2193     gPad->SetLogy();
2194     gPad->SetTickx();
2195     gPad->SetTicky();
2196     gPad->SetGrid();
2197     Int_t j = -1;
2198     Int_t k = -1;
2199     Int_t h = -1;;
2200     Int_t changed = 0;
2201     beginning:
2202     if ( j >= 0 ) l = j;
2203     stringstream fmippa;
2204     TF1 *tempf = 0;
2205     TH1F *temp;
2206     while ( l < 2){
2207     Int_t m = 0;
2208     if ( k >= 0 ) m = k;
2209     while ( m < 22 ){
2210     Int_t n =0 ;
2211     if ( h >= 0 ) n = h;
2212     while ( n < 96 ){
2213     c1->Clear();
2214     c1->cd();
2215     fmippa.str("");
2216     fmippa << "fmip " << l;
2217     fmippa << " " << m;
2218     fmippa << " " << n;
2219     temp = (TH1F*)hfile3.Get(fmippa.str().c_str());
2220     if ( ty ) {
2221     fmippa.str("");
2222     fmippa << "fit " << l;
2223     fmippa << " " << m;
2224     fmippa << " " << n;
2225     tempf = (TF1*)hfile3.Get(fmippa.str().c_str());
2226     };
2227     j = -1;
2228     k = -1;
2229     h = -1;
2230     temp->DrawCopy();
2231     if ( ty ) tempf->DrawCopy("lsame");
2232     c1->Update();
2233     //
2234     //
2235     //
2236     printf(" **** STRIP: %i %i %i **** \n",l,m,n);
2237     printf(" MIP: %f \n",calo->mip[l][m][n]);
2238     printf(" ERMIP: %f \n",calo->ermip[l][m][n]);
2239     printf(" FP[0-3]: %f / %f / %f / %f \n",calo->fp[0][l][m][n],calo->fp[1][l][m][n],calo->fp[2][l][m][n],calo->fp[3][l][m][n]);
2240     printf(" FPE[0-3]: %f / %f / %f / %f \n",calo->fpe[0][l][m][n],calo->fpe[1][l][m][n],calo->fpe[2][l][m][n],calo->fpe[3][l][m][n]);
2241     printf(" CHI2: %f \n",calo->chi2[l][m][n]);
2242     printf(" NDF: %f \n",calo->ndf[l][m][n]);
2243     printf(" MASK: %f \n",calo->mask[l][m][n]);
2244     //
2245     // what to do?
2246     //
2247     char tellme[256];
2248     char tellme2[256];
2249     stringstream input;
2250     stringstream input2;
2251     stringstream out;
2252     stringstream stc;
2253     input.str("a");
2254     out.str("a");
2255     while ( !strcmp(input.str().c_str(),out.str().c_str()) ) {
2256     printf("\nPress <enter> to continue, b<enter> to go backward, j<enter> to jump to a\nfigure, p<enter> to save the figure, f<enter> to do the fit, q<enter> to quit: \n");
2257     cin.getline(tellme,256);
2258     input.str("");
2259     input << tellme;
2260     out.str("1");
2261     //
2262     stc.str("f");
2263     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2264     printf("\n Do you want to give a number by hand, h<enter>, or do you want to try a better fit, f<enter>?");
2265     cin.getline(tellme,256);
2266     input.str("");
2267     input << tellme;
2268     out.str("f");
2269     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2270     Double_t fr[2],vtemp;
2271     Double_t sv[4], pllo[4], plhi[4], ffp[4], ffpe[4];
2272     for ( Int_t fk = 0; fk < 4; fk++){
2273     sv[fk] = (int)calo->fp[fk][l][m][n];
2274     printf(" Give input parameter [fp[%i]=%f] \n",fk,sv[fk]);
2275     cin.getline(tellme2,256);
2276     input2.str("");
2277     input2 << tellme2;
2278     vtemp = (Double_t)atoi(input2.str().c_str());
2279     if ( vtemp != 0. ) sv[fk] = vtemp;
2280     printf(" -> fp[%i] = %f \n\n",fk,sv[fk]);
2281     };
2282     printf(" Give lower limit of fitting range fr[0] \n");
2283     cin.getline(tellme2,256);
2284     input2.str("");
2285     input2 << tellme2;
2286     fr[0] = (Double_t)atoi(input2.str().c_str());
2287     printf(" -> fr[0] = %f \n\n",fr[0]);
2288     printf(" Give upper limit of fitting range fr[1] \n");
2289     cin.getline(tellme2,256);
2290     input2.str("");
2291     input2 << tellme2;
2292     fr[1] = (Double_t)atoi(input2.str().c_str());
2293     printf(" -> fr[1] = %f \n",fr[1]);
2294     pllo[0]=0.5; pllo[1]=5.0; pllo[2]=1.0; pllo[3]=0.2;
2295     plhi[0]=8.0; plhi[1]=50.0; plhi[2]=1000000.0; plhi[3]=8.0;
2296     Double_t chisqr;
2297     Int_t fndf;
2298     //
2299     // fit the figure pfmip and put fit results in pfitsnr
2300     //
2301     tempf = langaufit(temp,fr,sv,pllo,plhi,ffp,ffpe,&chisqr,&fndf,"R0");
2302     Double_t SNRPeak = langaupro(ffp);
2303     printf("\n Conversion factor: %f \n",SNRPeak);
2304     tempf->DrawCopy("lsame");
2305     c1->Update();
2306     c1->Modified();
2307     c1->Update();
2308     printf(" Do you want to save this result y/n<enter> ?\n");
2309     cin.getline(tellme,256);
2310     input.str("");
2311     input << tellme;
2312     out.str("y");
2313     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2314     calo->mip[l][m][n] = (float)SNRPeak;
2315     calo->ermip[l][m][n] = (float)ffpe[1];
2316     for (Int_t a = 0; a < 4 ; a++){
2317     calo->fp[a][l][m][n] = (float)ffp[a];
2318     calo->fpe[a][l][m][n] = (float)ffpe[a];
2319     };
2320     calo->ndf[l][m][n] = (float)fndf;
2321     calo->chi2[l][m][n] = (float)chisqr;
2322     //
2323     if ( fndf!=0 && (float)chisqr/(float)fndf < 2. && ffpe[1] < 0.15 && ffp[1] > 15. && ffp[1] < 40. ) {
2324     calo->mask[l][m][n] = 1.;
2325     } else {
2326     calo->mask[l][m][n] = 0.;
2327     };
2328     printf(" Saved! \n");
2329     changed = 1;
2330     };
2331     } else {
2332     printf(" Give the MIP conversion value \n");
2333     cin.getline(tellme2,256);
2334     input2.str("");
2335     input2 << tellme2;
2336     calo->mip[l][m][n] = (Float_t)atoi(input2.str().c_str());
2337     printf(" -> mip = %f \n",calo->mip[l][m][n]);
2338     printf(" Give the mask value \n");
2339     cin.getline(tellme2,256);
2340     input2.str("");
2341     input2 << tellme2;
2342     calo->mask[l][m][n] = (Float_t)atoi(input2.str().c_str());
2343     printf(" -> mask = %f \n",calo->mask[l][m][n]);
2344     changed = 1;
2345     };
2346     out.str("");
2347     out << input.str().c_str();
2348     };
2349     stc.str("b");
2350     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2351     if ( n > 0 ) {
2352     printf("WARNING: going one figure backward!\n\n");
2353     n -= 2;
2354     } else {
2355     printf("This is the first strip of plane, you can't go backward! \n");
2356     out.str("");
2357     out << input.str().c_str();
2358     };
2359     };
2360     stc.str("j");
2361     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2362     printf("\n Enter the view number you want to jump to (0 = x, 1 = y): ");
2363     cin.getline(tellme2,256);
2364     input2.str("");
2365     input2 << tellme2;
2366     j = atoi(input2.str().c_str());
2367     if ( j < 0 || j > 1 ) {
2368     printf("\n You can choose between 0 and 1 \n");
2369     out.str("");
2370     out << input.str().c_str();
2371     } else {
2372     printf("\n Enter the plane number you want to jump to (0 to 21): ");
2373     cin.getline(tellme2,256);
2374     input2.str("");
2375     input2 << tellme2;
2376     k = atoi(input2.str().c_str());
2377     if ( k < 0 || k > 21 ) {
2378     printf("\n You can choose between 0 and 21 \n");
2379     out.str("");
2380     out << input.str().c_str();
2381     } else {
2382     printf("\n Enter the strip number you want to jump to (0 to 95): ");
2383     cin.getline(tellme2,256);
2384     input2.str("");
2385     input2 << tellme2;
2386     h = atoi(input2.str().c_str());
2387     if ( h < 0 || h > 95 ) {
2388     printf("\n You can choose between 0 and 95 \n");
2389     out.str("");
2390     out << input.str().c_str();
2391     } else {
2392     printf("\n Jumping to strip %i %i %i \n\n",j,k,h);
2393     goto beginning;
2394     };
2395     };
2396     };
2397     };
2398     //
2399     stc.str("q");
2400     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2401     printf("Exiting...\n");
2402     goto end;
2403     };
2404     //
2405     stc.str("p");
2406     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2407     printf("Enter a file extension recognized by ROOT (ps, eps, gif,...):\n");
2408     cin.getline(tellme2,256);
2409     input2.str("");
2410     input2 << tellme2;
2411     out.str("");
2412     out << input.str().c_str();
2413     stringstream figsave;
2414     figsave.str("");
2415     figsave << startingdir << "/mip2adc_";
2416     figsave << l << "_";
2417     figsave << m << "_";
2418     figsave << n << ".";
2419     figsave << input2.str().c_str();
2420     c1->SaveAs(figsave.str().c_str());
2421     printf("\n");
2422     };
2423     };
2424     n++;
2425     };
2426     m++;
2427     };
2428     l++;
2429     };
2430     end: printf("\n");
2431     if ( changed ){
2432     tree->Fill();
2433     hfile->Write();
2434     };
2435     hfile->Close();
2436     }
2437    
2438     void FCalo4224MIPVALUES(TString filevalue = "", TString type=""){
2439     const char* startingdir = gSystem->WorkingDirectory();
2440     const char *pam_calib = pathtocalibration();
2441     //
2442     // this routine shows the 4224 figures with fit from the main program
2443     //
2444     stringstream filenome;
2445     filenome.str("");
2446     if ( filevalue == "" ){
2447     filenome << pam_calib << "/CaloADC2MIP.root";
2448     } else {
2449     filenome << filevalue.Data();
2450     };
2451     // EM
2452     const char *file2;
2453     file2 = filenome.str().c_str();
2454     TFile *hfile = 0;
2455     hfile = new TFile(file2,"READ","Calorimeter CALIBRATION data");
2456     CalorimeterCalibration *calo = 0;
2457     TTree *tree;
2458     tree = (TTree*)hfile->Get("CaloADC");
2459     tree->SetBranchAddress("Event", &calo);
2460     Long64_t nevents = tree->GetEntries();
2461     tree->GetEntry(nevents-1);
2462     // EM end
2463     //
2464     TCanvas *c1;
2465     Int_t ty = 1;
2466     if ( type != "" ) ty = 0;
2467     c1 = new TCanvas("canvas","canvas",1200,800);
2468     c1->Draw();
2469     gStyle->SetOptFit(111);
2470     gPad->SetTickx();
2471     gPad->SetTicky();
2472     Int_t k = -1;
2473     Int_t changed = 0;
2474     Float_t mipf[1]={0};
2475     Float_t val[1]={0};
2476     TLatex *text=new TLatex();
2477     TLine *linea;
2478     stringstream testo;
2479     beginning:
2480     Int_t m = 0;
2481     if ( k >= 0 ) m = k;
2482     while ( m < 22 ){
2483     Int_t l = 0;
2484     c1->Clear();
2485     c1->cd();
2486     TMultiGraph *mg = new TMultiGraph();
2487     while ( l < 2){
2488     Int_t n = 0;
2489     while ( n < 96 ){
2490     val[0] = (float)n + 100.*(float)l ;
2491     mipf[0] = (float)calo->mip[l][m][n];
2492     TGraph *graph = new TGraph(1, val, mipf);
2493     graph->SetMarkerColor(2);
2494     graph->SetMarkerStyle(22+l);
2495     graph->SetMarkerSize(0.7);
2496     mg->Add(graph);
2497     printf(" **** STRIP: %i %i %i **** \n",l,m,n);
2498     printf(" MIP: %f \n",calo->mip[l][m][n]);
2499     printf(" ERMIP: %f \n",calo->ermip[l][m][n]);
2500     printf(" FP[0-3]: %f / %f / %f / %f \n",calo->fp[0][l][m][n],calo->fp[1][l][m][n],calo->fp[2][l][m][n],calo->fp[3][l][m][n]);
2501     printf(" FPE[0-3]: %f / %f / %f / %f \n",calo->fpe[0][l][m][n],calo->fpe[1][l][m][n],calo->fpe[2][l][m][n],calo->fpe[3][l][m][n]);
2502     printf(" CHI2: %f \n",calo->chi2[l][m][n]);
2503     printf(" NDF: %f \n",calo->ndf[l][m][n]);
2504     printf(" MASK: %f \n",calo->mask[l][m][n]);
2505     n++;
2506     };
2507     l++;
2508     };
2509     mg->SetMaximum(50.);
2510     mg->SetMinimum(0.);
2511     mg->Draw("AP");
2512     for (Int_t e = 0; e<2 ; e++){
2513     for (Int_t ep = 0; ep<96 ; ep++){
2514     if ( ep%16 == 0 ) {
2515     linea = new TLine((float)ep+100.*(float)e,0.,(float)ep+100.*(float)e,50.);
2516     linea->SetLineColor(15);
2517     linea->SetLineStyle(2);
2518     linea->SetLineWidth(1);
2519     linea->Draw("lsame");
2520     if ( ep > 0 ){
2521     linea = new TLine((float)ep-1.+100.*(float)e,0.,(float)ep-1.+100.*(float)e,50.);
2522     linea->SetLineColor(15);
2523     linea->SetLineStyle(2);
2524     linea->SetLineWidth(1);
2525     linea->Draw("lsame");
2526     };
2527     };
2528     if ( ep == 95 ){
2529     linea = new TLine(95.+100.*(float)e,0.,95.+100.*(float)e,50.);
2530     linea->SetLineColor(15);
2531     linea->SetLineStyle(2);
2532     linea->SetLineWidth(1);
2533     linea->Draw("lsame");
2534     };
2535     };
2536     };
2537     text->SetTextAngle(0);
2538     text->SetTextFont(32);
2539     text->SetTextColor(1);
2540     text->SetTextSize(0.050);
2541     text->SetTextAlign(12);
2542     testo.str("");
2543     testo << "Plane " << m;
2544     text->DrawLatex(85.,52.,testo.str().c_str());
2545     c1->Update();
2546     //
2547     // what to do?
2548     //
2549    
2550     char tellme[256];
2551     char tellme2[256];
2552     stringstream input;
2553     stringstream input2;
2554     stringstream out;
2555     stringstream stc;
2556     input.str("a");
2557     out.str("a");
2558     while ( !strcmp(input.str().c_str(),out.str().c_str()) ) {
2559     printf("\nPress <enter> to continue, b<enter> to go backward, j<enter> to jump to a\nfigure, p<enter> to save the figure, q<enter> to quit: \n");
2560     cin.getline(tellme,256);
2561     input.str("");
2562     input << tellme;
2563     out.str("1");
2564     //
2565     stc.str("b");
2566     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2567     if ( m > 0 ) {
2568     printf("WARNING: going one figure backward!\n\n");
2569     m -= 2;
2570     } else {
2571     printf("This is the first plane, you can't go backward! \n");
2572     out.str("");
2573     out << input.str().c_str();
2574     };
2575     };
2576     stc.str("j");
2577     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2578     printf("\n Enter the plane number you want to jump to (0 to 21): ");
2579     cin.getline(tellme2,256);
2580     input2.str("");
2581     input2 << tellme2;
2582     k = atoi(input2.str().c_str());
2583     if ( k < 0 || k > 21 ) {
2584     printf("\n You can choose between 0 and 21 \n");
2585     out.str("");
2586     out << input.str().c_str();
2587     } else {
2588     printf("\n Jumping to plane %i \n\n",k);
2589     m = k;
2590     goto beginning;
2591     };
2592     };
2593     //
2594     stc.str("q");
2595     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2596     printf("Exiting...\n");
2597     goto end;
2598     };
2599     //
2600     stc.str("p");
2601     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2602     printf("Enter a file extension recognized by ROOT (ps, eps, gif,...):\n");
2603     cin.getline(tellme2,256);
2604     input2.str("");
2605     input2 << tellme2;
2606     out.str("");
2607     out << input.str().c_str();
2608     stringstream figsave;
2609     figsave.str("");
2610     figsave << startingdir << "/mip2adc_plane_";
2611     figsave << m << ".";
2612     figsave << input2.str().c_str();
2613     c1->SaveAs(figsave.str().c_str());
2614     printf("\n");
2615     };
2616     };
2617     m++;
2618     };
2619     end: printf("\n");
2620     if ( changed ){
2621     tree->Fill();
2622     hfile->Write();
2623     };
2624     hfile->Close();
2625     }
2626    
2627     void FCalo4224BAK(TString filename){
2628     //
2629     // this shows the 4224 figure from a backup file
2630     //
2631     const char* startingdir = gSystem->WorkingDirectory();
2632     const char *file3 = filename;
2633     TFile hfile3(file3);
2634     Int_t l = 0;
2635     TCanvas *c1;
2636     c1 = new TCanvas("canvas","canvas",800,800);
2637     c1->Draw();
2638     gStyle->SetOptFit(111);
2639     gPad->SetLogy();
2640     Int_t j = 0;
2641     Int_t k = 0;
2642     Int_t h = 0;
2643     beginning:
2644     if ( j ) l = j;
2645     stringstream bmippa;
2646     while ( l < 2){
2647     Int_t m = 0;
2648     if ( k ) m = k;
2649     while ( m < 22 ){
2650     Int_t n =0 ;
2651     if ( h ) n = h;
2652     while ( n < 96 ){
2653     c1->Clear();
2654     c1->cd();
2655     bmippa.str("");
2656     bmippa << "bmip " << l;
2657     bmippa << " " << m;
2658     bmippa << " " << n;
2659     TH1F *temp = (TH1F*)hfile3.Get(bmippa.str().c_str());
2660     j = 0;
2661     k = 0;
2662     h = 0;
2663     temp->DrawCopy();
2664     c1->Update();
2665     //
2666     // what to do?
2667     //
2668     char tellme[256];
2669     char tellme2[256];
2670     stringstream input;
2671     stringstream input2;
2672     stringstream out;
2673     stringstream stc;
2674     input.str("a");
2675     out.str("a");
2676     while ( !strcmp(input.str().c_str(),out.str().c_str()) ) {
2677     printf("\nPress <enter> to continue, b<enter> to go backward, j<enter> to jump to a\nfigure, p<enter> to save the figure, q<enter> to quit: \n");
2678     cin.getline(tellme,256);
2679     input.str("");
2680     input << tellme;
2681     out.str("1");
2682     //
2683     stc.str("b");
2684     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2685     if ( n > 0 ) {
2686     printf("WARNING: going one figure backward!\n\n");
2687     n -= 2;
2688     } else {
2689     printf("This is the first strip of plane, you can't go backward! \n");
2690     out.str("");
2691     out << input.str().c_str();
2692     };
2693     };
2694     stc.str("j");
2695     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2696     printf("\n Enter the view number you want to jump to (0 = x, 1 = y): ");
2697     cin.getline(tellme2,256);
2698     input2.str("");
2699     input2 << tellme2;
2700     j = atoi(input2.str().c_str());
2701     if ( j < 0 || j > 1 ) {
2702     printf("\n You can choose between 0 and 1 \n");
2703     out.str("");
2704     out << input.str().c_str();
2705     } else {
2706     printf("\n Enter the plane number you want to jump to (0 to 21): ");
2707     cin.getline(tellme2,256);
2708     input2.str("");
2709     input2 << tellme2;
2710     k = atoi(input2.str().c_str());
2711     if ( k < 0 || k > 21 ) {
2712     printf("\n You can choose between 0 and 21 \n");
2713     out.str("");
2714     out << input.str().c_str();
2715     } else {
2716     printf("\n Enter the strip number you want to jump to (0 to 95): ");
2717     cin.getline(tellme2,256);
2718     input2.str("");
2719     input2 << tellme2;
2720     h = atoi(input2.str().c_str());
2721     if ( h < 0 || h > 95 ) {
2722     printf("\n You can choose between 0 and 95 \n");
2723     out.str("");
2724     out << input.str().c_str();
2725     } else {
2726     printf("\n Jumping to strip %i %i %i \n\n",j,k,h);
2727     goto beginning;
2728     };
2729     };
2730     };
2731     };
2732     //
2733     stc.str("q");
2734     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2735     printf("Exiting...\n");
2736     goto end;
2737     };
2738     //
2739     stc.str("p");
2740     if ( !strcmp(input.str().c_str(),stc.str().c_str()) ) {
2741     printf("Enter a file extension recognized by ROOT (ps, eps, gif,...):\n");
2742     cin.getline(tellme2,256);
2743     input2.str("");
2744     input2 << tellme2;
2745     out.str("");
2746     out << input.str().c_str();
2747     stringstream figsave;
2748     figsave.str("");
2749     figsave << startingdir << "/mip2adcbak_";
2750     figsave << l << "_";
2751     figsave << m << "_";
2752     figsave << n << ".";
2753     figsave << input2.str().c_str();
2754     c1->SaveAs(figsave.str().c_str());
2755     printf("\n");
2756     };
2757     };
2758     n++;
2759     };
2760     m++;
2761     };
2762     l++;
2763     };
2764     end: printf("\n");
2765     }
2766    
2767     void FCaloBAKFIT(TString filename, TString OutDir=""){
2768     const char *pam_calib = pathtocalibration();
2769     if ( !strcmp(OutDir.Data(),"") ){
2770     OutDir = pam_calib;
2771     };
2772     //
2773     // this routine perform the 4224 fit on a backup figure file and save - with extension .bak - the two output file
2774     // in the correct format, ready to be updated etc. etc.
2775     // NB: you will LOSE information about used files
2776     //
2777     stringstream file2;
2778     file2.str("");
2779     file2 << OutDir.Data() << "/CaloADC2MIP.bak";
2780     stringstream file4;
2781     file4.str("");
2782     file4 << OutDir.Data() << "/CaloADC2MIPf.bak";
2783     //
2784     Int_t notgood = 0;
2785     //
2786     TFile *hfile = 0;
2787     CalorimeterCalibration *calo = new CalorimeterCalibration();
2788     hfile = new TFile(file2.str().c_str(),"RECREATE","Calorimeter CALIBRATION data");
2789     if ( !existfile(file2.str().c_str()) ){
2790     printf(" ERROR: Cannot create file!!! \n");
2791     return;
2792     };
2793     TTree *tree = 0;
2794     tree = new TTree("CaloADC","Calorimeter calibration data");
2795     tree->Branch("Event","CalorimeterCalibration",&calo);
2796     //
2797     const char *file3 = filename;
2798     TFile hfile3(file3);
2799     TCanvas *c1;
2800     c1 = new TCanvas("canvas","canvas",800,800);
2801     c1->Draw();
2802     gStyle->SetOptFit(111);
2803     TH1F *pfmip[2][22][96];
2804     TF1 *pfitsnr[2][22][96];
2805     stringstream Fmip;
2806     for (Int_t i=0; i<2;i++){
2807     for (Int_t j=0; j<22;j++){
2808     for (Int_t m=0; m<96;m++){
2809     Fmip.str("");
2810     Fmip << "Fmip " << i;
2811     Fmip << " " << j;
2812     Fmip << " " << m;
2813     pfmip[i][j][m] = new TH1F(Fmip.str().c_str(),"",56,0.,55.);
2814     pfitsnr[i][j][m] = new TF1;
2815     };
2816     };
2817     };
2818     gStyle->SetOptFit(111);
2819     gPad->SetLogy();
2820     //
2821     Int_t l = 0;
2822     stringstream bmippa;
2823     while ( l < 2){
2824     Int_t m = 0;
2825     while ( m < 22 ){
2826     Int_t n =0 ;
2827     Double_t SNRPeak = 0.;
2828     while ( n < 96 ){
2829     c1->Clear();
2830     c1->cd();
2831     bmippa.str("");
2832     bmippa << "bmip " << l;
2833     bmippa << " " << m;
2834     bmippa << " " << n;
2835     TH1F *temp = (TH1F*)hfile3.Get(bmippa.str().c_str());
2836     bmippa.str("");
2837     bmippa << "fmip " << l;
2838     bmippa << " " << m;
2839     bmippa << " " << n;
2840     pfmip[l][m][n] = (TH1F*)temp->Clone(bmippa.str().c_str());
2841     Int_t doitagain = 0;
2842     Double_t fr[2];
2843     Double_t sv[4], pllo[4], plhi[4], fp[4], fpe[4];
2844     if ( SNRPeak > 16. && SNRPeak < 35. ){
2845     fr[0] = (Float_t)SNRPeak - 7.;
2846     sv[0]= fp[0];
2847     sv[1]= fp[1];
2848     sv[2]= fp[2];
2849     sv[3]= fp[3];
2850     } else {
2851     fr[0] = 19.;
2852     sv[0] = 2.8;
2853     sv[1] = 21.0;
2854     sv[2] = 1000.0;
2855     sv[3] = 0.1;
2856     };
2857     fitting: printf("Fitting strip %i %i %i \n",l,m,n);
2858     fr[1] = 60.;
2859     pllo[0]=0.5; pllo[1]=5.0; pllo[2]=1.0; pllo[3]=0.2;
2860     plhi[0]=8.0; plhi[1]=50.0; plhi[2]=1000000.0; plhi[3]=8.0;
2861     Double_t chisqr;
2862     Int_t ndf;
2863     pfitsnr[l][m][n] = langaufit(pfmip[l][m][n],fr,sv,pllo,plhi,fp,fpe,&chisqr,&ndf,"QR0");
2864     //
2865     Double_t SNRPeak = langaupro(fp);
2866     printf("\n Conversion factor: %f \n\n",SNRPeak);
2867     //
2868     //
2869     //
2870     if ( (SNRPeak<0. || (SNRPeak < 16. || SNRPeak > 35.) || chisqr > 100.) && doitagain < 3 ){
2871     printf("\n The fit is not good, I will try again, step %i \n\n",doitagain);
2872     doitagain++;
2873     if ( chisqr > 100. ) {
2874     fr[0] = fr[0] + 5.;
2875     sv[0] = fp[0];
2876     sv[1] = fp[1];
2877     sv[2] = fp[2];
2878     sv[3] = fp[3];
2879     goto fitting;
2880     };
2881     if ( doitagain == 1 ) {
2882     fr[0] = 19.;
2883     sv[0] = 2.8;
2884     sv[1] = 21.0;
2885     sv[2] = 1000.0;
2886     sv[3] = 0.1;
2887     goto fitting;
2888     };
2889     if ( doitagain == 2 ) {
2890     fr[0] = 22.;
2891     sv[0] = 2.8;
2892     sv[1] = 25.0;
2893     sv[2] = 1000.0;
2894     sv[3] = 0.1;
2895     goto fitting;
2896     };
2897     if ( doitagain == 3 ) {
2898     fr[0] = 12.;
2899     sv[0] = 2.8;
2900     sv[1] = 15.0;
2901     sv[2] = 1000.0;
2902     sv[3] = 0.1;
2903     goto fitting;
2904     };
2905     };
2906     //
2907     calo->mip[l][m][n] = (float)SNRPeak;
2908     calo->ermip[l][m][n] = (float)fpe[1];
2909     for (Int_t a = 0; a < 4 ; a++){
2910     calo->fp[a][l][m][n] = (float)fp[a];
2911     calo->fpe[a][l][m][n] = (float)fpe[a];
2912     };
2913     calo->ndf[l][m][n] = (float)ndf;
2914     calo->chi2[l][m][n] = (float)chisqr;
2915     //
2916     if ( ndf!=0 && chisqr/ndf < 2. && fpe[1] < 0.15 && fp[1] > 15. && fp[1] < 40. && SNRPeak > 0. ) {
2917     printf("\n THIS IS A GOOD FIT, SAVED! \n\n");
2918     calo->mask[l][m][n] = 1.;
2919     } else {
2920     notgood++;
2921     calo->mask[l][m][n] = 0.;
2922     };
2923     printf("Fitting done, strip %i %i %i \n\n\n",l,m,n);
2924     c1->cd();
2925     pfmip[l][m][n]->DrawCopy();
2926     pfitsnr[l][m][n]->DrawCopy("lsame");
2927     c1->Modified();
2928     c1->Update();
2929     n++;
2930     };
2931     m++;
2932     };
2933     l++;
2934     };
2935     //
2936     calo->status = 0;
2937     if ( !notgood ) calo->status = 1;
2938     //
2939     tree->Fill();
2940     hfile->Write();
2941     hfile->Close();
2942     //
2943     // Save histograms
2944     //
2945     TFile *hfile2;
2946     hfile2 = new TFile(file4.str().c_str(),"RECREATE","Calorimeter CALIBRATION figures");
2947     if ( !existfile(file4.str().c_str()) ){
2948     printf(" ERROR: Cannot create file!!! \n");
2949     return;
2950     };
2951     TH1F *sfmip[2][22][96];
2952     TF1 *sfitsnr[2][22][96];
2953     stringstream fmipfi;
2954     for (Int_t l = 0; l < 2; l++){
2955     for (Int_t m = 0; m < 22; m++){
2956     for (Int_t n =0 ; n < 96; n++){
2957     fmipfi.str("");
2958     fmipfi << "fmip " << l;
2959     fmipfi << " " << m;
2960     fmipfi << " " << n;
2961     sfmip[l][m][n] = new TH1F(fmipfi.str().c_str(),"",56,0.,55.);
2962     sfmip[l][m][n] = (TH1F*)pfmip[l][m][n]->Clone(fmipfi.str().c_str());
2963     fmipfi.str("");
2964     fmipfi << "fit " << l;
2965     fmipfi << " " << m;
2966     fmipfi << " " << n;
2967     sfitsnr[l][m][n] = (TF1*)pfitsnr[l][m][n]->Clone(fmipfi.str().c_str());
2968     sfmip[l][m][n]->Write();
2969     sfitsnr[l][m][n]->Write();
2970     };
2971     };
2972     };
2973     hfile2->Close();
2974     printf("\n");
2975     hfile3.Close();
2976     FCalo4224STATUS("CaloADC2MIP.bak","");
2977     }
2978    
2979     void FCaloROOT2TXT(TString rootple, TString txtple){
2980     FILE *f;
2981     f = fopen(txtple.Data(),"wb");
2982     TTree *ctree = 0;
2983     TFile *chfile;
2984     CalorimeterCalibration *ccalo = new CalorimeterCalibration();
2985     chfile = new TFile(rootple.Data(),"READ","Calorimeter CALIBRATION data");
2986     if ( chfile->IsZombie() ){
2987     printf(" ERROR: no calorimeter calibration file! \n");
2988     } else {
2989     ctree = (TTree*)chfile->Get("CaloADC");
2990     ctree->SetBranchAddress("Event", &ccalo);
2991     //
2992     Long64_t cnevents = ctree->GetEntries();
2993     ctree->GetEntry(cnevents-1);
2994     };
2995     //
2996     Float_t mip = 0.;
2997     for (Int_t m = 0; m < 2 ; m++ ){
2998     for (Int_t k = 0; k < 22; k++ ){
2999     for (Int_t l = 0; l < 96; l++ ){
3000     mip = 0.;
3001     if ( (ccalo->fp[1][m][k][l] > 20. && ccalo->fp[1][m][k][l] < 32.) || ccalo->mask[m][k][l] == 1. ) {
3002     mip = ccalo->mip[m][k][l];
3003     } else {
3004     mip = 26. ;
3005     };
3006     if ( mip == 0 ) mip = 26. ;
3007     // if ( m == 1 && k == 17 ) printf(" %i mip = %f \n",l,mip);
3008     fwrite(&mip,sizeof(mip),1,f);
3009     };
3010     };
3011     };
3012     fclose(f);
3013     }
3014    
3015     void FCaloREADTXT(TString txtple){
3016     FILE *f;
3017     f = fopen(txtple.Data(),"rb");
3018     Float_t mip = 0.;
3019     for (Int_t m = 0; m < 2 ; m++ ){
3020     for (Int_t k = 0; k < 22; k++ ){
3021     for (Int_t l = 0; l < 96; l++ ){
3022     mip = 0.;
3023     fread(&mip,sizeof(mip),1,f);
3024     // if ( m == 1 && k == 17 ) printf(" %i mip = %f \n",l,mip);
3025     };
3026     };
3027     };
3028     fclose(f);
3029     }

  ViewVC Help
Powered by ViewVC 1.1.23