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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Wed Mar 22 15:07:46 2006 UTC (18 years, 8 months ago) by mocchiut
Branch: MAIN
Branch point for: FUTILITIES
Initial revision

1 mocchiut 1.1 //
2     // Given a calibration and a data file this program create an ntuple with LEVEL1 calorimeter variables - Emiliano Mocchiutti
3     //
4     // FCaloLEVEL1.cxx version 1.01 (2006-03-03)
5     //
6     // The only input needed is the path to the directory created by YODA for the data file you want to analyze.
7     //
8     // Changelog:
9     //
10     // 1.00 - 1.01 (2006-03-03): read unique YODA file and compile correctly with ROOT classes etc.
11     //
12     // 0.00 - 1.00 (2006-03-03): clone of CaloLEVEL1.c v 1.21.
13     //
14     #include <fstream>
15     #include <sstream>
16     #include <TTree.h>
17     #include <TBranch.h>
18     #include <TClassEdit.h>
19     #include <TObject.h>
20     #include <TList.h>
21     #include <TSystem.h>
22     #include <TSystemDirectory.h>
23     #include <TString.h>
24     #include <TFile.h>
25     #include <TClass.h>
26     #include <TCanvas.h>
27     #include <TH1.h>
28     #include <TH1F.h>
29     #include <TH2D.h>
30     #include <TLatex.h>
31     #include <TPad.h>
32     #include <TPaveLabel.h>
33     #include <TChain.h>
34     extern const char *pathtocalibration();
35     #include <PamelaRun.h>
36     #include <physics/calorimeter/CalorimeterEvent.h>
37     #include <physics/trigger/TriggerEvent.h>
38     #include <CalibCalPedEvent.h>
39     #include <fcalostructs.h>
40     //
41     extern char *getLEVname(TString, TString, TString, TString);
42     extern TString whatnamewith(TString, Int_t);
43     extern void stringcopy(TString&, const TString&, Int_t, Int_t);
44     extern int CaloPede(TString, Int_t, Int_t, Calib &);
45     extern void CaloFindBaseRaw(Calib &, Evento &, Int_t, Int_t, Int_t);
46     extern void CaloCompressData(Calib &, Evento &, Int_t, Int_t, Int_t);
47     extern int CaloFindCalibs(TString &, TString &, Int_t &, Calib &);
48     extern bool existfile(TString);
49     //
50     #include <caloclassesfun.h>
51    
52     short int FCaloLEVEL1(TString filename, TString OutDir="", TString calcalibfile = "", Int_t FORCE = 0){
53     const char* startingdir = gSystem->WorkingDirectory();
54     if ( !strcmp(OutDir.Data(),"") ) OutDir = startingdir;
55     stringstream calfile;
56     const char *pam_calib = pathtocalibration();
57     calfile.str("");
58     calfile << pam_calib << "/FCaloADC2MIP.dat";
59     //
60     if ( !existfile(filename) ){
61     printf(" %s :no such file or directory \n",filename.Data());
62     return(1);
63     };
64     TFile *File = new TFile(filename.Data());
65     TTree *otr = (TTree*)File->Get("Physics");
66     otr->SetBranchStatus("*",0); //disable all branches
67     otr->SetBranchStatus("iev*",1);
68     otr->SetBranchStatus("dexy*",1);
69     otr->SetBranchStatus("stwerr*",1);
70     otr->SetBranchStatus("perror*",1);
71     otr->SetBranchStatus("cal*",1);
72     otr->SetBranchStatus("base*",1);
73     otr->SetBranchStatus("Pscu*",1);
74     otr->SetBranchStatus("Calorimeter*",1);
75     otr->SetBranchStatus("Header*",1);
76     //
77     pamela::calorimeter::CalorimeterEvent *de = 0;
78     pamela::PscuHeader *ph = 0;
79     pamela::EventHeader *eh = 0;
80     //
81     otr->SetBranchAddress("Calorimeter", &de);
82     otr->SetBranchAddress("Header", &eh);
83     //
84     // calibration file
85     //
86     if ( calcalibfile == "" ) calcalibfile = filename;
87     //
88     struct Evento evento;
89     struct Evento evento2;
90     struct Calib calib;
91     evento.emin = 0.7;
92     Int_t tot0 = 0;
93     Int_t tot1 = 0;
94     Int_t tot2 = 0;
95     Int_t baseDIFFfull = 0;
96     //
97     // Define variables
98     //
99     Long64_t nevents = otr->GetEntries();
100     if ( nevents < 1 ) {
101     printf("The file is empty!\n");
102     return(1);
103     };
104     Float_t mip[2][22][96];
105     Int_t b[4];
106     Int_t etime,evno,stwerr[4];
107     // Int_t etime,pl,evno,stwerr[4];
108     // Float_t ener, basel,sbase[2][22][6],sdexy[2][22][96],sdexyc[2][22][96];
109     Float_t ener, basel,sdexy[2][22][96],sdexyc[2][22][96];
110     Float_t esup[2][22][96], einf[2][22][96], edelta[2][22][96];
111     //
112     // create the ntuple level1 name
113     //
114     // file = "dw_000000_00000.Physics.Level1.Calorimeter.Event.root"; // just to make space...
115     TString numb = "1"; // level
116     TString detc = "Calorimeter"; // detector
117     char *file = getLEVname(filename,detc,numb,"root");
118     //printf("file> %s\n",file);
119     //return;
120     //
121     // char *file2; // the full path and name of the level1 ntuple
122     stringstream file2;
123     const char *file3;
124     file3 = OutDir;
125     file2.str("");
126     file2 << file3 << "/";
127     file2 << file;
128     // file2 = Form("%s/Physics/Level1/%s",file3,file); // add the path where to save
129     printf("\n Filename will be: \n %s \n\n",file2.str().c_str());
130     //
131     // check if Level1 directory exists, if not create it.
132     //
133     stringstream savedir;
134     savedir.str("");
135     savedir << file3 << "/";
136     // char *savedir;
137     //savedir = Form("%s/Physics/Level1");
138     // Int_t ERR = gSystem->OpenDirectory(savedir.str().c_str());
139     //
140     // check if Level1 directory exists, if not create it.
141     //
142     Int_t ERR = gSystem->MakeDirectory(savedir.str().c_str());
143     //
144     if ( !ERR ) {
145     printf(" LEVEL1 directory doesn't exist! Creating LEVEL1 directory \n\n");
146     gSystem->MakeDirectory(savedir.str().c_str());
147     } else {
148     //
149     // directory exists, check if file exists (if we are not in the force mode)
150     //
151     if ( !FORCE ) {
152     printf(" Not in FORCE mode, check the existance of LEVEL1 data: \n\n");
153     TFile tfile(file2.str().c_str());
154     if ( !tfile.IsZombie() ) {
155     printf(" ERROR: file already exists! Use FORCE = 1 to override \n\n");
156     return(3);
157     } else {
158     printf("\n OK, I will create it!\n");
159     };
160     };
161     };
162     char *type;
163     type = "NEW";
164     if ( FORCE ) type = "RECREATE";
165     TFile *hfile = 0;
166     hfile = new TFile(file2.str().c_str(),type,"Calorimeter LEVEL1 data");
167     //
168     // variables which will fill the ntuple
169     //
170     Float_t perror[4];
171     Float_t nstrip;
172     Float_t qtot;
173     Float_t calevnum[4];
174     Float_t estrip[2][22][96];
175     Float_t estripnull[2][22][96];
176     Float_t diffbas[2][22][6];
177     //
178     // class CalorimeterLevel1
179     //
180     CalorimeterLevel1 *calo = new CalorimeterLevel1();
181     //
182     // Create a ROOT Tree
183     TTree *tree = 0;
184     // calo = new CalorimeterLevel1();
185     tree = new TTree("CaloLevel1","PAMELA Level1 calorimeter data");
186     tree->Branch("Event","CalorimeterLevel1",&calo);
187     //TBranch *branch = tree->Branch("Event",&calo,64000,0);
188     //
189     // this is the value of the mip for each strip. To be changed when we will have the real values
190     //
191     for (Int_t s=0; s<4;s++){
192     for (Int_t d = 0; d<51; d++){
193     calib.ttime[s][d] = 0 ;
194     calib.time[s][d] = 0 ;
195     };
196     };
197     //
198     Int_t okcalo = 0;
199     printf(" ADC to MIP conversion file: \n %s \n",calfile.str().c_str());
200     FILE *f;
201     f = fopen(calfile.str().c_str(),"rb");
202     if ( !f ){
203     printf(" WARNING: no calorimeter ADC to MIP file! \n Using 26 as conversion factor for all strips. \n");
204     } else {
205     okcalo = 1;
206     };
207     //
208     if ( okcalo ) {
209     for (Int_t m = 0; m < 2 ; m++ ){
210     for (Int_t k = 0; k < 22; k++ ){
211     for (Int_t l = 0; l < 96; l++ ){
212     fread(&mip[m][k][l],sizeof(mip[m][k][l]),1,f);
213     calib.calped[m][k][l] = 0. ;
214     evento.dexy[m][k][l] = 0. ;
215     evento.dexyc[m][k][l] = 0. ;
216     estrip[m][k][l] = 0.;
217     estripnull[m][k][l] = 0.;
218     einf[m][k][l] = 32000.;
219     esup[m][k][l] = 0.;
220     edelta[m][k][l] = 0.;
221     };
222     };
223     };
224     } else {
225     for (Int_t m = 0; m < 2 ; m++ ){
226     for (Int_t k = 0; k < 22; k++ ){
227     for (Int_t l = 0; l < 96; l++ ){
228     mip[m][k][l] = 26. ;
229     calib.calped[m][k][l] = 0. ;
230     evento.dexy[m][k][l] = 0. ;
231     evento.dexyc[m][k][l] = 0. ;
232     estrip[m][k][l] = 0.;
233     einf[m][k][l] = 32000.;
234     esup[m][k][l] = 0.;
235     edelta[m][k][l] = 0.;
236     };
237     };
238     };
239     };
240     if ( okcalo ) fclose(f);
241     // chfile->Close();
242     //
243     // first of all find the calibrations in the file
244     //
245     Int_t wused = 0;
246     CaloFindCalibs(filename, calcalibfile, wused, calib);
247     if ( wused == 1 ) calcalibfile = filename;
248     //
249     // print on the screen the results:
250     //
251     const char *ffile;
252     Int_t done = 0;
253     Int_t rdone = 0;
254     Int_t fcheck = 0;
255     Int_t fdone = 0;
256     // Int_t nn = 0;
257     ffile = filename;
258     printf(" ------ %s ------- \n \n",ffile);
259     Int_t calibex = 0;
260     TString pfile;
261     for (Int_t s=0; s<4;s++){
262     printf(" ** SECTION %i **\n",s);
263     for (Int_t d = 0; d<51; d++){
264     if ( calib.ttime[s][d] != 0 ) {
265     calibex++;
266     if ( calib.fcode[s][d] != 10 ){
267     TString file2f = "";
268     stringcopy(file2f,calcalibfile,0,0);
269     pfile = (TString)whatnamewith(file2f,calib.fcode[s][d]);
270     } else {
271     pfile = (TString)calcalibfile;
272     };
273     const char *ffile;
274     ffile = pfile;
275     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);
276     if ( !strcmp(ffile,"wrong") ) calibex--;
277     };
278     };
279     printf("\n");
280     };
281     printf(" ----------------------------------------------------------------------- \n \n");
282     //
283     if ( calibex < 4 ) {
284     printf("No full calibration data in this file, sorry!\n\n ");
285     //
286     // remove the empty file!
287     //
288     stringstream remfile;
289     remfile.str("");
290     remfile << "rm -f " << file2.str().c_str();
291     // char *remfile;
292     //remfile = Form("rm -f %s",file2.str().c_str());
293     gSystem->Exec(remfile.str().c_str());
294     return(4);
295     };
296     //
297     // calibrate before starting
298     //
299     for (Int_t s = 0; s < 4; s++){
300     b[s]=0;
301     if ( calib.fcode[s][b[s]] != 10 ){
302     TString file2f = "";
303     stringcopy(file2f,calcalibfile,0,0);
304     TString pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);
305     } else {
306     TString pfile(calcalibfile);
307     };
308     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
309     };
310     //
311     // run over all the events
312     //
313     printf("\n Processed events: \n\n");
314     //
315     for (Int_t i = 0; i < nevents; i++){
316     //for (Int_t i = 0; i < 1000; i++){
317     //calo = new pamela::calorimeter::CalorimeterLevel1();
318     calo = new CalorimeterLevel1();
319     evno = i;
320     if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);
321     //
322     //
323     //
324     qtot = 0.;
325     nstrip = 0.;
326     //
327     // read from the header of the event the absolute time at which it was recorded
328     //
329     otr->GetEntry(i);
330     ph = eh->GetPscuHeader();
331     etime = ph->GetOrbitalTime();
332     //
333     // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration
334     //
335     if ( !calib.obtjump ) {
336     for (Int_t s = 0; s < 4; s++){
337     if ( calib.ttime[s][b[s]] ){
338     while ( etime > calib.time[s][b[s]+1] ){
339     printf(" CALORIMETER: \n" );
340     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]);
341     printf(" END CALORIMETER. \n\n" );
342     b[s]++;
343     if ( calib.fcode[s][b[s]] != 10 ){
344     TString file2f = "";
345     stringcopy(file2f,calcalibfile,0,0);
346     TString pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);
347     } else {
348     TString pfile(calcalibfile);
349     };
350     CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
351     };
352     };
353     };
354     };
355     //
356     // do whatever you want with data
357     //
358     evento.iev = de->iev;
359     //
360     // run over views and planes
361     //
362     for (Int_t l = 0; l < 2; l++){
363     for (Int_t m = 0; m < 22; m++){
364     //
365     // determine the section number
366     //
367     Int_t se = 5;
368     if (l == 0 && m%2 == 0) se = 3;
369     if (l == 0 && m%2 != 0) se = 2;
370     if (l == 1 && m%2 == 0) se = 1;
371     if (l == 1 && m%2 != 0) se = 0;
372     //
373     // determine what kind of event we are going to analyze
374     //
375     bool isCOMP = 0;
376     bool isFULL = 0;
377     bool isRAW = 0;
378     if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;
379     if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;
380     if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;
381     //
382     // save the prevoius energy deposit and calibration in sbase, sdexy, sdexyc
383     //
384     Int_t pre = -1;
385     if ( isRAW ){
386     for (Int_t nn = 0; nn < 96; nn++){
387     if ( nn%16 == 0 ) pre++;
388     evento.base[l][m][pre] = calib.calbase[l][m][pre];
389     sdexy[l][m][nn] = evento.dexy[l][m][nn];
390     evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;
391     sdexyc[l][m][nn] = evento.dexyc[l][m][nn];
392     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
393     };
394     };
395     //
396     // run over strips
397     //
398     pre = -1;
399     for (Int_t n =0 ; n < 96; n++){
400     if ( n%16 == 0 ) {
401     pre++;
402     done = 0;
403     rdone = 0;
404     fdone = 0;
405     };
406     //
407     // baseline check and calculation
408     //
409     if ( !isRAW ) {
410     //
411     // if it isn't raw and we haven't checked yet, check that the baseline is fine, if not calculate it with a relaxed algorithm.
412     //
413     if ( !done ){
414     evento.base[l][m][pre] = de->base[l][m][pre] ;
415     evento.dexyc[l][m][n] = de->dexyc[l][m][n] ;
416     };
417     } else {
418     //
419     // if it is a raw event and we haven't checked yet, calculate the baseline. Then check that the baseline is fine,
420     // if not calculate it with relaxed algorithm.
421     //
422     if ( !rdone ){
423     CaloFindBaseRaw(calib,evento,l,m,pre);
424     tot1++;
425     rdone = 1;
426     };
427     };
428     //
429     // no suitable new baseline, use old ones and compress data!
430     //
431     if ( !done && (evento.base[l][m][pre] == 31000. || evento.base[l][m][pre] == 0.) ){
432     tot0++;
433     evento.base[l][m][pre] = calib.sbase[l][m][pre];
434     Int_t upnn = n+16;
435     if ( upnn > 96 ) n = 96;
436     for ( Int_t nn = n; nn<upnn; nn++ ){
437     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
438     };
439     CaloCompressData(calib,evento,l,m,pre);
440     done = 1;
441     };
442     //
443     // if here we don't have a valid baseline we will skip the event.
444     //
445     if ( evento.base[l][m][pre] == 31000. ) tot2++;
446     //
447     // check the baseline calculation in FULL mode: baseline recorded from DSP must be identical to the one calculated using RAW data.
448     //
449     if ( isFULL ) {
450     if ( !fdone) {
451     fdone = 1;
452     if ( de->base[l][m][pre] > 0. && de->base[l][m][pre] < 32000. ) {
453     fcheck = 0;
454     for (Int_t nn = pre*16; nn < (pre+1)*16 ; nn++){
455     if ( de->dexy[l][m][nn] > 0. && de->dexy[l][m][nn] < 32000. ) fcheck++;
456     };
457     if ( fcheck ) {
458     memcpy(&evento2, &evento, sizeof(evento));
459     Double_t prima = evento.base[l][m][pre];
460     CaloFindBaseRaw(calib,evento2,l,m,pre);
461     diffbas[l][m][pre] = 0.;
462     if ( evento2.base[l][m][pre] != 31000. ) {
463     Double_t prmndp = prima - evento2.base[l][m][pre];
464     if ( prmndp > 100000000. || prmndp < 100000000. ) prmndp = 0.;
465     diffbas[l][m][pre] = (float)prmndp;
466     calo->diffbas[l][m][pre] = (float)prmndp;
467     if ( prmndp != 0. ) {
468     baseDIFFfull++;
469     //printf("differenza! %f %i %i %i %i %i \n",prmndp,i,l,m,pre,nn);
470     //return;
471     };
472     };
473     };
474     };
475     };
476     };
477     //
478     // CALIBRATION ALGORITHM
479     //
480     basel = evento.base[l][m][pre];
481     ener = evento.dexyc[l][m][n];
482     estrip[l][m][n] = 0.;
483     if ( ener > esup[l][m][n] && ener < 32000 ) esup[l][m][n] = ener;
484     if ( ener < einf[l][m][n] && ener > 0 ) einf[l][m][n] = ener;
485     if ( basel>0 && basel < 30000. && ener > 0. ){
486     estrip[l][m][n] = (ener - calib.calped[l][m][n] - basel)/mip[l][m][n] ;
487     //
488     // OK, now in estrip we have the energy deposit in MIP of all the strips for this event (at the end of loops of course)
489     //
490     //if (estrip[l][m][n]<0.1 ) printf(" %i %i %i here %f \n",l,m,n,estrip[l][m][n]);
491     calo->good[l][m][n] = (int)calib.calgood[l][m][n];
492     if ( estrip[l][m][n] > evento.emin && calib.calgood[l][m][n] == 0 ) {
493     qtot += estrip[l][m][n];
494     nstrip += 1.;
495     };
496     };
497     calib.sbase[l][m][pre] = evento.base[l][m][pre];
498     };
499     };
500     };
501     //
502     // per ogni evento...
503     //
504     calo->qtot = qtot;
505     calo->nstrip = nstrip;
506     calo->evno = evno;
507     calo->nobase = tot2;
508     memcpy(calo->stwerr, de->stwerr, sizeof(stwerr));
509     memcpy(calo->perror, de->perror, sizeof(perror));
510     memcpy(calo->calevnum, de->calevnum, sizeof(calevnum));
511     memcpy(calo->estrip, estrip, sizeof(estrip));
512     tree->Fill();
513     memcpy(estrip, estripnull, sizeof(estrip));
514     };
515     hfile->Write();
516     hfile->Close();
517     printf("\n");
518     gSystem->ChangeDirectory(startingdir);
519     return(0);
520     }
521    
522    
523    

  ViewVC Help
Powered by ViewVC 1.1.23