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

Legend:
Removed from v.1.35  
changed lines
  Added in v.1.79

  ViewVC Help
Powered by ViewVC 1.1.23