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

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.23