/[PAMELA software]/DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp
ViewVC logotype

Diff of /DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp

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

revision 1.30 by mocchiut, Wed Oct 1 15:25:44 2008 UTC revision 1.78 by mocchiut, Fri Oct 10 13:35:10 2014 UTC
# Line 1  Line 1 
 //  
1  // C/C++ headers  // C/C++ headers
2  //  //
3  #include <fstream>  #include <fstream>
# Line 9  Line 8 
8  //  //
9  // ROOT headers  // ROOT headers
10  //  //
11    //#include <TCanvas.h>
12    #include <TH2F.h> //for test only. Vitaly.
13    #include <TVector3.h>
14    //#include <TF1.h>
15    
16  #include <TTree.h>  #include <TTree.h>
17  #include <TClassEdit.h>  #include <TClassEdit.h>
18  #include <TObject.h>  #include <TObject.h>
# Line 44  Line 48 
48  #include <OrbitalInfoCore.h>  #include <OrbitalInfoCore.h>
49  #include <InclinationInfo.h>  #include <InclinationInfo.h>
50    
51    //
52    // Tracker and ToF classes headers and definitions
53    //
54    #include <ToFLevel2.h>
55    #include <TrkLevel2.h>
56    #include <ExtTrack.h> // new tracking code
57    
58  using namespace std;  using namespace std;
59    
60  //  //
# Line 60  int OrbitalInfoCore(UInt_t run, TFile *f Line 71  int OrbitalInfoCore(UInt_t run, TFile *f
71    //    //
72    stringstream myquery;    stringstream myquery;
73    myquery.str("");    myquery.str("");
74    myquery << "SET time_zone='+0:00'";    myquery << "SET time_zone='+0:00'; SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';";
75    dbc->Query(myquery.str().c_str());    delete dbc->Query(myquery.str().c_str());
76    //    //
77    TString processFolder = Form("OrbitalInfoFolder_%u",run);    TString processFolder = Form("OrbitalInfoFolder_%u",run);
78    //    //
# Line 71  int OrbitalInfoCore(UInt_t run, TFile *f Line 82  int OrbitalInfoCore(UInt_t run, TFile *f
82    //    //
83    Bool_t verbose = false;    Bool_t verbose = false;
84    //    //
85    Bool_t standalone = true;    Bool_t standalone = false;
86    //    //
87    if ( OrbitalInfoargc > 0 ){    if ( OrbitalInfoargc > 0 ){
88      i = 0;      i = 0;
# Line 117  int OrbitalInfoCore(UInt_t run, TFile *f Line 128  int OrbitalInfoCore(UInt_t run, TFile *f
128    TTree *OrbitalInfotrclone = 0;    TTree *OrbitalInfotrclone = 0;
129    Bool_t reproc = false;    Bool_t reproc = false;
130    Bool_t reprocall = false;    Bool_t reprocall = false;
131      Bool_t igrfloaded = false;
132    UInt_t nobefrun = 0;    UInt_t nobefrun = 0;
133    UInt_t noaftrun = 0;    UInt_t noaftrun = 0;
134    UInt_t numbofrun = 0;    UInt_t numbofrun = 0;
# Line 124  int OrbitalInfoCore(UInt_t run, TFile *f Line 136  int OrbitalInfoCore(UInt_t run, TFile *f
136    TString fname;    TString fname;
137    UInt_t totfileentries = 0;    UInt_t totfileentries = 0;
138    UInt_t idRun = 0;    UInt_t idRun = 0;
139      UInt_t anni5 = 60 * 60 * 24 * 365 * 5 ;//1576800
140    //    //
141    // My variables. Vitaly.    // My variables. Vitaly.
142    //    //
143    //  UInt_t iev = 0;  //  UInt_t oi = 0;
   //  UInt_t j3 = 0;  
   UInt_t oi = 0;  
144    Int_t tmpSize = 0;    Int_t tmpSize = 0;
145    //    //
146    // variables needed to handle error signals    // variables needed to handle error signals
# Line 141  int OrbitalInfoCore(UInt_t run, TFile *f Line 152  int OrbitalInfoCore(UInt_t run, TFile *f
152    //    //
153    OrbitalInfo *orbitalinfo = new OrbitalInfo();    OrbitalInfo *orbitalinfo = new OrbitalInfo();
154    OrbitalInfo *orbitalinfoclone = new OrbitalInfo();    OrbitalInfo *orbitalinfoclone = new OrbitalInfo();
155    
156    //    //
157    // define variables for opening and reading level0 file    // define variables for opening and reading level0 file
158    //    //
159    TFile *l0File = 0;    TFile *l0File = 0;
160    TTree *l0tr = 0;    TTree *l0tr = 0;
161    TTree *l0trm = 0;    //  TTree *l0trm = 0;
162      TChain *ch = 0;
163    // EM: open also header branch    // EM: open also header branch
164    TBranch *l0head = 0;    TBranch *l0head = 0;
165    pamela::EventHeader *eh = 0;    pamela::EventHeader *eh = 0;
# Line 176  int OrbitalInfoCore(UInt_t run, TFile *f Line 189  int OrbitalInfoCore(UInt_t run, TFile *f
189    //    //
190    // IGRF stuff    // IGRF stuff
191    //    //
192    float dimo = 0.0; // dipole moment (computed from dat files)    Double_t dimo = 0.0; // dipole moment (computed from dat files) // EM GCC 4.7
193    float bnorth, beast, bdown, babs;    Float_t bnorth, beast, bdown, babs;
194    float xl; // L value    Float_t xl; // L value
195    float icode; // code value for L accuracy (see fortran code)    Float_t icode; // code value for L accuracy (see fortran code)
196    float bab1; // What's  the difference with babs?    Float_t bab1; // What's  the difference with babs?
197    float stps = 0.005; // step size for field line tracing    Float_t stps = 0.005; // step size for field line tracing
198    float bdel = 0.01; // required accuracy    Float_t bdel = 0.01; // required accuracy
199    float bequ;  // equatorial b value (also called b_0)    Float_t bequ;  // equatorial b value (also called b_0)
200    bool value = 0; // false if bequ is not the minimum b value    Bool_t value = 0; // false if bequ is not the minimum b value
201    float rr0; // equatorial radius normalized to earth radius    Float_t rr0; // equatorial radius normalized to earth radius
202    
203    //    //
204    // Working filename    // Working filename
# Line 209  int OrbitalInfoCore(UInt_t run, TFile *f Line 222  int OrbitalInfoCore(UInt_t run, TFile *f
222    OrbitalInfofolder << tempname.str().c_str();    OrbitalInfofolder << tempname.str().c_str();
223    tempname << "/OrbitalInfotree_run";    tempname << "/OrbitalInfotree_run";
224    tempname << run << ".root";      tempname << run << ".root";  
225      UInt_t totnorun = 0;
226    //    //
227    // DB classes    // DB classes
228    //    //
# Line 218  int OrbitalInfoCore(UInt_t run, TFile *f Line 232  int OrbitalInfoCore(UInt_t run, TFile *f
232    //    //
233    //Quaternions classes    //Quaternions classes
234    //    //
235    Quaternions *L_QQ_Q_l_lower = new Quaternions();    Quaternions *L_QQ_Q_l_lower = 0;
236    InclinationInfo *RYPang_lower = new InclinationInfo();    InclinationInfo *RYPang_lower = 0;
237    Quaternions *L_QQ_Q_l_upper = new Quaternions();    Quaternions *L_QQ_Q_l_upper = 0;
238    InclinationInfo *RYPang_upper = new InclinationInfo();    InclinationInfo *RYPang_upper = 0;
239        
240    cEci eCi;    cEci eCi;
241        
242    // Initialize fortran routines!!!    // Initialize fortran routines!!!
243      Int_t ltp1 = 0;
244    Int_t ltp2 = 0;    Int_t ltp2 = 0;
245    Int_t ltp3 = 0;    Int_t ltp3 = 0;
246    Int_t uno = 1;    //  Int_t uno = 1;
247    char *niente = " ";    //  const char *niente = " ";
248      GL_PARAM *glparam0 = new GL_PARAM();
249    GL_PARAM *glparam = new GL_PARAM();    GL_PARAM *glparam = new GL_PARAM();
250    GL_PARAM *glparam2 = new GL_PARAM();    GL_PARAM *glparam2 = new GL_PARAM();
251    Int_t parerror=glparam->Query_GL_PARAM(1,301,dbc); // parameters stored in DB in GL_PRAM table    GL_PARAM *glparam3 = new GL_PARAM();
252    
253    //    //
254    // Orientation variables    // Orientation variables. Vitaly
255    //    //
256    
257    UInt_t evfrom = 0;    UInt_t evfrom = 0;
258    UInt_t jumped = 0;    UInt_t jumped = 0;
259    Int_t itr = -1;        Int_t itr = -1;    
260    Double_t A1;    //  Double_t A1;
261    Double_t A2;    //  Double_t A2;
262    Double_t A3;    //  Double_t A3;
263    Double_t Px = 0;    Double_t Px = 0;
264    Double_t Py = 0;          Double_t Py = 0;      
265    Double_t Pz = 0;      Double_t Pz = 0;  
266    TTree *ttof = 0;    TTree *ttof = 0;
267    ToFLevel2 *tof = new ToFLevel2();    ToFLevel2 *tof = new ToFLevel2();
268      TTree *ttrke = 0;
269      TrkLevel2 *trke = new TrkLevel2();
270    OrientationInfo *PO = new OrientationInfo();    OrientationInfo *PO = new OrientationInfo();
271    Int_t nz = 6;    Int_t nz = 6;
272    Float_t zin[6];    Float_t zin[6];
273    Int_t nevtofl2 = 0;    Int_t nevtofl2 = 0;
274    //      Int_t nevtrkl2 = 0;
275      if ( verbose ) cout<<"Reading quaternions external file"<<endl;
276      cout.setf(ios::fixed,ios::floatfield);  
277      /******Reading recovered quaternions...*********/
278      vector<Double_t> recqtime;
279      vector<Float_t> recq0;
280      vector<Float_t> recq1;
281      vector<Float_t> recq2;
282      vector<Float_t> recq3;
283      Float_t Norm = 1;
284    
285      vector<UInt_t> RTtime1;
286      vector<UInt_t> RTtime2;
287      vector<Double_t> RTbank1;
288      vector<Double_t> RTbank2;
289      vector<Double_t> RTbpluto1;
290      vector<Double_t> RTbpluto2;
291      vector<Int_t> RTazim;
292      vector<UInt_t> RTstart;
293      vector<UInt_t> RTpluto2;
294      vector<UInt_t> RTpluto1;
295      vector<Int_t> RTerrq;
296    
297      TClonesArray *tcNucleiTrk = NULL;
298      TClonesArray *tcExtNucleiTrk = NULL;
299      TClonesArray *tcExtTrk = NULL;
300      TClonesArray *tcNucleiTof = NULL;
301      TClonesArray *tcExtNucleiTof = NULL;
302      TClonesArray *tcExtTof = NULL;
303      TClonesArray *torbNucleiTrk = NULL;
304      TClonesArray *torbExtNucleiTrk = NULL;
305      TClonesArray *torbExtTrk = NULL;
306      Bool_t hasNucleiTrk = false;
307      Bool_t hasExtNucleiTrk = false;
308      Bool_t hasExtTrk = false;
309      Bool_t hasNucleiTof = false;
310      Bool_t hasExtNucleiTof = false;
311      Bool_t hasExtTof = false;
312    
313      ifstream in;
314      ifstream an;
315      //  ofstream mc;
316      //  TString gr;
317      Int_t parerror2=0;
318    
319      Int_t parerror=glparam0->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table
320      if ( verbose ) cout<<parerror<<"\t"<<(char*)(glparam0->PATH+glparam0->NAME).Data()<<endl;
321    if ( parerror<0 ) {    if ( parerror<0 ) {
322      code = parerror;      code = parerror;
323      goto closeandexit;      goto closeandexit;
324    };    }
325    ltp2 = (Int_t)(glparam->PATH+glparam->NAME).Length();    in.open((char*)(glparam0->PATH+glparam0->NAME).Data(),ios::in);
326    if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());    while(!in.eof()){
327    //      recqtime.resize(recqtime.size()+1);
328    parerror=glparam2->Query_GL_PARAM(1,302,dbc); // parameters stored in DB in GL_PRAM table      Int_t sizee = recqtime.size();
329    if ( parerror<0 ) {      recq0.resize(sizee);
330        recq1.resize(sizee);
331        recq2.resize(sizee);
332        recq3.resize(sizee);
333        in>>recqtime[sizee-1];
334        in>>recq0[sizee-1];
335        in>>recq1[sizee-1];
336        in>>recq2[sizee-1];
337        in>>recq3[sizee-1];
338        in>>Norm;
339      }
340      in.close();
341      if ( verbose ) cout<<"We have read recovered data"<<endl;
342      if (debug) cout << "size of recovered quaterions data set is " << recqtime.size() << endl;
343    
344      if ( verbose ) cout<<"read Rotation Table"<<endl;
345      
346      parerror2=glparam0->Query_GL_PARAM(1,305,dbc);
347    
348      if ( verbose ) cout<<parerror2<<"\t"<<(char*)(glparam0->PATH+glparam0->NAME).Data()<<endl;
349      if ( parerror2<0 ) {
350      code = parerror;      code = parerror;
351      goto closeandexit;      goto closeandexit;
352    };    }
353    ltp3 = (Int_t)(glparam2->PATH+glparam2->NAME).Length();    an.open((char*)(glparam0->PATH+glparam0->NAME).Data(),ios::in);
354    if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());    while(!an.eof()){
355    //      RTtime1.resize(RTtime1.size()+1);
356    initize_((char *)niente,&uno,(char *)(glparam->PATH+glparam->NAME).Data(),&ltp2,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp3);      Int_t sizee = RTtime1.size();
357    //      RTbank1.resize(sizee+1);
358    // End IGRF stuff//      RTazim.resize(sizee+1);
359    //      RTerrq.resize(sizee+1);
360        RTstart.resize(sizee+1);
361        RTpluto1.resize(sizee+1);
362        RTbpluto1.resize(sizee+1);
363        an>>RTtime1[sizee-1];
364        an>>RTbank1[sizee-1];
365        an>>RTstart[sizee-1];
366        an>>RTpluto1[sizee-1];
367        an>>RTbpluto1[sizee-1];
368        an>>RTazim[sizee-1];
369        an>>RTerrq[sizee-1];
370        if(sizee>1) {
371          RTtime2.resize(sizee+1);
372          RTbank2.resize(sizee+1);
373          RTpluto2.resize(sizee+1);
374          RTbpluto2.resize(sizee+1);
375          RTtime2[sizee-2]=RTtime1[sizee-1];
376          RTpluto2[sizee-2]=RTpluto1[sizee-1];
377          RTbank2[sizee-2]=RTbank1[sizee-1];
378          RTbpluto2[sizee-2]=RTbpluto1[sizee-1];
379        }
380      }
381      an.close();
382      //cout<<"put some number here"<<endl;
383      //Int_t yupi;
384      //cin>>yupi;
385      
386      if ( verbose ) cout<<"We have read Rotation Table"<<endl;
387        //Geomagnetic coordinates calculations staff
388    
389      GMtype_CoordGeodetic location;
390      //  GMtype_CoordDipole GMlocation;
391      GMtype_Ellipsoid Ellip;
392      GMtype_Data G0, G1, H1;
393            
394      //  { // this braces is necessary to avoid jump to label 'closeandexit'  error   // but it is wrong since the variable "igpath" will not exist outside. To overcome the "jump to label 'closeandexit'  error" it is necessary to set the "igpath" before line 276
395      //    TString igpath="/data03/Malakhov/pam9Malakhov/installed10/calib/orb-param/";
396      //  }
397    
398      //  GM_ScanIGRF(glparam->PATH, &G0, &G1, &H1);
399      GM_ScanIGRF(dbc, &G0, &G1, &H1);
400    
401      //cout << G0.element[0] << "\t" << G1.element[0] << "\t" << H1.element[0] << endl;
402      //cout << G0.element[5] << "\t" << G1.element[5] << "\t" << H1.element[5] << endl;
403    
404      GM_SetEllipsoid(&Ellip);
405    
406      // IGRF stuff moved inside run loop!  
407    
408    for (Int_t ip=0;ip<nz;ip++){    for (Int_t ip=0;ip<nz;ip++){
409      zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));      zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));
410    };    };
411    //    //
412    if ( !standalone ){    if ( !standalone ){
413      //      //
414      // Does it contain the Tracker tree?      // Does it contain the Tracker and ToF trees?
415      //      //
416      ttof = (TTree*)file->Get("ToF");      ttof = (TTree*)file->Get("ToF");
417      if ( !ttof ) {      if ( !ttof ) {
418        if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");        if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");
419        code = -900;        code = -900;
420        goto closeandexit;        goto closeandexit;
421      };      }
422      ttof->SetBranchAddress("ToFLevel2",&tof);        ttof->SetBranchAddress("ToFLevel2",&tof);  
423      nevtofl2 = ttof->GetEntries();      nevtofl2 = ttof->GetEntries();
424    };  
425        //
426        // Look for extended tracking algorithm
427        //
428        if ( verbose ) printf("Look for extended and nuclei tracking algorithms in ToF\n");
429        // Nuclei tracking algorithm
430        Int_t checkAlgo = 0;
431        tcNucleiTof =  new TClonesArray("ToFTrkVar");
432        checkAlgo = ttof->SetBranchAddress("TrackNuclei",&tcNucleiTof);    
433        if ( !checkAlgo ){
434          if ( verbose ) printf(" Nuclei tracking algorithm ToF branch found! :D \n");
435          hasNucleiTof = true;
436        } else {
437          if ( verbose ) printf(" Nuclei tracking algorithm ToF branch not found :( !\n");
438          printf(" ok, this is not a problem (it depends on tracker settings) \n");
439          delete tcNucleiTof;
440          tcNucleiTof=NULL; // 10RED reprocessing bug  
441        }
442        // Nuclei tracking algorithm using calorimeter points
443        tcExtNucleiTof =  new TClonesArray("ToFTrkVar");
444        checkAlgo = ttof->SetBranchAddress("RecoveredTrackNuclei",&tcExtNucleiTof);    
445        if ( !checkAlgo ){
446          if ( verbose ) printf(" Recovered nuclei tracking algorithm ToF branch found! :D \n");
447          hasExtNucleiTof = true;
448        } else {
449          if ( verbose ) printf(" Recovered nuclei tracking algorithm ToF branch not found :( !\n");
450          printf(" ok, this is not a problem (it depends on tracker settings) \n");
451          delete tcExtNucleiTof;
452          tcExtNucleiTof=NULL; // 10RED reprocessing bug  
453        }
454        // Tracking algorithm using calorimeter points
455        tcExtTof =  new TClonesArray("ToFTrkVar");
456        checkAlgo = ttof->SetBranchAddress("RecoveredTrack",&tcExtTof);
457        if ( !checkAlgo ){
458          if ( verbose ) printf(" Recovered track algorithm ToF branch found! :D \n");
459          hasExtTof = true;
460        } else {
461          if ( verbose ) printf(" Recovered track algorithm ToF branch not found :( !\n");
462          printf(" ok, this is not a problem (it depends on tracker settings) \n");
463          delete tcExtTof;
464          tcExtTof=NULL; // 10RED reprocessing bug  
465        }
466    
467        ttrke = (TTree*)file->Get("Tracker");
468        if ( !ttrke ) {
469          if ( verbose ) printf(" OrbitalInfo - ERROR: no trk tree\n");
470          code = -903;
471          goto closeandexit;
472        }
473        ttrke->SetBranchAddress("TrkLevel2",&trke);  
474        nevtrkl2 = ttrke->GetEntries();
475    
476        //
477        // Look for extended tracking algorithm
478        //
479        if ( verbose ) printf("Look for extended and nuclei tracking algorithms\n");
480        // Nuclei tracking algorithm
481        checkAlgo = 0;
482        tcNucleiTrk =  new TClonesArray("TrkTrack");
483        checkAlgo = ttrke->SetBranchAddress("TrackNuclei",&tcNucleiTrk);    
484        if ( !checkAlgo ){
485          if ( verbose ) printf(" Nuclei tracking algorithm branch found! :D \n");
486          hasNucleiTrk = true;
487        } else {
488          if ( verbose ) printf(" Nuclei tracking algorithm branch not found :( !\n");
489          printf(" ok, this is not a problem (it depends on tracker settings) \n");
490          delete tcNucleiTrk;
491          tcNucleiTrk=NULL; // 10RED reprocessing bug  
492        }
493        // Nuclei tracking algorithm using calorimeter points
494        tcExtNucleiTrk =  new TClonesArray("ExtTrack");
495        checkAlgo = ttrke->SetBranchAddress("RecoveredTrackNuclei",&tcExtNucleiTrk);    
496        if ( !checkAlgo ){
497          if ( verbose ) printf(" Recovered nuclei tracking algorithm branch found! :D \n");
498          hasExtNucleiTrk = true;
499        } else {
500          if ( verbose ) printf(" Recovered nuclei tracking algorithm branch not found :( !\n");
501          printf(" ok, this is not a problem (it depends on tracker settings) \n");
502          delete tcExtNucleiTrk;
503          tcExtNucleiTrk=NULL; // 10RED reprocessing bug  
504        }
505        // Tracking algorithm using calorimeter points
506        tcExtTrk =  new TClonesArray("ExtTrack");
507        checkAlgo = ttrke->SetBranchAddress("RecoveredTrack",&tcExtTrk);
508        if ( !checkAlgo ){
509          if ( verbose ) printf(" Recovered track algorithm branch found! :D \n");
510          hasExtTrk = true;
511        } else {
512          if ( verbose ) printf(" Recovered track algorithm branch not found :( !\n");
513          printf(" ok, this is not a problem (it depends on tracker settings) \n");
514          delete tcExtTrk;
515          tcExtTrk=NULL; // 10RED reprocessing bug  
516        }
517    
518        if ( (hasNucleiTrk && !hasNucleiTof) || (!hasNucleiTrk && hasNucleiTof) ||
519             (hasExtNucleiTrk && !hasExtNucleiTof) || (!hasExtNucleiTrk && hasExtNucleiTof) ||
520             (hasExtTrk && !hasExtTof) || (!hasExtTrk && hasExtTof)
521             ){
522          if ( verbose ) printf(" ERROR: Mismatch between tracker and tof tree branches concerning extended tracking algorithm(s)\n");
523          if ( debug ) printf("hasNucleiTrk %i hasExtNucleiTrk %i hasExtTrk %i \n",hasNucleiTrk,hasExtNucleiTrk,hasExtTrk);
524          if ( debug ) printf("hasNucleiTof %i hasExtNucleiTof %i hasExtTof %i \n",hasNucleiTof,hasExtNucleiTof,hasExtTof);
525          throw -901;
526        }
527    
528      }
529    //    //
530    // Let's start!    // Let's start!
531    //    //
# Line 342  int OrbitalInfoCore(UInt_t run, TFile *f Line 580  int OrbitalInfoCore(UInt_t run, TFile *f
580    // number of run to be processed    // number of run to be processed
581    //    //
582    numbofrun = runinfo->GetNoRun();    numbofrun = runinfo->GetNoRun();
583    UInt_t totnorun = runinfo->GetRunEntries();    totnorun = runinfo->GetRunEntries();
584    //    //
585    // Try to access the OrbitalInfo tree in the file, if it exists we are reprocessing data if not we are processing a new run    // Try to access the OrbitalInfo tree in the file, if it exists we are reprocessing data if not we are processing a new run
586    //    //
# Line 376  int OrbitalInfoCore(UInt_t run, TFile *f Line 614  int OrbitalInfoCore(UInt_t run, TFile *f
614        //        //
615        reprocall = true;        reprocall = true;
616        //        //
617        if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n");        if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n Deleting old tree...\n");
618        //        //
619      } else {      } else {
620        //        //
# Line 394  int OrbitalInfoCore(UInt_t run, TFile *f Line 632  int OrbitalInfoCore(UInt_t run, TFile *f
632        tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");        tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");
633        tempOrbitalInfo->SetName("OrbitalInfo-old");        tempOrbitalInfo->SetName("OrbitalInfo-old");
634        tempfile->Write();        tempfile->Write();
635          tempOrbitalInfo->Delete();
636        tempfile->Close();          tempfile->Close();  
637      }      }
638      //      //
639      // Delete the old tree from old file and memory      // Delete the old tree from old file and memory
640      //      //
641        OrbitalInfotrclone->Clear();
642      OrbitalInfotrclone->Delete("all");      OrbitalInfotrclone->Delete("all");
643      //      //
644      if (verbose) printf(" ...done!\n");      if (verbose) printf(" ...done!\n");
# Line 413  int OrbitalInfoCore(UInt_t run, TFile *f Line 653  int OrbitalInfoCore(UInt_t run, TFile *f
653    orbitalinfo->Set();//ELENA **TEMPORANEO?**    orbitalinfo->Set();//ELENA **TEMPORANEO?**
654    OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);    OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);
655    //    //
656      // create new branches for new tracking algorithms
657      //
658      if ( hasNucleiTrk ){
659        torbNucleiTrk = new TClonesArray("OrbitalInfoTrkVar",1);
660        OrbitalInfotr->Branch("TrackNuclei",&torbNucleiTrk);
661      }
662      if ( hasExtNucleiTrk ){
663        torbExtNucleiTrk = new TClonesArray("OrbitalInfoTrkVar",1);
664        OrbitalInfotr->Branch("RecoveredTrackNuclei",&torbExtNucleiTrk);
665      }
666      if ( hasExtTrk ){
667        torbExtTrk = new TClonesArray("OrbitalInfoTrkVar",1);
668        OrbitalInfotr->Branch("RecoveredTrack",&torbExtTrk);
669      }
670    
671      //
672    if ( reproc && !reprocall ){    if ( reproc && !reprocall ){
673      //      //
674      //  open new file and retrieve also tree informations      //  open new file and retrieve also tree informations
# Line 430  int OrbitalInfoCore(UInt_t run, TFile *f Line 686  int OrbitalInfoCore(UInt_t run, TFile *f
686        }        }
687        for (UInt_t j = 0; j < nobefrun; j++){        for (UInt_t j = 0; j < nobefrun; j++){
688          //          //
689          OrbitalInfotrclone->GetEntry(j);                    if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;    
690          //          //
691          // copy orbitalinfoclone to mydec          // copy orbitalinfoclone to mydec
692          //          //
693          orbitalinfo->Clear();          //      orbitalinfo->Clear();
694          //          //
695          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
696          //          //
# Line 444  int OrbitalInfoCore(UInt_t run, TFile *f Line 700  int OrbitalInfoCore(UInt_t run, TFile *f
700          //          //
701        };        };
702        if (verbose) printf(" Finished successful copying!\n");        if (verbose) printf(" Finished successful copying!\n");
703      };                };
704    };    };
705    //    //
706      //
707    // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.    // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
708    //    //
709    runlist = runinfo->GetRunList();    runlist = runinfo->GetRunList();
# Line 454  int OrbitalInfoCore(UInt_t run, TFile *f Line 711  int OrbitalInfoCore(UInt_t run, TFile *f
711    // Loop over the run to be processed    // Loop over the run to be processed
712    //    //
713    for (UInt_t irun=0; irun < numbofrun; irun++){    for (UInt_t irun=0; irun < numbofrun; irun++){
714    
715        L_QQ_Q_l_lower = new Quaternions();
716        RYPang_lower = new InclinationInfo();
717        L_QQ_Q_l_upper = new Quaternions();
718        RYPang_upper = new InclinationInfo();
719    
720      //      //
721      // retrieve the first run ID to be processed using the RunInfo list      // retrieve the first run ID to be processed using the RunInfo list
722      //      //
# Line 502  int OrbitalInfoCore(UInt_t run, TFile *f Line 765  int OrbitalInfoCore(UInt_t run, TFile *f
765      fname = ftmpname.str().c_str();      fname = ftmpname.str().c_str();
766      ftmpname.str("");      ftmpname.str("");
767      //      //
768      // print out informations      // print nout informations
769      //      //
770      totevent = runinfo->NEVENTS;      totevent = runinfo->NEVENTS;
771      evfrom = runinfo->EV_FROM;      evfrom = runinfo->EV_FROM;
# Line 516  int OrbitalInfoCore(UInt_t run, TFile *f Line 779  int OrbitalInfoCore(UInt_t run, TFile *f
779      //      //
780      //    if ( !totevent ) goto closeandexit;      //    if ( !totevent ) goto closeandexit;
781      // Open Level0 file      // Open Level0 file
782        if ( l0File ) l0File->Close();
783      l0File = new TFile(fname.Data());      l0File = new TFile(fname.Data());
784      if ( !l0File ) {      if ( !l0File ) {
785        if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");        if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
# Line 554  int OrbitalInfoCore(UInt_t run, TFile *f Line 818  int OrbitalInfoCore(UInt_t run, TFile *f
818        code = -12;        code = -12;
819        goto closeandexit;        goto closeandexit;
820      };      };
821    
822      //      //
823  //     TTree *tp = (TTree*)l0File->Get("RunHeader");      // open IGRF files and do it only once if we are processing a full level2 file
824  //     tp->SetBranchAddress("Header", &eH);      //
825  //     tp->SetBranchAddress("RunHeader", &reh);      if ( !igrfloaded ){
826  //     tp->GetEntry(0);  
827  //     ph = eH->GetPscuHeader();        if ( l0head->GetEntry(runinfo->EV_FROM) > 0 ){
828  //     ULong_t TimeSync = reh->LAST_TIME_SYNC_INFO;          igrfloaded = true;
829  //     ULong_t ObtSync = reh->OBT_TIME_SYNC;              //
830  //     if ( debug ) printf(" 1 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",TimeSync,ObtSync,TimeSync-ObtSync);          // absolute time of first event of the run (it should not matter a lot)
831  //          //
832            ph = eh->GetPscuHeader();
833            atime = dbtime->DBabsTime(ph->GetOrbitalTime());
834            
835            parerror=glparam->Query_GL_PARAM(atime-anni5,301,dbc); // parameters stored in DB in GL_PRAM table  
836            if ( parerror<0 ) {
837              code = parerror;
838              goto closeandexit;
839            }
840            ltp1 = (Int_t)(glparam->PATH+glparam->NAME).Length();
841            if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
842            //
843            parerror=glparam2->Query_GL_PARAM(atime,301,dbc); // parameters stored in DB in GL_PRAM table  
844            if ( parerror<0 ) {
845              code = parerror;
846              goto closeandexit;
847            }
848            ltp2 = (Int_t)(glparam2->PATH+glparam2->NAME).Length();
849            if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());
850            //
851            parerror=glparam3->Query_GL_PARAM(atime,302,dbc); // parameters stored in DB in GL_PRAM table
852            if ( parerror<0 ) {
853              code = parerror;
854              goto closeandexit;
855            }
856            ltp3 = (Int_t)(glparam3->PATH+glparam3->NAME).Length();
857            if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam3->PATH+glparam3->NAME).Data());
858            //
859            initize_((char *)(glparam->PATH+glparam->NAME).Data(),&ltp1,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp2,(char *)(glparam3->PATH+glparam3->NAME).Data(),&ltp3);
860            //
861            if (debug) cout<<"initize: "<<(char *)(glparam->PATH+glparam->NAME).Data()<<"\t"<<(char *)(glparam2->PATH+glparam2->NAME).Data()<<"\t"<<(char *)(glparam3->PATH+glparam3->NAME).Data()<<endl;
862          }
863        }
864        //
865        // End IGRF stuff//
866        //
867    
868      ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();      ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();
869      ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);      ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);
870      ULong_t DeltaOBT = TimeSync - ObtSync;      ULong_t DeltaOBT = TimeSync - ObtSync;
871    
872      if ( debug ) printf(" 2 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",(ULong_t)(dbtime->GetTimesync()/1000),(ULong_t)dbtime->GetObt0(),TimeSync-ObtSync);      if ( debug ) printf(" 2 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",(ULong_t)(dbtime->GetTimesync()/1000),(ULong_t)dbtime->GetObt0(),TimeSync-ObtSync);
       
     l0trm = (TTree*)l0File->Get("Mcmd");  
     neventsm = l0trm->GetEntries();  
     //    neventsm = 0;  
873      //      //
874      if (neventsm == 0){      // Read MCMDs from up to 11 files, 5 before and 5 after the present one in order to have some kind of inclination information
       if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");  
       //      l0File->Close();  
       code = 900;  
       //      goto closeandexit;  
     }  
875      //      //
876            ch = new TChain("Mcmd","Mcmd");
     l0trm->SetBranchAddress("Mcmd", &mcmdev);  
     //    l0trm->SetBranchAddress("Header", &eh);  
877      //      //
878        // look in the DB to find the closest files to this run
879      //      //
880        TSQLResult *pResult = 0;
881        TSQLRow *Row = 0;
882        stringstream myquery;
883        UInt_t l0fid[10];
884        Int_t i = 0;
885        memset(l0fid,0,10*sizeof(Int_t));
886      //      //
887      UInt_t mctren = 0;          myquery.str("");
888      UInt_t mcreen = 0;        myquery << "select ID_ROOT_L0 from GL_RUN where RUNHEADER_TIME<=" << runinfo->RUNHEADER_TIME << " group by ID_ROOT_L0 order by RUNHEADER_TIME desc limit 5;";
     UInt_t numrec = 0;  
889      //      //
890      Double_t upperqtime = 0;      pResult = dbc->Query(myquery.str().c_str());
891      Double_t lowerqtime = 0;      //
892            i = 9;
893      Double_t incli = 0;      if( pResult ){
894      oi = 0;        //
895      UInt_t ooi = 0;        Row = pResult->Next();
896          //
897          while ( Row ){
898            //
899            // store infos and exit
900            //
901            l0fid[i] = (UInt_t)atoll(Row->GetField(0));
902            i--;
903            Row = pResult->Next();  
904            //
905          };
906          pResult->Delete();
907        };
908        //
909        myquery.str("");
910        myquery << "select ID_ROOT_L0 from GL_RUN where RUNHEADER_TIME>" << runinfo->RUNHEADER_TIME << " group by ID_ROOT_L0 order by RUNHEADER_TIME asc limit 5;";
911        //
912        pResult = dbc->Query(myquery.str().c_str());
913        //
914        i = 0;
915        if( pResult ){
916          //
917          Row = pResult->Next();
918          //
919          while ( Row ){
920            //
921            // store infos and exit
922            //
923            l0fid[i] = (UInt_t)atoll(Row->GetField(0));
924            i++;
925            Row = pResult->Next();  
926            //
927          };
928          pResult->Delete();
929        };
930        //
931        i = 0;
932        UInt_t previd = 0;
933        while ( i < 10 ){
934          if ( l0fid[i] && previd != l0fid[i] ){
935            previd = l0fid[i];
936            myquery.str("");
937            myquery << "select PATH,NAME from GL_ROOT where ID=" << l0fid[i] << " ;";
938            //
939            pResult = dbc->Query(myquery.str().c_str());
940            //
941            if( pResult ){
942              //
943              Row = pResult->Next();
944              //
945              if ( debug ) printf(" Using inclination informations from file: %s \n",(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1)).Data());
946              ch->Add(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1));
947              //
948              pResult->Delete();
949            };
950          };
951          i++;
952        };
953        //
954        ch->SetBranchAddress("Mcmd",&mcmdev);
955        neventsm = ch->GetEntries();
956        if ( debug ) printf(" entries %u \n", neventsm);
957        //
958        if (neventsm == 0){
959          if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");
960          code = 900;
961        }
962        //
963        Double_t lowerqtime = 0;    
964      //      //
965      // init quaternions sync      // init quaternions information from mcmd-packets
966      //      //
967      Bool_t isf = true;      Bool_t isf = true;
968      Int_t fgh = 0;  
969        vector<Float_t> q0;
970        vector<Float_t> q1;
971        vector<Float_t> q2;
972        vector<Float_t> q3;
973        vector<Double_t> qtime;
974        vector<Float_t> qPitch;
975        vector<Float_t> qRoll;
976        vector<Float_t> qYaw;
977        vector<Int_t> qmode;
978    
979        Int_t nt = 0;
980        UInt_t must = 0;
981    
982      //      //
983      // run over all the events of the run      // run over all the events of the run
984      //      //
985      if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");      if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
986      //      //
987       //      //
988      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
       
989        //        //
990        if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);          if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);  
991        if ( debug ) printf(" %i \n",procev);              if ( debug ) printf(" %i \n",procev);      
992        //        //
993        l0head->GetEntry(re);        if ( l0head->GetEntry(re) <= 0 ) throw -36;
994        //        //
995        // absolute time of this event        // absolute time of this event
996        //        //
997        ph = eh->GetPscuHeader();        ph = eh->GetPscuHeader();
998        atime = dbtime->DBabsTime(ph->GetOrbitalTime());        atime = dbtime->DBabsTime(ph->GetOrbitalTime());
999          if ( debug ) printf(" %i absolute time \n",procev);      
1000        //        //
1001        // paranoid check        // paranoid check
1002        //        //
1003        if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME)  ) {        if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1))  ) {
1004          if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");          if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");
1005          jumped++;          jumped++;
1006  //      debug = true;          //      debug = true;
1007          continue;          continue;
1008        }        }
1009    
# Line 645  int OrbitalInfoCore(UInt_t run, TFile *f Line 1022  int OrbitalInfoCore(UInt_t run, TFile *f
1022            if ( verbose ) printf(" OrbitalInfo - ERROR: no tof events with entry = %i in Level2 file\n",itr);            if ( verbose ) printf(" OrbitalInfo - ERROR: no tof events with entry = %i in Level2 file\n",itr);
1023            if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);            if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1024            l0File->Close();            l0File->Close();
1025            code = -901;            code = -904;
1026            goto closeandexit;            goto closeandexit;
1027          };          };
1028          //          //
1029          tof->Clear();          tof->Clear();
1030          //          //
1031          ttof->GetEntry(itr);          // Clones array must be cleared before going on
1032            //
1033            if ( hasNucleiTof ){
1034              tcNucleiTof->Delete();
1035            }
1036            if ( hasExtNucleiTof ){
1037              tcExtNucleiTof->Delete();
1038            }          
1039            if ( hasExtTof ){
1040              tcExtTof->Delete();
1041            }
1042            //
1043            if ( verbose ) printf(" get tof tree entries... entry = %i in Level2 file\n",itr);
1044            if ( ttof->GetEntry(itr) <= 0 ){
1045              if ( verbose ) printf(" problems with tof tree entries... entry = %i in Level2 file\n",itr);
1046              if ( verbose ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1047              throw -36;
1048            }
1049            if ( verbose ) printf(" gat0\n");
1050          //          //
1051        };        }
1052          //
1053          // retrieve tracker informations
1054          //
1055          if ( !standalone ){
1056            if ( itr > nevtrkl2 ){  
1057              if ( verbose ) printf(" OrbitalInfo - ERROR: no trk events with entry = %i in Level2 file\n",itr);
1058              if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1059              l0File->Close();
1060              code = -905;
1061              goto closeandexit;
1062            }
1063            //
1064            if ( verbose ) printf(" gat1\n");
1065            trke->Clear();
1066            //
1067            // Clones array must be cleared before going on
1068            //
1069            if ( hasNucleiTrk ){
1070              if ( verbose ) printf(" gat2\n");
1071              tcNucleiTrk->Delete();
1072              if ( verbose ) printf(" gat3\n");
1073              torbNucleiTrk->Delete();
1074            }
1075            if ( hasExtNucleiTrk ){
1076              if ( verbose ) printf(" gat4\n");
1077              tcExtNucleiTrk->Delete();
1078              if ( verbose ) printf(" gat5\n");
1079              torbExtNucleiTrk->Delete();
1080            }          
1081            if ( hasExtTrk ){
1082              if ( verbose ) printf(" gat6\n");
1083              tcExtTrk->Delete();
1084              if ( verbose ) printf(" gat7\n");
1085              torbExtTrk->Delete();
1086            }
1087            //
1088            if ( verbose ) printf(" get trk tree entries... entry = %i in Level2 file\n",itr);
1089            if ( ttrke->GetEntry(itr) <= 0 ) throw -36;
1090            //
1091          }
1092    
1093        //        //
1094        procev++;        procev++;
1095        //        //
1096        // start processing        // start processing
1097        //        //
1098          if ( debug ) printf(" %i start processing \n",procev);      
1099        orbitalinfo->Clear();        orbitalinfo->Clear();
1100        //        //
1101        OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();        OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();
1102        if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);        if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);
1103        TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;        TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;
1104    
1105          // Geomagnetic coordinates calculation variables
1106          GMtype_CoordSpherical CoordSpherical, DipoleSpherical;
1107          GMtype_CoordCartesian CoordCartesian, DipoleCartesian;
1108          GMtype_Model Model;
1109          GMtype_Pole Pole;
1110    
1111        //        //
1112        // Fill OBT, pkt_num and absTime        // Fill OBT, pkt_num and absTime
1113        //              //      
1114        orbitalinfo->pkt_num = ph->GetCounter();        orbitalinfo->pkt_num = ph->GetCounter();
1115        orbitalinfo->OBT = ph->GetOrbitalTime();        orbitalinfo->OBT = ph->GetOrbitalTime();
1116        orbitalinfo->absTime = atime;        orbitalinfo->absTime = atime;
1117          if ( debug ) printf(" %i pktnum obt abstime \n",procev);      
1118        //        //
1119        // Propagate the orbit from the tle time to atime, using SGP(D)4.        // Propagate the orbit from the tle time to atime, using SGP(D)4.
1120        //        //
1121          if ( debug ) printf(" %i sgp4 \n",procev);      
1122        cCoordGeo coo;        cCoordGeo coo;
1123        float jyear=0;            Float_t jyear=0.;    
1124        //        //
1125        if(atime >= gltle->GetToTime()) {        if(atime >= gltle->GetToTime()) {
1126          if ( !gltle->Query(atime, dbc) ){          if ( !gltle->Query(atime, dbc) ){
1127            //                  //      
1128            // Compute the magnetic dipole moment.            // Compute the magnetic dipole moment.
1129            //            //
1130              if ( debug ) printf(" %i compute magnetic dipole moment \n",procev);      
1131            UInt_t year, month, day, hour, min, sec;            UInt_t year, month, day, hour, min, sec;
1132            //            //
1133            TTimeStamp t = TTimeStamp(atime, kTRUE);            TTimeStamp t = TTimeStamp(atime, kTRUE);
# Line 688  int OrbitalInfoCore(UInt_t run, TFile *f Line 1135  int OrbitalInfoCore(UInt_t run, TFile *f
1135            t.GetTime(kTRUE, 0, &hour, &min, &sec);            t.GetTime(kTRUE, 0, &hour, &min, &sec);
1136            jyear = (float) year            jyear = (float) year
1137              + (month*31.+ (float) day)/365.              + (month*31.+ (float) day)/365.
1138              + (hour*3600.+min*60.+(float)sec)/(24*3600*365.);              + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);
1139            //            //
1140              if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);            
1141              if ( debug ) printf(" %i jyear %f dimo %f \n",procev,jyear,dimo);
1142            feldcof_(&jyear, &dimo); // get dipole moment for year            feldcof_(&jyear, &dimo); // get dipole moment for year
1143              if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);
1144    
1145              GM_TimeAdjustCoefs(year, jyear, G0, G1, H1, &Model);
1146              GM_PoleLocation(Model, &Pole);
1147              
1148          } else {          } else {
1149            code = -56;            code = -56;
1150            goto closeandexit;            goto closeandexit;
# Line 700  int OrbitalInfoCore(UInt_t run, TFile *f Line 1154  int OrbitalInfoCore(UInt_t run, TFile *f
1154        //        //
1155        cOrbit orbits(*gltle->GetTle());        cOrbit orbits(*gltle->GetTle());
1156        //        //
       if ( debug ) printf(" I am Here \n");  
       //  
1157        // synchronize with quaternions data        // synchronize with quaternions data
1158        //        //
1159        if ( isf && neventsm>0 ){        if ( isf && neventsm>0 ){
         if ( debug ) printf(" I am here \n");  
1160          //          //
1161          // First event          // First event
1162          //          //
1163          isf = false;          isf = false;
1164          upperqtime = atime;          //      upperqtime = atime;
1165          lowerqtime = runinfo->RUNHEADER_TIME;          lowerqtime = runinfo->RUNHEADER_TIME;
1166          for ( ik = 0; ik < neventsm; ik++){          for ( ik = 0; ik < neventsm; ik++){  //number of macrocommad packets
1167            l0trm->GetEntry(ik);            if ( ch->GetEntry(ik) <= 0 ) throw -36;
1168            tmpSize = mcmdev->Records->GetEntries();            tmpSize = mcmdev->Records->GetEntries();
1169            numrec = tmpSize;            //      numrec = tmpSize;
1170            for (Int_t j3 = 0;j3<tmpSize;j3++){            if ( debug ) cout << "packet number " << ik <<"\tnumber of subpackets is " << tmpSize << endl;
1171              if ( debug ) printf(" eh eh eh \n");            for (Int_t j3 = 0;j3<tmpSize;j3++){  //number of subpackets
1172              mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);              mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);
1173              if ((int)mcmdrc->ID1 == 226){              if ( mcmdrc ){ // missing inclination bug [8RED 090116]
1174                L_QQ_Q_l_upper->fill(mcmdrc->McmdData);                if ( debug ) printf(" pluto \n");
1175                for (UInt_t ui = 0; ui < 6; ui++){                if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet
1176                  if (ui>0){                 L_QQ_Q_l_upper->fill(mcmdrc->McmdData);
1177                    if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){                  for (UInt_t ui = 0; ui < 6; ui++){
1178                      if (dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000))<atime){                    if (ui>0){
1179                        upperqtime = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));                      if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){
1180                        orbits.getPosition((double) (upperqtime - gltle->GetFromTime())/60., &eCi);                        if ( debug ) printf(" here1 %i \n",ui);
1181                        RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[ui][0],L_QQ_Q_l_upper->quat[ui][1],L_QQ_Q_l_upper->quat[ui][2],L_QQ_Q_l_upper->quat[ui][3]);                        Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));
1182                      }else {                        Int_t recSize = recqtime.size();
1183                        lowerqtime = upperqtime;                        if(lowerqtime > recqtime[recSize-1]){
1184                        upperqtime = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));                           // to avoid interpolation between bad quaternions arrays
1185                        orbits.getPosition((double) (upperqtime - gltle->GetFromTime())/60., &eCi);                           if(sqrt(pow(L_QQ_Q_l_upper->quat[ui][0],2)+pow(L_QQ_Q_l_upper->quat[ui][1],2)+pow(L_QQ_Q_l_upper->quat[ui][2],2)+pow(L_QQ_Q_l_upper->quat[ui][3],2))>0.99999){
1186                        RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[ui][0],L_QQ_Q_l_upper->quat[ui][1],L_QQ_Q_l_upper->quat[ui][2],L_QQ_Q_l_upper->quat[ui][3]);                            Int_t sizeqmcmd = qtime.size();
1187                        mcreen = j3;                            inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1188                        mctren = ik;                            qtime[sizeqmcmd]=u_time;
1189                        if(fgh==0){                            q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
1190                          CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);                            q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
1191                          CopyAng(RYPang_lower,RYPang_upper);                            q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
1192                              q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
1193                              qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1194                              lowerqtime = u_time;
1195                              orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1196                              RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[ui][0],L_QQ_Q_l_upper->quat[ui][1],L_QQ_Q_l_upper->quat[ui][2],L_QQ_Q_l_upper->quat[ui][3]);
1197                              qRoll[sizeqmcmd]=RYPang_upper->Kren;
1198                              qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1199                              qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1200                             }
1201                          }
1202                          for(Int_t mu = nt;mu<recSize;mu++){
1203                            if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1204                              if(sqrt(pow(recq0[mu],2)+pow(recq1[mu],2)+pow(recq2[mu],2)+pow(recq3[mu],2))>0.99999){
1205                                nt=mu;
1206                                Int_t sizeqmcmd = qtime.size();
1207                                inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1208                                qtime[sizeqmcmd]=recqtime[mu];
1209                                q0[sizeqmcmd]=recq0[mu];
1210                                q1[sizeqmcmd]=recq1[mu];
1211                                q2[sizeqmcmd]=recq2[mu];
1212                                q3[sizeqmcmd]=recq3[mu];
1213                                qmode[sizeqmcmd]=-10;
1214                                orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1215                                RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[mu],recq1[mu],recq2[mu],recq3[mu]);
1216                                qRoll[sizeqmcmd]=RYPang_upper->Kren;
1217                                qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1218                                qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1219                              }
1220                            }
1221                            if(recqtime[mu]>=u_time){
1222                              if(sqrt(pow(L_QQ_Q_l_upper->quat[ui][0],2)+pow(L_QQ_Q_l_upper->quat[ui][1],2)+pow(L_QQ_Q_l_upper->quat[ui][2],2)+pow(L_QQ_Q_l_upper->quat[ui][3],2))>0.99999){
1223                                Int_t sizeqmcmd = qtime.size();
1224                                inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1225                                qtime[sizeqmcmd]=u_time;
1226                                q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
1227                                q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
1228                                q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
1229                                q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
1230                                qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1231                                lowerqtime = u_time;
1232                                orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1233                                RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[ui][0],L_QQ_Q_l_upper->quat[ui][1],L_QQ_Q_l_upper->quat[ui][2],L_QQ_Q_l_upper->quat[ui][3]);
1234                                qRoll[sizeqmcmd]=RYPang_upper->Kren;
1235                                qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1236                                qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1237                                break;
1238                              }
1239                            }
1240                        }                        }
                       oi=ui;  
                       goto closethisloop;  
1241                      }                      }
1242                      fgh++;                    }else{
1243                      CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);                      if ( debug ) printf(" here2 %i \n",ui);
1244                      CopyAng(RYPang_lower,RYPang_upper);                      Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));
1245                    }                      if(lowerqtime>u_time)nt=0;
1246                  }else{                      Int_t recSize = recqtime.size();
1247                    if (dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))<atime){                      if(lowerqtime > recqtime[recSize-1]){
1248                      upperqtime = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));                        if(sqrt(pow(L_QQ_Q_l_upper->quat[ui][0],2)+pow(L_QQ_Q_l_upper->quat[ui][1],2)+pow(L_QQ_Q_l_upper->quat[ui][2],2)+pow(L_QQ_Q_l_upper->quat[ui][3],2))>0.99999){
1249                      orbits.getPosition((double) (upperqtime - gltle->GetFromTime())/60., &eCi);                          Int_t sizeqmcmd = qtime.size();
1250                      RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);                          inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1251                    }                          qtime[sizeqmcmd]=u_time;
1252                    else {                          q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1253                      lowerqtime = upperqtime;                          q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1254                      upperqtime = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));                          q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1255                      orbits.getPosition((double) (upperqtime - gltle->GetFromTime())/60., &eCi);                          q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1256                      RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);                          qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1257                      mcreen = j3;                          lowerqtime = u_time;
1258                      mctren = ik;                          orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1259                      if(fgh==0){                          RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);
1260                        CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);                          qRoll[sizeqmcmd]=RYPang_upper->Kren;
1261                        CopyAng(RYPang_lower,RYPang_upper);                          qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1262                        lowerqtime = atime-1;                          qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1263                          }
1264                        }
1265                        for(Int_t mu = nt;mu<recSize;mu++){
1266                          if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1267                             if(sqrt(pow(recq0[mu],2)+pow(recq1[mu],2)+pow(recq2[mu],2)+pow(recq3[mu],2))>0.99999){
1268                               nt=mu;
1269                               Int_t sizeqmcmd = qtime.size();
1270                               inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1271                               qtime[sizeqmcmd]=recqtime[mu];
1272                               q0[sizeqmcmd]=recq0[mu];
1273                               q1[sizeqmcmd]=recq1[mu];
1274                               q2[sizeqmcmd]=recq2[mu];
1275                               q3[sizeqmcmd]=recq3[mu];
1276                               qmode[sizeqmcmd]=-10;
1277                               orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1278                               RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[mu],recq1[mu],recq2[mu],recq3[mu]);
1279                               qRoll[sizeqmcmd]=RYPang_upper->Kren;
1280                               qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1281                               qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1282                             }
1283                          }
1284                          if(recqtime[mu]>=u_time){
1285                             if(sqrt(pow(L_QQ_Q_l_upper->quat[0][0],2)+pow(L_QQ_Q_l_upper->quat[0][1],2)+pow(L_QQ_Q_l_upper->quat[0][2],2)+pow(L_QQ_Q_l_upper->quat[0][3],2))>0.99999){
1286                               Int_t sizeqmcmd = qtime.size();
1287                               inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1288                               qtime[sizeqmcmd]=u_time;
1289                               q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1290                               q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1291                               q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1292                               q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1293                               qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1294                               lowerqtime = u_time;
1295                               orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1296                               RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);
1297                               qRoll[sizeqmcmd]=RYPang_upper->Kren;
1298                               qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1299                               qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1300                               CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);
1301                               break;
1302                             }
1303                          }
1304                      }                      }
                     oi=ui;  
                     goto closethisloop;  
                     //_0 = true;  
1305                    }                    }
1306                    fgh++;                  }
                   CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);  
                   CopyAng(RYPang_lower,RYPang_upper);  
                   //_0 = true;  
                 };  
                 //cin>>grib;  
               };  
             };  
           };  
         };  
       };  
     closethisloop:  
       //  
       if ( debug ) printf(" I am There \n");  
       //  
       if (((atime>(UInt_t)upperqtime)||(atime<(UInt_t)lowerqtime)) && neventsm>0 ){  
         if ( debug ) printf(" I am there \n");  
         //  
         lowerqtime = upperqtime;  
         Long64_t maxloop = 100000000LL;  
         Long64_t mn = 0;  
         bool gh=false;  
         ooi=oi;  
         if ( verbose ) printf(" OrbitalInfoCore: sync with quaternions data upperqtime %u lowerqtime %u atime %u \n",(UInt_t)upperqtime,(UInt_t)lowerqtime,atime);  
         while (!gh){        
           if ( mn > maxloop ){  
             if ( verbose ) printf(" OrbitalInfoCore: quaternions sync out of range! exiting\n");  
             gh = true;  
             neventsm = 0;  
           };  
           mn++;  
           if (oi<5) oi++;  
           else oi=0;  
           if (oi==0 && numrec > 0){  
             if ( debug ) printf(" mumble \n");  
             mcreen++;  
             if (mcreen == numrec){  
               mctren++;  
               mcreen = 0;  
               l0trm->GetEntry(mctren);  
               numrec = mcmdev->Records->GetEntries();  
             }  
             CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);  
             CopyAng(RYPang_lower,RYPang_upper);  
             mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(mcreen);  
             if ((int)mcmdrc->ID1 == 226){  
               L_QQ_Q_l_upper->fill(mcmdrc->McmdData);  
               upperqtime = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
               if (upperqtime<lowerqtime){  
                 upperqtime=runinfo->RUNTRAILER_TIME;  
                 CopyQ(L_QQ_Q_l_upper,L_QQ_Q_l_lower);  
                 CopyAng(RYPang_upper,RYPang_lower);  
               }else{  
                 orbits.getPosition((double) (upperqtime - gltle->GetFromTime())/60., &eCi);  
                 RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[0][0],L_QQ_Q_l_upper->quat[0][1],L_QQ_Q_l_upper->quat[0][2],L_QQ_Q_l_upper->quat[0][3]);  
1307                }                }
               //              re--;  
               gh=true;  
1308              }              }
1309            }else{              //if ( debug ) cout << "subpacket " << j3 << "\t qtime = " << qtime[qtime.size()-1] << endl;
             if ((Int_t)L_QQ_Q_l_upper->time[oi]>(Int_t)L_QQ_Q_l_upper->time[0]){  
               upperqtime = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));  
               orbits.getPosition((double) (upperqtime - gltle->GetFromTime())/60., &eCi);  
               RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[oi][0],L_QQ_Q_l_upper->quat[oi][1],L_QQ_Q_l_upper->quat[oi][2],L_QQ_Q_l_upper->quat[oi][3]);  
               orbits.getPosition((double) (lowerqtime - gltle->GetFromTime())/60., &eCi);  
               RYPang_lower->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,L_QQ_Q_l_upper->quat[oi-1][0],L_QQ_Q_l_upper->quat[oi-1][1],L_QQ_Q_l_upper->quat[oi-1][2],L_QQ_Q_l_upper->quat[oi-1][3]);  
               //              re--;  
               gh=true;  
             };  
           };  
         };  
         if ( verbose ) printf(" OrbitalInfoCore: sync with quaternions data now we have upperqtime %u lowerqtime %u atime %u \n",(UInt_t)upperqtime,(UInt_t)lowerqtime,atime);  
       };  
       //  
       if ( debug ) printf(" I am THIS \n");  
       //  
       // Fill in quaternions and angles  
       //  
       if ((atime<=(UInt_t)upperqtime)&&(atime>=(UInt_t)lowerqtime)&& neventsm>0){        
         if ( debug ) printf(" I am this \n");  
         UInt_t tut = holeq(lowerqtime, upperqtime, L_QQ_Q_l_lower, L_QQ_Q_l_upper, oi);  
         if (oi == 0){  
           if ((tut!=5)||(tut!=6)){  
             incli = (L_QQ_Q_l_upper->quat[0][0]-L_QQ_Q_l_lower->quat[ooi][0])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
             orbitalinfo->q0 =  incli*atime+L_QQ_Q_l_upper->quat[0][0]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
             incli =     (L_QQ_Q_l_upper->quat[0][1]-L_QQ_Q_l_lower->quat[ooi][1])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
             orbitalinfo->q1 =  incli*atime+L_QQ_Q_l_upper->quat[0][1]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
             incli = (L_QQ_Q_l_upper->quat[0][2]-L_QQ_Q_l_lower->quat[ooi][2])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
             orbitalinfo->q2 =  incli*atime+L_QQ_Q_l_upper->quat[0][2]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
             incli = (L_QQ_Q_l_upper->quat[0][3]-L_QQ_Q_l_lower->quat[ooi][3])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
             orbitalinfo->q3 =  incli*atime+L_QQ_Q_l_upper->quat[0][3]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
           
             incli = (RYPang_upper->Tangazh-RYPang_lower->Tangazh)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
             orbitalinfo->theta =  incli*atime+RYPang_upper->Tangazh-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
             incli = (RYPang_upper->Ryskanie-RYPang_lower->Ryskanie)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
             orbitalinfo->phi =  incli*atime+RYPang_upper->Ryskanie-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
             incli = (RYPang_upper->Kren-RYPang_lower->Kren)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000)));  
             orbitalinfo->etha =  incli*atime+RYPang_upper->Kren-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
1310            }            }
1311            if (tut==6){          }
             if (fabs(RYPang_lower->Kren-RYPang_upper->Kren)<0.1){  
               incli = (L_QQ_Q_l_upper->quat[0][0]-L_QQ_Q_l_lower->quat[ooi][0])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->q0 =  incli*atime+L_QQ_Q_l_upper->quat[0][0]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
               incli =           (L_QQ_Q_l_upper->quat[0][1]-L_QQ_Q_l_lower->quat[ooi][1])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->q1 =  incli*atime+L_QQ_Q_l_upper->quat[0][1]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
               incli = (L_QQ_Q_l_upper->quat[0][2]-L_QQ_Q_l_lower->quat[ooi][2])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->q2 =  incli*atime+L_QQ_Q_l_upper->quat[0][2]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
               incli = (L_QQ_Q_l_upper->quat[0][3]-L_QQ_Q_l_lower->quat[ooi][3])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->q3 =  incli*atime+L_QQ_Q_l_upper->quat[0][3]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
           
               incli = (RYPang_upper->Tangazh-RYPang_lower->Tangazh)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->theta =  incli*atime+RYPang_upper->Tangazh-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
               incli = (RYPang_upper->Ryskanie-RYPang_lower->Ryskanie)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->phi =  incli*atime+RYPang_upper->Ryskanie-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
               //cout<<"upper = "<<RYPang_upper->Ryskanie<<" lower = "<<RYPang_lower->Ryskanie<<" timeupper[0] = "<<L_QQ_Q_l_upper->time[0]-5500000<<" timelower["<<ooi<<"] = "<<L_QQ_Q_l_lower->time[ooi]-5500000<<" Ryscanie = "<<orbitalinfo->phi<<" incli = "<<incli<<" upper-lower = "<<RYPang_upper->Ryskanie-RYPang_lower->Ryskanie<<" Dtime = "<<dbtime->DBabsTime((UInt_t)L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000)<<"-"<<dbtime->DBabsTime((UInt_t)L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)<<" atime = "<<atime<<"\n";  
               //cin>>grib;  
               incli = (RYPang_upper->Kren-RYPang_lower->Kren)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_lower->time[ooi]*1000-DeltaOBT*1000)));  
               orbitalinfo->etha =  incli*atime+RYPang_upper->Kren-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));  
             }  
           }  
         } else {  
           if((tut!=6)||(tut!=7)||(tut!=9)){  
             incli = (L_QQ_Q_l_upper->quat[oi][0]-L_QQ_Q_l_upper->quat[oi-1][0])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));  
             orbitalinfo->q0 =  incli*atime+L_QQ_Q_l_upper->quat[oi][0]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));  
             incli = (L_QQ_Q_l_upper->quat[oi][1]-L_QQ_Q_l_upper->quat[oi-1][1])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));  
             orbitalinfo->q1 =  incli*atime+L_QQ_Q_l_upper->quat[oi][1]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));  
             incli = (L_QQ_Q_l_upper->quat[oi][2]-L_QQ_Q_l_upper->quat[oi-1][2])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));  
             orbitalinfo->q2 =  incli*atime+L_QQ_Q_l_upper->quat[oi][2]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));  
             incli = (L_QQ_Q_l_upper->quat[oi][3]-L_QQ_Q_l_upper->quat[oi-1][3])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));  
             orbitalinfo->q3 =  incli*atime+L_QQ_Q_l_upper->quat[oi][3]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));  
1312                    
1313              incli = (RYPang_upper->Tangazh-RYPang_lower->Tangazh)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));          if(qtime.size()==0){                            // in case if no orientation information in data
1314              orbitalinfo->theta =  incli*atime+RYPang_upper->Tangazh-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));            if ( debug ) cout << "qtime.size() = 0" << endl;
1315              incli = (RYPang_upper->Ryskanie-RYPang_lower->Ryskanie)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));            for(UInt_t my=0;my<recqtime.size();my++){
1316              orbitalinfo->phi =  incli*atime+RYPang_upper->Ryskanie-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));              if(sqrt(pow(recq0[my],2)+pow(recq1[my],2)+pow(recq2[my],2)+pow(recq3[my],2))>0.99999){
1317              //cout<<"upper = "<<RYPang_upper->Ryskanie<<" lower = "<<RYPang_lower->Ryskanie<<" timeupper["<<oi<<"] = "<<L_QQ_Q_l_upper->time[oi]-5500000<<" timelower["<<oi-1<<"] = "<<L_QQ_Q_l_lower->time[oi-1]-5500000<<" Ryscanie = "<<orbitalinfo->phi<<" incli = "<<incli<<" upper-lower = "<<RYPang_upper->Ryskanie-RYPang_lower->Ryskanie<<" Dtime = "<<dbtime->DBabsTime((UInt_t)L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000)<<"-"<<dbtime->DBabsTime((UInt_t)L_QQ_Q_l_lower->time[oi-1]*1000-DeltaOBT*1000)<<" atime = "<<atime<<"\n";                Int_t sizeqmcmd = qtime.size();
1318              //cin>>grib;                inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1319              incli = (RYPang_upper->Kren-RYPang_lower->Kren)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));                qtime[sizeqmcmd]=recqtime[my];
1320              orbitalinfo->etha =  incli*atime+RYPang_upper->Kren-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));                q0[sizeqmcmd]=recq0[my];
1321            }                q1[sizeqmcmd]=recq1[my];
1322            if (tut==6){                q2[sizeqmcmd]=recq2[my];
1323              if (fabs(RYPang_lower->Kren-RYPang_upper->Kren)<0.1){                q3[sizeqmcmd]=recq3[my];
1324                incli = (L_QQ_Q_l_upper->quat[oi][0]-L_QQ_Q_l_upper->quat[oi-1][0])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));                qmode[sizeqmcmd]=-10;
1325                orbitalinfo->q0 =  incli*atime+L_QQ_Q_l_upper->quat[oi][0]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));                orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1326                incli = (L_QQ_Q_l_upper->quat[oi][1]-L_QQ_Q_l_upper->quat[oi-1][1])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));                RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[my],recq1[my],recq2[my],recq3[my]);
1327                orbitalinfo->q1 =  incli*atime+L_QQ_Q_l_upper->quat[oi][1]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));                qRoll[sizeqmcmd]=RYPang_upper->Kren;
1328                incli = (L_QQ_Q_l_upper->quat[oi][2]-L_QQ_Q_l_upper->quat[oi-1][2])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));                qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1329                orbitalinfo->q2 =  incli*atime+L_QQ_Q_l_upper->quat[oi][2]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));                qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1330                incli = (L_QQ_Q_l_upper->quat[oi][3]-L_QQ_Q_l_upper->quat[oi-1][3])/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));              }
1331                orbitalinfo->q3 =  incli*atime+L_QQ_Q_l_upper->quat[oi][3]-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));            }
1332            }
1333                    
1334                incli = (RYPang_upper->Tangazh-RYPang_lower->Tangazh)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));  
1335                orbitalinfo->theta =  incli*atime+RYPang_upper->Tangazh-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));          if ( debug ) printf(" puffi \n");
1336                incli = (RYPang_upper->Ryskanie-RYPang_lower->Ryskanie)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));          Double_t tmin = 9999999999.;
1337                orbitalinfo->phi =  incli*atime+RYPang_upper->Ryskanie-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));          Double_t tmax = 0.;
1338                //cout<<"upper = "<<RYPang_upper->Ryskanie<<" lower = "<<RYPang_lower->Ryskanie<<" timeupper["<<oi<<"] = "<<L_QQ_Q_l_upper->time[oi]-5500000<<" timelower["<<oi-1<<"] = "<<L_QQ_Q_l_lower->time[oi-1]-5500000<<" Ryscanie = "<<orbitalinfo->phi<<" incli = "<<incli<<" upper-lower = "<<RYPang_upper->Ryskanie-RYPang_lower->Ryskanie<<" Dtime = "<<dbtime->DBabsTime((UInt_t)L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000)<<"-"<<dbtime->DBabsTime((UInt_t)L_QQ_Q_l_lower->time[oi-1]*1000-DeltaOBT*1000)<<" atime = "<<atime<<"\n";          for(UInt_t tre = 0;tre<qtime.size();tre++){
1339                //cin>>grib;            if(qtime[tre]>tmax)tmax = qtime[tre];
1340                incli = (RYPang_upper->Kren-RYPang_lower->Kren)/(dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000))-dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi-1]*1000-DeltaOBT*1000)));            if(qtime[tre]<tmin)tmin = qtime[tre];
1341                orbitalinfo->etha =  incli*atime+RYPang_upper->Kren-incli*dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[oi]*1000-DeltaOBT*1000));          }
1342            // sorting quaternions by time
1343           Bool_t t = true;
1344            while(t){
1345             t=false;
1346              for(UInt_t i=0;i<qtime.size()-1;i++){
1347                if(qtime[i]>qtime[i+1]){
1348                  Double_t tmpr = qtime[i];
1349                  qtime[i]=qtime[i+1];
1350                  qtime[i+1] = tmpr;
1351                  tmpr = q0[i];
1352                  q0[i]=q0[i+1];
1353                  q0[i+1] = tmpr;
1354                  tmpr = q1[i];
1355                  q1[i]=q1[i+1];
1356                  q1[i+1] = tmpr;
1357                  tmpr = q2[i];
1358                  q2[i]=q2[i+1];
1359                  q2[i+1] = tmpr;
1360                  tmpr = q3[i];
1361                  q3[i]=q3[i+1];
1362                  q3[i+1] = tmpr;
1363                  tmpr = qRoll[i];
1364                  qRoll[i]=qRoll[i+1];
1365                  qRoll[i+1] = tmpr;
1366                  tmpr = qYaw[i];
1367                  qYaw[i]=qYaw[i+1];
1368                  qYaw[i+1] = tmpr;
1369                  tmpr = qPitch[i];
1370                  qPitch[i]=qPitch[i+1];
1371                  qPitch[i+1] = tmpr;
1372                    t=true;
1373              }              }
1374            }                        }
1375          }          }
1376          //  
1377          orbitalinfo->mode = holeq(lowerqtime, upperqtime, L_QQ_Q_l_lower, L_QQ_Q_l_upper, oi);          if ( debug ){
1378          //            cout << "we have loaded quaternions: size of quaternions set is "<< qtime.size() << endl;
1379        } else {           for(UInt_t i=0;i<qtime.size();i++) cout << qtime[i] << "\t";
1380          if ( debug ) printf(" ops no incl! \n");            cout << endl << endl;
1381          orbitalinfo->mode = 10;            Int_t lopu;
1382        };            cin >> lopu;
1383           }
1384    
1385          } // if we processed first event
1386    
1387          
1388          //Filling Inclination information
1389          Double_t incli = 0;
1390          if ( qtime.size() > 1 ){
1391            if ( debug ) cout << "ok quaternions is exist and mu = " << must << endl;
1392            if ( debug ) cout << "qtimes[ " << qtime[0] << " , " << qtime[qtime.size()-1] << " ]\tatime = "<<atime<<endl;
1393            for(UInt_t mu = must;mu<qtime.size()-1;mu++){
1394              if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);
1395              if(qtime[mu+1]>qtime[mu]){
1396                if ( debug ) cout << "qtime[" << mu << "] = " << qtime[mu] << "\tqtime[" << mu+1 << "] = " << qtime[mu+1] << "\tatime = " << atime << endl;
1397                if(atime<=qtime[mu+1] && atime>=qtime[mu]){
1398                  if ( debug ) cout << "here we have found proper quaternions for interpolation: mu = "<<mu<<endl;
1399                  must = mu;
1400                  incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);
1401                  orbitalinfo->theta =  incli*atime+qPitch[mu+1]-incli*qtime[mu+1];
1402                  incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);
1403                  orbitalinfo->etha =  incli*atime+qRoll[mu+1]-incli*qtime[mu+1];
1404                  incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1405                  orbitalinfo->phi =  incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1406                  
1407                  incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1408                  orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];
1409                  incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1410                  orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];
1411                  incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1412                  orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];
1413                  incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1414                  orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];
1415                  Float_t tg = (qtime[mu+1]-qtime[mu])/1000.0;
1416                  if(tg>=1) tg=0.00;
1417                  orbitalinfo->TimeGap = TMath::Min(TMath::Abs(qtime[mu+1])-atime,TMath::Abs(atime-qtime[mu]))+tg;//qtime[mu+1]-qtime[mu];
1418                  orbitalinfo->mode = qmode[mu+1];
1419                  //if(atime==qtime[mu] || atime==qtime[mu+1]) orbitalinfo->qkind = 0; else orbitalinfo->qkind=1;
1420                  //if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;
1421                  if ( debug ) printf(" grfuffi4 %i \n",mu);
1422                  break;
1423                }
1424              }
1425            }
1426          }
1427          if ( debug ) printf(" grfuffi5  \n");
1428        //        //
1429        // ops no inclination information        // ops no inclination information
1430        //        //
1431          
1432        if ( orbitalinfo->q0< -999 || orbitalinfo->q1 < -999 || orbitalinfo->q2 < -999 || orbitalinfo->q3 < -999 || orbitalinfo->q0 != orbitalinfo->q0 || orbitalinfo->q1 != orbitalinfo->q1 || orbitalinfo->q2 != orbitalinfo->q2 || orbitalinfo->q3 != orbitalinfo->q3 ){        if ( orbitalinfo->q0< -999 || orbitalinfo->q1 < -999 || orbitalinfo->q2 < -999 || orbitalinfo->q3 < -999 || orbitalinfo->q0 != orbitalinfo->q0 || orbitalinfo->q1 != orbitalinfo->q1 || orbitalinfo->q2 != orbitalinfo->q2 || orbitalinfo->q3 != orbitalinfo->q3 ){
1433            if ( debug ) cout << "ops no iclination information" << endl;
1434          orbitalinfo->mode = 10;          orbitalinfo->mode = 10;
1435          orbitalinfo->q0 = -1000.;          orbitalinfo->q0 = -1000.;
1436          orbitalinfo->q1 = -1000.;          orbitalinfo->q1 = -1000.;
# Line 949  int OrbitalInfoCore(UInt_t run, TFile *f Line 1439  int OrbitalInfoCore(UInt_t run, TFile *f
1439          orbitalinfo->etha = -1000.;          orbitalinfo->etha = -1000.;
1440          orbitalinfo->phi = -1000.;          orbitalinfo->phi = -1000.;
1441          orbitalinfo->theta = -1000.;          orbitalinfo->theta = -1000.;
1442        };          orbitalinfo->TimeGap = -1000.;
1443            //orbitalinfo->qkind = -1000;
1444            
1445            //      if ( debug ){
1446            //        Int_t lopu;
1447            //         cin >> lopu;
1448            //      }
1449            if ( debug ) printf(" grfuffi6 \n");
1450          }
1451        //        //
1452          if ( debug ) printf(" filling \n");
1453        // #########################################################################################################################          // #########################################################################################################################  
1454        //        //
1455        // fill orbital positions        // fill orbital positions
# Line 961  int OrbitalInfoCore(UInt_t run, TFile *f Line 1460  int OrbitalInfoCore(UInt_t run, TFile *f
1460        lon = (coo.m_Lon > M_PI) ? rad2deg(coo.m_Lon - 2*M_PI) : rad2deg(coo.m_Lon);        lon = (coo.m_Lon > M_PI) ? rad2deg(coo.m_Lon - 2*M_PI) : rad2deg(coo.m_Lon);
1461        lat = rad2deg(coo.m_Lat);        lat = rad2deg(coo.m_Lat);
1462        alt = coo.m_Alt;        alt = coo.m_Alt;
1463        //  
1464          cOrbit orbits2(*gltle->GetTle());
1465          orbits2.getPosition((double) (atime - gltle->GetFromTime())/60., &eCi);
1466          //      Float_t x=eCi.getPos().m_x;
1467          //      Float_t y=eCi.getPos().m_y;
1468          //      Float_t z=eCi.getPos().m_z;
1469          
1470          TVector3 V(eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z);
1471          TVector3 Pos(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z);
1472          
1473          Float_t dlon=Pos.Phi()*TMath::RadToDeg()-lon;
1474          
1475          Pos.RotateZ(-dlon*TMath::DegToRad());
1476          V.RotateZ(-dlon*TMath::DegToRad());
1477          Float_t diro;
1478          if(V.Z()>0) diro=1; else diro=-1;
1479          
1480          // 10REDNEW
1481          Int_t errq=0;
1482          Int_t azim=0;
1483          Int_t MU=0;
1484          for(UInt_t mu = must;mu<RTtime2.size()-1;mu++){
1485            if(atime<=RTstart[mu+1] && atime>=RTstart[mu]){
1486              errq=RTerrq[mu];
1487              azim=RTazim[mu];
1488              MU=mu;
1489              break;
1490            }
1491          }
1492          orbitalinfo->errq = errq;
1493          orbitalinfo->azim = azim;
1494          orbitalinfo->qkind = 0;
1495          
1496          if ( debug ) printf(" coord done \n");
1497        if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){          if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){  
1498          //                //      
1499          orbitalinfo->lon = lon;          orbitalinfo->lon = lon;
1500          orbitalinfo->lat = lat;          orbitalinfo->lat = lat;
1501          orbitalinfo->alt = alt ;          orbitalinfo->alt = alt;
1502            orbitalinfo->V = V;
1503    
1504            //      GMtype_CoordGeodetic  location;
1505            location.lambda = lon;
1506            location.phi = lat;
1507            location.HeightAboveEllipsoid = alt;
1508    
1509            GM_GeodeticToSpherical(Ellip, location, &CoordSpherical);
1510            GM_SphericalToCartesian(CoordSpherical,  &CoordCartesian);
1511            GM_EarthCartToDipoleCartCD(Pole, CoordCartesian, &DipoleCartesian);
1512            GM_CartesianToSpherical(DipoleCartesian, &DipoleSpherical);
1513            orbitalinfo->londip = DipoleSpherical.lambda;
1514            orbitalinfo->latdip = DipoleSpherical.phig;
1515    
1516            if(debug)cout<<"geodetic:\t"<<lon<<"\t"<<lat<<"\tgeomagnetic:\t"<<orbitalinfo->londip<<"\t"<<orbitalinfo->latdip<<endl;
1517    
1518          //          //
1519          // compute mag field components and L shell.          // compute mag field components and L shell.
1520          //          //
1521            if ( debug ) printf(" call igrf feldg \n");
1522          feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);          feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);
1523            if ( debug ) printf(" call igrf shellg \n");
1524          shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);          shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);
1525            if ( debug ) printf(" call igrf findb \n");
1526          findb0_(&stps, &bdel, &value, &bequ, &rr0);          findb0_(&stps, &bdel, &value, &bequ, &rr0);
1527          //          //
1528            if ( debug ) printf(" done igrf \n");
1529          orbitalinfo->Bnorth = bnorth;          orbitalinfo->Bnorth = bnorth;
1530          orbitalinfo->Beast = beast;          orbitalinfo->Beast = beast;
1531          orbitalinfo->Bdown = bdown;          orbitalinfo->Bdown = bdown;
1532          orbitalinfo->Babs = babs;          orbitalinfo->Babs = babs;
1533            orbitalinfo->M = dimo;
1534          orbitalinfo->BB0 = babs/bequ;          orbitalinfo->BB0 = babs/bequ;
1535          orbitalinfo->L = xl;                orbitalinfo->L = xl;      
1536          // Set Stormer vertical cutoff using L shell.          // Set Stormer vertical cutoff using L shell.
1537          orbitalinfo->cutoff[0] = 14.9/(xl*xl);          orbitalinfo->cutoffsvl = 14.295 / (xl*xl); //
1538            if(debug)cout << "L = " << xl << "\tM = " << dimo << "\tvertical cutoff:  "<< orbitalinfo->cutoffsvl << endl;
1539    
1540            /*
1541              ---------- Forwarded message ----------
1542              Date: Wed, 09 May 2012 12:16:47 +0200
1543              From: Alessandro Bruno <alessandro.bruno@ba.infn.it>
1544              To: Mirko Boezio <mirko.boezio@ts.infn.it>
1545              Cc: Francesco S. Cafagna <Francesco.Cafagna@ba.infn.it>
1546              Subject: Störmer vertical cutoff
1547    
1548              Ciao Mirko,
1549              volevo segnalarti che il valore dello Störmer vertical cutoff nel Level2 è
1550              sovrastimato di circa il 4%.
1551              Dopo un'approfondita analisi con l'IGRF-05 abbiamo ricavano un valore pari
1552              a: 14.295 / L^2 anzichè 14.9 / L^2, valore obsoleto in quanto riferito agli
1553              anni '50.
1554            */
1555            //14.9/(xl*xl);
1556            orbitalinfo->igrf_icode = icode;
1557          //          //
1558        };              }      
1559          //
1560          if ( debug ) printf(" pitch angle \n");
1561        //        //
1562        // pitch angles        // pitch angles
1563        //        //
1564        if ( orbitalinfo->mode != 10 && orbitalinfo->mode != 5 && orbitalinfo->mode !=7 && orbitalinfo->mode != 9 ){        if( orbitalinfo->TimeGap>0){
1565          //          //
1566          Float_t Bx = -orbitalinfo->Bdown;                       //don't need for PamExp ExpOnly for all geography areas          if ( debug ) printf(" timegap %f \n",orbitalinfo->TimeGap);
1567          Float_t By = orbitalinfo->Beast;                        //don't need for PamExp ExpOnly for all geography areas          Float_t Bx = -orbitalinfo->Bdown;
1568          Float_t Bz = orbitalinfo->Bnorth;                       //don't need for PamExp ExpOnly for all geography areas          Float_t By = orbitalinfo->Beast;
1569          //          Float_t Bz = orbitalinfo->Bnorth;
1570          TMatrixD Fij = PO->ECItoGreenwich(PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3),orbitalinfo->absTime);  
1571          TMatrixD Dij = PO->GreenwichtoGEO(orbitalinfo->lat,orbitalinfo->lon,Fij);          TMatrixD Qiji(3,3);
1572            TMatrixD Qij = PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3);
1573            TMatrixD Dij = PO->ECItoGEO(Qij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon);
1574    
1575    //10REDNEW
1576            /* If initial orientation data have reason to be inaccurate */
1577            Float_t tg = 0.00;
1578           Float_t tmptg;
1579           if(MU!=0){
1580    //      if(orbitalinfo->TimeGap>0 && errq==0 && azim==0){               // 10RED CHECK  (comparison between three metod of recovering orientation)
1581           if((atime>=RTstart[MU] && atime<RTstart[MU+1] && RTbank1[MU]==0 && RTbank2[MU]==0 && TMath::Abs(orbitalinfo->etha)>0.01) || ((RTbank1[MU]!=0 || RTbank2[MU]!=0) && atime>=RTstart[MU] && atime<RTstart[MU+1] && azim==0 && (errq!=0 || orbitalinfo->TimeGap>10.0 || ((modf(orbitalinfo->TimeGap,&tmptg)*1000>10 || modf(orbitalinfo->TimeGap,&tmptg)*1000==0.0) && orbitalinfo->TimeGap>2.0)))){
1582            /*  found in Rotation Table this data for this time interval*/
1583            if(atime<RTtime1[0])
1584              orbitalinfo->azim = 5;        //means that RotationTable no started yet
1585           else{
1586                    // search for angle betwean velosity and direction to north in tangential to Earth surfase plane in satellite position
1587                  Double_t bank=RTstart[MU];
1588                  Double_t tlat=orbitalinfo->lat;
1589    
1590                  tg=modf(orbitalinfo->TimeGap,&tg)*1000;
1591    
1592                  if(atime>=RTpluto1[MU] && atime<=RTpluto2[MU]){
1593                    Double_t kar=(RTbank2[MU]-RTbank1[MU])/(RTtime2[MU]-RTtime1[MU]);
1594                    Double_t bak=RTbank1[MU]-kar*RTtime1[MU];
1595                    bank=kar*atime+bak;
1596                  }
1597                  if(atime>=RTstart[MU] && atime<RTpluto1[MU]){
1598                     Double_t s_dBdt2=(RTbpluto1[MU]-RTbank1[MU])/(RTpluto1[MU]-RTstart[MU]);
1599                     Double_t s_t2=((Double_t)RTpluto1[MU]+(Double_t)RTstart[MU])/2. - RTstart[MU];
1600                     Double_t s_t1=RTstart[MU]-RTstart[MU];
1601                     Double_t s_k=s_dBdt2/(s_t2-s_t1);
1602                     Double_t s_b=-s_k*s_t1;
1603                     Double_t s_t3=RTpluto1[MU]-RTstart[MU];
1604                     Double_t s_b3=RTbpluto1[MU];
1605                     Double_t s_c=s_b3-0.5*s_k*s_t3*s_t3 -s_b*s_t3;
1606                     bank=0.5*s_k*(atime-RTstart[MU])*(atime-RTstart[MU]) + s_b*(atime-RTstart[MU]) + s_c;
1607                 }
1608                  if(atime>RTpluto2[MU] && atime<=RTstart[MU+1]){
1609                     Double_t s_dBdt2=(RTbpluto2[MU] - RTbank2[MU])/(RTpluto2[MU]-RTstart[MU+1]);
1610                     Double_t s_t2=((Double_t)RTpluto2[MU]+(Double_t)RTstart[MU+1])/2. - RTstart[MU];
1611                     Double_t s_t1=RTstart[MU+1]-RTstart[MU];
1612                     Double_t s_k=s_dBdt2/(s_t2-s_t1);
1613                     Double_t s_b=-s_k*s_t1;
1614                     Double_t s_t3=RTpluto2[MU]-RTstart[MU];
1615                     Double_t s_b3=RTbpluto2[MU];
1616                     Double_t s_c=s_b3-0.5*s_k*s_t3*s_t3 -s_b*s_t3;
1617                   bank=0.5*s_k*(atime-RTstart[MU])*(atime-RTstart[MU]) + s_b*(atime-RTstart[MU]) + s_c;
1618                 }
1619                  orbitalinfo->etha=bank;
1620                  Double_t spitch = 0.00001;  // temprary not zero to avoid problem with tranzition from Euler angles to orientation matrix
1621    
1622                    //Estimations of pitch angle of satellite
1623                  if(TMath::Abs(bank)>0.7){
1624                     Float_t spitch1=TMath::DegToRad()*0.7*diro;//RTdir1[MU];
1625                     Float_t spitch2=TMath::DegToRad()*0.7*diro;//RTdir2[MU];
1626                     Float_t kva=(spitch2-spitch1)/(RTtime2[MU]-RTtime1[MU]);
1627                     Float_t bva=spitch1-kva*RTtime1[MU];
1628                     spitch=kva*atime+bva;
1629                  }
1630    
1631                  //Calculate Yaw angle accordingly with fit, see picture FitYaw.jpg
1632                  Double_t yaw=0.00001;  // temprary not zero to avoid problem with tranzition from Euler angles to orientation matrix
1633                  if(TMath::Abs(tlat)<70)
1634                    yaw = -3.7e-8*tlat*tlat*tlat*tlat + 1.4e-7*tlat*tlat*tlat - 0.0005*tlat*tlat - 0.00025*tlat + 3.6;
1635                  yaw = diro*yaw;   //because should be different sign for ascending and descending orbits!
1636                  orbitalinfo->phi=yaw;
1637    
1638                  if(TMath::Abs(bank)>0.5 && TMath::Abs(yaw-orbitalinfo->phi)<3.0) yaw=orbitalinfo->phi;
1639    
1640    //            Qiji = PO->EulertoEci(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,bank,yaw,spitch);             // 10RED CHECK
1641                  Qij = PO->EulertoEci(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,bank,yaw,spitch);              // STANDARD
1642                  orbitalinfo->qkind = 1;
1643    
1644              //Qij = PO->GEOtoECI(Dij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon); // to convert from Dij to Qij
1645    
1646            } // end of if(atime<RTtime1[0]
1647            } // end of f(((orbitalinfo->TimeGap>60.0 && TMath...
1648           } // end of MU~=0
1649    
1650            TMatrixD qij = PO->ColPermutation(Qij);
1651            TMatrixD Fij = PO->ECItoGreenwich(Qij,orbitalinfo->absTime);
1652            TMatrixD Gij = PO->ColPermutation(Fij);
1653            Dij = PO->ECItoGEO(Qij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon);
1654          TMatrixD Iij = PO->ColPermutation(Dij);          TMatrixD Iij = PO->ColPermutation(Dij);
1655            TVector3 SP = PO->GetSunPosition(orbitalinfo->absTime);
1656            // go to Pamela reference frame from Resurs reference frame
1657            Float_t tmpy = SP.Y();
1658            SP.SetY(SP.Z());
1659            SP.SetZ(-tmpy);
1660            TVector3 SunZenith;
1661            SunZenith.SetMagThetaPhi(1,23.439281*TMath::DegToRad(),TMath::Pi()/2.);
1662            TVector3 SunMag = -SP;
1663            SunMag.Rotate(-45*TMath::DegToRad(),SunZenith);
1664            tmpy=SunMag.Y();
1665            SunMag.SetY(SunMag.Z());
1666            SunMag.SetZ(-tmpy);
1667    
1668            orbitalinfo->Iij.ResizeTo(Iij);
1669            orbitalinfo->Iij = Iij;
1670          //          //
1671          A1 = Iij(0,2);          //      A1 = Iij(0,2);
1672          A2 = Iij(1,2);          //      A2 = Iij(1,2);
1673          A3 = Iij(2,2);          //      A3 = Iij(2,2);
1674          //                //
1675          orbitalinfo->pamzenitangle = (Float_t)PO->GetPitchAngle(1,0,0,A1,A2,A3);                        // Angle between zenit and Pamela's main axiz          //      orbitalinfo->pamzenitangle = (Float_t)PO->GetPitchAngle(1,0,0,A1,A2,A3);                        // Angle between zenit and Pamela's main axiz
1676          orbitalinfo->pamBangle = (Float_t)PO->GetPitchAngle(A1,A2,A3,Bx,By,Bz);                 // Angle between Pamela's main axiz and B          //      orbitalinfo->pamBangle = (Float_t)PO->GetPitchAngle(A1,A2,A3,Bx,By,Bz);                 // Angle between Pamela's main axiz and B
1677          //          //
1678          if ( !standalone && tof->ntrk() > 0 ){          if ( debug ) printf(" matrixes done  \n");
1679            if ( !standalone ){
1680              if ( debug ) printf(" !standalone \n");
1681            //            //
1682              // Standard tracking algorithm
1683              //
1684            Int_t nn = 0;            Int_t nn = 0;
1685              if ( verbose ) printf(" standard tracking \n");
1686            for(Int_t nt=0; nt < tof->ntrk(); nt++){              for(Int_t nt=0; nt < tof->ntrk(); nt++){  
1687              //              //
1688              ToFTrkVar *ptt = tof->GetToFTrkVar(nt);              ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1689                if (debug) cout<<"tof->ntrk() = "<<tof->ntrk()<<"\tptt->trkseqno = "<<ptt->trkseqno<<"\ttrke->ntrk() = "<<trke->ntrk()<<endl;
1690              Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];              Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1691              Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];              Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1692              Double_t E11z = zin[0];              Double_t E11z = zin[0];
1693              Double_t E22x = ptt->xtr_tof[3];//tr->x[3];              Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1694              Double_t E22y = ptt->ytr_tof[3];//tr->y[3];              Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1695              Double_t E22z = zin[3];              Double_t E22z = zin[3];
1696              if ( E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.  ){              if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1697                  TrkTrack *mytrack = trke->GetStoredTrack(ptt->trkseqno);
1698                  Float_t rig=1/mytrack->GetDeflection();
1699                Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));                Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1700                Double_t MyAzim = TMath::RadToDeg()*atan(TMath::Abs(E22y-E11y)/TMath::Abs(E22x-E11x));                //
               if(E22x-E11x>=0 && E22y-E11y <0) MyAzim =  360. - MyAzim;  
               if(E22x-E11x>=0 && E22y-E11y >=0) MyAzim = MyAzim;  
               if(E22x-E11x<0 && E22y-E11y >0) MyAzim = 180. - MyAzim;  
               if(E22x-E11x<0 && E22y-E11y <0) MyAzim = 180. + MyAzim;  
1701                Px = (E22x-E11x)/norm;                Px = (E22x-E11x)/norm;
1702                Py = (E22y-E11y)/norm;                Py = (E22y-E11y)/norm;
1703                Pz = (E22z-E11z)/norm;                Pz = (E22z-E11z)/norm;
1704                //                //
               TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);              
               //              
1705                t_orb->trkseqno = ptt->trkseqno;                t_orb->trkseqno = ptt->trkseqno;
1706                  //
1707                  TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1708                  t_orb->Eij.ResizeTo(Eij);
1709                  t_orb->Eij = Eij;
1710                  //
1711                  TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1712                  t_orb->Sij.ResizeTo(Sij);
1713                  t_orb->Sij = Sij;
1714                  //            
1715                t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);                t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1716                  //
1717                  // SunPosition in instrumental reference frame
1718                  TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1719                  TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1720                  t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1721                  t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1722                  //
1723                  //
1724                  Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1725                  TVector3 Bxy(0,By,Bz);
1726                  TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1727                  Double_t dzeta=Bxy.Angle(Exy);
1728                  if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1729                  
1730                  if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1731    
1732                  // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1733                  if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1734                  else       t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1735                  if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1736    
1737                  //
1738                  if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1739                  if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1740                  if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1741                  if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1742                  //
1743                  if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1744                //                //
1745                new(tor[nn]) OrbitalInfoTrkVar(*t_orb);                new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1746                nn++;                nn++;
1747                //                //
1748                t_orb->Clear();                t_orb->Clear();
1749                //                //
1750              };              }
1751              //              //
1752            };            } // end standard tracking algorithm
1753    
1754              //
1755              // Code for extended tracking algorithm:
1756              //
1757              if ( hasNucleiTrk ){
1758                Int_t ttentry = 0;
1759                if ( verbose ) printf(" hasNucleiTrk \n");
1760                for(Int_t nt=0; nt < tcNucleiTof->GetEntries() ; nt++){  
1761                  //
1762                  if ( verbose ) printf(" got1\n");
1763                  ToFTrkVar *ptt = (ToFTrkVar*)(tcNucleiTof->At(nt));
1764                  if (verbose) cout<<" tcNucleiTof->GetEntries() = "<<tcNucleiTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1765                  Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1766                  Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1767                  Double_t E11z = zin[0];
1768                  Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1769                  Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1770                  Double_t E22z = zin[3];
1771                  if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1772                    TrkTrack *mytrack = (TrkTrack*)(tcNucleiTrk->At(ptt->trkseqno));
1773                    if ( verbose ) printf(" got tcNucleiTrk \n");
1774                    Float_t rig=1/mytrack->GetDeflection();
1775                    Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1776                    //
1777                    Px = (E22x-E11x)/norm;
1778                    Py = (E22y-E11y)/norm;
1779                    Pz = (E22z-E11z)/norm;
1780                    //
1781                    t_orb->trkseqno = ptt->trkseqno;
1782                    //
1783                    TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1784                    t_orb->Eij.ResizeTo(Eij);      
1785                    t_orb->Eij = Eij;      
1786                    //
1787                    TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1788                    t_orb->Sij.ResizeTo(Sij);
1789                    t_orb->Sij = Sij;
1790                    //          
1791                    t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1792                    //
1793                    // SunPosition in instrumental reference frame
1794                    TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1795                    TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1796                    t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1797                    t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1798                    //
1799                    //
1800                    Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1801                    TVector3 Bxy(0,By,Bz);
1802                    TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1803                    Double_t dzeta=Bxy.Angle(Exy);
1804                    if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1805                    
1806                    if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1807                    
1808                    // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1809                    if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1810                    else       t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1811                    if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1812                    
1813                    //
1814                    if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1815                    if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1816                    if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1817                    if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1818                    //
1819                    if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1820                    //
1821                    TClonesArray &tt1 = *torbNucleiTrk;
1822                    new(tt1[ttentry]) OrbitalInfoTrkVar(*t_orb);
1823                    ttentry++;
1824                    //
1825                    t_orb->Clear();
1826                    //
1827                  }
1828                  //
1829                }
1830              } // end standard tracking algorithm: nuclei
1831              if ( hasExtNucleiTrk ){
1832                Int_t ttentry = 0;
1833                if ( verbose ) printf(" hasExtNucleiTrk \n");
1834                for(Int_t nt=0; nt < tcExtNucleiTof->GetEntries() ; nt++){  
1835                  //
1836                  if ( verbose ) printf(" got2\n");
1837                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtNucleiTof->At(nt));
1838                  if (verbose) cout<<" tcExtNucleiTof->GetEntries() = "<<tcExtNucleiTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1839                  Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1840                  Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1841                  Double_t E11z = zin[0];
1842                  Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1843                  Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1844                  Double_t E22z = zin[3];
1845                  if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1846                    ExtTrack *mytrack = (ExtTrack*)(tcExtNucleiTrk->At(ptt->trkseqno));
1847                    if ( verbose ) printf(" got tcExtNucleiTrk \n");
1848                    Float_t rig=1/mytrack->GetDeflection();
1849                    Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1850                    //
1851                    Px = (E22x-E11x)/norm;
1852                    Py = (E22y-E11y)/norm;
1853                    Pz = (E22z-E11z)/norm;
1854                    //
1855                    t_orb->trkseqno = ptt->trkseqno;
1856                    //
1857                    TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1858                    t_orb->Eij.ResizeTo(Eij);      
1859                    t_orb->Eij = Eij;      
1860                    //
1861                    TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1862                    t_orb->Sij.ResizeTo(Sij);
1863                    t_orb->Sij = Sij;
1864                    //          
1865                    t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1866                    //
1867                    // SunPosition in instrumental reference frame
1868                    TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1869                    TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1870                    t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1871                    t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1872                    //
1873                    //
1874                    Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1875                    TVector3 Bxy(0,By,Bz);
1876                    TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1877                    Double_t dzeta=Bxy.Angle(Exy);
1878                    if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1879                    
1880                    if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1881                    
1882                    // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1883                    if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1884                    else       t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1885                    if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1886                    
1887                    //
1888                    if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1889                    if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1890                    if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1891                    if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1892                    //
1893                    if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1894                    //
1895                    TClonesArray &tt2 = *torbExtNucleiTrk;
1896                    new(tt2[ttentry]) OrbitalInfoTrkVar(*t_orb);
1897                    ttentry++;
1898                    //
1899                    t_orb->Clear();
1900                    //
1901                  }
1902                  //
1903                }
1904              } // end standard tracking algorithm: nuclei extended
1905             if ( hasExtTrk ){
1906                Int_t ttentry = 0;
1907                if ( verbose ) printf(" hasExtTrk \n");
1908                for(Int_t nt=0; nt < tcExtTof->GetEntries() ; nt++){  
1909                  //
1910                  if ( verbose ) printf(" got3\n");
1911                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtTof->At(nt));
1912                  if (verbose) cout<<" tcExtTof->GetEntries() = "<<tcExtTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1913                  Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1914                  Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1915                  Double_t E11z = zin[0];
1916                  Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1917                  Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1918                  Double_t E22z = zin[3];
1919                  if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1920                    ExtTrack *mytrack = (ExtTrack*)(tcExtTrk->At(ptt->trkseqno));
1921                    if ( verbose ) printf(" got tcExtTrk \n");
1922                    Float_t rig=1/mytrack->GetDeflection();
1923                    Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1924                    //
1925                    Px = (E22x-E11x)/norm;
1926                    Py = (E22y-E11y)/norm;
1927                    Pz = (E22z-E11z)/norm;
1928                    //
1929                    t_orb->trkseqno = ptt->trkseqno;
1930                    //
1931                    TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1932                    t_orb->Eij.ResizeTo(Eij);      
1933                    t_orb->Eij = Eij;      
1934                    //
1935                    TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1936                    t_orb->Sij.ResizeTo(Sij);
1937                    t_orb->Sij = Sij;
1938                    //          
1939                    t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1940                    //
1941                    // SunPosition in instrumental reference frame
1942                    TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1943                    TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1944                    t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1945                    t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1946                    //
1947                    //
1948                    Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1949                    TVector3 Bxy(0,By,Bz);
1950                    TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1951                    Double_t dzeta=Bxy.Angle(Exy);
1952                    if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1953                    
1954                    if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1955                    
1956                    // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1957                    if(rig>=0) t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1958                    else       t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow(1+sqrt(1-sin(omega)*sin(TMath::Pi()+dzeta)*pow(cos(orbitalinfo->lat*TMath::DegToRad()),3)),2));
1959                    if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1960                    
1961                    //
1962                    if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1963                    if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1964                    if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1965                    if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1966                    //
1967                    if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1968                    //
1969                    TClonesArray &tt3 = *torbExtTrk;
1970                    new(tt3[ttentry]) OrbitalInfoTrkVar(*t_orb);
1971                    ttentry++;
1972                    //
1973                    t_orb->Clear();
1974                    //
1975                  }
1976                  //
1977                }
1978              } // end standard tracking algorithm: extended
1979    
1980          } else {          } else {
1981            if ( debug ) printf(" mmm... mode %u standalone %i ntrk %i \n",orbitalinfo->mode,standalone,tof->ntrk());            if ( debug ) printf(" mmm... mode %u standalone  \n",orbitalinfo->mode);
1982          };          }
1983          //          //
1984        };        } else { // HERE IT MISS ALL CODE FOR EXTENDED TRACKING!
1985            if ( !standalone ){
1986              //
1987              if ( verbose ) printf(" no orb info for tracks \n");
1988              Int_t nn = 0;
1989              for(Int_t nt=0; nt < tof->ntrk(); nt++){  
1990                //
1991                ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1992                if ( ptt->trkseqno != -1  ){
1993                  //
1994                  t_orb->trkseqno = ptt->trkseqno;
1995                  //
1996                  t_orb->Eij = 0;  
1997                  //
1998                  t_orb->Sij = 0;
1999                  //            
2000                  t_orb->pitch = -1000.;
2001                  //
2002                  t_orb->sunangle = -1000.;
2003                  //
2004                  t_orb->sunmagangle = -1000;
2005                  //
2006                  t_orb->cutoff = -1000.;
2007                  //
2008                  new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
2009                  nn++;
2010                  //
2011                  t_orb->Clear();
2012                  //
2013                }
2014                //
2015              }
2016              //
2017              // Code for extended tracking algorithm:
2018              //
2019              if ( hasNucleiTrk ){
2020                Int_t ttentry = 0;
2021                if ( verbose ) printf(" hasNucleiTrk \n");
2022                for(Int_t nt=0; nt < tcNucleiTof->GetEntries() ; nt++){  
2023                  //  
2024                  ToFTrkVar *ptt = (ToFTrkVar*)(tcNucleiTof->At(nt));
2025                  if ( ptt->trkseqno != -1  ){
2026                    //
2027                    t_orb->trkseqno = ptt->trkseqno;
2028                    //
2029                    t_orb->Eij = 0;
2030                    //
2031                    t_orb->Sij = 0;
2032                    //          
2033                    t_orb->pitch = -1000.;
2034                    //
2035                    t_orb->sunangle = -1000.;
2036                    //
2037                    t_orb->sunmagangle = -1000;
2038                    //
2039                    t_orb->cutoff = -1000.;
2040                    //
2041                    TClonesArray &tz1 = *torbNucleiTrk;
2042                    new(tz1[ttentry]) OrbitalInfoTrkVar(*t_orb);
2043                    ttentry++;
2044                    //
2045                    t_orb->Clear();
2046                    //
2047                  }
2048                  //
2049                }
2050              }
2051              if ( hasExtNucleiTrk ){
2052                Int_t ttentry = 0;
2053                if ( verbose ) printf(" hasExtNucleiTrk \n");
2054                for(Int_t nt=0; nt < tcExtNucleiTof->GetEntries() ; nt++){  
2055                  //
2056                  if ( verbose ) printf(" got2\n");
2057                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtNucleiTof->At(nt));
2058                  if ( ptt->trkseqno != -1  ){
2059                    //
2060                    t_orb->trkseqno = ptt->trkseqno;
2061                    //
2062                    t_orb->Eij = 0;
2063                    //
2064                    t_orb->Sij = 0;
2065                    //          
2066                    t_orb->pitch = -1000.;
2067                    //
2068                    t_orb->sunangle = -1000.;
2069                    //
2070                    t_orb->sunmagangle = -1000;
2071                    //
2072                    t_orb->cutoff = -1000.;
2073                    //
2074                    TClonesArray &tz2 = *torbExtNucleiTrk;
2075                    new(tz2[ttentry]) OrbitalInfoTrkVar(*t_orb);
2076                    ttentry++;
2077                    //
2078                    t_orb->Clear();
2079                    //
2080                  }
2081                  //
2082                }
2083              }
2084              if ( hasExtTrk ){
2085                Int_t ttentry = 0;
2086                if ( verbose ) printf(" hasExtTrk \n");
2087                for(Int_t nt=0; nt < tcExtTof->GetEntries() ; nt++){  
2088                  //
2089                  if ( verbose ) printf(" got3\n");
2090                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtTof->At(nt));
2091                  if ( ptt->trkseqno != -1  ){
2092                    //
2093                    t_orb->trkseqno = ptt->trkseqno;
2094                    //
2095                    t_orb->Eij = 0;
2096                    //
2097                    t_orb->Sij = 0;
2098                    //          
2099                    t_orb->pitch = -1000.;
2100                    //
2101                    t_orb->sunangle = -1000.;
2102                    //
2103                    t_orb->sunmagangle = -1000;
2104                    //
2105                    t_orb->cutoff = -1000.;
2106                    //
2107                    TClonesArray &tz3 = *torbExtNucleiTrk;
2108                    new(tz3[ttentry]) OrbitalInfoTrkVar(*t_orb);
2109                    ttentry++;
2110                    //
2111                    t_orb->Clear();
2112                    //
2113                  }
2114                  //
2115                }
2116              }
2117            }
2118          } // if( orbitalinfo->TimeGap>0){
2119        //        //
2120        // Fill the class        // Fill the class
2121        //        //
# Line 1056  int OrbitalInfoCore(UInt_t run, TFile *f Line 2127  int OrbitalInfoCore(UInt_t run, TFile *f
2127      //      //
2128      // Here you may want to clear some variables before processing another run        // Here you may want to clear some variables before processing another run  
2129      //      //
2130    
2131        if ( verbose ) printf(" Clear before new run \n");
2132      delete dbtime;      delete dbtime;
2133      if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;  
2134        if ( mcmdrc ) mcmdrc->Clear();
2135        mcmdrc = 0;
2136        
2137        if ( verbose ) printf(" Clear before new run1 \n");
2138      if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;      if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;
2139        if ( verbose ) printf(" Clear before new run2 \n");
2140        if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;
2141        if ( verbose ) printf(" Clear before new run3 \n");
2142      if ( RYPang_upper ) delete RYPang_upper;      if ( RYPang_upper ) delete RYPang_upper;
2143        if ( verbose ) printf(" Clear before new run4 \n");
2144      if ( RYPang_lower ) delete RYPang_lower;      if ( RYPang_lower ) delete RYPang_lower;
2145    
2146        if ( l0tr ) l0tr->Delete();
2147        
2148        if ( verbose ) printf(" End run \n");
2149    
2150    }; // process all the runs    }; // process all the runs
2151        
2152    if (verbose) printf("\n Finished processing data \n");    if (verbose) printf("\n Finished processing data \n");
# Line 1080  int OrbitalInfoCore(UInt_t run, TFile *f Line 2166  int OrbitalInfoCore(UInt_t run, TFile *f
2166          //          //
2167          // Get entry from old tree          // Get entry from old tree
2168          //          //
2169          OrbitalInfotrclone->GetEntry(j);                    if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;    
2170          //          //
2171          // copy orbitalinfoclone to OrbitalInfo          // copy orbitalinfoclone to OrbitalInfo
2172          //          //
2173          orbitalinfo->Clear();          //      orbitalinfo->Clear();
2174          //          //
2175          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
2176          //          //
# Line 1094  int OrbitalInfoCore(UInt_t run, TFile *f Line 2180  int OrbitalInfoCore(UInt_t run, TFile *f
2180        };        };
2181        if (verbose) printf(" Finished successful copying!\n");        if (verbose) printf(" Finished successful copying!\n");
2182      };      };
2183        //if ( OrbitalInfotrclone )    OrbitalInfotrclone->Clear();        
2184        //if ( OrbitalInfotrclone )    OrbitalInfotrclone->Delete();        
2185    };    };
2186    //    //
2187    // Close files, delete old tree(s), write and close level2 file    // Close files, delete old tree(s), write and close level2 file
2188    //    //
2189    
2190    if ( l0File ) l0File->Close();    if ( l0File ) l0File->Close();
   if ( tempfile ) tempfile->Close();              
2191    if ( myfold ) gSystem->Unlink(tempname.str().c_str());    if ( myfold ) gSystem->Unlink(tempname.str().c_str());
2192    //    //
   if ( runinfo ) runinfo->Close();      
2193    if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");        if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");    
   if ( tof ) tof->Delete();  
   if ( ttof ) ttof->Delete();  
2194    //    //
2195    if ( file ){    if ( file ){
2196      file->cd();      file->cd();
2197      file->Write();      if ( OrbitalInfotr ) OrbitalInfotr->Write("OrbitalInfo", TObject::kOverwrite); // 10 RED bug fixed
2198    };    };
2199    //    //
2200      if (verbose) printf("\n Exiting...\n");
2201    
2202    if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());    if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());
2203    //    //
2204    // the end    // the end
# Line 1120  int OrbitalInfoCore(UInt_t run, TFile *f Line 2207  int OrbitalInfoCore(UInt_t run, TFile *f
2207      dbc->Close();      dbc->Close();
2208      delete dbc;      delete dbc;
2209    };    };
   if (verbose) printf("\n Exiting...\n");  
   if(OrbitalInfotr)OrbitalInfotr->Delete();  
2210    //    //
2211      if (verbose) printf("\n Exiting...\n");
2212      if ( tempfile ) tempfile->Close();            
2213      
2214    if ( PO ) delete PO;    if ( PO ) delete PO;
2215    if ( orbitalinfo ) delete orbitalinfo;    if ( gltle ) delete gltle;
2216    if ( orbitalinfoclone ) delete orbitalinfoclone;    if ( glparam ) delete glparam;
2217      if ( glparam2 ) delete glparam2;
2218      if ( glparam3 ) delete glparam3;
2219      if (verbose) printf("\n Exiting3...\n");
2220    if ( glroot ) delete glroot;    if ( glroot ) delete glroot;
2221      if (verbose) printf("\n Exiting4...\n");
2222      if ( runinfo ) runinfo->Close();    
2223    if ( runinfo ) delete runinfo;    if ( runinfo ) delete runinfo;
2224    
2225      if ( tcNucleiTrk ){
2226        tcNucleiTrk->Delete();
2227        delete tcNucleiTrk;
2228        tcNucleiTrk = NULL;
2229      }
2230      if ( tcExtNucleiTrk ){
2231        tcExtNucleiTrk->Delete();
2232        delete tcExtNucleiTrk;
2233        tcExtNucleiTrk = NULL;
2234      }
2235      if ( tcExtTrk ){
2236        tcExtTrk->Delete();
2237        delete tcExtTrk;
2238        tcExtTrk = NULL;
2239      }
2240    
2241      if ( tcNucleiTof ){
2242        tcNucleiTof->Delete();
2243        delete tcNucleiTof;
2244        tcNucleiTof = NULL;
2245      }
2246      if ( tcExtNucleiTof ){
2247        tcExtNucleiTof->Delete();
2248        delete tcExtNucleiTof;
2249        tcExtNucleiTof = NULL;
2250      }
2251      if ( tcExtTof ){
2252        tcExtTof->Delete();
2253        delete tcExtTof;
2254        tcExtTrk = NULL;
2255      }
2256    
2257    
2258      if ( tof ) delete tof;
2259      if ( trke ) delete trke;
2260    
2261      if ( debug ){  
2262      cout << "1   0x" << OrbitalInfotr << endl;
2263      cout << "2   0x" << OrbitalInfotrclone << endl;
2264      cout << "3   0x" << l0tr << endl;
2265      cout << "4   0x" << tempOrbitalInfo << endl;
2266      cout << "5   0x" << ttof << endl;
2267      }
2268      //
2269      if ( debug )  file->ls();
2270    //    //
2271    if(code < 0)  throw code;    if(code < 0)  throw code;
2272    return(code);    return(code);
# Line 1172  void CopyAng(InclinationInfo *A1, Inclin Line 2311  void CopyAng(InclinationInfo *A1, Inclin
2311  UInt_t holeq(Double_t lower,Double_t upper,Quaternions *Qlower, Quaternions *Qupper, UInt_t f){  UInt_t holeq(Double_t lower,Double_t upper,Quaternions *Qlower, Quaternions *Qupper, UInt_t f){
2312        
2313    UInt_t hole = 10;    UInt_t hole = 10;
2314    bool R10l = false;     // Sign of R10 mode in lower quaternions array    Bool_t R10l = false;     // Sign of R10 mode in lower quaternions array
2315    bool R10u = false;     // Sign of R10 mode in upper quaternions array    Bool_t R10u = false;     // Sign of R10 mode in upper quaternions array
2316    bool insm = false;     // Sign that we inside quaternions array    Bool_t insm = false;     // Sign that we inside quaternions array
2317    bool mxtml = false;    // Sign of mixt mode in lower quaternions array    //  Bool_t mxtml = false;    // Sign of mixt mode in lower quaternions array
2318    bool mxtmu = false;    // Sign of mixt mode in upper quaternions array    //  Bool_t mxtmu = false;    // Sign of mixt mode in upper quaternions array
2319    bool npasm = false;     // Sign of normall pass between R10 and non R10 or between non R10 and R10    Bool_t npasm = false;     // Sign of normall pass between R10 and non R10 or between non R10 and R10
2320    UInt_t NCQl = 6;       // Number of correct quaternions in lower array    UInt_t NCQl = 6;       // Number of correct quaternions in lower array
2321    UInt_t NCQu = 6;       // Number of correct quaternions in upper array    //  UInt_t NCQu = 6;       // Number of correct quaternions in upper array
2322    if (f>0){    if (f>0){
2323      insm = true;      insm = true;
2324      if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;      if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;
# Line 1191  UInt_t holeq(Double_t lower,Double_t upp Line 2330  UInt_t holeq(Double_t lower,Double_t upp
2330      if((Qlower->time[5]-Qlower->time[0]==150)&&(Qlower->time[1]-Qlower->time[0]==30)) R10l = false;      if((Qlower->time[5]-Qlower->time[0]==150)&&(Qlower->time[1]-Qlower->time[0]==30)) R10l = false;
2331      if((Qupper->time[5]-Qupper->time[0]==150)&&(Qupper->time[1]-Qupper->time[0]==30)) R10u = false;      if((Qupper->time[5]-Qupper->time[0]==150)&&(Qupper->time[1]-Qupper->time[0]==30)) R10u = false;
2332      if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]==30)){      if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]==30)){
2333        mxtml = true;        //      mxtml = true;
2334        for(UInt_t i = 1; i < 6; i++){        for(UInt_t i = 1; i < 6; i++){
2335          if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;          if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;
2336        }        }
2337      }      }
2338      if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]==30)){      //    if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]==30)){
2339        mxtmu = true;        //      mxtmu = true;
2340        for(UInt_t i = 1; i < 6; i++){        //      for(UInt_t i = 1; i < 6; i++){
2341          if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;        //        if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;
2342        }        //      }
2343      }      //    }
2344    }    }
2345        
2346    if(((upper-lower==1.5)||(upper-lower==3.)||(upper-lower==30.)||(upper-lower==31.5)||(upper-lower==33.)||(upper-lower==181.5)||(upper-lower==210.)||(upper-lower==211.5))&&!insm) npasm = true;    if(((upper-lower==1.5)||(upper-lower==3.)||(upper-lower==30.)||(upper-lower==31.5)||(upper-lower==33.)||(upper-lower==181.5)||(upper-lower==210.)||(upper-lower==211.5))&&!insm) npasm = true;
# Line 1220  UInt_t holeq(Double_t lower,Double_t upp Line 2359  UInt_t holeq(Double_t lower,Double_t upp
2359    return hole;    return hole;
2360  }  }
2361    
2362    void inclresize(vector<Double_t>& t,vector<Float_t>& q0,vector<Float_t>& q1,vector<Float_t>& q2,vector<Float_t>& q3,vector<Int_t>& mode,vector<Float_t>& Roll,vector<Float_t>& Pitch,vector<Float_t>& Yaw){
2363      Int_t sizee = t.size()+1;
2364      t.resize(sizee);
2365      q0.resize(sizee);
2366      q1.resize(sizee);
2367      q2.resize(sizee);
2368      q3.resize(sizee);
2369      mode.resize(sizee);
2370      Roll.resize(sizee);
2371      Pitch.resize(sizee);
2372      Yaw.resize(sizee);
2373    }
2374    
2375    // geomagnetic calculation staff
2376    
2377    //void GM_ScanIGRF(TString PATH, GMtype_Data *G0, GMtype_Data *G1, GMtype_Data *H1)
2378    void GM_ScanIGRF(TSQLServer *dbc, GMtype_Data *G0, GMtype_Data *G1, GMtype_Data *H1)
2379    {
2380      GL_PARAM *glp = new GL_PARAM();
2381      Int_t parerror=glp->Query_GL_PARAM(1,304,dbc); // parameters stored in DB in GL_PRAM table  
2382      if ( parerror<0 ) {
2383        throw -902;
2384      }
2385            /*This function scans inputs G0, G1, and H1 of the IGRF table into 3 data arrays*/
2386      //    TString SATH="/data03/Malakhov/pam9Malakhov/installed10/calib/orb-param/";
2387            int i;
2388            double temp;
2389            char buffer[200];
2390            FILE *IGRF;
2391            IGRF = fopen((glp->PATH+glp->NAME).Data(), "r");
2392            //      IGRF = fopen(PATH+"IGRF.tab", "r");
2393            G0->size = 25;
2394            G1->size = 25;
2395            H1->size = 25;
2396            for( i = 0; i < 4; i++)
2397            {
2398                    fgets(buffer, 200, IGRF);
2399            }
2400            fscanf(IGRF, "g 1 0 %lf ", &G0->element[0]);
2401            for(i = 1; i <= 22; i++)
2402            {
2403                    fscanf(IGRF ,"%lf ", &G0->element[i]);
2404            }
2405            fscanf(IGRF ,"%lf\n", &temp);
2406            G0->element[23] = temp * 5 + G0->element[22];
2407            G0->element[24] = G0->element[23] + 5 * temp;
2408            fscanf(IGRF, "g 1 1 %lf ", &G1->element[0]);
2409            for(i = 1; i <= 22; i++)
2410            {
2411                    fscanf( IGRF, "%lf ", &G1->element[i]);
2412            }
2413            fscanf(IGRF, "%lf\n", &temp);
2414            G1->element[23] = temp * 5 + G1->element[22];
2415            G1->element[24] = temp * 5 + G1->element[23];
2416            fscanf(IGRF, "h 1 1 %lf ", &H1->element[0]);
2417            for(i = 1; i <= 22; i++)
2418            {
2419                    fscanf( IGRF, "%lf ", &H1->element[i]);
2420            }
2421            fscanf(IGRF, "%lf\n", &temp);
2422            H1->element[23] = temp * 5 + H1->element[22];
2423            H1->element[24] = temp * 5 + H1->element[23];
2424      if ( glp ) delete glp;
2425    } /*GM_ScanIGRF*/
2426    
2427    void GM_SetEllipsoid(GMtype_Ellipsoid *Ellip)
2428    {
2429            /*This function sets the WGS84 reference ellipsoid to its default values*/
2430            Ellip->a        =                       6378.137; /*semi-major axis of the ellipsoid in */
2431            Ellip->b        =                       6356.7523142;/*semi-minor axis of the ellipsoid in */
2432            Ellip->fla      =                       1/298.257223563;/* flattening */
2433            Ellip->eps      =                       sqrt(1- ( Ellip->b *    Ellip->b) / (Ellip->a * Ellip->a ));  /*first eccentricity */
2434            Ellip->epssq    =                       (Ellip->eps * Ellip->eps);   /*first eccentricity squared */
2435            Ellip->re       =                       6371.2;/* Earth's radius */
2436    } /*GM_SetEllipsoid*/
2437    
2438    
2439    void GM_EarthCartToDipoleCartCD(GMtype_Pole Pole, GMtype_CoordCartesian EarthCoord, GMtype_CoordCartesian *DipoleCoords)
2440    {
2441            /*This function converts from Earth centered cartesian coordinates to dipole centered cartesian coordinates*/
2442            double X, Y, Z, CosPhi, SinPhi, CosLambda, SinLambda;
2443            CosPhi = cos(TMath::DegToRad()*Pole.phi);
2444            SinPhi = sin(TMath::DegToRad()*Pole.phi);
2445            CosLambda = cos(TMath::DegToRad()*Pole.lambda);
2446            SinLambda = sin(TMath::DegToRad()*Pole.lambda);
2447            X = EarthCoord.x;
2448            Y = EarthCoord.y;
2449            Z = EarthCoord.z;
2450            
2451            /*These equations are taken from a document by Wallace H. Campbell*/
2452            DipoleCoords->x = X * CosPhi * CosLambda + Y * CosPhi * SinLambda - Z * SinPhi;
2453            DipoleCoords->y = -X * SinLambda + Y * CosLambda;
2454            DipoleCoords->z = X * SinPhi * CosLambda + Y * SinPhi * SinLambda + Z * CosPhi;
2455    } /*GM_EarthCartToDipoleCartCD*/
2456    
2457    void GM_GeodeticToSpherical(GMtype_Ellipsoid Ellip, GMtype_CoordGeodetic CoordGeodetic, GMtype_CoordSpherical *CoordSpherical)
2458    {
2459            double CosLat, SinLat, rc, xp, zp; /*all local variables */
2460            /*
2461            ** Convert geodetic coordinates, (defined by the WGS-84
2462            ** reference ellipsoid), to Earth Centered Earth Fixed Cartesian
2463            ** coordinates, and then to spherical coordinates.
2464            */
2465    
2466            CosLat = cos(TMath::DegToRad()*CoordGeodetic.phi);
2467            SinLat = sin(TMath::DegToRad()*CoordGeodetic.phi);
2468    
2469            /* compute the local radius of curvature on the WGS-84 reference ellipsoid */
2470    
2471            rc = Ellip.a / sqrt(1.0 - Ellip.epssq * SinLat * SinLat);
2472    
2473            /* compute ECEF Cartesian coordinates of specified point (for longitude=0) */
2474    
2475            xp = (rc + CoordGeodetic.HeightAboveEllipsoid) * CosLat;
2476            zp = (rc*(1.0 - Ellip.epssq) + CoordGeodetic.HeightAboveEllipsoid) * SinLat;
2477    
2478            /* compute spherical radius and angle lambda and phi of specified point */
2479    
2480            CoordSpherical->r = sqrt(xp * xp + zp * zp);
2481            CoordSpherical->phig = TMath::RadToDeg()*asin(zp / CoordSpherical->r);     /* geocentric latitude */
2482            CoordSpherical->lambda = CoordGeodetic.lambda;                   /* longitude */
2483    } /*GM_GeodeticToSpherical*/
2484    
2485    void GM_PoleLocation(GMtype_Model Model, GMtype_Pole *Pole)
2486    {
2487            /*This function finds the location of the north magnetic pole in spherical coordinates.  The equations are
2488            **from Wallace H. Campbell's Introduction to Geomagnetic Fields*/
2489    
2490            Pole->phi = TMath::RadToDeg()*-atan(sqrt(Model.h1 * Model.h1 + Model.g1 * Model.g1)/Model.g0);
2491            Pole->lambda = TMath::RadToDeg()*atan(Model.h1/Model.g1);
2492    } /*GM_PoleLocation*/
2493    
2494    void GM_SphericalToCartesian(GMtype_CoordSpherical CoordSpherical, GMtype_CoordCartesian *CoordCartesian)
2495    {
2496            /*This function converts spherical coordinates into Cartesian coordinates*/
2497            double CosPhi = cos(TMath::DegToRad()*CoordSpherical.phig);
2498            double SinPhi = sin(TMath::DegToRad()*CoordSpherical.phig);
2499            double CosLambda = cos(TMath::DegToRad()*CoordSpherical.lambda);
2500            double SinLambda = sin(TMath::DegToRad()*CoordSpherical.lambda);
2501            
2502            CoordCartesian->x = CoordSpherical.r * CosPhi * CosLambda;
2503            CoordCartesian->y = CoordSpherical.r * CosPhi * SinLambda;
2504            CoordCartesian->z = CoordSpherical.r * SinPhi;
2505    } /*GM_SphericalToCartesian*/
2506    
2507    void GM_TimeAdjustCoefs(Float_t year, Float_t jyear, GMtype_Data g0d, GMtype_Data g1d, GMtype_Data h1d, GMtype_Model *Model)
2508    {
2509            /*This function calls GM_LinearInterpolation for the coefficients to estimate the value of the
2510            **coefficient for the given date*/
2511            int index;
2512            double x;
2513            index = (year - GM_STARTYEAR) / 5;
2514            x = (jyear - GM_STARTYEAR) / 5;
2515            Model->g0 = GM_LinearInterpolation(index, index+1, g0d.element[index], g0d.element[index+1], x);
2516            Model->g1 = GM_LinearInterpolation(index, index+1, g1d.element[index], g1d.element[index+1], x);
2517            Model->h1 = GM_LinearInterpolation(index, index+1, h1d.element[index], h1d.element[index+1], x);
2518    } /*GM_TimeAdjustCoefs*/
2519    
2520    double GM_LinearInterpolation(double x1, double x2, double y1, double y2, double x)
2521    {
2522            /*This function takes a linear interpolation between two given points for x*/
2523            double weight, y;
2524            weight  = (x - x1) / (x2 - x1);
2525            y = y1 * (1 - weight) + y2 * weight;
2526            return y;
2527    }/*GM_LinearInterpolation*/
2528    
2529    void GM_CartesianToSpherical(GMtype_CoordCartesian CoordCartesian, GMtype_CoordSpherical *CoordSpherical)
2530    {
2531            /*This function converts a point from Cartesian coordinates into spherical coordinates*/
2532            double X, Y, Z;
2533            
2534            X = CoordCartesian.x;
2535            Y = CoordCartesian.y;
2536            Z = CoordCartesian.z;
2537    
2538            CoordSpherical->r = sqrt(X * X + Y * Y + Z * Z);
2539            CoordSpherical->phig = TMath::RadToDeg()*asin(Z / (CoordSpherical->r));
2540            CoordSpherical->lambda = TMath::RadToDeg()*atan2(Y, X);
2541    } /*GM_CartesianToSpherical*/

Legend:
Removed from v.1.30  
changed lines
  Added in v.1.78

  ViewVC Help
Powered by ViewVC 1.1.23