/[PAMELA software]/calo/ground/LEVEL2/macros/CaloLEVEL2.cra
ViewVC logotype

Diff of /calo/ground/LEVEL2/macros/CaloLEVEL2.cra

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by mocchiut, Mon Dec 5 16:13:53 2005 UTC revision 1.4 by mocchiut, Mon Mar 20 10:12:13 2006 UTC
# Line 1  Line 1 
1  //  //
2  //   Given a calibration and a data file this program create an ntuple with LEVEL2 calorimeter variables - Emiliano Mocchiutti  //   Given a calibration and a data file this program create an ntuple with LEVEL2 calorimeter variables - Emiliano Mocchiutti
3  //  //
4  //   CaloLEVEL2.c      version 4.00  (2005-11-29)  //   CaloLEVEL2.c      version 4.04  (2006-03-20)
5  //  //
6  //   The only input needed is the path to the directory created by YODA for the data file you want to analyze.  //   The only input needed is the path to the directory created by YODA for the data file you want to analyze.
7  //  //
8  //   Changelog:  //   Changelog:
9  //  //
10    //   4.03 - 4.04 (2006-03-20): Fixed erroneous distance between x/y planes in the core routine
11    //
12    //   4.02 - 4.03 (2006-03-09): Adapted to work with tracker software release r3v02.
13    //
14    //   4.01 - 4.02 (2006-01-26): Bug: wrong baselines calculation in some cases, fixed.
15    //
16    //   4.00 - 4.01 (2006-01-11): Bugs: in Makefile (nothing to worry about) and here clevel1.trkchi2 never filled! fixed. Not really processing self trigger events! fixed.
17    //                             Bug: not setting small values to zero after applying the cross-talk correction! fixed.
18    //
19  //   3.17 - 4.00 (2005-11-29): preparing for the final release, small changes in the makefile.  //   3.17 - 4.00 (2005-11-29): preparing for the final release, small changes in the makefile.
20  //  //
21  //   3.16 - 3.17 (2005-10-13): do not exclude all strips marked "bad" but only the ones with RMS>6.  //   3.16 - 3.17 (2005-10-13): do not exclude all strips marked "bad" but only the ones with RMS>6.
# Line 133  void unloadf77lib(TString Framework, Int Line 142  void unloadf77lib(TString Framework, Int
142  #endif  #endif
143  #if defined (__CINT__)                            #if defined (__CINT__)                          
144  void unloadf77lib(TString Framework, Int_t MySQL){  void unloadf77lib(TString Framework, Int_t MySQL){
145      const char *franame = Framework;    const char *franame = Framework;
146      //    //
147      char *pam_lib=gSystem->Getenv("PAM_LIB");    char *pam_lib=gSystem->Getenv("PAM_LIB");
148      if ( MySQL == 1 || MySQL == -1 ){      if ( MySQL == 1 || MySQL == -1 ){    
         stringstream libload;  
         libload.str("");  
         libload << pam_lib << "/libreadmy";  
         gSystem->Unload(libload.str().c_str());  
         libload.str("");  
         libload << pam_lib << "/libreadmy_C";  
         gSystem->Unload(libload.str().c_str());  
     };  
     if ( !strcmp(franame,"paw") ) {  
         stringstream libload;  
         libload.str("");  
         libload << pam_lib << "/libclcalol2_C";  
         gSystem->Unload(libload.str().c_str());  
         libload.str("");  
         libload << pam_lib << "/libclcalol2";  
         gSystem->Unload(libload.str().c_str());  
         libload.str("");  
         libload << pam_lib << "/libopcalol2_C";  
         gSystem->Unload(libload.str().c_str());  
         libload.str("");  
         libload << pam_lib << "/libopcalol2";  
         gSystem->Unload(libload.str().c_str());  
     };      
149      stringstream libload;      stringstream libload;
150      libload.str("");      libload.str("");
151      libload << pam_lib << "/libcrcalol2_C";      libload << pam_lib << "/libreadmy";
152      gSystem->Unload(libload.str().c_str());      gSystem->Unload(libload.str().c_str());
     char *pam_lib=gSystem->Getenv("PAM_LIB");  
     stringstream libload;  
153      libload.str("");      libload.str("");
154      libload << pam_lib << "/libcrcalol2";      libload << pam_lib << "/libreadmy_C";
155      gSystem->Unload(libload.str().c_str());      gSystem->Unload(libload.str().c_str());
156      char *pam_lib=gSystem->Getenv("PAM_LIB");    };
157      if ( !strcmp(franame,"paw") ) {
158      stringstream libload;      stringstream libload;
159      libload.str("");      libload.str("");
160      libload << pam_lib << "/libreadb2maps_C";      libload << pam_lib << "/libclcalol2_C";
161      gSystem->Unload(libload.str().c_str());      gSystem->Unload(libload.str().c_str());
     char *pam_lib=gSystem->Getenv("PAM_LIB");  
     stringstream libload;  
162      libload.str("");      libload.str("");
163      libload << pam_lib << "/libreadb2maps";      libload << pam_lib << "/libclcalol2";
164      gSystem->Unload(libload.str().c_str());      gSystem->Unload(libload.str().c_str());
     char *pam_lib=gSystem->Getenv("PAM_LIB");  
     stringstream libload;  
165      libload.str("");      libload.str("");
166      libload << pam_lib << "/libtrack";      libload << pam_lib << "/libopcalol2_C";
167      gSystem->Unload(libload.str().c_str());      gSystem->Unload(libload.str().c_str());
168        libload.str("");
169        libload << pam_lib << "/libopcalol2";
170        gSystem->Unload(libload.str().c_str());
171      };    
172      stringstream libload;
173      libload.str("");
174      libload << pam_lib << "/libcrcalol2_C";
175      gSystem->Unload(libload.str().c_str());
176      char *pam_lib=gSystem->Getenv("PAM_LIB");
177      stringstream libload;
178      libload.str("");
179      libload << pam_lib << "/libcrcalol2";
180      gSystem->Unload(libload.str().c_str());
181      char *pam_lib=gSystem->Getenv("PAM_LIB");
182      stringstream libload;
183      libload.str("");
184      libload << pam_lib << "/libreadb2maps_C";
185      gSystem->Unload(libload.str().c_str());
186      char *pam_lib=gSystem->Getenv("PAM_LIB");
187      stringstream libload;
188      libload.str("");
189      libload << pam_lib << "/libreadb2maps";
190      gSystem->Unload(libload.str().c_str());
191      char *pam_lib=gSystem->Getenv("PAM_LIB");
192      stringstream libload;
193      libload.str("");
194      libload << pam_lib << "/libtrack";
195      gSystem->Unload(libload.str().c_str());
196  }  }
197  #endif  #endif
198    
199  short int calolevel2core(Int_t ei, Int_t b[4] , TTree *otr, TTree *tree, CaLevel2 & clevel2, Evento & evento, Calib & calib, TString calcalibfile){  short int calolevel2core(Int_t ei, Int_t b[4] , TTree *otr, TTree *tree, CaLevel2 & clevel2, Evento & evento, Calib & calib, TString calcalibfile){
200      //
201      // Define variables
202      //
203      struct CaLevel1 clevel1;
204      clevel1.xalig = 120.4;
205      clevel1.yalig = 118.6;
206      clevel1.zalig = -260.;
207      struct Mystruct mystruct;
208      clevel1.paw = calib.ispaw;
209      Int_t done = 0;
210      Int_t pre = -1;
211      Int_t etime;
212      Int_t S3 = 0;
213      Int_t S2 = 0;
214      Int_t S12 = 0;
215      Int_t S11 = 0;
216      Int_t se = 5;
217      bool isCOMP = 0;
218      bool isFULL = 0;
219      bool isRAW = 0;
220      Float_t ener;
221      Float_t estripnull[96][22][2];
222      //
223      Int_t doneb = 0;
224      Int_t donec = 0;
225      Int_t ck = 0;
226      Int_t ipre = 0;
227      Int_t ip[3] = {0};
228      Float_t base0, base1, base2;
229      base0 = 0.;
230      base1 = 0.;
231      base2 = 0.;
232      Float_t qpre[6] = {0};
233      Float_t ene[96] = {0};
234      //
235      pamela::calorimeter::CalorimeterEvent *de = 0;  
236      pamela::PscuHeader *ph = 0;
237      pamela::EventHeader *eh = 0;
238      pamela::trigger::TriggerEvent  *trig = 0;
239      otr->SetBranchAddress("Header", &eh);
240      otr->SetBranchAddress("Calorimeter.Event", &de);
241      otr->SetBranchAddress("Trigger.Event", &trig);
242      //
243      // other variables
244      //
245      char *yodala2;
246      Int_t chdone[4] = {0,0,0,0};
247      Int_t pe = 0;
248      Float_t tmptrigty = -1.;
249      //
250      // start processing
251      //
252      //
253      // read from the header of the event the OBT and event number
254      //
255      otr->GetEntry(ei);
256      ph = eh->GetPscuHeader();
257      clevel2.pkt_num = ph->GetCounter();
258      etime = ph->GetOrbitalTime();
259      clevel2.OBT = etime;
260      clevel2.pro_num = (ei+1);
261      //
262      // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
263      //
264      S3 = 0;
265      S2 = 0;
266      S12 = 0;
267      S11 = 0;
268      S3 = trig->patterntrig[2];
269      S2 = trig->patterntrig[3];
270      S12 = trig->patterntrig[4];
271      S11 = trig->patterntrig[5];
272      if ( trig->patterntrig[0] ) tmptrigty = 2.;
273      if ( S3 || S2 || S12 || S11 )  tmptrigty = 0.;    
274      if ( trig->patterntrig[1] & (1<<0) || (!trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11) ) tmptrigty = 1.;
275      clevel2.trigty = tmptrigty;
276      //
277      yodala2 = "";
278      if ( calib.mysql ){
279        if ( !calib.obtjump ){
280          for (Int_t s = 0; s < 4; s++){
281            if ( etime > calib.time[s][0] ){
282            tryagain:
283              printf(" ** SECTION %i **\n",s);
284              stringstream qy;
285              qy.str("");
286              Int_t chkmysql = 0;
287              stringstream myfile;
288              const char *tabula = calib.tablename;
289              qy << "select * from " << tabula;
290              qy << " where section = " << s;
291              qy << " and fromtime >= " << calib.time[s][0];
292              qy << " limit 1";
293              chkmysql = creadmysql(calib.db,(char *)qy.str().c_str(),mystruct);    
294              if ( chkmysql ) {
295                printf("- ERROR: problems querying MySQL database! -\n- ERROR: Empty table? -\n\n");
296                return(1);
297              } else {
298                if ( mystruct.totime == -1 ){
299                  printf("- ERROR: problems querying MySQL database! -\n- ERROR: Corrupted table? -\n\n");
300                  return(2);
301                };
302                calib.time[s][0] = mystruct.totime;
303                calib.time[s][1] = mystruct.fromtime;
304                calib.ttime[s][0] = mystruct.calibtime;
305                const char *caliba = calib.basepath;
306                const char *yodala = calib.yodalev;
307                yodala2 = (char *)yodala;
308                const char *fila = mystruct.calcalibfile;
309                myfile.str("");
310                myfile << caliba;
311                if ( !calib.DW ) {
312                  myfile << fila;
313                } else {
314                  myfile << "DW";
315                  string myfil = (const char*)fila;
316                  Int_t myposiz = myfil.find("dw_");
317                  TString mytmp;
318                  Int_t mspos = myposiz+2;
319                  Int_t mepos = myposiz+13;
320                  stringcopy(mytmp,fila,mspos,mepos);
321                  const char *myf = mytmp;                    
322                  myfile << mytmp;
323                };
324                myfile << yodala << "/";
325                calib.obtjump = mystruct.obtjump;
326                printf(" - event at time %i. From time %i to time %i \n   use calibration at time %i, file %s\n",etime,calib.time[s][1],calib.time[s][0],calib.ttime[s][0],myfile.str().c_str());
327                if ( calib.obtjump ) printf("\n WARNING: jump in the OBT! this calibration might not be the best! \n");
328                if ( etime > calib.time[s][0] && !calib.obtjump ){
329                  printf("\n WARNING: event at time greater than the upper limit for this calibration!\nSearch for the correct calibration\n");
330                  goto tryagain;
331                };
332              };
333              Int_t pedeerr = 0;
334              pedeerr = CaloPede(myfile.str().c_str(),s,calib.ttime[s][0],calib);
335              if ( pedeerr ) {
336                printf("\n\nERROR: problems opening calibration file! \n\nERROR: YODA version of the calibration file is not %s? \n",yodala2);
337                return(1);
338              };
339              printf("\n");
340            };
341          };
342        };
343      } else {      
344      //      //
345      // Define variables      // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration
     //  
     struct CaLevel1 clevel1;  
     clevel1.xalig = 120.4;  
     clevel1.yalig = 118.6;  
     clevel1.zalig = -260.;  
     struct Mystruct mystruct;  
     clevel1.paw = calib.ispaw;  
     Int_t upnn ;  
     Int_t done = 0;  
     Int_t pre = -1;  
     Int_t etime;  
     Int_t S3 = 0;  
     Int_t S2 = 0;  
     Int_t S12 = 0;  
     Int_t S11 = 0;  
     Int_t se = 5;  
     bool isCOMP = 0;  
     bool isFULL = 0;  
     bool isRAW = 0;  
     Float_t ener, basel;  
     Float_t estripnull[96][22][2];  
     //  
     Int_t doneb = 0;  
     Int_t nn;  
     Int_t ck = 0;  
     Int_t ipre = 0;  
     Int_t ip[3] = {0};  
     Float_t base0, base1, base2;  
     base0 = 0.;  
     base1 = 0.;  
     base2 = 0.;  
     Float_t qpre[6] = {0};  
     Float_t ene[96] = {0};  
     //  
     pamela::calorimeter::CalorimeterEvent *de = 0;    
     pamela::PscuHeader *ph = 0;  
     pamela::EventHeader *eh = 0;  
     pamela::trigger::TriggerEvent  *trig = 0;  
     otr->SetBranchAddress("Header", &eh);  
     otr->SetBranchAddress("Calorimeter.Event", &de);  
     otr->SetBranchAddress("Trigger.Event", &trig);  
     //  
     // other variables  
     //  
     char *yodala2;  
     Int_t chdone[4] = {0,0,0,0};  
     Int_t pe = 0;  
     Float_t tmptrigty = -1.;  
     //  
     // start processing  
     //  
     //  
     // read from the header of the event the OBT and event number  
346      //      //
347      otr->GetEntry(ei);      if ( !calib.obtjump ) {
348      ph = eh->GetPscuHeader();        for (Int_t s = 0; s < 4; s++){
349      clevel2.pkt_num = ph->GetCounter();          if ( calib.ttime[s][b[s]] ){
350      etime = ph->GetOrbitalTime();            while ( etime > calib.time[s][b[s]+1] ){                              
351      clevel2.OBT = etime;              printf(" CALORIMETER: \n" );
352      clevel2.pro_num = (ei+1);              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]);
353      //              printf(" END CALORIMETER. \n\n" );
354      // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)              b[s]++;
355      //              TString pfile;
356      S3 = 0;              if ( calib.fcode[s][b[s]] != 10 ){
357      S2 = 0;                TString file2f = "";
358      S12 = 0;                stringcopy(file2f,calcalibfile);
359      S11 = 0;                TString pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);                    
360      S3 = trig->patterntrig[2];              } else {
361      S2 = trig->patterntrig[3];                pfile = (TString)calcalibfile;
     S12 = trig->patterntrig[4];  
     S11 = trig->patterntrig[5];  
     if ( trig->patterntrig[0] ) tmptrigty = 2.;  
     if ( S3 || S2 || S12 || S11 )  tmptrigty = 0.;      
     if ( trig->patterntrig[1] & (1<<0) || (!trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11) ) tmptrigty = 1.;  
     clevel2.trigty = tmptrigty;  
     //  
     yodala2 = "";  
     if ( calib.mysql ){  
         if ( !calib.obtjump ){  
             for (Int_t s = 0; s < 4; s++){  
                 if ( etime > calib.time[s][0] ){  
                 tryagain:  
                     printf(" ** SECTION %i **\n",s);  
                     stringstream qy;  
                     qy.str("");  
                     Int_t chkmysql = 0;  
                     stringstream myfile;  
                     const char *tabula = calib.tablename;  
                     qy << "select * from " << tabula;  
                     qy << " where section = " << s;  
                     qy << " and fromtime >= " << calib.time[s][0];  
                     qy << " limit 1";  
                     chkmysql = creadmysql(calib.db,(char *)qy.str().c_str(),mystruct);    
                     if ( chkmysql ) {  
                         printf("- ERROR: problems querying MySQL database! -\n- ERROR: Empty table? -\n\n");  
                         return(1);  
                     } else {  
                         if ( mystruct.totime == -1 ){  
                             printf("- ERROR: problems querying MySQL database! -\n- ERROR: Corrupted table? -\n\n");  
                             return(2);  
                         };  
                         calib.time[s][0] = mystruct.totime;  
                         calib.time[s][1] = mystruct.fromtime;  
                         calib.ttime[s][0] = mystruct.calibtime;  
                         const char *caliba = calib.basepath;  
                         const char *yodala = calib.yodalev;  
                         yodala2 = (char *)yodala;  
                         const char *fila = mystruct.calcalibfile;  
                         myfile.str("");  
                         myfile << caliba;  
                         if ( !calib.DW ) {  
                           myfile << fila;  
                         } else {  
                           myfile << "DW";  
                           string myfil = (const char*)fila;  
                           Int_t myposiz = myfil.find("dw_");  
                           TString mytmp;  
                           Int_t mspos = myposiz+2;  
                           Int_t mepos = myposiz+13;  
                           stringcopy(mytmp,fila,mspos,mepos);  
                           const char *myf = mytmp;                          
                           myfile << mytmp;  
                         };  
                         myfile << yodala << "/";  
                         calib.obtjump = mystruct.obtjump;  
                         printf(" - event at time %i. From time %i to time %i \n   use calibration at time %i, file %s\n",etime,calib.time[s][1],calib.time[s][0],calib.ttime[s][0],myfile.str().c_str());  
                         if ( calib.obtjump ) printf("\n WARNING: jump in the OBT! this calibration might not be the best! \n");  
                         if ( etime > calib.time[s][0] && !calib.obtjump ){  
                             printf("\n WARNING: event at time greater than the upper limit for this calibration!\nSearch for the correct calibration\n");  
                             goto tryagain;  
                         };  
                     };  
                     Int_t pedeerr = 0;  
                     pedeerr = CaloPede(myfile.str().c_str(),s,calib.ttime[s][0],calib);  
                     if ( pedeerr ) {  
                         printf("\n\nERROR: problems opening calibration file! \n\nERROR: YODA version of the calibration file is not %s? \n",yodala2);  
                         return(1);  
                     };  
                     printf("\n");  
                 };  
362              };              };
363                CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
364              };
365            };
366          };
367        };
368      };
369      //
370      // run over views and planes
371      //
372      for (Int_t l = 0; l < 2; l++){
373        for (Int_t m = 0; m < 22; m++){
374          //
375          // determine the section number
376          //
377          se = 5;
378          if (l == 0 && m%2 == 0) se = 3;
379          if (l == 0 && m%2 != 0) se = 2;
380          if (l == 1 && m%2 == 0) se = 1;
381          if (l == 1 && m%2 != 0) se = 0;          
382          //
383          // determine what kind of event we are going to analyze
384          //
385          isCOMP = 0;
386          isFULL = 0;
387          isRAW = 0;
388          if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;
389          if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;
390          if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;
391          if ( !chdone[se] ){
392            //
393            // check for any error in the event
394            //
395            clevel2.crc[se] = 0;
396            if ( de->perror[se] == 132 ){
397              clevel2.crc[se] = 1;
398              pe++;
399            };
400            clevel2.perr[se] = 0;
401            if ( de->perror[se] != 0 ){
402              clevel2.perr[se] = 1;
403              pe++;
404            };
405            clevel2.swerr[se] = 0;
406            for (Int_t j = 0; j < 7 ; j++){
407              if ( (j != 3) && (de->stwerr[se] & (1 << j)) ){
408                clevel2.swerr[se] = 1;
409                pe++;
410              };
411            };          
412            chdone[se] = 1;
413          };
414          if ( clevel2.crc[se] == 0 && (calib.good2 == 1 || clevel2.trigty == 2)){  
415            pre = -1;
416            if ( isRAW ){
417              for (Int_t nn = 0; nn < 96; nn++){                
418                evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;
419                evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;
420              };
421          };          };
     } else {      
422          //          //
423          // for each event check that the calibration we are using are still within calibration limits, if not call the next calibration          // run over preamplifiers
424          //          //
425          if ( !calib.obtjump ) {          pre = -1;
426              for (Int_t s = 0; s < 4; s++){          for (Int_t i = 0; i < 3; i++){
427                  if ( calib.ttime[s][b[s]] ){            for (Int_t j = 0; j < 2; j++){
428                      while ( etime > calib.time[s][b[s]+1] ){                                          pre = j + i*2;
429                          printf(" CALORIMETER: \n" );              //
430                          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]);              // baseline check and calculation
431                          printf(" END CALORIMETER. \n\n" );              //
432                          b[s]++;              if ( !isRAW ) {
433                          TString pfile;                evento.base[l][m][pre] = de->base[l][m][pre] ;  
434                          if ( calib.fcode[s][b[s]] != 10 ){              } else {
435                              TString file2f = "";                //
436                              stringcopy(file2f,calcalibfile);                // if it is a raw event and we haven't checked
437                              TString pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);                              // yet, calculate the baseline.
438                          } else {                //
439                              pfile = (TString)calcalibfile;                CaloFindBaseRaw(calib,evento,l,m,pre);
                         };  
                         CaloPede(pfile,s,calib.ttime[s][b[s]],calib);  
                     };  
                 };  
440              };              };
441              };
442          };          };
443      };          //
444      //          // run over strips
445      // run over views and planes          //
446      //          pre = -1;
447      for (Int_t l = 0; l < 2; l++){          for (Int_t i = 0 ; i < 3 ; i++){
448          for (Int_t m = 0; m < 22; m++){            //      ck = 0;
449              //            ip[i] = 0;
450              // determine the section number            for (Int_t n = i*32 ; n < (i+1)*32 ; n++){                
451                if (n%16 == 0) {
452                  done = 0;
453                  doneb = 0;
454                  donec = 0;
455                  ck = 0;
456                  pre++;
457                  qpre[pre] = 0;
458                };
459                //
460                // baseline check and calculation
461              //              //
462              se = 5;              if ( !isRAW ) {
463              if (l == 0 && m%2 == 0) se = 3;                evento.dexy[l][m][n] = de->dexy[l][m][n] ;
464              if (l == 0 && m%2 != 0) se = 2;                evento.dexyc[l][m][n] = de->dexyc[l][m][n] ;
465              if (l == 1 && m%2 == 0) se = 1;              };                                      
             if (l == 1 && m%2 != 0) se = 0;              
466              //              //
467              // determine what kind of event we are going to analyze              // no suitable new baseline, use old ones and compress data!
468              //              //
469              isCOMP = 0;              if ( !done ){
470              isFULL = 0;                if ( (evento.base[l][m][pre] == 31000. || evento.base[l][m][pre] == 0.) ){
471              isRAW = 0;                  ck = 1;
472              if ( de->stwerr[se] & (1 << 16) ) isCOMP = 1;                  if (pre%2 == 0) {
473              if ( de->stwerr[se] & (1 << 17) ) isFULL = 1;                    ip[i] = pre + 1;
474              if ( de->stwerr[se] & (1 << 3) ) isRAW = 1;                  } else {
475              if ( !chdone[se] ){                    ip[i] = pre - 1;
                 //  
                 // check for any error in the event  
                 //  
                 clevel2.crc[se] = 0;  
                 if ( de->perror[se] == 132 ){  
                     clevel2.crc[se] = 1;  
                     pe++;  
                 };  
                 clevel2.perr[se] = 0;  
                 if ( de->perror[se] != 0 ){  
                     clevel2.perr[se] = 1;  
                     pe++;  
476                  };                  };
477                  clevel2.swerr[se] = 0;                  if ( (evento.base[l][m][ip[i]] == 31000. || evento.base[l][m][ip[i]] == 0.) ){
478                  for (Int_t j = 0; j < 7 ; j++){                    //
479                      if ( (j != 3) && (de->stwerr[se] & (1 << j)) ){                    ck = 2;
480                          clevel2.swerr[se] = 1;                    if ( calib.sbase[l][m][pre] == 31000. || calib.sbase[l][m][pre] == 0. ) {
481                          pe++;                      ck = 3;
                     };  
                 };            
                 chdone[se] = 1;  
             };  
             if ( clevel2.crc[se] == 0 && calib.good2 == 1 ){      
                 pre = -1;  
                 if ( isRAW ){  
                   for (Int_t nn = 0; nn < 96; nn++){                  
                     evento.dexy[l][m][nn] = de->dexy[l][m][nn] ;  
                     evento.dexyc[l][m][nn] = de->dexyc[l][m][nn] ;  
482                    };                    };
                 };  
                 //  
                 // run over preamplifiers  
                 //  
                 pre = -1;  
                 for (Int_t i = 0; i < 3; i++){  
                     for (Int_t j = 0; j < 2; j++){  
                         pre = j + i*2;  
                         //  
                         // baseline check and calculation  
                         //  
                         if ( !isRAW ) {  
                             evento.base[l][m][pre] = de->base[l][m][pre] ;    
                         } else {  
                             //  
                             // if it is a raw event and we haven't checked  
                             // yet, calculate the baseline.  
                             //  
                             CaloFindBaseRaw(calib,evento,l,m,pre);  
                         };  
                     };  
483                  };                  };
484                    //                              upnn = n+16;
485                    //if ( upnn > 96 ) nn = 96;
486                    //for ( Int_t nn = n; nn<upnn; nn++ ){
487                    //  evento.dexyc[l][m][nn] = de->dexyc[l][m][nn];
488                    //};
489                    //CaloCompressData(calib,evento,l,m,pre);
490                    done = 1;
491                  };
492                };                  
493                //
494                // CALIBRATION ALGORITHM
495                //
496                if ( !doneb ){
497                  switch (ck) {
498                  case 0:
499                    base0 = evento.base[l][m][pre];
500                    base2 = calib.calbase[l][m][pre];
501                    break;
502                  case 1:
503                    base0 = evento.base[l][m][ip[i]];
504                    base2 = calib.calbase[l][m][ip[i]];
505                    break;
506                  case 2:
507                    base0 = calib.sbase[l][m][pre];
508                    base2 = calib.calbase[l][m][pre];
509                    break;
510                  case 3:
511                    base0 = calib.calbase[l][m][pre];
512                    base2 = calib.calbase[l][m][pre];
513                    break;
514                  };
515                  base1 = calib.calbase[l][m][pre];
516                  doneb = 1;
517                };
518                ener = evento.dexyc[l][m][n];
519                clevel1.estrip[n][m][l] = 0.;
520                if ( base0>0 && base0 < 30000. ){
521                  if ( !donec && (base0 - base1 + base2) != 0. ){
522                    calib.sbase[l][m][pre] = base0 - base1 + base2;
523                    donec = 1;
524                  };
525                  if ( ener > 0. ){
526                    clevel1.estrip[n][m][l] = (ener - calib.calped[l][m][n] - base0 - base1 + base2)/calib.mip[l][m][n] ;
527                  //                  //
528                  // run over strips                  // 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)
                 //  
                 pre = -1;  
                 for (Int_t i = 0 ; i < 3 ; i++){  
                     ck = 0;  
                     ip[i] = 0;  
                     for (Int_t n = i*32 ; n < (i+1)*32 ; n++){                
                         if (n%16 == 0) {  
                             done = 0;  
                             doneb = 0;  
                             pre++;  
                             qpre[pre] = 0;  
                         };  
                         //  
                         // baseline check and calculation  
                         //  
                         if ( !isRAW ) {  
                             evento.dexy[l][m][n] = de->dexy[l][m][n] ;  
                             evento.dexyc[l][m][n] = de->dexyc[l][m][n] ;  
                         };                                            
                         //  
                         // no suitable new baseline, use old ones and compress data!  
                         //  
                         if ( !done ){  
                             if ( (evento.base[l][m][pre] == 31000. || evento.base[l][m][pre] == 0.) ){  
                                 ck = 1;  
                                 if (pre%2 == 0)  
                                     ip[i] = pre + 1;  
                                 else  
                                     ip[i] = pre - 1;  
                                 if ( (evento.base[l][m][ip[i]] == 31000. || evento.base[l][m][ip[i]] == 0.) ){  
                                     //  
                                     evento.base[l][m][pre] = calib.sbase[l][m][pre];  
                                     //                                
                                     ck = 2;  
                                     upnn = n+16;  
                                     if ( upnn > 96 ) nn = 96;  
                                     for ( Int_t nn = n; nn<upnn; nn++ ){  
                                         evento.dexyc[l][m][nn] = de->dexyc[l][m][nn];  
                                     };  
                                     CaloCompressData(calib,evento,l,m,pre);  
                                     done = 1;  
                                 };  
                             };  
                         };                        
                         //  
                         // CALIBRATION ALGORITHM  
                         //  
                         basel = evento.base[l][m][pre];              
                         if ( !doneb ){  
                             switch (ck) {  
                             case 0:  
                                 base0 = evento.base[l][m][pre];  
                                 base2 = calib.calbase[l][m][pre];  
                                 break;  
                             case 1:  
                                 base0 = evento.base[l][m][ip[i]];  
                                 base2 = calib.calbase[l][m][ip[i]];  
                                 break;  
                             case 2:  
                                 base0 = calib.sbase[l][m][pre];  
                                 base2 = calib.calbase[l][m][pre];  
                                 break;  
                             };  
                             base1 = calib.calbase[l][m][pre];  
                             doneb = 1;  
                         };  
                         ener = evento.dexyc[l][m][n];  
                         clevel1.estrip[n][m][l] = 0.;  
                         if ( base0>0 && base0 < 30000. && ener > 0. ){  
                             clevel1.estrip[n][m][l] = (ener - calib.calped[l][m][n] - base0 - base1 + base2)/calib.mip[l][m][n] ;  
                             //  
                             // 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)  
                             //  
                             //                      if ( clevel1.estrip[n][m][l] < evento.emin || calib.calgood[l][m][n] != 0 ) {  
                             if ( clevel1.estrip[n][m][l] < evento.emin || calib.calrms[l][m][n] > 26 ) {  
                                 clevel1.estrip[n][m][l] = 0.;  
                             };  
                             qpre[pre] += clevel1.estrip[n][m][l];  
                         };  
                         calib.sbase[l][m][pre] = evento.base[l][m][pre];  
                     };  
                     if (ck == 1){  
                         if (ip[i]%2 == 0)  
                             ipre = ip[i] + 1;  
                         else  
                             ipre = ip[i] - 1;  
                         for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){  
                             clevel1.estrip[j][m][l] = clevel1.estrip[j][m][l] + (qpre[ipre] - qpre[ip[i]]) * 0.00478;  
                         };  
                     };  
                     if (ck == 2){  
                         for (Int_t j = i*32 ; j < (i+1)*32 ; j++){  
                             ipre = j/16 + 1;  
                             clevel1.estrip[j][m][l] = clevel1.estrip[j][m][l] + qpre[ipre] * 0.00478;  
                         };  
                     };  
                 };  
                 //  
                 Int_t jj = -2;  
                 Int_t jjj = -3;  
                 for (Int_t j = 0 ; j < 98 ; j++){  
                     jj++;  
                     jjj++;  
                     if ( j < 96 ) ene[j] = clevel1.estrip[j][m][l];  
                     if ( jj >= 0 && jj < 96 ){  
                         if ( jj != 0 && jj != 32 && jj != 64 ) ene[jj-1] = ene[jj-1] - clevel1.estrip[jj][m][l] * 0.01581;  
                         if ( jj != 31 && jj != 63 && jj != 95 ) ene[jj+1] = ene[jj+1] - clevel1.estrip[jj][m][l] * 0.01581;                      
                     };  
                     if ( jjj >= 0 && jjj < 96 ){  
                         if ( jjj != 0 && jjj != 32 && jjj != 64 ) clevel1.estrip[jjj-1][m][l] = clevel1.estrip[jjj-1][m][l] - ene[jjj] * 0.01581;  
                         if ( jjj != 31 && jjj != 63 && jjj != 95 ) clevel1.estrip[jjj+1][m][l] = clevel1.estrip[jjj+1][m][l] - ene[jjj] * 0.01581;  
                     };  
                 };  
529                  //                  //
530              } else {                  qpre[pre] += clevel1.estrip[n][m][l];
531                  for (Int_t nn = 0; nn < 96; nn++){                                  };
                     clevel1.estrip[nn][m][l] = 0.;  
                 };  
532              };              };
533              };
534              if (ck == 1){
535                if (ip[i]%2 == 0)
536                  ipre = ip[i] + 1;
537                else
538                  ipre = ip[i] - 1;
539                for (Int_t j = ipre*16 ; j < (ipre+1)*16 ; j++){
540                  clevel1.estrip[j][m][l] = clevel1.estrip[j][m][l] + (qpre[ipre] - qpre[ip[i]]) * 0.00478;
541                };
542              };
543              if (ck == 2){
544                for (Int_t j = i*32 ; j < (i+1)*32 ; j++){
545                  ipre = j/16 + 1;
546                  clevel1.estrip[j][m][l] = clevel1.estrip[j][m][l] + qpre[ipre] * 0.00478;
547                };
548              };
549            };
550            //
551            Int_t j4 = -4;
552            Int_t jjj = -3;
553            Int_t jj = -2;
554            for (Int_t j = 0 ; j < 100 ; j++){
555              jj++;
556              jjj++;
557              j4++;
558              if ( j < 96 ) ene[j] = clevel1.estrip[j][m][l];
559              if ( jj >= 0 && jj < 96 ){
560                if ( jj != 0 && jj != 32 && jj != 64 ) ene[jj-1] = ene[jj-1] - clevel1.estrip[jj][m][l] * 0.01581;
561                if ( jj != 31 && jj != 63 && jj != 95 ) ene[jj+1] = ene[jj+1] - clevel1.estrip[jj][m][l] * 0.01581;                
562              };
563              if ( jjj >= 0 && jjj < 96 ){
564                if ( jjj != 0 && jjj != 32 && jjj != 64 ) clevel1.estrip[jjj-1][m][l] = clevel1.estrip[jjj-1][m][l] - ene[jjj] * 0.01581;
565                if ( jjj != 31 && jjj != 63 && jjj != 95 ) clevel1.estrip[jjj+1][m][l] = clevel1.estrip[jjj+1][m][l] - ene[jjj] * 0.01581;
566              };
567              if ( j4 >= 0 && j4 < 96 ){
568                if ( clevel1.estrip[j4][m][l]!=0. && ( clevel1.estrip[j4][m][l] < evento.emin || calib.calrms[l][m][j4] > 26 )){
569                  clevel1.estrip[j4][m][l] = 0.;
570                };
571              };
572            };
573            //
574          } else {
575            for (Int_t nn = 0; nn < 96; nn++){                  
576              clevel1.estrip[nn][m][l] = 0.;
577          };          };
     };  
     //  
     //  
     //  
     if ( !pe && calib.good2 ){  
         clevel2.good = 1;  
     } else {  
         clevel2.good = 0;  
     };  
     //  
     // all we need from the tracker is the state vector al_p and the boolean for good events:  
     //  
     if ( calib.good2 ){  
       for (Int_t e = 0; e<5 ; e++){  
         clevel1.al_p[e][0] = calib.al_p[e][0];  
         clevel1.al_p[e][1] = calib.al_p[e][1];  
       };  
       clevel1.good2 = 1;  
     } else {  
       for (Int_t e = 0; e<5 ; e++){  
         clevel1.al_p[e][0] = 0.;  
         clevel1.al_p[e][1] = 0.;  
578        };        };
       clevel1.good2 = 0;  
579      };      };
580      //    };
581      // for each event generate level2 data (clevel2) calling the fortran routine and passing calibrated data (clevel1):    //
582      //    //
583      cfillcalol2(clevel1,clevel2);    //
584      //          if ( !pe && calib.good2 ){
585      // save in the class the level2 calorimeter informations      clevel2.good = 1;
586      //    } else {
587      //      clevel2.good = 0;
588      // fill the rootple if not in paw mode    };
589      //    //
590      if ( clevel1.paw == 0. ) tree->Fill();    // all we need from the tracker is the state vector al_p and the boolean for good events:
591      //    //
592      // clear estrip values.    clevel1.trkchi2 = calib.trkchi2;
593      //    if ( calib.good2 ){
594      memcpy(clevel1.estrip, estripnull, sizeof(estripnull));      for (Int_t e = 0; e<5 ; e++){
595      //        clevel1.al_p[e][0] = calib.al_p[e][0];
596      return(0);        clevel1.al_p[e][1] = calib.al_p[e][1];
597        };
598        clevel1.good2 = 1;
599      } else {
600        for (Int_t e = 0; e<5 ; e++){
601          clevel1.al_p[e][0] = 0.;
602          clevel1.al_p[e][1] = 0.;
603        };
604        clevel1.good2 = 0;
605      };
606      //
607      // for each event generate level2 data (clevel2) calling the fortran routine and passing calibrated data (clevel1):
608      //    
609      cfillcalol2(clevel1,clevel2);
610      //      
611      // save in the class the level2 calorimeter informations
612      //
613      //
614      // fill the rootple if not in paw mode
615      //
616      if ( clevel1.paw == 0. ) tree->Fill();
617      //
618      // clear estrip values.
619      //
620      memcpy(clevel1.estrip, estripnull, sizeof(estripnull));
621      //
622      return(0);
623  }  }
624  //  //
625  short int CaloLEVEL2(TString filename,  TString TrackerDir="", TString outDir ="", TString Framework = "root", Int_t FORCE = 0){  short int CaloLEVEL2(TString filename,  TString TrackerDir="", TString outDir ="", TString Framework = "root", Int_t FORCE = 0){
626      //    //
627      // define variables    // define variables
628      //    //
629      Int_t swcode = 04;    Int_t swcode = 04;
630      Int_t swtrkcode = 2;    Int_t swtrkcode = 0;
631      Int_t MySQL = 1;    Int_t MySQL = 1;
632      Int_t debug = 0;    Int_t debug = 0;
633      //    //
634  #if defined(__CINT__)  #if defined(__CINT__)
635      if ( !gSystem->Getenv("PAM_INC") || !gSystem->Getenv("PAM_LIB") || !gSystem->Getenv("PAM_CALIB") ){    if ( !gSystem->Getenv("PAM_INC") || !gSystem->Getenv("PAM_LIB") || !gSystem->Getenv("PAM_CALIB") ){
636          printf("\n ERROR: you first must set the PAMELA enviromental variables\n        read the README file \n");      printf("\n ERROR: you first must set the PAMELA enviromental variables\n        read the README file \n");
637          printf("\n Exiting... \n");      printf("\n Exiting... \n");
638          return(11);      return(11);
639      };    };
640      emicheckLib();    emicheckLib();
641  #endif  #endif
642      Int_t trkdir = 1;    Int_t trkdir = 1;
643      TString trackerdir;    TString trackerdir;
644      if ( TrackerDir=="" ){    if ( TrackerDir=="" ){
645          trackerdir = filename;      trackerdir = filename;
646          trkdir = 0;      trkdir = 0;
647      } else {    } else {
648          trackerdir = TrackerDir;      trackerdir = TrackerDir;
649      };    };
650      TString calcalibfile;    TString calcalibfile;
651      //    //
652      // other variables    // other variables
653      //    //
654      TFile       *headerFile;    TFile       *headerFile;
655      TFile       *caloFile;    TFile       *caloFile;
656      TFile       *triggerFile;    TFile       *triggerFile;
657      TString filety;    TString filety;
658      stringstream file2;    stringstream file2;
659      stringstream file3;    stringstream file3;
660      const char *file4;    const char *file4;
661      Int_t okcalo = 0;    Int_t okcalo = 0;
662      TTree *ctree = 0;    TTree *ctree = 0;
663      TFile *chfile;    TFile *chfile;
664      Long64_t cnevents=0;    Long64_t cnevents=0;
665      stringstream qy;    stringstream qy;
666      qy.str("");    qy.str("");
667      Int_t chkmysql = 0;    Int_t chkmysql = 0;
668      Int_t imtrack;    Int_t imtrack;
669      //    //
670      // load libraries to determine the track information.    // load libraries to determine the track information.
671      //    //
672  #if defined (__CINT__)      #if defined (__CINT__)    
673      const char *pam_lib=gSystem->Getenv("PAM_LIB");    const char *pam_lib=gSystem->Getenv("PAM_LIB");
674      stringstream libload;    stringstream libload;
675      libload.str("");
676      libload << pam_lib << "/libtrack";
677      gSystem->Load(libload.str().c_str());
678      libload.str("");
679      libload << pam_lib << "/libreadb2maps";
680      gSystem->Load(libload.str().c_str());
681      libload.str("");
682      libload << pam_lib << "/libreadb2maps_C";
683      gSystem->Load(libload.str().c_str());
684      //
685      // open libraries to extract calorimeter level2 data
686      //
687      libload.str("");
688      libload << pam_lib << "/libcrcalol2";
689      gSystem->Load(libload.str().c_str());
690      libload.str("");
691      libload << pam_lib << "/libcrcalol2_C";
692      gSystem->Load(libload.str().c_str());
693    #endif
694      const char *franame = Framework;
695    #if defined (__CINT__)    
696      if ( !strcmp(franame,"paw") ) {
697      libload.str("");      libload.str("");
698      libload << pam_lib << "/libtrack";      libload << pam_lib << "/libopcalol2";
699      gSystem->Load(libload.str().c_str());      gSystem->Load(libload.str().c_str());
700      libload.str("");      libload.str("");
701      libload << pam_lib << "/libreadb2maps";      libload << pam_lib << "/libopcalol2_C";
702      gSystem->Load(libload.str().c_str());      gSystem->Load(libload.str().c_str());
703      libload.str("");      libload.str("");
704      libload << pam_lib << "/libreadb2maps_C";      libload << pam_lib << "/libclcalol2";
705      gSystem->Load(libload.str().c_str());      gSystem->Load(libload.str().c_str());
     //  
     // open libraries to extract calorimeter level2 data  
     //  
706      libload.str("");      libload.str("");
707      libload << pam_lib << "/libcrcalol2";      libload << pam_lib << "/libclcalol2_C";
     gSystem->Load(libload.str().c_str());  
     libload.str("");  
     libload << pam_lib << "/libcrcalol2_C";  
708      gSystem->Load(libload.str().c_str());      gSystem->Load(libload.str().c_str());
709      };
710  #endif  #endif
711      const char *franame = Framework;    if ( MySQL != 1 ){
712  #if defined (__CINT__)          printf("\nWARNING: you MUST run this script using the MySQL database. Reverting MySQL to 1 \n\n");
713      if ( !strcmp(franame,"paw") ) {      MySQL = 1;
714          libload.str("");    };
715          libload << pam_lib << "/libopcalol2";    //MySQL = 0;
716          gSystem->Load(libload.str().c_str());    gDirectory->GetList()->Delete();
717          libload.str("");    const char* startingdir = gSystem->WorkingDirectory();
718          libload << pam_lib << "/libopcalol2_C";    TString path;
719          gSystem->Load(libload.str().c_str());    stringcopy(path,startingdir);
720          libload.str("");    //
         libload << pam_lib << "/libclcalol2";  
         gSystem->Load(libload.str().c_str());  
         libload.str("");  
         libload << pam_lib << "/libclcalol2_C";  
         gSystem->Load(libload.str().c_str());  
     };  
 #endif  
     if ( MySQL != 1 ){  
         printf("\nWARNING: you MUST run this script using the MySQL database. Reverting MySQL to 1 \n\n");  
         MySQL = 1;  
     };  
     //MySQL = 0;  
     gDirectory->GetList()->Delete();  
     const char* startingdir = gSystem->WorkingDirectory();  
     TString path;  
     stringcopy(path,startingdir);  
     //  
721  #if defined (__CINT__)    #if defined (__CINT__)  
722      Int_t MySQLload = 0;    Int_t MySQLload = 0;
723  #endif  #endif
724      //    //
725      // Open files    // Open files
726      //    //
727      headerFile = emigetFile(filename, "Physics", "Header");    headerFile = emigetFile(filename, "Physics", "Header");
728      if ( !headerFile ){    if ( !headerFile ){
729          printf("\n\n ERROR: no header file, exiting...\n\n");      printf("\n\n ERROR: no header file, exiting...\n\n");
730  #if defined (__CINT__)                            #if defined (__CINT__)                          
731          gSystem->ChangeDirectory(path);      gSystem->ChangeDirectory(path);
732          unloadf77lib(Framework,MySQLload);      unloadf77lib(Framework,MySQLload);
733  #endif  #endif
734          return(1);      return(1);
735      };    };
736      caloFile = emigetFile(filename, "Calorimeter");    caloFile = emigetFile(filename, "Calorimeter");
737      if ( !caloFile ){    if ( !caloFile ){
738          printf("\n\n ERROR: no calorimeter file! \n\n");      printf("\n\n ERROR: no calorimeter file! \n\n");
739  #if defined (__CINT__)                            #if defined (__CINT__)                          
740          gSystem->ChangeDirectory(path);      gSystem->ChangeDirectory(path);
741          unloadf77lib(Framework,MySQLload);      unloadf77lib(Framework,MySQLload);
742  #endif  #endif
743          return(2);      return(2);
744      };        };    
745      //    //
746      TTree *otr = (TTree*)headerFile->Get("Pscu");    TTree *otr = (TTree*)headerFile->Get("Pscu");
747      otr->AddFriend("Calorimeter",caloFile);    otr->AddFriend("Calorimeter",caloFile);
748      triggerFile = emigetFile(filename, "Trigger");    triggerFile = emigetFile(filename, "Trigger");
749      if ( !triggerFile ){    if ( !triggerFile ){
750          printf("\n\n ERROR: No trigger file! \n");      printf("\n\n ERROR: No trigger file! \n");
751  #if defined (__CINT__)                            #if defined (__CINT__)                          
752          gSystem->ChangeDirectory(path);      gSystem->ChangeDirectory(path);
753          unloadf77lib(Framework,MySQLload);      unloadf77lib(Framework,MySQLload);
754  #endif  #endif
755          return(1);      return(1);
756      } else {    } else {
757          otr->AddFriend("Trigger",triggerFile);      otr->AddFriend("Trigger",triggerFile);
758      };    };
759      //    //
760      // Define some basic variables    // Define some basic variables
761      //    //
762      struct Evento evento;    struct Evento evento;
763      struct Calib calib;    struct Calib calib;
764      struct Mystruct mystruct;    struct Mystruct mystruct;
765      evento.emin = 0.7;    evento.emin = 0.7;
766      Long64_t nevents    = otr->GetEntries();    Long64_t nevents    = otr->GetEntries();
767      Int_t snevents = (Int_t)nevents;    Int_t snevents = (Int_t)nevents;
768      if ( nevents < 1 ) {    if ( nevents < 1 ) {
769          printf(" ERROR: the file is empty!\n\n");      printf(" ERROR: the file is empty!\n\n");
770    #if defined (__CINT__)                          
771        gSystem->ChangeDirectory(path);
772        unloadf77lib(Framework,MySQLload);
773    #endif
774        return(7);
775      };
776      Int_t b[4];
777      Int_t paw = 0;
778      //
779      const string myfil = (const char *)filename;
780      Int_t myposiz = myfil.find("dw_");    
781      calib.DW = 0;
782      if ( myposiz == -1 ) {
783        myposiz = myfil.find("DW_");
784        calib.DW = 1;
785      };
786      if ( myposiz == -1 ) return(6);
787      stringcopy(calib.basepath,filename,0,myposiz);
788      stringcopy(calib.yodalev,filename,myposiz+13,myposiz+15);
789      //
790      // calibration filename
791      //
792      calcalibfile = filename;
793      //
794      // create the ntuple level2 name depending on framework and starting from the YODA name
795      //    
796      if ( !strcmp(franame,"paw") ) paw = 1;
797      //
798      if ( paw ){
799        calib.ispaw = 1.;
800        filety = "rz";
801      } else {
802        calib.ispaw = 0.;
803        filety = "root";
804      };
805      //
806      TString numb("2");  // level
807      TString detc("Calorimeter");  // detector      
808      char *file = getLEVname(filename,detc,numb,filety);
809      stringstream truefile;
810      truefile.str("");
811      truefile << file;
812      //
813      if ( outDir == "" ){
814        file4 = filename;
815        file3.str("");
816        file3 << file4 << "/Physics/Level2";
817      } else {
818        file4 = outDir;
819        file3.str("");
820        file3 << file4;
821      }
822      //
823      file2.str("");
824      file2 << file3.str().c_str() << "/";
825      file2 << truefile.str().c_str();
826      printf("\n Filename will be: \n %s \n\n",file2.str().c_str());
827      //
828      // check if Level2 directory exists, if not create it.
829      //    
830      Int_t ERR = gSystem->MakeDirectory(file3.str().c_str());    
831      //
832      if ( !ERR ) {
833        printf(" LEVEL2 directory doesn't exist! Creating LEVEL2 directory \n\n");
834      } else {
835        //
836        // directory exists, check if file exists (if we are not in the force mode)
837        //          
838        if ( !FORCE ) {
839          printf(" Not in FORCE mode, check the existence of LEVEL2 data: \n\n");      
840          Int_t nofile = 0;
841          if ( paw ){
842            ifstream mypawfile;
843            mypawfile.open(file2.str().c_str());
844            if ( mypawfile ){
845              nofile = 1;
846            } else {
847              printf("Error in opening file: file %s does not exist \n",file2.str().c_str());
848            };
849          } else {
850            TFile tfile(file2.str().c_str());
851            if ( !tfile.IsZombie() ) nofile = 1;
852          };
853          if ( nofile ){
854            printf(" ERROR: file already exists! Use FORCE = 1 to override \n\n");
855  #if defined (__CINT__)                            #if defined (__CINT__)                          
856          gSystem->ChangeDirectory(path);          gSystem->ChangeDirectory(path);
857          unloadf77lib(Framework,MySQLload);          unloadf77lib(Framework,MySQLload);
858  #endif  #endif
859          return(7);          return(3);          
860      };        } else {
861      Int_t b[4];          printf("\n OK, I will create it!\n\n");
862      Int_t paw = 0;        };
     //  
     const string myfil = (const char *)filename;  
     Int_t myposiz = myfil.find("dw_");      
     calib.DW = 0;  
     if ( myposiz == -1 ) {  
       myposiz = myfil.find("DW_");  
       calib.DW = 1;  
     };  
     if ( myposiz == -1 ) return(6);  
     stringcopy(calib.basepath,filename,0,myposiz);  
     stringcopy(calib.yodalev,filename,myposiz+13,myposiz+15);  
     //  
     // calibration filename  
     //  
     calcalibfile = filename;  
     //  
     // create the ntuple level2 name depending on framework and starting from the YODA name  
     //      
     if ( !strcmp(franame,"paw") ) paw = 1;  
     //  
     if ( paw ){  
         calib.ispaw = 1.;  
         filety = "rz";  
     } else {  
         calib.ispaw = 0.;  
         filety = "root";  
863      };      };
864      //    };
865      TString numb("2");  // level    //    
866      TString detc("Calorimeter");  // detector          // clear calibration time intervals
867      char *file = getLEVname(filename,detc,numb,filety);    //
868      stringstream truefile;    for (Int_t s=0; s<4;s++){
869      truefile.str("");      for (Int_t d = 0; d<51; d++){
870      truefile << file;        calib.ttime[s][d] = 0 ;
871      //        calib.time[s][d] = 0 ;
872      if ( outDir == "" ){      };
873          file4 = filename;    };
874          file3.str("");    //
875          file3 << file4 << "/Physics/Level2";    // open and read the rootple containing the ADC to MIP conversion values
876      //
877      stringstream calfile;
878      calfile.str("");
879      const char *pam_calib = pathtocalibration();
880      calfile << pam_calib << "/CaloADC2MIP.root";
881      //
882      printf(" Using calorimeter calibration file: \n %s \n",calfile.str().c_str());
883      chfile = new TFile(calfile.str().c_str(),"READ","Calorimeter CALIBRATION data");
884      CalorimeterCalibration *ccalo = 0;
885      if ( chfile->IsZombie() ){    
886        if ( !FORCE ){
887          printf("\n ERROR: no calorimeter calibration file! \n If you want to proceed using 26 as conversion factor for all strips use FORCE = 1. \n\n");
888          return(10);
889      } else {      } else {
890          file4 = outDir;        printf("\n WARNING: no calorimeter calibration file! \n WARNING: using 26 as conversion factor for all strips. \n");
891          file3.str("");        okcalo = 0;
         file3 << file4;  
     }  
     //  
     file2.str("");  
     file2 << file3.str().c_str() << "/";  
     file2 << truefile.str().c_str();  
     printf("\n Filename will be: \n %s \n\n",file2.str().c_str());  
     //  
     // check if Level2 directory exists, if not create it.  
     //      
     Int_t ERR = gSystem->MakeDirectory(file3.str().c_str());      
     //  
     if ( !ERR ) {  
         printf(" LEVEL2 directory doesn't exist! Creating LEVEL2 directory \n\n");  
     } else {  
         //  
         // directory exists, check if file exists (if we are not in the force mode)  
         //        
         if ( !FORCE ) {  
             printf(" Not in FORCE mode, check the existence of LEVEL2 data: \n\n");          
             Int_t nofile = 0;  
             if ( paw ){  
                 ifstream mypawfile;  
                 mypawfile.open(file2.str().c_str());  
                 if ( mypawfile ){  
                     nofile = 1;  
                 } else {  
                     printf("Error in opening file: file %s does not exist \n",file2.str().c_str());  
                 };  
             } else {  
                 TFile tfile(file2.str().c_str());  
                 if ( !tfile.IsZombie() ) nofile = 1;  
             };  
             if ( nofile ){  
                 printf(" ERROR: file already exists! Use FORCE = 1 to override \n\n");  
 #if defined (__CINT__)                            
                 gSystem->ChangeDirectory(path);  
                 unloadf77lib(Framework,MySQLload);  
 #endif  
                 return(3);            
             } else {  
                 printf("\n OK, I will create it!\n\n");  
             };  
         };  
892      };      };
893      //        } else {
894      // clear calibration time intervals      okcalo = 1;
895      //      ctree = (TTree*)chfile->Get("CaloADC");  
896      for (Int_t s=0; s<4;s++){      ctree->SetBranchAddress("Event", &ccalo);    
897          for (Int_t d = 0; d<51; d++){      //
898              calib.ttime[s][d] = 0 ;      cnevents = ctree->GetEntries();
899              calib.time[s][d] = 0 ;      ctree->GetEntry(cnevents-1);
900          };    };
901      };    //
902      //    if ( okcalo ) {
903      // open and read the rootple containing the ADC to MIP conversion values      for (Int_t m = 0; m < 2 ; m++ ){
904      //        for (Int_t k = 0; k < 22; k++ ){
905      stringstream calfile;          for (Int_t l = 0; l < 96; l++ ){
906      calfile.str("");            if ( (ccalo->fp[1][m][k][l] > 20. && ccalo->fp[1][m][k][l] < 32.) || ccalo->mask[m][k][l] == 1. ) {
907      const char *pam_calib = pathtocalibration();              calib.mip[m][k][l] = ccalo->mip[m][k][l];
908      calfile << pam_calib << "/CaloADC2MIP.root";              if ( calib.mip[m][k][l] == 0  ) calib.mip[m][k][l] = 26. ;
909      //            } else {
910      printf(" Using calorimeter calibration file: \n %s \n",calfile.str().c_str());              calib.mip[m][k][l] = 26. ;
911      chfile = new TFile(calfile.str().c_str(),"READ","Calorimeter CALIBRATION data");            };
912      CalorimeterCalibration *ccalo = 0;            calib.calped[m][k][l] = 0. ;
913      if ( chfile->IsZombie() ){              evento.dexy[m][k][l] = 0. ;
914          if ( !FORCE ){            evento.dexyc[m][k][l] = 0. ;
             printf("\n ERROR: no calorimeter calibration file! \n If you want to proceed using 26 as conversion factor for all strips use FORCE = 1. \n\n");  
             return(10);  
         } else {  
             printf("\n WARNING: no calorimeter calibration file! \n WARNING: using 26 as conversion factor for all strips. \n");  
             okcalo = 0;  
915          };          };
916      } else {        };
         okcalo = 1;  
         ctree = (TTree*)chfile->Get("CaloADC");    
         ctree->SetBranchAddress("Event", &ccalo);      
         //  
         cnevents = ctree->GetEntries();  
         ctree->GetEntry(cnevents-1);  
917      };      };
918      //    } else {
919      if ( okcalo ) {      for (Int_t m = 0; m < 2 ; m++ ){
920          for (Int_t m = 0; m < 2 ; m++ ){        for (Int_t k = 0; k < 22; k++ ){
921              for (Int_t k = 0; k < 22; k++ ){          for (Int_t l = 0; l < 96; l++ ){
922                  for (Int_t l = 0; l < 96; l++ ){            calib.mip[m][k][l] = 26. ;
923                      if ( (ccalo->fp[1][m][k][l] > 20. && ccalo->fp[1][m][k][l] < 32.) || ccalo->mask[m][k][l] == 1. ) {            calib.calped[m][k][l] = 0. ;
924                          calib.mip[m][k][l] = ccalo->mip[m][k][l];            evento.dexy[m][k][l] = 0. ;
925                          if ( calib.mip[m][k][l] == 0  ) calib.mip[m][k][l] = 26. ;            evento.dexyc[m][k][l] = 0. ;
                     } else {  
                         calib.mip[m][k][l] = 26. ;  
                     };  
                     calib.calped[m][k][l] = 0. ;  
                     evento.dexy[m][k][l] = 0. ;  
                     evento.dexyc[m][k][l] = 0. ;  
                 };  
             };  
         };  
     } else {  
         for (Int_t m = 0; m < 2 ; m++ ){  
             for (Int_t k = 0; k < 22; k++ ){  
                 for (Int_t l = 0; l < 96; l++ ){  
                     calib.mip[m][k][l] = 26. ;  
                     calib.calped[m][k][l] = 0. ;  
                     evento.dexy[m][k][l] = 0. ;  
                     evento.dexyc[m][k][l] = 0. ;  
                 };  
             };  
926          };          };
927          };
928      };      };
929      chfile->Close();    };
930      //    chfile->Close();
931      // Are we able to connect to the MySQL server?    //
932      //    // Are we able to connect to the MySQL server?
933      if ( MySQL ){    //
934          calib.mysql = 1;    if ( MySQL ){
935          gSystem->ChangeDirectory(path);      calib.mysql = 1;
936        gSystem->ChangeDirectory(path);
937  #if defined (__CINT__)      #if defined (__CINT__)    
938          libload.str("");      libload.str("");
939          libload << pam_lib << "/libreadmy";      libload << pam_lib << "/libreadmy";
940          gSystem->Load(libload.str().c_str());      gSystem->Load(libload.str().c_str());
941          libload.str("");      libload.str("");
942          libload << pam_lib << "/libreadmy_C";      libload << pam_lib << "/libreadmy_C";
943          gSystem->Load(libload.str().c_str());      gSystem->Load(libload.str().c_str());
944  #endif  #endif
945          calib.db = "romemuons";      calib.db = "romemuons";
946          string myfil = (const char*)filename;      string myfil = (const char*)filename;
947          Int_t myposiz = myfil.find("dw_");      Int_t myposiz = myfil.find("dw_");
948          if ( myposiz == -1 ) myposiz = myfil.find("DW_");            if ( myposiz == -1 ) myposiz = myfil.find("DW_");      
949          if ( myposiz == -1 ) return(6);      if ( myposiz == -1 ) return(6);
950          //      //
951          TString mytmp;      TString mytmp;
952          Int_t mspos = myposiz+2;      Int_t mspos = myposiz+2;
953          Int_t mepos = myposiz+13;      Int_t mepos = myposiz+13;
954          stringcopy(mytmp,filename,mspos,mepos);      stringcopy(mytmp,filename,mspos,mepos);
955          const char *myfile = mytmp;      const char *myfile = mytmp;
956          //      //
957          stringstream tablename;      stringstream tablename;
958          tablename.str("");      tablename.str("");
959          tablename << "calocalib_dw" << myfile;      tablename << "calocalib_dw" << myfile;
960          TString tabula;      TString tabula;
961          tabula = tablename.str().c_str();      tabula = tablename.str().c_str();
962          //      //
963          printf("\n Try the connection to the MySQL database in Trieste...\n");      printf("\n Try the connection to the MySQL database in Trieste...\n");
964          qy.str("");      qy.str("");
965          qy << "select * from " << tablename.str().c_str();      qy << "select * from " << tablename.str().c_str();
966          chkmysql = creadmysql(calib.db,(char *)qy.str().c_str(),mystruct);            chkmysql = creadmysql(calib.db,(char *)qy.str().c_str(),mystruct);  
967          if ( chkmysql || mystruct.totime == -1 ) {      if ( chkmysql || mystruct.totime == -1 ) {
968              printf("\n- ERROR: problems querying MySQL database! -\n\n- ERROR: Cannot use the MySQL database! -\n\n");        printf("\n- ERROR: problems querying MySQL database! -\n\n- ERROR: Cannot use the MySQL database! -\n\n");
969              if ( mystruct.totime == -1 ) printf("- ERROR: it seems there are no data in table called %s -\n\n",tablename.str().c_str());        if ( mystruct.totime == -1 ) printf("- ERROR: it seems there are no data in table called %s -\n\n",tablename.str().c_str());
970              if ( chkmysql == 1 ) printf("- ERROR: problems in MySQL initialization -\n\n");        if ( chkmysql == 1 ) printf("- ERROR: problems in MySQL initialization -\n\n");
971              if ( chkmysql == 2 ) printf("- ERROR: problems in MySQL login, check username and password -\n\n");        if ( chkmysql == 2 ) printf("- ERROR: problems in MySQL login, check username and password -\n\n");
972              if ( chkmysql == 3 ) printf("- ERROR: it seems there is no table called %s -\n\n",tablename.str().c_str());        if ( chkmysql == 3 ) printf("- ERROR: it seems there is no table called %s -\n\n",tablename.str().c_str());
973              //      if ( !FORCE ){        //            if ( !FORCE ){
974              //          printf(" Exiting! Use FORCE = 1 or MySQL = 0 to run in standalone mode \n\n");        //                printf(" Exiting! Use FORCE = 1 or MySQL = 0 to run in standalone mode \n\n");
975              printf(" Exiting! Check your installation or contact calorimeter developers \n\n");        printf(" Exiting! Check your installation or contact calorimeter developers \n\n");
976  #if defined (__CINT__)                            #if defined (__CINT__)                          
977              gSystem->ChangeDirectory(path);        gSystem->ChangeDirectory(path);
978              unloadf77lib(Framework,MySQL);        unloadf77lib(Framework,MySQL);
979  #endif  #endif
980              return(5);        return(5);
981              //};        //};
982          //          printf("WARNING: running in standalone mode! \n\n");        //            printf("WARNING: running in standalone mode! \n\n");
983          //   printf("WARNING: I will search for the best calibration\n\n");        //   printf("WARNING: I will search for the best calibration\n\n");
984          //   calib.mysql = 0;        //   calib.mysql = 0;
985          //    MySQL = -1;        //    MySQL = -1;
         } else {  
             printf(" ...OK, the connection is fine! \n\n Using database \"%s\", table \"%s\"\n\n",calib.db,tablename.str().c_str());  
         };  
         calib.tablename = tablename.str().c_str();  
986      } else {      } else {
987          calib.tablename = "";        printf(" ...OK, the connection is fine! \n\n Using database \"%s\", table \"%s\"\n\n",calib.db,tablename.str().c_str());
         calib.mysql = 0;  
988      };      };
989      if ( MySQL < 1 ){      calib.tablename = tablename.str().c_str();
990          //    } else {
991          // first of all find the best calibrations for this file      calib.tablename = "";
992          //      calib.mysql = 0;
993          Int_t wused = 0;    };
994          TString nfilen;    if ( MySQL < 1 ){
995          stringcopy(nfilen,file2.str().c_str());      //
996          CaloFindCalibs(filename, calcalibfile, wused, calib);              // first of all find the best calibrations for this file
997          if ( wused == 1 ) calcalibfile = filename;      //
998          //      Int_t wused = 0;
999          // print on the screen the results:        TString nfilen;
1000          //      stringcopy(nfilen,file2.str().c_str());
1001          const char *ffile = filename;                CaloFindCalibs(filename, calcalibfile, wused, calib);        
1002          printf(" ------ %s ------- \n \n",ffile);            if ( wused == 1 ) calcalibfile = filename;
1003          Int_t calibex = 0;      //
1004          for (Int_t s=0; s<4;s++){      // print on the screen the results:  
1005              printf(" ** SECTION %i **\n",s);      //
1006              for (Int_t d = 0; d<51; d++){      const char *ffile = filename;              
1007                  TString pfile;      printf(" ------ %s ------- \n \n",ffile);  
1008                  if ( calib.ttime[s][d] != 0 ) {      Int_t calibex = 0;
1009                      calibex++;      for (Int_t s=0; s<4;s++){
1010                      if ( calib.fcode[s][d] != 10 ){        printf(" ** SECTION %i **\n",s);
1011                          TString file2f = "";        for (Int_t d = 0; d<51; d++){
1012                          stringcopy(file2f,calcalibfile);          TString pfile;
1013                          pfile = whatnamewith(file2f,calib.fcode[s][d]);                      if ( calib.ttime[s][d] != 0 ) {
1014                      } else {            calibex++;
1015                          pfile = (TString)calcalibfile;            if ( calib.fcode[s][d] != 10 ){
1016                      };              TString file2f = "";
1017                      const char *ffile = pfile;                        stringcopy(file2f,calcalibfile);
1018                      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);              pfile = whatnamewith(file2f,calib.fcode[s][d]);                
1019                      if ( !strcmp(ffile,"wrong") ) calibex--;            } else {
1020                  };              pfile = (TString)calcalibfile;
1021              };            };
1022              printf("\n");                  const char *ffile = pfile;            
1023              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);
1024              if ( !strcmp(ffile,"wrong") ) calibex--;
1025          };          };
1026          printf(" ----------------------------------------------------------------------- \n \n");        };
1027          printf("\n");    
1028        };
1029        printf(" ----------------------------------------------------------------------- \n \n");
1030            
1031          //      //
1032          //      const char *file2 = nfilen; // the full path and name of the level1 ntuple        //  const char *file2 = nfilen; // the full path and name of the level1 ntuple  
1033          file2.str("");      file2.str("");
1034          file2 << nfilen;      file2 << nfilen;
1035          if ( calibex < 4 ) {      if ( calibex < 4 ) {
1036              printf("No full calibration data in this file, sorry!\n\n ");        printf("No full calibration data in this file, sorry!\n\n ");
1037              //        //
1038              // remove the empty file!        // remove the empty file!
1039              //        //
1040              stringstream remfile;        stringstream remfile;
1041              remfile.str("");        remfile.str("");
1042              remfile << "rm -f " << file2.str().c_str();        remfile << "rm -f " << file2.str().c_str();
1043              gSystem->Exec(remfile.str().c_str());        gSystem->Exec(remfile.str().c_str());
1044  #if defined (__CINT__)                            #if defined (__CINT__)                          
1045              gSystem->ChangeDirectory(path);        gSystem->ChangeDirectory(path);
1046              unloadf77lib(Framework,MySQL);        unloadf77lib(Framework,MySQL);
1047  #endif  #endif
1048              return(4);        return(4);
         };  
           
         //  
         // fill with the first calibration values the common containing calibration informations  
         //  
         for (Int_t s = 0; s < 4; s++){  
             b[s]=0;  
             TString pfile;  
             if ( calib.fcode[s][b[s]] != 10 ){  
                 TString file2f = "";  
                 stringcopy(file2f,calcalibfile);  
                 pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);                    
             } else {  
                 pfile = (TString)calcalibfile;  
             };  
             CaloPede(pfile,s,calib.ttime[s][b[s]],calib);  
         };            
     } else {  
         calib.obtjump = 0;  
         calib.time[0][0] = -1;  
         calib.time[1][0] = -1;  
         calib.time[2][0] = -1;  
         calib.time[3][0] = -1;  
1049      };      };
1050            
     //  
     // Open file to write depending on the framework  
     //  
     //  
     // In any case we must create a tree  
     //  
     TTree *tree = 0;  
     //  
     // Open tracker ntuple or rootples and load magnetic field maps.  
     //  
1051      //      //
1052      printf(" TRACKER: loading the magnetic field maps...\n\n\n");                            // fill with the first calibration values the common containing calibration informations
     pam_calib = pathtocalibration();  
     stringstream bdir;  
     bdir.str("");  
     bdir << pam_calib << ".";  
     creadB(bdir.str().c_str());  
1053      //      //
1054      printf(" ...done! \n");      for (Int_t s = 0; s < 4; s++){
1055          b[s]=0;
1056          TString pfile;
1057          if ( calib.fcode[s][b[s]] != 10 ){
1058            TString file2f = "";
1059            stringcopy(file2f,calcalibfile);
1060            pfile = whatnamewith(file2f,calib.fcode[s][b[s]]);                  
1061          } else {
1062            pfile = (TString)calcalibfile;
1063          };
1064          CaloPede(pfile,s,calib.ttime[s][b[s]],calib);
1065        };      
1066      } else {
1067        calib.obtjump = 0;
1068        calib.time[0][0] = -1;
1069        calib.time[1][0] = -1;
1070        calib.time[2][0] = -1;
1071        calib.time[3][0] = -1;
1072      };
1073    
1074      //
1075      // Open file to write depending on the framework
1076      //
1077      //
1078      // In any case we must create a tree
1079      //
1080      TTree *tree = 0;
1081      //
1082      // Open tracker ntuple or rootples and load magnetic field maps.
1083      //
1084      //
1085      printf(" TRACKER: loading the magnetic field maps...\n\n\n");                
1086      pam_calib = pathtocalibration();
1087      stringstream bdir;
1088      bdir.str("");
1089      bdir << pam_calib << ".";
1090      creadB(bdir.str().c_str());
1091      //
1092      printf(" ...done! \n");
1093      //
1094      printf("\n Check the existence of tracker data... \n");
1095      Int_t isrootple = 1;
1096      struct Tracklev2 trk;
1097      TFile *trfile = emigetFile(trackerdir,"Physics.Level2","Tracker");
1098      TTree *tr = 0;
1099      Int_t trkpaw = 0;
1100      Int_t trnevents = 0;    
1101      stringstream filetrk;
1102      if ( !trfile ) {      
1103        const char *file0 = filename;
1104        string fil = (const char *)filename;
1105        Int_t posiz = fil.find("dw_");
1106        if ( posiz == -1 ) posiz = fil.find("DW_");
1107        if ( posiz == -1 ) return(5);
1108        //
1109        TString trntmp;
1110        Int_t spos = posiz+3;
1111        Int_t epos = posiz+13;
1112        stringcopy(trntmp,filename,spos,epos);
1113        const char *trname = trntmp;
1114        if ( !trkdir ){
1115          filetrk.str("");
1116          filetrk << file0 << "/Physics/Level2/DW_";
1117          filetrk << trname << "_level2.rz";
1118        } else {
1119          const char *trackdir = trackerdir;
1120          filetrk.str("");
1121          filetrk << trackdir << "/DW_";
1122          filetrk << trname << "_level2.rz";
1123        };
1124      //      //
1125      printf("\n Check the existence of tracker data... \n");      ifstream mypawfile;
1126      Int_t isrootple = 1;      mypawfile.open(filetrk.str().c_str());
1127      struct Tracklev2 trk;      if ( mypawfile ){
1128      TFile *trfile = emigetFile(trackerdir,"Physics.Level2","Tracker");        trkpaw = 1;
1129      TTree *tr = 0;        isrootple = 0;    
1130      Int_t trkpaw = 0;        printf(" ...found tracker level2 NTUPLE:\n %s \n\n",filetrk.str().c_str());
     Int_t trnevents = 0;      
     stringstream filetrk;  
     if ( !trfile ) {      
         const char *file0 = filename;  
         string fil = (const char *)filename;  
         Int_t posiz = fil.find("dw_");  
         if ( posiz == -1 ) posiz = fil.find("DW_");  
         if ( posiz == -1 ) return(5);  
         //  
         TString trntmp;  
         Int_t spos = posiz+3;  
         Int_t epos = posiz+13;  
         stringcopy(trntmp,filename,spos,epos);  
         const char *trname = trntmp;  
         if ( !trkdir ){  
             filetrk.str("");  
             filetrk << file0 << "/Physics/Level2/DW_";  
             filetrk << trname << "_level2.rz";  
         } else {  
             const char *trackdir = trackerdir;  
             filetrk.str("");  
             filetrk << trackdir << "/DW_";  
             filetrk << trname << "_level2.rz";  
         };  
         //  
         ifstream mypawfile;  
         mypawfile.open(filetrk.str().c_str());  
         if ( mypawfile ){  
             trkpaw = 1;  
             isrootple = 0;        
             printf(" ...found tracker level2 NTUPLE:\n %s \n\n",filetrk.str().c_str());  
1131  #if defined (__CINT__)      #if defined (__CINT__)    
1132              const char *sdir=gSystem->Getenv("PAM_LIB");        const char *sdir=gSystem->Getenv("PAM_LIB");
1133              bdir.str("");        bdir.str("");
1134              bdir << sdir << "/liboptrklev2.so";        bdir << sdir << "/liboptrklev2.so";
1135              gSystem->Load(bdir.str().c_str());        gSystem->Load(bdir.str().c_str());
1136              bdir.str("");        bdir.str("");
1137              bdir << sdir << "/liboptrklev2_C.so";        bdir << sdir << "/liboptrklev2_C.so";
1138              gSystem->Load(bdir.str().c_str());        gSystem->Load(bdir.str().c_str());
1139              bdir.str("");        bdir.str("");
1140              bdir << sdir << "/libretrklev2.so";        bdir << sdir << "/libretrklev2.so";
1141              gSystem->Load(bdir.str().c_str());        gSystem->Load(bdir.str().c_str());
1142              bdir.str("");        bdir.str("");
1143              bdir << sdir << "/libretrklev2_C.so";        bdir << sdir << "/libretrklev2_C.so";
1144              gSystem->Load(bdir.str().c_str());        gSystem->Load(bdir.str().c_str());
1145              bdir.str("");        bdir.str("");
1146              bdir << sdir << "/libcltrklev2.so";        bdir << sdir << "/libcltrklev2.so";
1147              gSystem->Load(bdir.str().c_str());        gSystem->Load(bdir.str().c_str());
1148              bdir.str("");        bdir.str("");
1149              bdir << sdir << "/libcltrklev2_C.so";        bdir << sdir << "/libcltrklev2_C.so";
1150              gSystem->Load(bdir.str().c_str());        gSystem->Load(bdir.str().c_str());
1151  #endif        #endif      
1152              trnevents = 0;        trnevents = 0;
1153              coptrklev2((char *)filetrk.str().c_str(),trk,trnevents);                coptrklev2((char *)filetrk.str().c_str(),trk,trnevents);      
         } else {  
             printf("Error in opening file: file %s does not exist \n",file2.str().c_str());  
             printf("\n No tracker data! You must first process data with tracker programs\n and then you can run CaloLEVEL2. \n Exiting... \n\n");  
             return(8);  
         };  
1154      } else {      } else {
1155          //        printf("Error in opening file: file %s does not exist \n",file2.str().c_str());
1156          const char *file0 = filename;        printf("\n No tracker data! You must first process data with tracker programs\n and then you can run CaloLEVEL2. \n Exiting... \n\n");
1157          string fil = (const char *)filename;        return(8);
1158          Int_t posiz = fil.find("dw_");      };
1159          if ( posiz == -1 ) posiz = fil.find("DW_");    } else {
1160          if ( posiz == -1 ) return(5);      //
1161          //      const char *file0 = filename;
1162          TString trntmp;      string fil = (const char *)filename;
1163          Int_t spos = posiz;      Int_t posiz = fil.find("dw_");
1164          Int_t epos = posiz+15;      if ( posiz == -1 ) posiz = fil.find("DW_");
1165          stringcopy(trntmp,filename,spos,epos);      if ( posiz == -1 ) return(5);
1166          const char *trname = trntmp;      //
1167          //      TString trntmp;
1168          if ( !trkdir ){      Int_t spos = posiz;
1169              filetrk.str("");      Int_t epos = posiz+15;
1170              filetrk << file0 << "/Physics/Level2/";      stringcopy(trntmp,filename,spos,epos);
1171              filetrk << trname << ".Physics.Level2.Tracker.Event.root";      const char *trname = trntmp;
1172          } else {      //
1173              const char *trkdirname = trackerdir;      if ( !trkdir ){
1174              filetrk.str("");        filetrk.str("");
1175              filetrk << trkdirname << "/";        filetrk << file0 << "/Physics/Level2/";
1176              filetrk << trname << ".Physics.Level2.Tracker.Event.root";        filetrk << trname << ".Physics.Level2.Tracker.Event.root";
         };  
         //  
         printf(" ...found tracker level2 ROOTPLE:\n %s \n",filetrk.str().c_str());  
         tr = (TTree*) trfile->Get("TrkLevel2");  
         settrklev2(tr,trk);      
         trnevents = tr->GetEntries();      
     };  
     if ( trnevents != nevents ){  
         printf("\n WARNING: different length for tracker and calorimeter ntuples!\n");  
         printf("          calorimeter: %i events \n",(int)nevents);  
         printf("          tracker    : %i events \n\n",(int)trnevents);  
     };  
     //  
     stringstream name;  
     TString tmpfile2;  
     stringcopy(tmpfile2,file2.str().c_str());  
     const char *tmpname = tmpfile2;  
     TFile *hfile = 0;  
     struct CaLevel2 clevel2;  
     if ( paw ){  
         name.str("");  
         name << tmpname;  
         copencalol2((char *)name.str().c_str(),trkpaw,swcode,swtrkcode);  
1177      } else {      } else {
1178          char *type;        const char *trkdirname = trackerdir;
1179          type = "NEW";        filetrk.str("");
1180          if ( FORCE ) type = "RECREATE";        filetrk << trkdirname << "/";
1181          hfile = new TFile(file2.str().c_str(),type,"Calorimeter LEVEL2 data");        filetrk << trname << ".Physics.Level2.Tracker.Event.root";
1182          //      };
1183          //      hfile = new TFile(file2,type,"Calorimeter LEVEL2 data",0);      //
1184          //      printf(" ...found tracker level2 ROOTPLE:\n %s \n",filetrk.str().c_str());
1185          tree = new TTree("CaloLevel2","PAMELA Level2 calorimeter data");      tr = (TTree*) trfile->Get("TrkLevel2");
1186          tree->Branch("OBT",&clevel2.OBT,"OBT/I");      settrklev2(tr,trk);
1187          tree->Branch("pkt_num",&clevel2.pkt_num,"pkt_num/I");      trnevents = tr->GetEntries();    
1188          tree->Branch("pro_num",&clevel2.pro_num,"pro_num/I");    };
1189          tree->Branch("trigty",&clevel2.trigty,"trigty/F");    if ( trnevents != nevents ){
1190          tree->Branch("good",&clevel2.good,"good/I");      printf("\n WARNING: different length for tracker and calorimeter ntuples!\n");
1191          tree->Branch("perr",clevel2.perr,"perr[4]/I");      printf("          calorimeter: %i events \n",(int)nevents);
1192          tree->Branch("swerr",clevel2.swerr,"swerr[4]/I");      printf("          tracker    : %i events \n\n",(int)trnevents);
1193          tree->Branch("crc",clevel2.crc,"crc[4]/I");    };
1194          tree->Branch("nstrip",&clevel2.nstrip,"nstrip/F");    //
1195          tree->Branch("qtot",&clevel2.qtot,"qtot/F");    swtrkcode = trk.swcode;
1196          tree->Branch("ncore",&clevel2.ncore,"ncore/F");    //
1197          tree->Branch("qcore",&clevel2.qcore,"qcore/F");    stringstream name;
1198          tree->Branch("impx",&clevel2.impx,"impx/F");    TString tmpfile2;
1199          tree->Branch("impy",&clevel2.impy,"impy/F");    stringcopy(tmpfile2,file2.str().c_str());
1200          tree->Branch("tanx",&clevel2.tanx,"tanx/F");    const char *tmpname = tmpfile2;
1201          tree->Branch("tany",&clevel2.tany,"tany/F");    TFile *hfile = 0;
1202          tree->Branch("nint",&clevel2.nint,"nint/F");    struct CaLevel2 clevel2;
1203          tree->Branch("ncyl",&clevel2.ncyl,"ncyl/F");    if ( paw ){
1204          tree->Branch("qcyl",&clevel2.qcyl,"qcyl/F");      name.str("");
1205          tree->Branch("qtrack",&clevel2.qtrack,"qtrack/F");      name << tmpname;
1206          tree->Branch("qmax",&clevel2.qmax,"qmax/F");      copencalol2((char *)name.str().c_str(),trkpaw,swcode,swtrkcode);
1207          tree->Branch("nx22",&clevel2.nx22,"nx22/F");    } else {
1208          tree->Branch("qx22",&clevel2.nx22,"qx22/F");      char *type;
1209          tree->Branch("qq",clevel2.qq,"qq[4]/F");      type = "NEW";
1210          tree->Branch("qtrackx",&clevel2.qtrackx,"qtrackx/F");      if ( FORCE ) type = "RECREATE";
1211          tree->Branch("qtracky",&clevel2.qtracky,"qtracky/F");      hfile = new TFile(file2.str().c_str(),type,"Calorimeter LEVEL2 data");
1212          tree->Branch("dxtrack",&clevel2.dxtrack,"dxtrack/F");      //
1213          tree->Branch("dytrack",&clevel2.dytrack,"dytrack/F");      //  hfile = new TFile(file2,type,"Calorimeter LEVEL2 data",0);
1214          tree->Branch("qlast",&clevel2.qlast,"qlast/F");      //
1215          tree->Branch("nlast",&clevel2.nlast,"nlast/F");      tree = new TTree("CaloLevel2","PAMELA Level2 calorimeter data");
1216          tree->Branch("qpre",&clevel2.qpre,"qpre/F");      tree->Branch("OBT",&clevel2.OBT,"OBT/I");
1217          tree->Branch("npre",&clevel2.npre,"npre/F");      tree->Branch("pkt_num",&clevel2.pkt_num,"pkt_num/I");
1218          tree->Branch("qpresh",&clevel2.qpresh,"qpresh/F");      tree->Branch("pro_num",&clevel2.pro_num,"pro_num/I");
1219          tree->Branch("npresh",&clevel2.npresh,"npresh/F");      tree->Branch("trigty",&clevel2.trigty,"trigty/F");
1220          tree->Branch("qlow",&clevel2.qlow,"qlow/F");      tree->Branch("good",&clevel2.good,"good/I");
1221          tree->Branch("nlow",&clevel2.nlow,"nlow/F");      tree->Branch("perr",clevel2.perr,"perr[4]/I");
1222          tree->Branch("qtr",&clevel2.qtr,"qtr/F");      tree->Branch("swerr",clevel2.swerr,"swerr[4]/I");
1223          tree->Branch("ntr",&clevel2.ntr,"ntr/F");      tree->Branch("crc",clevel2.crc,"crc[4]/I");
1224          tree->Branch("planetot",&clevel2.planetot,"planetot/F");      tree->Branch("nstrip",&clevel2.nstrip,"nstrip/F");
1225          tree->Branch("qmean",&clevel2.qmean,"qmean/F");      tree->Branch("qtot",&clevel2.qtot,"qtot/F");
1226          tree->Branch("varcfit",clevel2.varcfit,"varcfit[2]/F");      tree->Branch("ncore",&clevel2.ncore,"ncore/F");
1227          tree->Branch("npcfit",clevel2.npcfit,"npcfit[2]/I");      tree->Branch("qcore",&clevel2.qcore,"qcore/F");
1228          tree->Branch("thex",&clevel2.thex,"thex/F");      tree->Branch("impx",&clevel2.impx,"impx/F");
1229          tree->Branch("they",&clevel2.they,"they/F");      tree->Branch("impy",&clevel2.impy,"impy/F");
1230          tree->Branch("cibar",clevel2.cibar,"cibar[22][2]/I");      tree->Branch("tanx",&clevel2.tanx,"tanx/F");
1231          tree->Branch("tibar",clevel2.tibar,"tibar[22][2]/I");      tree->Branch("tany",&clevel2.tany,"tany/F");
1232          tree->Branch("cbar",clevel2.cbar,"cbar[22][2]/F");      tree->Branch("nint",&clevel2.nint,"nint/F");
1233          tree->Branch("tbar",clevel2.tbar,"tbar[22][2]/F");      tree->Branch("ncyl",&clevel2.ncyl,"ncyl/F");
1234          //      tree->Branch("qcyl",&clevel2.qcyl,"qcyl/F");
1235          TTree *software = 0;      tree->Branch("qtrack",&clevel2.qtrack,"qtrack/F");
1236          software = new TTree("Software","Software used to generate data");      tree->Branch("qmax",&clevel2.qmax,"qmax/F");
1237          software->Branch("swcode",&swcode,"swcode/I");      tree->Branch("nx22",&clevel2.nx22,"nx22/F");
1238          software->Branch("swtrkcode",&swtrkcode,"swtrkcode/I");      tree->Branch("qx22",&clevel2.nx22,"qx22/F");
1239          software->Fill();      tree->Branch("qq",clevel2.qq,"qq[4]/F");
1240      };        tree->Branch("qtrackx",&clevel2.qtrackx,"qtrackx/F");
1241        tree->Branch("qtracky",&clevel2.qtracky,"qtracky/F");
1242        tree->Branch("dxtrack",&clevel2.dxtrack,"dxtrack/F");
1243        tree->Branch("dytrack",&clevel2.dytrack,"dytrack/F");
1244        tree->Branch("qlast",&clevel2.qlast,"qlast/F");
1245        tree->Branch("nlast",&clevel2.nlast,"nlast/F");
1246        tree->Branch("qpre",&clevel2.qpre,"qpre/F");
1247        tree->Branch("npre",&clevel2.npre,"npre/F");
1248        tree->Branch("qpresh",&clevel2.qpresh,"qpresh/F");
1249        tree->Branch("npresh",&clevel2.npresh,"npresh/F");
1250        tree->Branch("qlow",&clevel2.qlow,"qlow/F");
1251        tree->Branch("nlow",&clevel2.nlow,"nlow/F");
1252        tree->Branch("qtr",&clevel2.qtr,"qtr/F");
1253        tree->Branch("ntr",&clevel2.ntr,"ntr/F");
1254        tree->Branch("planetot",&clevel2.planetot,"planetot/F");
1255        tree->Branch("qmean",&clevel2.qmean,"qmean/F");
1256        tree->Branch("varcfit",clevel2.varcfit,"varcfit[2]/F");
1257        tree->Branch("npcfit",clevel2.npcfit,"npcfit[2]/I");
1258        tree->Branch("thex",&clevel2.thex,"thex/F");
1259        tree->Branch("they",&clevel2.they,"they/F");
1260        tree->Branch("cibar",clevel2.cibar,"cibar[22][2]/I");
1261        tree->Branch("tibar",clevel2.tibar,"tibar[22][2]/I");
1262        tree->Branch("cbar",clevel2.cbar,"cbar[22][2]/F");
1263        tree->Branch("tbar",clevel2.tbar,"tbar[22][2]/F");
1264        //
1265        TTree *software = 0;
1266        software = new TTree("Software","Software used to generate data");
1267        software->Branch("swcode",&swcode,"swcode/I");
1268        software->Branch("swtrkcode",&swtrkcode,"swtrkcode/I");
1269        software->Fill();
1270      };    
1271      //
1272      // run over all the events
1273      //
1274      printf("\n Processed events: \n\n");
1275      //
1276      pamela::PscuHeader *ph = 0;
1277      pamela::EventHeader *eh = 0;
1278      Int_t caloerr = 0;
1279      Int_t i = -1;    
1280      Int_t itr;
1281      if ( isrootple ){
1282        itr = -1;
1283      } else {
1284        itr = 0;
1285      };
1286      Int_t retval = 0;
1287      Int_t syncro = 1;
1288      Int_t trklost = 0;
1289      Int_t calolost = 0;
1290      Int_t pktnum = 0;
1291      Int_t obt = 0;
1292      //
1293      //
1294      while ( i < (nevents-1) ){    
1295        //
1296        if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);      
1297        //  printf(" %i \n",i);    
1298        //
1299        // look for tracker data
1300        //
1301        itr++;
1302        i++;
1303        syncro = 1;
1304    #if !defined (__CINT__)
1305      trkcalosync:
1306    #endif
1307    #if defined (__CINT__)
1308      trkcalosync: printf("");
1309    #endif
1310        //
1311        // check if we have tracker data
1312      //      //
1313      // run over all the events      if ( i >= nevents ){
1314          printf(" WARNING: no more calorimeter data.\n");
1315          if ( nevents > trnevents ) calolost += (snevents-trnevents);
1316          if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1317          retval = 9;
1318          goto closeandexit;
1319        };
1320        if ( itr > trnevents && isrootple ){
1321          printf(" WARNING: no more tracker data.\n");
1322          if ( nevents > trnevents ) calolost += (snevents-trnevents);
1323          if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1324          retval = 9;
1325          goto closeandexit;
1326        };
1327        if ( itr >= trnevents+1 && !isrootple ){
1328          printf(" WARNING: no more tracker data.\n");
1329          if ( nevents > trnevents ) calolost += (snevents-trnevents);
1330          if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1331          retval = 9;
1332          goto closeandexit;
1333        };
1334      //      //
1335      printf("\n Processed events: \n\n");      // retrieve tracker informations
1336      //      //
     pamela::PscuHeader *ph = 0;  
     pamela::EventHeader *eh = 0;  
     Int_t caloerr = 0;  
     Int_t i = -1;      
     Int_t itr;  
1337      if ( isrootple ){      if ( isrootple ){
1338          itr = -1;        if ( debug ) printf(" itr %i i %i nevents %i trnevents %i \n",itr,i,(int)nevents,trnevents);
1339          tr->GetEntry(itr);
1340      } else {      } else {
1341          itr = 0;        if ( debug ) printf(" itr %i i %i nevents %i trnevents %i \n",itr,i,(int)nevents,trnevents);
1342          cretrklev2(itr,trk);
1343      };      };
     Int_t retval = 0;  
     Int_t syncro = 1;  
     Int_t trklost = 0;  
     Int_t calolost = 0;  
     Int_t pktnum = 0;  
     Int_t obt = 0;  
1344      //      //
1345      while ( i < (nevents-1) ){          // check synchronization tracker and calorimeter informations:
1346          //      //
1347          if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000);        otr->SetBranchAddress("Header", &eh);
1348          //      printf(" %i \n",i);          otr->GetEntry(i);
1349          //      ph = eh->GetPscuHeader();
1350          // look for tracker data      pktnum = ph->GetCounter();
1351          //      obt = ph->GetOrbitalTime();
1352        if ( pktnum != trk.pkt_num || obt != trk.obt ){
1353          if ( pktnum > trk.pkt_num || obt > trk.obt ){
1354            if ( debug ) printf("itr++ %i pktnum calo %i trk %i obt calo %i trk %i \n",itr,pktnum,trk.pkt_num,obt,trk.obt);
1355          itr++;          itr++;
1356            trklost++;
1357            if ( syncro ) printf(" WARNING: lost sync! try to recover... \n");
1358            syncro = 0;            
1359            goto trkcalosync;
1360          };
1361          if ( pktnum < trk.pkt_num || obt < trk.obt ){
1362            if ( debug ) printf("i++ %i pktnum calo %i trk %i obt calo %i trk %i \n",i,pktnum,trk.pkt_num,obt,trk.obt);
1363          i++;          i++;
1364          syncro = 1;          calolost++;
1365  #if !defined (__CINT__)          if ( syncro ) printf(" WARNING: lost sync! try to recover... \n");
1366      trkcalosync:          syncro = 0;
1367  #endif          goto trkcalosync;
1368  #if defined (__CINT__)        };
1369      trkcalosync: printf("");      };  
1370  #endif      //
1371          //      // here we have synchronized data
1372          // check if we have tracker data      //
1373          //      if ( !syncro ) {
1374          if ( i >= nevents ){        printf(" ...synchronization recovered! \n");        
1375              printf(" WARNING: no more calorimeter data.\n");        printf(" Sync info: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n\n",trklost,calolost);
1376              if ( nevents > trnevents ) calolost += (snevents-trnevents);        syncro = 1;
1377              if ( nevents < trnevents ) trklost += (-snevents+trnevents);        if ( debug ) printf("pktnum calo %i trk %i obt calo %i trk %i \n",pktnum,trk.pkt_num,obt,trk.obt);
1378              retval = 9;      };      
1379              goto closeandexit;      //
1380          };      // store track information in the calib structure
1381          if ( itr > trnevents && isrootple ){      //
1382              printf(" WARNING: no more tracker data.\n");      if ( trk.ntrk > 0 ){
1383              if ( nevents > trnevents ) calolost += (snevents-trnevents);        imtrack = 0;
1384              if ( nevents < trnevents ) trklost += (-snevents+trnevents);        for (Int_t e = 0; e<trk.ntrk ; e++){
1385              retval = 9;          if ( trk.image[e] != 0 ) imtrack++;
1386              goto closeandexit;        };
1387          };        if ( (trk.ntrk-(imtrack/2)) == 1 ){
1388          if ( itr >= trnevents+1 && !isrootple ){          calib.good2 = trk.good2;
1389              printf(" WARNING: no more tracker data.\n");          for (Int_t e = 0; e<5 ; e++){
1390              if ( nevents > trnevents ) calolost += (snevents-trnevents);            calib.al_p[e][0] = trk.al[0][e];
1391              if ( nevents < trnevents ) trklost += (-snevents+trnevents);            if ( imtrack != 0 ) calib.al_p[e][1] = trk.al[1][e];
             retval = 9;  
             goto closeandexit;  
         };  
         //  
         // retrieve tracker informations  
         //  
         if ( isrootple ){  
             if ( debug ) printf(" itr %i i %i nevents %i trnevents %i \n",itr,i,(int)nevents,trnevents);  
             tr->GetEntry(itr);  
         } else {  
             if ( debug ) printf(" itr %i i %i nevents %i trnevents %i \n",itr,i,(int)nevents,trnevents);  
             cretrklev2(itr,trk);  
1392          };          };
1393          //          if ( imtrack != 0 ) {
1394          // check synchronization tracker and calorimeter informations:            if ( trk.chi2[0]>trk.chi2[1] ) {
1395          //              calib.trkchi2 = 1;
         otr->SetBranchAddress("Header", &eh);  
         otr->GetEntry(i);  
         ph = eh->GetPscuHeader();  
         pktnum = ph->GetCounter();  
         obt = ph->GetOrbitalTime();  
         if ( pktnum != trk.pkt_num || obt != trk.obt ){  
             if ( pktnum > trk.pkt_num || obt > trk.obt ){  
                 if ( debug ) printf("itr++ %i pktnum calo %i trk %i obt calo %i trk %i \n",itr,pktnum,trk.pkt_num,obt,trk.obt);  
                 itr++;  
                 trklost++;  
                 if ( syncro ) printf(" WARNING: lost sync! try to recover... \n");  
                 syncro = 0;              
                 goto trkcalosync;  
             };  
             if ( pktnum < trk.pkt_num || obt < trk.obt ){  
                 if ( debug ) printf("i++ %i pktnum calo %i trk %i obt calo %i trk %i \n",i,pktnum,trk.pkt_num,obt,trk.obt);  
                 i++;  
                 calolost++;  
                 if ( syncro ) printf(" WARNING: lost sync! try to recover... \n");  
                 syncro = 0;  
                 goto trkcalosync;  
             };  
         };        
         //  
         // here we have synchronized data  
         //  
         if ( !syncro ) {  
             printf(" ...synchronization recovered! \n");            
             printf(" Sync info: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n\n",trklost,calolost);  
             syncro = 1;  
             if ( debug ) printf("pktnum calo %i trk %i obt calo %i trk %i \n",pktnum,trk.pkt_num,obt,trk.obt);  
         };        
         //  
         // store track information in the calib structure  
         //  
         if ( trk.ntrk > 0 ){  
           imtrack = 0;  
           for (Int_t e = 0; e<trk.ntrk ; e++){  
             if ( trk.image[e] != 0 ) imtrack++;  
           };  
           if ( (trk.ntrk-(imtrack/2)) == 1 ){  
             calib.good2 = trk.good2;  
             for (Int_t e = 0; e<5 ; e++){  
               calib.al_p[e][0] = trk.al[0][e];  
               if ( imtrack != 0 ) calib.al_p[e][1] = trk.al[1][e];  
             };  
             if ( imtrack != 0 ) {  
               if ( trk.chi2[0]>trk.chi2[1] ) {  
                 calib.trkchi2 = 1;  
               } else {  
                 calib.trkchi2 = 2;  
               };  
             } else {  
               calib.trkchi2 = 1;  
             };  
1396            } else {            } else {
1397              calib.good2 = false;              calib.trkchi2 = 2;
             for (Int_t e = 0; e<5 ; e++){  
               calib.al_p[e][0] = 0.;  
               calib.al_p[e][1] = 0.;  
             };                
1398            };            };
1399          } else {          } else {
1400            calib.good2 = false;            calib.trkchi2 = 1;
           for (Int_t e = 0; e<5 ; e++){  
             calib.al_p[e][0] = 0.;  
             calib.al_p[e][1] = 0.;  
           };          
         };  
         //  
         // calibrate calorimeter data and retrieve level2 informations  
         //  
         // ====> NOTICE: in the case of no tracks or not good tracker events the program will fill the ntuple with zero values;                <====  
         // ====> in case of multiple tracks the program will calculate variables using the state vector and rigidity of the first track stored <====  
         //  
         caloerr = calolevel2core(i,b,otr,tree,clevel2,evento,calib,calcalibfile);  
         //  
         //  
         //  
         if ( caloerr ){  
             if ( i > 0 ){  
                 printf("\nTrying to close the file anyway...\n");  
                 if ( paw ){  
                     name.str("");  
                     name << tmpname;  
                     cclosecalol2((char *)name.str().c_str());  
                 } else {  
                     hfile->Write();  
                     hfile->Close();  
                 };  
                 printf("...done!\n");  
             } else {  
                 printf("\nERROR: output file is empty! \n");  
                 stringstream remfile;  
                 remfile.str("");  
                 remfile << "rm -f " << file2.str().c_str();  
                 gSystem->Exec(remfile.str().c_str());  
             };  
             printf("\nERROR: processed %i out of %i events\n",i,(int)nevents);  
             printf("\nERROR: an error occurred during processing!\n\n Exiting...\n\n");  
             goto theend;  
1401          };          };
1402          Int_t ciccio;        } else {
1403          ciccio = 1;          calib.good2 = false;
1404      };          for (Int_t e = 0; e<5 ; e++){
1405      if ( (i+1) < trnevents ){            calib.al_p[e][0] = 0.;
1406          printf(" WARNING: no more calorimeter data. \n");            calib.al_p[e][1] = 0.;
1407          if ( nevents > trnevents ) calolost += (snevents-trnevents);          };                  
1408          if ( nevents < trnevents ) trklost += (-snevents+trnevents);        };
1409          printf(" Sync info: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n",trklost,calolost);                      } else {
1410          retval = 9;        calib.good2 = false;
1411          for (Int_t e = 0; e<5 ; e++){
1412            calib.al_p[e][0] = 0.;
1413            calib.al_p[e][1] = 0.;
1414          };            
1415      };      };
1416      //      //
1417      // close rootple/ntuple files      // calibrate calorimeter data and retrieve level2 informations
1418      //      //
1419   closeandexit:      // ====> NOTICE: in the case of no tracks or not good tracker events the program will fill the ntuple with zero values;                <====
1420      if ( paw ){      // ====> in case of multiple tracks the program will calculate variables using the state vector and rigidity of the first track stored <====
         name.str("");  
         name << tmpname;  
         cclosecalol2((char *)name.str().c_str());  
     } else {  
         hfile->Write();  
         hfile->Close();  
     };  
     if ( isrootple ){  
         trfile->Close();  
     } else {  
         ccltrklev2(trk);  
     };  
     printf("\n");  
     printf(" Finished, exiting... \n");  
     printf("\n");  
     if ( trklost || calolost ){  
         printf("\n Sync info over the whole file: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n",trklost,calolost);            
     };  
     printf("\n");  
  theend:  
1421      //      //
1422      // go back to the starting path and unload fortran libraries      caloerr = calolevel2core(i,b,otr,tree,clevel2,evento,calib,calcalibfile);
1423      //      //
1424      gSystem->ChangeDirectory(path);      //
1425        //
1426        if ( caloerr ){
1427          if ( i > 0 ){
1428            printf("\nTrying to close the file anyway...\n");
1429            if ( paw ){
1430              name.str("");
1431              name << tmpname;
1432              cclosecalol2((char *)name.str().c_str());
1433            } else {
1434              hfile->Write();
1435              hfile->Close();
1436            };
1437            printf("...done!\n");
1438          } else {
1439            printf("\nERROR: output file is empty! \n");
1440            stringstream remfile;
1441            remfile.str("");
1442            remfile << "rm -f " << file2.str().c_str();
1443            gSystem->Exec(remfile.str().c_str());
1444          };
1445          printf("\nERROR: processed %i out of %i events\n",i,(int)nevents);
1446          printf("\nERROR: an error occurred during processing!\n\n Exiting...\n\n");
1447          goto theend;
1448        };
1449        Int_t ciccio;
1450        ciccio = 1;
1451      };
1452      if ( (i+1) < trnevents ){
1453        printf(" WARNING: no more calorimeter data. \n");
1454        if ( nevents > trnevents ) calolost += (snevents-trnevents);
1455        if ( nevents < trnevents ) trklost += (-snevents+trnevents);
1456        printf(" Sync info: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n",trklost,calolost);            
1457        retval = 9;
1458      };
1459      //
1460      // close rootple/ntuple files
1461      //
1462     closeandexit:
1463      if ( paw ){
1464        name.str("");
1465        name << tmpname;
1466        cclosecalol2((char *)name.str().c_str());
1467      } else {
1468        hfile->Write();
1469        hfile->Close();
1470      };
1471      if ( isrootple ){
1472        trfile->Close();
1473      } else {
1474        ccltrklev2(trk);
1475      };
1476      printf("\n");
1477      printf(" Finished, exiting... \n");
1478      printf("\n");
1479      if ( trklost || calolost ){
1480        printf("\n Sync info over the whole file: \n - tracker packets without calorimeter %i\n - calorimeter packets without tracker %i\n",trklost,calolost);              
1481      };
1482      printf("\n");
1483     theend:
1484      //
1485      // go back to the starting path and unload fortran libraries
1486      //
1487      gSystem->ChangeDirectory(path);
1488  #if defined (__CINT__)                            #if defined (__CINT__)                          
1489      if ( !isrootple ){    if ( !isrootple ){
1490          const char *sdir=gSystem->Getenv("PAM_LIB");      const char *sdir=gSystem->Getenv("PAM_LIB");
1491          bdir.str("");      bdir.str("");
1492          bdir << sdir << "/libcltrklev2_C.so";      bdir << sdir << "/libcltrklev2_C.so";
1493          gSystem->Unload(bdir.str().c_str());      gSystem->Unload(bdir.str().c_str());
1494          bdir.str("");      bdir.str("");
1495          bdir << sdir << "/libcltrklev2.so";      bdir << sdir << "/libcltrklev2.so";
1496          gSystem->Unload(bdir.str().c_str());      gSystem->Unload(bdir.str().c_str());
1497          bdir.str("");      bdir.str("");
1498          bdir << sdir << "/libretrklev2_C.so";      bdir << sdir << "/libretrklev2_C.so";
1499          gSystem->Unload(bdir.str().c_str());      gSystem->Unload(bdir.str().c_str());
1500          bdir.str("");      bdir.str("");
1501          bdir << sdir << "/libretrklev2.so";      bdir << sdir << "/libretrklev2.so";
1502          gSystem->Unload(bdir.str().c_str());      gSystem->Unload(bdir.str().c_str());
1503          bdir.str("");      bdir.str("");
1504          bdir << sdir << "/liboptrklev2_C.so";      bdir << sdir << "/liboptrklev2_C.so";
1505          gSystem->Unload(bdir.str().c_str());      gSystem->Unload(bdir.str().c_str());
1506          bdir.str("");      bdir.str("");
1507          bdir << sdir << "/liboptrklev2.so";      bdir << sdir << "/liboptrklev2.so";
1508          gSystem->Unload(bdir.str().c_str());      gSystem->Unload(bdir.str().c_str());
1509      };    };
1510      //    unloadf77lib(Framework,MySQL);    //    unloadf77lib(Framework,MySQL);
1511  #endif  #endif
1512      return(retval);    return(retval);
1513  }  }
1514    

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.23