/[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.3 - (hide annotations) (download)
Fri Jun 30 12:08:13 2006 UTC (18 years, 5 months ago) by mocchiut
Branch: MAIN
CVS Tags: v1r03
Changes since 1.2: +19 -7 lines
Added FCaloCHECKCRC utility

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

  ViewVC Help
Powered by ViewVC 1.1.23