/[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.47 by mocchiut, Mon Feb 1 05:34:39 2010 UTC revision 1.92 by pamela, Tue Nov 17 10:10:06 2015 UTC
# Line 9  Line 9 
9  // ROOT headers  // ROOT headers
10  //  //
11  //#include <TCanvas.h>  //#include <TCanvas.h>
12  //#include <TH2F.h> //for test only. Vitaly.  #include <TH2F.h> //for test only. Vitaly.
13    #include <TVector3.h>
14  //#include <TF1.h>  //#include <TF1.h>
15    
16  #include <TTree.h>  #include <TTree.h>
# Line 25  Line 26 
26  #include <TSQLServer.h>  #include <TSQLServer.h>
27  #include <TSQLRow.h>  #include <TSQLRow.h>
28  #include <TSQLResult.h>  #include <TSQLResult.h>
29    #include <TObjectTable.h>
30  //  //
31  // RunInfo header  // RunInfo header
32  //  //
# Line 47  Line 49 
49  #include <OrbitalInfoCore.h>  #include <OrbitalInfoCore.h>
50  #include <InclinationInfo.h>  #include <InclinationInfo.h>
51    
52    //
53    // Tracker and ToF classes headers and definitions
54    //
55    #include <ToFLevel2.h>
56    #include <TrkLevel2.h>
57    #include <ExtTrack.h> // new tracking code
58    
59  using namespace std;  using namespace std;
60    
# Line 54  using namespace std; Line 62  using namespace std;
62  // CORE ROUTINE  // CORE ROUTINE
63  //  //
64  //  //
65  int OrbitalInfoCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){  int OrbitalInfoCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){  
66    //    //
67    Int_t i = 0;    Int_t i = 0;
68    TString host = glt->CGetHost();    TString host = glt->CGetHost();
# Line 64  int OrbitalInfoCore(UInt_t run, TFile *f Line 72  int OrbitalInfoCore(UInt_t run, TFile *f
72    //    //
73    stringstream myquery;    stringstream myquery;
74    myquery.str("");    myquery.str("");
75    myquery << "SET time_zone='+0:00'";    myquery << "SET time_zone='+0:00';";
76    dbc->Query(myquery.str().c_str());    delete dbc->Query(myquery.str().c_str());
77      delete dbc->Query("SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';");
78    //    //
79    TString processFolder = Form("OrbitalInfoFolder_%u",run);    TString processFolder = Form("OrbitalInfoFolder_%u",run);
80    //    //
# Line 83  int OrbitalInfoCore(UInt_t run, TFile *f Line 92  int OrbitalInfoCore(UInt_t run, TFile *f
92        if ( !strcmp(OrbitalInfoargv[i],"-processFolder") ) {        if ( !strcmp(OrbitalInfoargv[i],"-processFolder") ) {
93          if ( OrbitalInfoargc < i+1 ){          if ( OrbitalInfoargc < i+1 ){
94            throw -3;            throw -3;
95          };          }
96          processFolder = (TString)OrbitalInfoargv[i+1];          processFolder = (TString)OrbitalInfoargv[i+1];
97          i++;          i++;
98        };        }
99        if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {        if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {
100          verbose = true;          verbose = true;
101          debug = true;          debug = true;
102        };        }
103        if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {        if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {
104          verbose = true;          verbose = true;
105        };        }
106        if ( (!strcmp(OrbitalInfoargv[i],"--standalone")) ) {        if ( (!strcmp(OrbitalInfoargv[i],"--standalone")) ) {
107          standalone = true;          standalone = true;
108        };        }
109        if ( (!strcmp(OrbitalInfoargv[i],"--calculate-pitch")) ) {        if ( (!strcmp(OrbitalInfoargv[i],"--calculate-pitch")) ) {
110          standalone = false;          standalone = false;
111        };        }
112        i++;        i++;
113      };      }
114    };    }
115      if ( debug ){
116        printf("START\n");
117        gObjectTable->Print();
118      }
119    //    //
120    const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));    const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
121    //    //
# Line 121  int OrbitalInfoCore(UInt_t run, TFile *f Line 134  int OrbitalInfoCore(UInt_t run, TFile *f
134    TTree *OrbitalInfotrclone = 0;    TTree *OrbitalInfotrclone = 0;
135    Bool_t reproc = false;    Bool_t reproc = false;
136    Bool_t reprocall = false;    Bool_t reprocall = false;
137      Bool_t igrfloaded = false;
138    UInt_t nobefrun = 0;    UInt_t nobefrun = 0;
139    UInt_t noaftrun = 0;    UInt_t noaftrun = 0;
140    UInt_t numbofrun = 0;    UInt_t numbofrun = 0;
# Line 128  int OrbitalInfoCore(UInt_t run, TFile *f Line 142  int OrbitalInfoCore(UInt_t run, TFile *f
142    TString fname;    TString fname;
143    UInt_t totfileentries = 0;    UInt_t totfileentries = 0;
144    UInt_t idRun = 0;    UInt_t idRun = 0;
145      UInt_t anni5 = 60 * 60 * 24 * 365 * 5 ;//1576800
146    //    //
147    // My variables. Vitaly.    // My variables. Vitaly.
148    //    //
# Line 180  int OrbitalInfoCore(UInt_t run, TFile *f Line 195  int OrbitalInfoCore(UInt_t run, TFile *f
195    //    //
196    // IGRF stuff    // IGRF stuff
197    //    //
198    Float_t dimo = 0.0; // dipole moment (computed from dat files)    Float_t dimo = 0.0; // dipole moment (computed from dat files) // EM GCC 4.7
199    Float_t bnorth, beast, bdown, babs;    Float_t bnorth, beast, bdown, babs;
200    Float_t xl; // L value    Float_t xl; // L value
201    Float_t icode; // code value for L accuracy (see fortran code)    Int_t icode; // code value for L accuracy (see fortran code)
202    Float_t bab1; // What's  the difference with babs?    Float_t bab1; // What's  the difference with babs?
203    Float_t stps = 0.005; // step size for field line tracing    Float_t stps = 0.005; // step size for field line tracing
204    Float_t bdel = 0.01; // required accuracy    Float_t bdel = 0.01; // required accuracy
# Line 223  int OrbitalInfoCore(UInt_t run, TFile *f Line 238  int OrbitalInfoCore(UInt_t run, TFile *f
238    //    //
239    //Quaternions classes    //Quaternions classes
240    //    //
241    Quaternions *L_QQ_Q_l_lower = new Quaternions();    Quaternions *L_QQ_Q_l_lower = 0;
242    InclinationInfo *RYPang_lower = new InclinationInfo();    InclinationInfo *RYPang_lower = 0;
243    Quaternions *L_QQ_Q_l_upper = new Quaternions();    Quaternions *L_QQ_Q_l_upper = 0;
244    InclinationInfo *RYPang_upper = new InclinationInfo();    InclinationInfo *RYPang_upper = 0;
245        
246    cEci eCi;    cEci eCi;
247        
248    // Initialize fortran routines!!!    // Initialize fortran routines!!!
249      Int_t ltp1 = 0;
250    Int_t ltp2 = 0;    Int_t ltp2 = 0;
251    Int_t ltp3 = 0;    GL_PARAM *glparam0 = new GL_PARAM();
   Int_t uno = 1;  
   const char *niente = " ";  
252    GL_PARAM *glparam = new GL_PARAM();    GL_PARAM *glparam = new GL_PARAM();
253    GL_PARAM *glparam2 = new GL_PARAM();    GL_PARAM *glparam2 = new GL_PARAM();
254    
255    //    //
256    // Orientation variables. Vitaly    // Orientation variables. Vitaly
257    //    //
258    
259    UInt_t evfrom = 0;    UInt_t evfrom = 0;
260    UInt_t jumped = 0;    UInt_t jumped = 0;
261    Int_t itr = -1;        Int_t itr = -1;    
262    Double_t A1;    //  Double_t A1;
263    Double_t A2;    //  Double_t A2;
264    Double_t A3;    //  Double_t A3;
265    Double_t Px = 0;    Double_t Px = 0;
266    Double_t Py = 0;          Double_t Py = 0;      
267    Double_t Pz = 0;      Double_t Pz = 0;  
268    TTree *ttof = 0;    TTree *ttof = 0;
269    ToFLevel2 *tof = new ToFLevel2();    ToFLevel2 *tof = new ToFLevel2();
270      TTree *ttrke = 0;
271      TrkLevel2 *trke = new TrkLevel2();
272    OrientationInfo *PO = new OrientationInfo();    OrientationInfo *PO = new OrientationInfo();
273    Int_t nz = 6;    Int_t nz = 6;
274    Float_t zin[6];    Float_t zin[6];
275    Int_t nevtofl2 = 0;    Int_t nevtofl2 = 0;
276      Int_t nevtrkl2 = 0;
277    if ( verbose ) cout<<"Reading quaternions external file"<<endl;    if ( verbose ) cout<<"Reading quaternions external file"<<endl;
278    cout.setf(ios::fixed,ios::floatfield);      cout.setf(ios::fixed,ios::floatfield);  
279    /******Reading recovered quaternions...*********/    /******Reading recovered quaternions...*********/
# Line 265  int OrbitalInfoCore(UInt_t run, TFile *f Line 283  int OrbitalInfoCore(UInt_t run, TFile *f
283    vector<Float_t> recq2;    vector<Float_t> recq2;
284    vector<Float_t> recq3;    vector<Float_t> recq3;
285    Float_t Norm = 1;    Float_t Norm = 1;
286    Int_t parerror=glparam->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table      recqtime.reserve(1500000);
287    ifstream in((glparam->PATH+glparam->NAME).Data(),ios::in);    recq0.reserve(1500000);
288      recq1.reserve(1500000);
289      recq2.reserve(1500000);
290      recq3.reserve(1500000);
291    
292      vector<UInt_t> RTtime1;
293      vector<UInt_t> RTtime2;
294      vector<Double_t> RTbank1;
295      vector<Double_t> RTbank2;
296      vector<Double_t> RTbpluto1;
297      vector<Double_t> RTbpluto2;
298      vector<Int_t> RTazim;
299      vector<UInt_t> RTstart;
300      vector<UInt_t> RTpluto2;
301      vector<UInt_t> RTpluto1;
302      vector<Int_t> RTerrq;
303      vector<Int_t> RTqual;
304      RTtime1.reserve(200000);
305      RTtime2.reserve(200000);
306      RTbank1.reserve(200000);
307      RTbank2.reserve(200000);
308      RTbpluto1.reserve(200000);
309      RTbpluto2.reserve(200000);
310      RTazim.reserve(200000);
311      RTstart.reserve(200000);
312      RTpluto1.reserve(200000);
313      RTpluto2.reserve(200000);
314      RTerrq.reserve(200000);
315      RTqual.reserve(200000);
316    
317      TClonesArray *tcNucleiTrk = NULL;
318      TClonesArray *tcExtNucleiTrk = NULL;
319      TClonesArray *tcExtTrk = NULL;
320      TClonesArray *tcNucleiTof = NULL;
321      TClonesArray *tcExtNucleiTof = NULL;
322      TClonesArray *tcExtTof = NULL;
323      TClonesArray *torbNucleiTrk = NULL;
324      TClonesArray *torbExtNucleiTrk = NULL;
325      TClonesArray *torbExtTrk = NULL;
326      Bool_t hasNucleiTrk = false;
327      Bool_t hasExtNucleiTrk = false;
328      Bool_t hasExtTrk = false;
329      Bool_t hasNucleiTof = false;
330      Bool_t hasExtNucleiTof = false;
331      Bool_t hasExtTof = false;
332    
333      ifstream in;
334      ifstream an;
335      //  ofstream mc;
336      //  TString gr;
337      Int_t parerror2=0;
338    
339      Int_t parerror=glparam0->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table
340      if ( verbose ) cout<<parerror<<"\t"<<(char*)(glparam0->PATH+glparam0->NAME).Data()<<endl;
341    if ( parerror<0 ) {    if ( parerror<0 ) {
342      code = parerror;      code = parerror;
343      goto closeandexit;      goto closeandexit;
344    };    }
345      in.open((char*)(glparam0->PATH+glparam0->NAME).Data(),ios::in);
346    while(!in.eof()){    while(!in.eof()){
347      recqtime.resize(recqtime.size()+1);      recqtime.resize(recqtime.size()+1);
348      Int_t sizee = recqtime.size();      Int_t sizee = recqtime.size();
# Line 284  int OrbitalInfoCore(UInt_t run, TFile *f Line 356  int OrbitalInfoCore(UInt_t run, TFile *f
356      in>>recq2[sizee-1];      in>>recq2[sizee-1];
357      in>>recq3[sizee-1];      in>>recq3[sizee-1];
358      in>>Norm;      in>>Norm;
359    /* CHECK RECOVERED QUATERNIONS PROBLEM
360        if(recqtime[sizee-1]>=1160987921.75 && recqtime[sizee-1]<=1160987932.00){
361          cout<<"We found it at start"<<"\t"<<recqtime[sizee-1]<<endl;
362        } */
363    }    }
364      in.close();
365    if ( verbose ) cout<<"We have read recovered data"<<endl;    if ( verbose ) cout<<"We have read recovered data"<<endl;
366      if (debug) cout << "size of recovered quaterions data set is " << recqtime.size() << endl;
367      if ( debug )  printf(" RQ size %i RQ capacity %i \n",(int)recqtime.size(),(int)recqtime.capacity());
368      
369      if ( verbose ) cout<<"read Rotation Table"<<endl;
370      
371      parerror2=glparam0->Query_GL_PARAM(1,305,dbc);
372    
373      if ( verbose ) cout<<parerror2<<"\t"<<(char*)(glparam0->PATH+glparam0->NAME).Data()<<endl;
374    parerror=glparam->Query_GL_PARAM(1,301,dbc); // parameters stored in DB in GL_PRAM table      if ( parerror2<0 ) {
   if ( parerror<0 ) {  
     code = parerror;  
     goto closeandexit;  
   };  
   ltp2 = (Int_t)(glparam->PATH+glparam->NAME).Length();  
   if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());  
   //  
   parerror=glparam2->Query_GL_PARAM(1,302,dbc); // parameters stored in DB in GL_PRAM table  
   if ( parerror<0 ) {  
375      code = parerror;      code = parerror;
376      goto closeandexit;      goto closeandexit;
377    };    }
378    ltp3 = (Int_t)(glparam2->PATH+glparam2->NAME).Length();    an.open((char*)(glparam0->PATH+glparam0->NAME).Data(),ios::in);
379    if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());    while(!an.eof()){
380    //      RTtime1.resize(RTtime1.size()+1);
381    initize_((char *)niente,&uno,(char *)(glparam->PATH+glparam->NAME).Data(),&ltp2,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp3);      Int_t sizee = RTtime1.size();
382    //      RTbank1.resize(sizee+1);
383    // End IGRF stuff//      RTazim.resize(sizee+1);
384    //      RTerrq.resize(sizee+1);
385        RTstart.resize(sizee+1);
386        RTpluto1.resize(sizee+1);
387        RTbpluto1.resize(sizee+1);
388        RTqual.resize(sizee+1);
389        an>>RTtime1[sizee-1];
390        an>>RTbank1[sizee-1];
391        an>>RTstart[sizee-1];
392        an>>RTpluto1[sizee-1];
393        an>>RTbpluto1[sizee-1];
394        an>>RTazim[sizee-1];
395        an>>RTerrq[sizee-1];
396        an>>RTqual[sizee-1];
397        if(sizee>1) {
398          RTtime2.resize(sizee+1);
399          RTbank2.resize(sizee+1);
400          RTpluto2.resize(sizee+1);
401          RTbpluto2.resize(sizee+1);
402          RTtime2[sizee-2]=RTtime1[sizee-1];
403          RTpluto2[sizee-2]=RTpluto1[sizee-1];
404          RTbank2[sizee-2]=RTbank1[sizee-1];
405          RTbpluto2[sizee-2]=RTbpluto1[sizee-1];
406        }
407      }
408      an.close();
409      //cout<<"put some number here"<<endl;
410      //Int_t yupi;
411      //cin>>yupi;
412      
413      if ( verbose ) cout<<"We have read Rotation Table"<<endl;
414        //Geomagnetic coordinates calculations staff
415      
416      if ( debug ) printf(" RT size %i RT capacity %i \n",(int)RTtime2.size(),(int)RTtime2.capacity());
417    
418      GMtype_CoordGeodetic location;
419      //  GMtype_CoordDipole GMlocation;
420      GMtype_Ellipsoid Ellip;
421      GMtype_Data G0, G1, H1;
422            
423      //  { // 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
424      //    TString igpath="/data03/Malakhov/pam9Malakhov/installed10/calib/orb-param/";
425      //  }
426    
427      //cout << G0.element[0] << "\t" << G1.element[0] << "\t" << H1.element[0] << endl;
428      //cout << G0.element[5] << "\t" << G1.element[5] << "\t" << H1.element[5] << endl;
429    
430      GM_SetEllipsoid(&Ellip);
431    
432      // IGRF stuff moved inside run loop!  
433    
434    for (Int_t ip=0;ip<nz;ip++){    for (Int_t ip=0;ip<nz;ip++){
435      zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));      zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));
436    };    };
437    //    //
438    if ( !standalone ){    if ( !standalone ){
439      //      //
440      // Does it contain the Tracker tree?      // Does it contain the Tracker and ToF trees?
441      //      //
442      ttof = (TTree*)file->Get("ToF");      ttof = (TTree*)file->Get("ToF");
443      if ( !ttof ) {      if ( !ttof ) {
444        if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");        if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");
445        code = -900;        code = -900;
446        goto closeandexit;        goto closeandexit;
447      };      }
448      ttof->SetBranchAddress("ToFLevel2",&tof);        ttof->SetBranchAddress("ToFLevel2",&tof);  
449      nevtofl2 = ttof->GetEntries();      nevtofl2 = ttof->GetEntries();
450    };  
451        //
452        // Look for extended tracking algorithm
453        //
454        if ( verbose ) printf("Look for extended and nuclei tracking algorithms in ToF\n");
455        // Nuclei tracking algorithm
456        Int_t checkAlgo = 0;
457        tcNucleiTof =  new TClonesArray("ToFTrkVar");
458        checkAlgo = ttof->SetBranchAddress("TrackNuclei",&tcNucleiTof);    
459        if ( !checkAlgo ){
460          if ( verbose ) printf(" Nuclei tracking algorithm ToF branch found! :D \n");
461          hasNucleiTof = true;
462        } else {
463          if ( verbose ) printf(" Nuclei tracking algorithm ToF branch not found :( !\n");
464          printf(" ok, this is not a problem (it depends on tracker settings) \n");
465          delete tcNucleiTof;
466          tcNucleiTof=NULL; // 10RED reprocessing bug  
467        }
468        // Nuclei tracking algorithm using calorimeter points
469        tcExtNucleiTof =  new TClonesArray("ToFTrkVar");
470        checkAlgo = ttof->SetBranchAddress("RecoveredTrackNuclei",&tcExtNucleiTof);    
471        if ( !checkAlgo ){
472          if ( verbose ) printf(" Recovered nuclei tracking algorithm ToF branch found! :D \n");
473          hasExtNucleiTof = true;
474        } else {
475          if ( verbose ) printf(" Recovered nuclei tracking algorithm ToF branch not found :( !\n");
476          printf(" ok, this is not a problem (it depends on tracker settings) \n");
477          delete tcExtNucleiTof;
478          tcExtNucleiTof=NULL; // 10RED reprocessing bug  
479        }
480        // Tracking algorithm using calorimeter points
481        tcExtTof =  new TClonesArray("ToFTrkVar");
482        checkAlgo = ttof->SetBranchAddress("RecoveredTrack",&tcExtTof);
483        if ( !checkAlgo ){
484          if ( verbose ) printf(" Recovered track algorithm ToF branch found! :D \n");
485          hasExtTof = true;
486        } else {
487          if ( verbose ) printf(" Recovered track algorithm ToF branch not found :( !\n");
488          printf(" ok, this is not a problem (it depends on tracker settings) \n");
489          delete tcExtTof;
490          tcExtTof=NULL; // 10RED reprocessing bug  
491        }
492    
493        ttrke = (TTree*)file->Get("Tracker");
494        if ( !ttrke ) {
495          if ( verbose ) printf(" OrbitalInfo - ERROR: no trk tree\n");
496          code = -903;
497          goto closeandexit;
498        }
499        ttrke->SetBranchAddress("TrkLevel2",&trke);  
500        nevtrkl2 = ttrke->GetEntries();
501    
502        //
503        // Look for extended tracking algorithm
504        //
505        if ( verbose ) printf("Look for extended and nuclei tracking algorithms\n");
506        // Nuclei tracking algorithm
507        checkAlgo = 0;
508        tcNucleiTrk =  new TClonesArray("TrkTrack");
509        checkAlgo = ttrke->SetBranchAddress("TrackNuclei",&tcNucleiTrk);    
510        if ( !checkAlgo ){
511          if ( verbose ) printf(" Nuclei tracking algorithm branch found! :D \n");
512          hasNucleiTrk = true;
513        } else {
514          if ( verbose ) printf(" Nuclei tracking algorithm branch not found :( !\n");
515          printf(" ok, this is not a problem (it depends on tracker settings) \n");
516          delete tcNucleiTrk;
517          tcNucleiTrk=NULL; // 10RED reprocessing bug  
518        }
519        // Nuclei tracking algorithm using calorimeter points
520        tcExtNucleiTrk =  new TClonesArray("ExtTrack");
521        checkAlgo = ttrke->SetBranchAddress("RecoveredTrackNuclei",&tcExtNucleiTrk);    
522        if ( !checkAlgo ){
523          if ( verbose ) printf(" Recovered nuclei tracking algorithm branch found! :D \n");
524          hasExtNucleiTrk = true;
525        } else {
526          if ( verbose ) printf(" Recovered nuclei tracking algorithm branch not found :( !\n");
527          printf(" ok, this is not a problem (it depends on tracker settings) \n");
528          delete tcExtNucleiTrk;
529          tcExtNucleiTrk=NULL; // 10RED reprocessing bug  
530        }
531        // Tracking algorithm using calorimeter points
532        tcExtTrk =  new TClonesArray("ExtTrack");
533        checkAlgo = ttrke->SetBranchAddress("RecoveredTrack",&tcExtTrk);
534        if ( !checkAlgo ){
535          if ( verbose ) printf(" Recovered track algorithm branch found! :D \n");
536          hasExtTrk = true;
537        } else {
538          if ( verbose ) printf(" Recovered track algorithm branch not found :( !\n");
539          printf(" ok, this is not a problem (it depends on tracker settings) \n");
540          delete tcExtTrk;
541          tcExtTrk=NULL; // 10RED reprocessing bug  
542        }
543    
544        if ( (hasNucleiTrk && !hasNucleiTof) || (!hasNucleiTrk && hasNucleiTof) ||
545             (hasExtNucleiTrk && !hasExtNucleiTof) || (!hasExtNucleiTrk && hasExtNucleiTof) ||
546             (hasExtTrk && !hasExtTof) || (!hasExtTrk && hasExtTof)
547             ){
548          if ( verbose ) printf(" ERROR: Mismatch between tracker and tof tree branches concerning extended tracking algorithm(s)\n");
549          if ( debug ) printf("hasNucleiTrk %i hasExtNucleiTrk %i hasExtTrk %i \n",hasNucleiTrk,hasExtNucleiTrk,hasExtTrk);
550          if ( debug ) printf("hasNucleiTof %i hasExtNucleiTof %i hasExtTof %i \n",hasNucleiTof,hasExtNucleiTof,hasExtTof);
551          throw -901;
552        }
553    
554      }
555    //    //
556    // Let's start!    // Let's start!
557    //    //
# Line 413  int OrbitalInfoCore(UInt_t run, TFile *f Line 640  int OrbitalInfoCore(UInt_t run, TFile *f
640        //        //
641        reprocall = true;        reprocall = true;
642        //        //
643        if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n");        if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n Deleting old tree...\n");
644        //        //
645      } else {      } else {
646        //        //
# Line 431  int OrbitalInfoCore(UInt_t run, TFile *f Line 658  int OrbitalInfoCore(UInt_t run, TFile *f
658        tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");        tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");
659        tempOrbitalInfo->SetName("OrbitalInfo-old");        tempOrbitalInfo->SetName("OrbitalInfo-old");
660        tempfile->Write();        tempfile->Write();
661          tempOrbitalInfo->Delete();
662        tempfile->Close();          tempfile->Close();  
663      }      }
664      //      //
665      // Delete the old tree from old file and memory      // Delete the old tree from old file and memory
666      //      //
667        OrbitalInfotrclone->Clear();
668      OrbitalInfotrclone->Delete("all");      OrbitalInfotrclone->Delete("all");
669      //      //
670      if (verbose) printf(" ...done!\n");      if (verbose) printf(" ...done!\n");
# Line 450  int OrbitalInfoCore(UInt_t run, TFile *f Line 679  int OrbitalInfoCore(UInt_t run, TFile *f
679    orbitalinfo->Set();//ELENA **TEMPORANEO?**    orbitalinfo->Set();//ELENA **TEMPORANEO?**
680    OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);    OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);
681    //    //
682      // create new branches for new tracking algorithms
683      //
684      if ( hasNucleiTrk ){
685        torbNucleiTrk = new TClonesArray("OrbitalInfoTrkVar",1);
686        OrbitalInfotr->Branch("TrackNuclei",&torbNucleiTrk);
687      }
688      if ( hasExtNucleiTrk ){
689        torbExtNucleiTrk = new TClonesArray("OrbitalInfoTrkVar",1);
690        OrbitalInfotr->Branch("RecoveredTrackNuclei",&torbExtNucleiTrk);
691      }
692      if ( hasExtTrk ){
693        torbExtTrk = new TClonesArray("OrbitalInfoTrkVar",1);
694        OrbitalInfotr->Branch("RecoveredTrack",&torbExtTrk);
695      }
696    
697      //
698    if ( reproc && !reprocall ){    if ( reproc && !reprocall ){
699      //      //
700      //  open new file and retrieve also tree informations      //  open new file and retrieve also tree informations
# Line 471  int OrbitalInfoCore(UInt_t run, TFile *f Line 716  int OrbitalInfoCore(UInt_t run, TFile *f
716          //          //
717          // copy orbitalinfoclone to mydec          // copy orbitalinfoclone to mydec
718          //          //
719          orbitalinfo->Clear();          //      orbitalinfo->Clear();
720          //          //
721          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
722          //          //
# Line 481  int OrbitalInfoCore(UInt_t run, TFile *f Line 726  int OrbitalInfoCore(UInt_t run, TFile *f
726          //          //
727        };        };
728        if (verbose) printf(" Finished successful copying!\n");        if (verbose) printf(" Finished successful copying!\n");
729      };                };
730    };    };
731    //    //
732    //    //
733    // 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.
734    //    //
735    runlist = runinfo->GetRunList();    runlist = runinfo->GetRunList();
736      if ( debug ){
737        printf("BEFORE LOOP ON RUN\n");
738        gObjectTable->Print();
739      }
740    //    //
741    // Loop over the run to be processed    // Loop over the run to be processed
742    //    //
743    for (UInt_t irun=0; irun < numbofrun; irun++){    for (UInt_t irun=0; irun < numbofrun; irun++){ //===>
744    
745        L_QQ_Q_l_lower = new Quaternions();
746        RYPang_lower = new InclinationInfo();
747        L_QQ_Q_l_upper = new Quaternions();
748        RYPang_upper = new InclinationInfo();
749    
750      //      //
751      // retrieve the first run ID to be processed using the RunInfo list      // retrieve the first run ID to be processed using the RunInfo list
752      //      //
# Line 554  int OrbitalInfoCore(UInt_t run, TFile *f Line 809  int OrbitalInfoCore(UInt_t run, TFile *f
809      //      //
810      //    if ( !totevent ) goto closeandexit;      //    if ( !totevent ) goto closeandexit;
811      // Open Level0 file      // Open Level0 file
812        if ( l0File ) l0File->Close();
813      l0File = new TFile(fname.Data());      l0File = new TFile(fname.Data());
814      if ( !l0File ) {      if ( !l0File ) {
815        if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");        if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
# Line 592  int OrbitalInfoCore(UInt_t run, TFile *f Line 848  int OrbitalInfoCore(UInt_t run, TFile *f
848        code = -12;        code = -12;
849        goto closeandexit;        goto closeandexit;
850      };      };
851      //  
     //     TTree *tp = (TTree*)l0File->Get("RunHeader");  
     //     tp->SetBranchAddress("Header", &eH);  
     //     tp->SetBranchAddress("RunHeader", &reh);  
     //     tp->GetEntry(0);  
     //     ph = eH->GetPscuHeader();  
     //     ULong_t TimeSync = reh->LAST_TIME_SYNC_INFO;  
     //     ULong_t ObtSync = reh->OBT_TIME_SYNC;      
     //     if ( debug ) printf(" 1 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",TimeSync,ObtSync,TimeSync-ObtSync);  
     //  
852      ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();      ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();
853      ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);      ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);
854      ULong_t DeltaOBT = TimeSync - ObtSync;      ULong_t DeltaOBT = TimeSync - ObtSync;
# Line 637  int OrbitalInfoCore(UInt_t run, TFile *f Line 884  int OrbitalInfoCore(UInt_t run, TFile *f
884          //          //
885          l0fid[i] = (UInt_t)atoll(Row->GetField(0));          l0fid[i] = (UInt_t)atoll(Row->GetField(0));
886          i--;          i--;
887            if (Row){  // memleak!
888              delete Row;
889              Row = 0;
890            }
891          Row = pResult->Next();            Row = pResult->Next();  
892          //          //
893        };        }
894          if (Row) delete Row;
895        pResult->Delete();        pResult->Delete();
896      };      }
897      //      //
898      myquery.str("");      myquery.str("");
899      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;";      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;";
# Line 659  int OrbitalInfoCore(UInt_t run, TFile *f Line 911  int OrbitalInfoCore(UInt_t run, TFile *f
911          //          //
912          l0fid[i] = (UInt_t)atoll(Row->GetField(0));          l0fid[i] = (UInt_t)atoll(Row->GetField(0));
913          i++;          i++;
914            if (Row){  // memleak!
915              delete Row;
916              Row = 0;
917            }
918          Row = pResult->Next();            Row = pResult->Next();  
919          //          //
920        };        }
921          if (Row) delete Row;
922        pResult->Delete();        pResult->Delete();
923      };      }
924      //      //
925      i = 0;      i = 0;
926      UInt_t previd = 0;      UInt_t previd = 0;
# Line 682  int OrbitalInfoCore(UInt_t run, TFile *f Line 939  int OrbitalInfoCore(UInt_t run, TFile *f
939            if ( debug ) printf(" Using inclination informations from file: %s \n",(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1)).Data());            if ( debug ) printf(" Using inclination informations from file: %s \n",(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1)).Data());
940            ch->Add(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1));            ch->Add(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1));
941            //            //
942              if (Row) delete Row;
943            pResult->Delete();            pResult->Delete();
944          };          }
945        };        }
946        i++;        i++;
947      };      }
948      //      //
     //    l0trm = (TTree*)l0File->Get("Mcmd");  
     //    ch->ls();  
949      ch->SetBranchAddress("Mcmd",&mcmdev);      ch->SetBranchAddress("Mcmd",&mcmdev);
     //    printf(" entries %llu \n", ch->GetEntries());  
     //    l0trm = ch->GetTree();  
     //    neventsm = l0trm->GetEntries();  
950      neventsm = ch->GetEntries();      neventsm = ch->GetEntries();
951      if ( debug ) printf(" entries %u \n", neventsm);      if ( debug ) printf(" entries %u \n", neventsm);
     //    neventsm = 0;  
952      //      //
953      if (neventsm == 0){      if (neventsm == 0){
954        if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");        if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");
       //      l0File->Close();  
955        code = 900;        code = 900;
       //      goto closeandexit;  
956      }      }
957      //      //
958            Double_t lowerqtime = 0;    
     //    l0trm->SetBranchAddress("Mcmd", &mcmdev);  
     //    l0trm->SetBranchAddress("Header", &eh);  
     //  
     //  
     //  
   
 //    UInt_t mctren = 0;      
 //    UInt_t mcreen = 0;          
     UInt_t numrec = 0;  
     //  
     Double_t upperqtime = 0;  
     Double_t lowerqtime = 0;  
       
 //    Double_t incli = 0;  
 //    oi = 0;  
 //    UInt_t ooi = 0;  
959      //      //
960      // init quaternions information from mcmd-packets      // init quaternions information from mcmd-packets
961      //      //
962      Bool_t isf = true;      Bool_t isf = true;
 //    Int_t fgh = 0;  
963    
964      vector<Float_t> q0;      vector<Float_t> q0;
965      vector<Float_t> q1;      vector<Float_t> q1;
# Line 738  int OrbitalInfoCore(UInt_t run, TFile *f Line 971  int OrbitalInfoCore(UInt_t run, TFile *f
971      vector<Float_t> qYaw;      vector<Float_t> qYaw;
972      vector<Int_t> qmode;      vector<Int_t> qmode;
973    
974        q0.reserve(4096);
975        q1.reserve(4096);
976        q2.reserve(4096);
977        q3.reserve(4096);
978        qtime.reserve(4096);
979        qPitch.reserve(4096);
980        qRoll.reserve(4096);
981        qYaw.reserve(4096);
982        qmode.reserve(4096);
983        if ( debug ) printf(" q0 capa %i \n",(int)q0.capacity());
984      Int_t nt = 0;      Int_t nt = 0;
       
     //init sine-function interpolation  
       
     //cout<<"Sine coeficient initialisation..."<<endl;  
     vector<Sine> q0sine;  
     vector<Sine> q1sine;  
     vector<Sine> q2sine;  
     vector<Sine> q3sine;  
     vector<Sine> Yawsine;  
   
     /*TH2F* q0testing = new TH2F();  
       TH2F* q1testing = new TH2F();  
       TH2F* q2testing = new TH2F();  
       TH2F* q3testing = new TH2F();  
       TH2F* Pitchtesting = new TH2F();  
     */  
985      UInt_t must = 0;      UInt_t must = 0;
986    
987        Int_t currentYear = 0;
988        Int_t previousYear = 0;
989    
990      //      //
991      // run over all the events of the run      // run over all the events of the run
992      //      //
993      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");
994        if ( debug ){
995          printf("BEFORE LOOP ON EVENTS\n");
996          gObjectTable->Print();
997        }
998      //      //
999      //      //
1000      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
1001          //for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+10); re++){
1002    
1003        //        //
1004        if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);          if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);  
1005        if ( debug ) printf(" %i \n",procev);              if ( debug ) printf(" %i \n",procev);      
# Line 785  int OrbitalInfoCore(UInt_t run, TFile *f Line 1021  int OrbitalInfoCore(UInt_t run, TFile *f
1021          continue;          continue;
1022        }        }
1023    
1024          // just for testing:
1025          //      if (re >= 5+runinfo->EV_FROM) atime += anni5;
1026          //      if (re >= 7+runinfo->EV_FROM) atime += anni5;
1027          //      if (re >= 9+runinfo->EV_FROM) atime += anni5;
1028    
1029          //
1030          // open IGRF files and do it only once if we are processing a full level2 file
1031          //
1032          Float_t kkyear;
1033          UInt_t kyear, kmonth, kday, khour, kmin, ksec;
1034          //
1035          TTimeStamp kt = TTimeStamp(atime, kTRUE);
1036          kt.GetDate(kTRUE, 0, &kyear, &kmonth, &kday);
1037          kt.GetTime(kTRUE, 0, &khour, &kmin, &ksec);
1038          kkyear = (float) kyear
1039            + (kmonth*31.+ (float) kday)/365.
1040            + (khour*3600.+kmin*60.+(float)ksec)/(24.*3600.*365.);
1041          currentYear = int(kkyear/5.) * 5;
1042          if ( debug ) printf(" prevy %i curry %i igrfloaded %i \n",previousYear,currentYear,igrfloaded);
1043          if ( currentYear != previousYear ) igrfloaded = false;
1044          previousYear = currentYear;
1045          if ( debug ) printf(" prevy %i curry %i igrfloaded %i \n",previousYear,currentYear,igrfloaded);
1046          //
1047          if ( !igrfloaded ){
1048            
1049            igrfloaded = true;
1050            
1051            parerror=glparam->Query_GL_PARAM(atime,302,dbc); // parameters stored in DB in GL_PRAM table  
1052            if ( parerror<0 ) {
1053              code = parerror;
1054              goto closeandexit;
1055            }
1056            ltp1 = (Int_t)(glparam->PATH+glparam->NAME).Length();
1057            if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
1058            //
1059            if ( glparam->NAME.EndsWith("s.txt") || glparam->NAME.EndsWith("s.dat") ){
1060              if ( verbose ) printf("ERROR: Current date is beyond the latest secular variation file time span. Please update IGRF files to process data\n");
1061              throw -906;
1062            }
1063            //
1064            int isSecular = false;
1065            //
1066            parerror=glparam2->Query_GL_PARAM(atime+anni5,302,dbc); // parameters stored in DB in GL_PRAM table  
1067            if ( parerror<0 ) {
1068              code = parerror;
1069              goto closeandexit;
1070            }
1071            ltp2 = (Int_t)(glparam2->PATH+glparam2->NAME).Length();
1072            if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());
1073            if ( glparam2->NAME.EndsWith("s.txt") || glparam2->NAME.EndsWith("s.dat") ){
1074              isSecular = true;
1075              if ( verbose ) printf(" Using secular variation file and hence fortran subroutine 'extrapolation'\n");
1076            } else {
1077              if ( verbose ) printf(" Using two field measurement files and hence fortran subroutine 'interpolation'\n");
1078            }
1079            //
1080            initize_(&isSecular,(char *)(glparam->PATH+glparam->NAME).Data(),&ltp1,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp2);
1081            //
1082            if (debug) cout<<"initize: "<<(char *)(glparam->PATH+glparam->NAME).Data()<<"\t"<<(char *)(glparam2->PATH+glparam2->NAME).Data()<<"\t isSecular? "<<isSecular<<endl;  
1083    
1084            //        GM_ScanIGRF(dbc, &G0, &G1, &H1);
1085            TString igrfFile1 = glparam->PATH+glparam->NAME;
1086            TString igrfFile2 = glparam2->PATH+glparam2->NAME;
1087            GM_SetIGRF(isSecular,igrfFile1,igrfFile2, &G0, &G1, &H1);
1088          }
1089          //
1090          // End IGRF stuff//
1091          //
1092    
1093        //        //
1094        // retrieve tof informations        // retrieve tof informations
1095        //        //
# Line 800  int OrbitalInfoCore(UInt_t run, TFile *f Line 1105  int OrbitalInfoCore(UInt_t run, TFile *f
1105            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);
1106            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);
1107            l0File->Close();            l0File->Close();
1108            code = -901;            code = -904;
1109            goto closeandexit;            goto closeandexit;
1110          };          };
1111          //          //
1112          tof->Clear();          tof->Clear();
1113          //          //
1114          if ( ttof->GetEntry(itr) <= 0 ) throw -36;          // Clones array must be cleared before going on
1115            //
1116            if ( hasNucleiTof ){
1117              tcNucleiTof->Delete();
1118            }
1119            if ( hasExtNucleiTof ){
1120              tcExtNucleiTof->Delete();
1121            }          
1122            if ( hasExtTof ){
1123              tcExtTof->Delete();
1124            }
1125            //
1126            if ( verbose ) printf(" get tof tree entries... entry = %i in Level2 file\n",itr);
1127            if ( ttof->GetEntry(itr) <= 0 ){
1128              if ( verbose ) printf(" problems with tof tree entries... entry = %i in Level2 file\n",itr);
1129              if ( verbose ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1130              throw -36;
1131            }
1132            if ( verbose ) printf(" gat0\n");
1133          //          //
1134        };        }
1135          //
1136          // retrieve tracker informations
1137          //
1138          if ( !standalone ){
1139            if ( itr > nevtrkl2 ){  
1140              if ( verbose ) printf(" OrbitalInfo - ERROR: no trk events with entry = %i in Level2 file\n",itr);
1141              if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
1142              l0File->Close();
1143              code = -905;
1144              goto closeandexit;
1145            }
1146            //
1147            if ( verbose ) printf(" gat1\n");
1148            trke->Clear();
1149            //
1150            // Clones array must be cleared before going on
1151            //
1152            if ( hasNucleiTrk ){
1153              if ( verbose ) printf(" gat2\n");
1154              tcNucleiTrk->Delete();
1155              if ( verbose ) printf(" gat3\n");
1156              torbNucleiTrk->Delete();
1157            }
1158            if ( hasExtNucleiTrk ){
1159              if ( verbose ) printf(" gat4\n");
1160              tcExtNucleiTrk->Delete();
1161              if ( verbose ) printf(" gat5\n");
1162              torbExtNucleiTrk->Delete();
1163            }          
1164            if ( hasExtTrk ){
1165              if ( verbose ) printf(" gat6\n");
1166              tcExtTrk->Delete();
1167              if ( verbose ) printf(" gat7\n");
1168              torbExtTrk->Delete();
1169            }
1170            //
1171            if ( verbose ) printf(" get trk tree entries... entry = %i in Level2 file\n",itr);
1172            if ( ttrke->GetEntry(itr) <= 0 ) throw -36;
1173            //
1174          }
1175    
1176        //        //
1177        procev++;        procev++;
1178        //        //
# Line 816  int OrbitalInfoCore(UInt_t run, TFile *f Line 1180  int OrbitalInfoCore(UInt_t run, TFile *f
1180        //        //
1181        if ( debug ) printf(" %i start processing \n",procev);              if ( debug ) printf(" %i start processing \n",procev);      
1182        orbitalinfo->Clear();        orbitalinfo->Clear();
1183    
1184        //        //
1185        OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();        OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();
1186        if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);        if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);
1187        TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;        TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;
1188    
1189          // Geomagnetic coordinates calculation variables
1190          GMtype_CoordSpherical CoordSpherical, DipoleSpherical;
1191          GMtype_CoordCartesian CoordCartesian, DipoleCartesian;
1192          GMtype_Model Model;
1193          GMtype_Pole Pole;
1194    
1195        //        //
1196        // Fill OBT, pkt_num and absTime        // Fill OBT, pkt_num and absTime
1197        //              //      
# Line 832  int OrbitalInfoCore(UInt_t run, TFile *f Line 1204  int OrbitalInfoCore(UInt_t run, TFile *f
1204        //        //
1205        if ( debug ) printf(" %i sgp4 \n",procev);              if ( debug ) printf(" %i sgp4 \n",procev);      
1206        cCoordGeo coo;        cCoordGeo coo;
1207        Float_t jyear=0.;            Float_t jyear=0.;
1208        //        //
1209        if(atime >= gltle->GetToTime()) {        if(atime >= gltle->GetToTime() || atime < gltle->GetFromTime() ) {  // AGH! bug when reprocessing??
1210    
1211          if ( !gltle->Query(atime, dbc) ){          if ( !gltle->Query(atime, dbc) ){
1212            //                  //      
1213            // Compute the magnetic dipole moment.            // Compute the magnetic dipole moment.
# Line 849  int OrbitalInfoCore(UInt_t run, TFile *f Line 1222  int OrbitalInfoCore(UInt_t run, TFile *f
1222              + (month*31.+ (float) day)/365.              + (month*31.+ (float) day)/365.
1223              + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);              + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);
1224            //            //
1225            if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);                  if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);            
1226              if ( debug ) printf(" %i jyear %f dimo %f \n",procev,jyear,dimo);
1227            feldcof_(&jyear, &dimo); // get dipole moment for year            feldcof_(&jyear, &dimo); // get dipole moment for year
1228            if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);                  if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);
1229    
1230              //      GM_TimeAdjustCoefs(year, jyear, G0, G1, H1, &Model);
1231              GM_TimeAdjustCoefs(GM_STARTYEAR, (jyear-currentYear+GM_STARTYEAR), G0, G1, H1, &Model);  // EM: input this way due to the new way of storing data into Gn,H1 and to avoid changing GM_Time...
1232              GM_PoleLocation(Model, &Pole);
1233              
1234          } else {          } else {
1235            code = -56;            code = -56;
1236            goto closeandexit;            goto closeandexit;
# Line 861  int OrbitalInfoCore(UInt_t run, TFile *f Line 1240  int OrbitalInfoCore(UInt_t run, TFile *f
1240        //        //
1241        cOrbit orbits(*gltle->GetTle());        cOrbit orbits(*gltle->GetTle());
1242        //        //
       if ( debug ) printf(" I am Here \n");  
       //  
1243        // synchronize with quaternions data        // synchronize with quaternions data
1244        //        //
1245        if ( isf && neventsm>0 ){        if ( isf && neventsm>0 ){
# Line 870  int OrbitalInfoCore(UInt_t run, TFile *f Line 1247  int OrbitalInfoCore(UInt_t run, TFile *f
1247          // First event          // First event
1248          //          //
1249          isf = false;          isf = false;
1250          upperqtime = atime;          //      upperqtime = atime;
1251          lowerqtime = runinfo->RUNHEADER_TIME;          lowerqtime = runinfo->RUNHEADER_TIME;
1252          for ( ik = 0; ik < neventsm; ik++){  //number of macrocommad packets          for ( ik = 0; ik < neventsm; ik++){  //number of macrocommad packets
1253            if ( ch->GetEntry(ik) <= 0 ) throw -36;            if ( ch->GetEntry(ik) <= 0 ) throw -36;
1254            tmpSize = mcmdev->Records->GetEntries();            tmpSize = mcmdev->Records->GetEntries();
1255            numrec = tmpSize;            //      numrec = tmpSize;
1256              if ( debug ) cout << "packet number " << ik <<"\tnumber of subpackets is " << tmpSize << endl;
1257            for (Int_t j3 = 0;j3<tmpSize;j3++){  //number of subpackets            for (Int_t j3 = 0;j3<tmpSize;j3++){  //number of subpackets
             if ( debug ) printf(" ik %i j3 %i eh eh eh \n",ik,j3);  
1258              mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);              mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);
1259              if ( mcmdrc ){ // missing inclination bug [8RED 090116]              if ( mcmdrc ){ // missing inclination bug [8RED 090116]
1260                if ( debug ) printf(" pluto \n");                if ( debug ) printf(" pluto \n");
1261                if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet                if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet
1262                  L_QQ_Q_l_upper->fill(mcmdrc->McmdData);                 L_QQ_Q_l_upper->fill(mcmdrc->McmdData);
1263                  for (UInt_t ui = 0; ui < 6; ui++){                  for (UInt_t ui = 0; ui < 6; ui++){
1264                    if (ui>0){                    if (ui>0){
1265                      if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){                      if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){
                         if ( debug ) printf(" here1 %i \n",ui);  
1266                        Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));                        Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));
1267                        Int_t recSize = recqtime.size();                        Int_t recSize = recqtime.size();
1268                        for(Int_t mu = nt;mu<recSize;mu++){                        if(lowerqtime > recqtime[recSize-1]){
1269                          if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){                           // to avoid interpolation between bad quaternions arrays
1270                            nt=mu;                           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){
                           Int_t sizeqmcmd = qtime.size();  
                           inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);  
                           qtime[sizeqmcmd]=recqtime[mu];  
                           q0[sizeqmcmd]=recq0[mu];  
                           q1[sizeqmcmd]=recq1[mu];  
                           q2[sizeqmcmd]=recq2[mu];  
                           q3[sizeqmcmd]=recq3[mu];  
                           qmode[sizeqmcmd]=-10;  
                           orbits.getPosition((double) (qtime[sizeqmcmd] - 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,recq0[mu],recq1[mu],recq2[mu],recq3[mu]);  
                           qRoll[sizeqmcmd]=RYPang_upper->Kren;  
                           qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;  
                           qPitch[sizeqmcmd]=RYPang_upper->Tangazh;  
                         }  
                         if(recqtime[mu]>=u_time){  
1271                            Int_t sizeqmcmd = qtime.size();                            Int_t sizeqmcmd = qtime.size();
1272                            inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);                            inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1273                            qtime[sizeqmcmd]=u_time;                            qtime[sizeqmcmd]=u_time;
# Line 921  int OrbitalInfoCore(UInt_t run, TFile *f Line 1282  int OrbitalInfoCore(UInt_t run, TFile *f
1282                            qRoll[sizeqmcmd]=RYPang_upper->Kren;                            qRoll[sizeqmcmd]=RYPang_upper->Kren;
1283                            qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;                            qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1284                            qPitch[sizeqmcmd]=RYPang_upper->Tangazh;                            qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1285                            break;                           }
1286                          }
1287                          for(Int_t mu = nt;mu<recSize;mu++){
1288                            if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1289                              if(sqrt(pow(recq0[mu],2)+pow(recq1[mu],2)+pow(recq2[mu],2)+pow(recq3[mu],2))>0.99999){
1290                                nt=mu;
1291                                Int_t sizeqmcmd = qtime.size();
1292                                inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1293                                qtime[sizeqmcmd]=recqtime[mu];
1294                                q0[sizeqmcmd]=recq0[mu];
1295                                q1[sizeqmcmd]=recq1[mu];
1296                                q2[sizeqmcmd]=recq2[mu];
1297                                q3[sizeqmcmd]=recq3[mu];
1298                                qmode[sizeqmcmd]=-10;
1299                                orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1300                                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]);
1301                                qRoll[sizeqmcmd]=RYPang_upper->Kren;
1302                                qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1303                                qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1304                              }
1305                            }
1306                            if(recqtime[mu]>=u_time){
1307                              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){
1308                                Int_t sizeqmcmd = qtime.size();
1309                                inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1310                                qtime[sizeqmcmd]=u_time;
1311                                q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
1312                                q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
1313                                q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
1314                                q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
1315                                qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1316                                lowerqtime = u_time;
1317                                orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1318                                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]);
1319                                qRoll[sizeqmcmd]=RYPang_upper->Kren;
1320                                qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1321                                qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1322                                break;
1323                              }
1324                          }                          }
1325                        }                        }
1326                      }                      }
1327                    }else{                    }else{
1328                          if ( debug ) printf(" here2 %i \n",ui);  //                    if ( debug ) printf(" here2 %i \n",ui);
1329                      Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));                      Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));
1330                      if(lowerqtime>u_time)nt=0;                      if(lowerqtime>u_time)nt=0;
1331                      Int_t recSize = recqtime.size();                      Int_t recSize = recqtime.size();
1332                      for(Int_t mu = nt;mu<recSize;mu++){                      if(lowerqtime > recqtime[recSize-1]){
1333                        if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){                        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){
                         nt=mu;  
                         Int_t sizeqmcmd = qtime.size();  
                         inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);  
                         qtime[sizeqmcmd]=recqtime[mu];  
                         q0[sizeqmcmd]=recq0[mu];  
                         q1[sizeqmcmd]=recq1[mu];  
                         q2[sizeqmcmd]=recq2[mu];  
                         q3[sizeqmcmd]=recq3[mu];  
                         qmode[sizeqmcmd]=-10;  
                         orbits.getPosition((double) (qtime[sizeqmcmd] - 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,recq0[mu],recq1[mu],recq2[mu],recq3[mu]);  
                         qRoll[sizeqmcmd]=RYPang_upper->Kren;  
                         qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;  
                         qPitch[sizeqmcmd]=RYPang_upper->Tangazh;  
                       }  
                       if(recqtime[mu]>=u_time){  
1334                          Int_t sizeqmcmd = qtime.size();                          Int_t sizeqmcmd = qtime.size();
1335                          inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);                          inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1336                          qtime[sizeqmcmd]=u_time;                          qtime[sizeqmcmd]=u_time;
# Line 962  int OrbitalInfoCore(UInt_t run, TFile *f Line 1345  int OrbitalInfoCore(UInt_t run, TFile *f
1345                          qRoll[sizeqmcmd]=RYPang_upper->Kren;                          qRoll[sizeqmcmd]=RYPang_upper->Kren;
1346                          qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;                          qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1347                          qPitch[sizeqmcmd]=RYPang_upper->Tangazh;                          qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1348                          CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);                        }
1349                          break;                      }
1350                        for(Int_t mu = nt;mu<recSize;mu++){
1351                          if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1352                             if(sqrt(pow(recq0[mu],2)+pow(recq1[mu],2)+pow(recq2[mu],2)+pow(recq3[mu],2))>0.99999){
1353    //                         nt=mu;
1354                               Int_t sizeqmcmd = qtime.size();
1355                               inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1356                               qtime[sizeqmcmd]=recqtime[mu];
1357                               q0[sizeqmcmd]=recq0[mu];
1358                               q1[sizeqmcmd]=recq1[mu];
1359                               q2[sizeqmcmd]=recq2[mu];
1360                               q3[sizeqmcmd]=recq3[mu];
1361                               qmode[sizeqmcmd]=-10;
1362                               orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1363                               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]);
1364                               qRoll[sizeqmcmd]=RYPang_upper->Kren;
1365                               qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1366                               qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1367                             }
1368                          }
1369                          if(recqtime[mu]>=u_time){
1370                             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){
1371                               Int_t sizeqmcmd = qtime.size();
1372                               inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1373                               qtime[sizeqmcmd]=u_time;
1374                               q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1375                               q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1376                               q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1377                               q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1378                               qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1379                               lowerqtime = u_time;
1380                               orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1381                               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]);
1382                               qRoll[sizeqmcmd]=RYPang_upper->Kren;
1383                               qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1384                               qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1385                               CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);
1386                               break;
1387                             }
1388                        }                        }
1389                      }                      }
1390                    }                    }
1391                  }                  }
1392                }                }
1393              }              }
             if ( debug ) printf(" ciccio \n");  
1394            }            }
1395          }          }
1396                    
1397          if(qtime.size()==0){          if(qtime.size()==0){                            // in case if no orientation information in data
1398              for(Int_t my=0;my<recqtime.size();my++){            if ( debug ) cout << "qtime.size() = 0" << endl;
1399                  Int_t sizeqmcmd = qtime.size();            for(UInt_t my=0;my<recqtime.size();my++){
1400                  inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);              if(sqrt(pow(recq0[my],2)+pow(recq1[my],2)+pow(recq2[my],2)+pow(recq3[my],2))>0.99999){
1401                  qtime[sizeqmcmd]=recqtime[my];                Int_t sizeqmcmd = qtime.size();
1402                  q0[sizeqmcmd]=recq0[my];                inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1403                  q1[sizeqmcmd]=recq1[my];                qtime[sizeqmcmd]=recqtime[my];
1404                  q2[sizeqmcmd]=recq2[my];                q0[sizeqmcmd]=recq0[my];
1405                  q3[sizeqmcmd]=recq3[my];                q1[sizeqmcmd]=recq1[my];
1406                  qmode[sizeqmcmd]=-10;                q2[sizeqmcmd]=recq2[my];
1407                  orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);                q3[sizeqmcmd]=recq3[my];
1408                  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]);                qmode[sizeqmcmd]=-10;
1409                  qRoll[sizeqmcmd]=RYPang_upper->Kren;                orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1410                  qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;                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]);
1411                  qPitch[sizeqmcmd]=RYPang_upper->Tangazh;                qRoll[sizeqmcmd]=RYPang_upper->Kren;
1412              }                qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1413                  qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1414                }
1415              }
1416          }          }
1417                    
1418          if ( debug ) printf(" fuffi \n");  
1419          sineparam(q0sine,qtime,q0,qRoll,qPitch,0.60);  //      if ( debug ) printf(" puffi \n");
         sineparam(q1sine,qtime,q1,qRoll,qPitch,0.82);  
         sineparam(q2sine,qtime,q2,qRoll,qPitch,0.82);  
         sineparam(q3sine,qtime,q3,qRoll,qPitch,0.60);  
         sineparam(Yawsine,qtime,qYaw,qRoll,qPitch,4);  
         if ( debug ) printf(" puffi \n");  
1420          Double_t tmin = 9999999999.;          Double_t tmin = 9999999999.;
1421          Double_t tmax = 0.;          Double_t tmax = 0.;
1422          for(UInt_t tre = 0;tre<qtime.size();tre++){          for(UInt_t tre = 0;tre<qtime.size();tre++){
1423            if(qtime[tre]>tmax)tmax = qtime[tre];            if(qtime[tre]>tmax)tmax = qtime[tre];
1424            if(qtime[tre]<tmin)tmin = qtime[tre];            if(qtime[tre]<tmin)tmin = qtime[tre];
1425          }          }
1426          if ( debug ) printf(" gnfuffi \n");          // sorting quaternions by time
1427           Bool_t t = true;
1428            while(t){
1429             t=false;
1430              for(UInt_t i=0;i<qtime.size()-1;i++){
1431                if(qtime[i]>qtime[i+1]){
1432                  Double_t tmpr = qtime[i];
1433                  qtime[i]=qtime[i+1];
1434                  qtime[i+1] = tmpr;
1435                  tmpr = q0[i];
1436                  q0[i]=q0[i+1];
1437                  q0[i+1] = tmpr;
1438                  tmpr = q1[i];
1439                  q1[i]=q1[i+1];
1440                  q1[i+1] = tmpr;
1441                  tmpr = q2[i];
1442                  q2[i]=q2[i+1];
1443                  q2[i+1] = tmpr;
1444                  tmpr = q3[i];
1445                  q3[i]=q3[i+1];
1446                  q3[i+1] = tmpr;
1447                  tmpr = qRoll[i];
1448                  qRoll[i]=qRoll[i+1];
1449                  qRoll[i+1] = tmpr;
1450                  tmpr = qYaw[i];
1451                  qYaw[i]=qYaw[i+1];
1452                  qYaw[i+1] = tmpr;
1453                  tmpr = qPitch[i];
1454                  qPitch[i]=qPitch[i+1];
1455                  qPitch[i+1] = tmpr;
1456                    t=true;
1457                }
1458              }
1459            }
1460    
1461            if ( debug ){
1462              cout << "we have loaded quaternions: size of quaternions set is "<< qtime.size() << endl;
1463             for(UInt_t i=0;i<qtime.size();i++) cout << qtime[i] << "\t";
1464              cout << endl << endl;
1465              Int_t lopu;
1466              cin >> lopu;
1467           }
1468    
         //q0testing->SetName("q0testing");  
         //q1testing->SetName("q1testing");  
         //q2testing->SetName("q2testing");  
         //q3testing->SetName("q3testing");  
           
 //      Int_t ss=10.*(tmax-tmin);  
         //q0testing->SetBins(ss,tmin,tmax,1000,-1.,1.);  
         //Pitchtesting->SetBins(ss,tmin,tmax,1000,-40.,40.);  
   
 //      for(Int_t tre = 0;tre<qtime.size();tre++){  
           //cout<<"q0["<<tre<<" = "<<q0[tre]<<endl;  
           //q0testing->Fill(qtime[tre],q0[tre]);  
           //q1testing->Fill(qtime[tre],q1[tre]);  
           //Pitchtesting->Fill(qtime[tre],qPitch[tre],100);  
           //if(qmode[tre] == -10)Pitchtesting->Fill(qtime[tre],10,100);  
           //q2testing->Fill(qtime[tre],q2[tre],100);  
           //q3testing->Fill(qtime[tre],q3[tre],100);  
 //      }  
           
         //for(Int_t tre=0;tre<q0sine.size();tre++)cout<<q1sine[tre].A<<"*sin("<<q1sine[tre].b<<"x+"<<q1sine[tre].c<<")\t time start: "<<q1sine[tre].startPoint<<"\ttime end: "<<q1sine[tre].finishPoint<<endl;  
         //for(Int_t tre=0;tre<q0sine.size();tre++)cout<<q1sine[tre].A<<"*sin("<<q1sine[tre].b<<"x+"<<q1sine[tre].c<<")\t time start: "<<q0sine[tre].startPoint<<"\ttime end: "<<q0sine[tre].finishPoint<<endl;  
1469        } // if we processed first event        } // if we processed first event
1470    
1471                
1472        //Filling Inclination information        //Filling Inclination information
1473        Double_t incli = 0;        Double_t incli = 0;
1474        if ( qtime.size() > 1 ){        if ( qtime.size() > 1 ){
1475        for(UInt_t mu = must;mu<qtime.size()-1;mu++){          if(atime<qtime[0]){
1476          if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);            for(UInt_t mu = 1;mu<qtime.size()-1;mu++){
1477          if(qtime[mu+1]>qtime[mu]){              if(qtime[mu]>qtime[0]){
1478            if ( debug ) printf(" grfuffi2 %i \n",mu);                incli = (qPitch[mu]-qPitch[0])/(qtime[mu]-qtime[0]);
1479            if(atime<=qtime[mu+1] && atime>=qtime[mu]){                orbitalinfo->theta =  incli*atime+qPitch[mu]-incli*qtime[mu];
1480              must = mu;                incli = (qRoll[mu]-qRoll[0])/(qtime[mu]-qtime[0]);
1481              incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);                orbitalinfo->etha =  incli*atime+qRoll[mu]-incli*qtime[mu];
1482              orbitalinfo->theta =  incli*atime+qPitch[mu+1]-incli*qtime[mu+1];                incli = (qYaw[mu]-qYaw[0])/(qtime[mu]-qtime[0]);
1483              incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);                orbitalinfo->phi =  incli*atime+qYaw[mu]-incli*qtime[mu];
1484              orbitalinfo->etha =  incli*atime+qRoll[mu+1]-incli*qtime[mu+1];                
1485              incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);                incli = (q0[mu]-q0[0])/(qtime[mu]-qtime[0]);
1486              orbitalinfo->phi =  incli*atime+qYaw[mu+1]-incli*qtime[mu+1];                orbitalinfo->q0 =  incli*atime+q0[mu]-incli*qtime[mu];
1487                              incli = (q1[mu]-q1[0])/(qtime[mu]-qtime[0]);
1488              incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);                orbitalinfo->q1 =  incli*atime+q1[mu]-incli*qtime[mu];
1489              orbitalinfo->q0t =  incli*atime+q0[mu+1]-incli*qtime[mu+1];                incli = (q2[mu]-q2[0])/(qtime[mu]-qtime[0]);
1490              incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);                orbitalinfo->q2 =  incli*atime+q2[mu]-incli*qtime[mu];
1491              orbitalinfo->q1t =  incli*atime+q1[mu+1]-incli*qtime[mu+1];                incli = (q3[mu]-q3[0])/(qtime[mu]-qtime[0]);
1492              incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);                orbitalinfo->q3 =  incli*atime+q3[mu]-incli*qtime[mu];
1493              orbitalinfo->q2t =  incli*atime+q2[mu+1]-incli*qtime[mu+1];                orbitalinfo->TimeGap=qtime[0]-atime;
1494              incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);                break;
1495              orbitalinfo->q3t =  incli*atime+q3[mu+1]-incli*qtime[mu+1];              }
1496                          }
1497              orbitalinfo->TimeGap = qtime[mu+1]-qtime[mu];          }
1498              orbitalinfo->mode = qmode[mu+1];          Float_t eend=qtime.size()-1;
1499              if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;          if(atime>qtime[eend]){
1500              if(qmode[mu+1]==-10 || qmode[mu+1]==0 || qmode[mu+1]==1 || qmode[mu+1]==3 || qmode[mu+1]==4 || qmode[mu+1]==6){            for(UInt_t mu=eend-1;mu>=0;mu--){
1501                //linear interpolation              if(qtime[mu]<qtime[eend]){
1502                incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);                incli = (qPitch[eend]-qPitch[mu])/(qtime[eend]-qtime[mu]);
1503                orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];                orbitalinfo->theta =  incli*atime+qPitch[eend]-incli*qtime[eend];
1504                incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);                incli = (qRoll[eend]-qRoll[mu])/(qtime[eend]-qtime[mu]);
1505                orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];                orbitalinfo->etha =  incli*atime+qRoll[eend]-incli*qtime[eend];
1506                incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);                incli = (qYaw[eend]-qYaw[mu])/(qtime[eend]-qtime[mu]);
1507                orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];                orbitalinfo->phi =  incli*atime+qYaw[eend]-incli*qtime[eend];
1508                incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);            
1509                orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];                incli = (q0[eend]-q0[mu])/(qtime[eend]-qtime[mu]);
1510              }else{                orbitalinfo->q0 =  incli*atime+q0[eend]-incli*qtime[eend];
1511                //sine interpolation                incli = (q1[eend]-q1[mu])/(qtime[eend]-qtime[mu]);
1512                for(UInt_t mt=0;mt<q0sine.size();mt++){                orbitalinfo->q1 =  incli*atime+q1[eend]-incli*qtime[eend];
1513                  if(atime<=q0sine[mt].finishPoint && atime>=q0sine[mt].startPoint){                incli = (q2[eend]-q2[mu])/(qtime[eend]-qtime[mu]);
1514                    if(!q0sine[mt].NeedFit)orbitalinfo->q0=q0sine[mt].A*sin(q0sine[mt].b*atime+q0sine[mt].c);else{                orbitalinfo->q2 =  incli*atime+q2[eend]-incli*qtime[eend];
1515                      incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);                incli = (q3[eend]-q3[mu])/(qtime[eend]-qtime[mu]);
1516                      orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];                orbitalinfo->q3 =  incli*atime+q3[eend]-incli*qtime[eend];
1517                    }                break;
1518                  }              }
1519                  if(atime<=q1sine[mt].finishPoint && atime>=q1sine[mt].startPoint){            }
1520                    if(!q1sine[mt].NeedFit)orbitalinfo->q1=q1sine[mt].A*sin(q1sine[mt].b*atime+q1sine[mt].c);else{          }
1521                      incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);          for(UInt_t mu = must;mu<qtime.size()-1;mu++){
1522                      orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];            if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);
1523                    }            if(qtime[mu+1]>qtime[mu]){
1524                  }              if ( debug ) cout << "qtime[" << mu << "] = " << qtime[mu] << "\tqtime[" << mu+1 << "] = " << qtime[mu+1] << "\tatime = " << atime << endl;
1525                  if(atime<=q2sine[mt].finishPoint && atime>=q2sine[mt].startPoint){              if(atime<=qtime[mu+1] && atime>=qtime[mu]){
1526                    if(!q2sine[mt].NeedFit)orbitalinfo->q2=q0sine[mt].A*sin(q2sine[mt].b*atime+q2sine[mt].c);else{                if ( debug ) cout << "here we have found proper quaternions for interpolation: mu = "<<mu<<endl;
1527                      incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);                must = mu;
1528                      orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];                incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);
1529                    }                orbitalinfo->theta =  incli*atime+qPitch[mu+1]-incli*qtime[mu+1];
1530                  }                incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);
1531                  if(atime<=q3sine[mt].finishPoint && atime>=q3sine[mt].startPoint){                orbitalinfo->etha =  incli*atime+qRoll[mu+1]-incli*qtime[mu+1];
1532                    if(!q3sine[mt].NeedFit)orbitalinfo->q3=q0sine[mt].A*sin(q3sine[mt].b*atime+q3sine[mt].c);else{                incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1533                      incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);                orbitalinfo->phi =  incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1534                      orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];                
1535                    }                incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1536                  }                orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];
1537                  if(atime<=Yawsine[mt].finishPoint && atime>=Yawsine[mt].startPoint){                incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1538                    if(!Yawsine[mt].NeedFit)orbitalinfo->phi=Yawsine[mt].A*sin(Yawsine[mt].b*atime+Yawsine[mt].c);else{                orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];
1539                      incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);                incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1540                      orbitalinfo->phi =  incli*atime+qYaw[mu+1]-incli*qtime[mu+1];                orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];
1541                    }                incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1542                  }                orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];
1543                }                Float_t tg = (qtime[mu+1]-qtime[mu])/1000.0;
1544              }                if(tg>=1) tg=0.00;
1545              //q0testing->Fill(atime,orbitalinfo->q0,100);                orbitalinfo->TimeGap = TMath::Min(TMath::Abs(qtime[mu+1]-atime),TMath::Abs(atime-qtime[mu]))+tg;//qtime[mu+1]-qtime[mu];
1546              //q1testing->Fill(atime,orbitalinfo->q1,100);                orbitalinfo->mode = qmode[mu+1];
1547              //Pitchtesting->Fill(atime,orbitalinfo->etha);                //if(atime==qtime[mu] || atime==qtime[mu+1]) orbitalinfo->qkind = 0; else orbitalinfo->qkind=1;
1548              //q2testing->Fill(atime,orbitalinfo->q2);                //if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;
1549              //q3testing->Fill(atime,orbitalinfo->q3);                if ( debug ) printf(" grfuffi4 %i \n",mu);
1550              break;                break;
1551            }              }
1552          }            }
1553        }          }
1554        }        }
1555          if ( debug ) printf(" grfuffi5  \n");
1556        //        //
1557        // ops no inclination information        // ops no inclination information
1558        //        //
1559          
1560        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 ){
1561            if (debug) cout << "Warning: no iclination information "<< endl;
1562          orbitalinfo->mode = 10;          orbitalinfo->mode = 10;
1563          orbitalinfo->q0 = -1000.;          orbitalinfo->q0 = -1000.;
1564          orbitalinfo->q1 = -1000.;          orbitalinfo->q1 = -1000.;
# Line 1126  int OrbitalInfoCore(UInt_t run, TFile *f Line 1567  int OrbitalInfoCore(UInt_t run, TFile *f
1567          orbitalinfo->etha = -1000.;          orbitalinfo->etha = -1000.;
1568          orbitalinfo->phi = -1000.;          orbitalinfo->phi = -1000.;
1569          orbitalinfo->theta = -1000.;          orbitalinfo->theta = -1000.;
1570        };          orbitalinfo->TimeGap = -1000.;
1571            TMatrixD Iij(3,3);
1572            Iij(0,0)=0; Iij(0,1)=0; Iij(0,2)=0;
1573            Iij(1,0)=0; Iij(1,1)=0; Iij(1,2)=0;
1574            Iij(2,0)=0; Iij(2,1)=0; Iij(2,2)=0;
1575            orbitalinfo->Iij.ResizeTo(Iij);
1576            orbitalinfo->Iij = Iij;
1577            
1578            //orbitalinfo->qkind = -1000;
1579            
1580            //      if ( debug ){
1581            //        Int_t lopu;
1582            //         cin >> lopu;
1583            //      }
1584            if ( debug ) printf(" grfuffi6 \n");
1585          }
1586        //        //
1587          if ( debug ) printf(" filling \n");
1588        // #########################################################################################################################          // #########################################################################################################################  
1589        //        //
1590        // fill orbital positions        // fill orbital positions
# Line 1138  int OrbitalInfoCore(UInt_t run, TFile *f Line 1595  int OrbitalInfoCore(UInt_t run, TFile *f
1595        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);
1596        lat = rad2deg(coo.m_Lat);        lat = rad2deg(coo.m_Lat);
1597        alt = coo.m_Alt;        alt = coo.m_Alt;
1598        //  
1599          cOrbit orbits2(*gltle->GetTle());
1600          orbits2.getPosition((double) (atime - gltle->GetFromTime())/60., &eCi);
1601          //      Float_t x=eCi.getPos().m_x;
1602          //      Float_t y=eCi.getPos().m_y;
1603          //      Float_t z=eCi.getPos().m_z;
1604          
1605          TVector3 V(eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z);
1606          TVector3 Pos(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z);
1607          
1608          Float_t dlon=Pos.Phi()*TMath::RadToDeg()-lon;
1609          
1610          Pos.RotateZ(-dlon*TMath::DegToRad());
1611          V.RotateZ(-dlon*TMath::DegToRad());
1612          Float_t diro;
1613          if(V.Z()>0) diro=1; else diro=-1;
1614          
1615          // 10REDNEW
1616          Int_t errq=0;
1617          Int_t azim=0;
1618          Int_t qual=0;
1619          Int_t MU=0;
1620          for(UInt_t mu = 0;mu<RTtime2.size()-1;mu++){
1621            if(atime<RTstart[mu+1] && atime>=RTstart[mu]){
1622              errq=RTerrq[mu];
1623              azim=RTazim[mu];
1624              qual=RTqual[mu];
1625              MU=mu;
1626              break;
1627            }
1628          }
1629          orbitalinfo->errq = errq;
1630          orbitalinfo->azim = azim;
1631          orbitalinfo->rtqual=qual;
1632          orbitalinfo->qkind = 0;
1633          
1634          if ( debug ) printf(" coord done \n");
1635        if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){          if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){  
         //        
1636          orbitalinfo->lon = lon;          orbitalinfo->lon = lon;
1637          orbitalinfo->lat = lat;          orbitalinfo->lat = lat;
1638          orbitalinfo->alt = alt ;          orbitalinfo->alt = alt;
1639            orbitalinfo->V = V;
1640    
1641            //      GMtype_CoordGeodetic  location;
1642            location.lambda = lon;
1643            location.phi = lat;
1644            location.HeightAboveEllipsoid = alt;
1645    
1646            GM_GeodeticToSpherical(Ellip, location, &CoordSpherical);
1647            GM_SphericalToCartesian(CoordSpherical,  &CoordCartesian);
1648            GM_EarthCartToDipoleCartCD(Pole, CoordCartesian, &DipoleCartesian);
1649            GM_CartesianToSpherical(DipoleCartesian, &DipoleSpherical);
1650            orbitalinfo->londip = DipoleSpherical.lambda;
1651            orbitalinfo->latdip = DipoleSpherical.phig;
1652    
1653            if(debug)cout<<"geodetic:\t"<<lon<<"\t"<<lat<<"\tgeomagnetic:\t"<<orbitalinfo->londip<<"\t"<<orbitalinfo->latdip<<endl;
1654    
1655          //          //
1656          // compute mag field components and L shell.          // compute mag field components and L shell.
1657          //          //
1658            if ( debug ) printf(" call igrf feldg \n");
1659          feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);          feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);
1660            if ( debug ) printf(" call igrf shellg \n");
1661          shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);          shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);
1662            if ( debug ) printf(" call igrf findb \n");
1663          findb0_(&stps, &bdel, &value, &bequ, &rr0);          findb0_(&stps, &bdel, &value, &bequ, &rr0);
1664          //          //
1665            if ( debug ) printf(" done igrf \n");
1666          orbitalinfo->Bnorth = bnorth;          orbitalinfo->Bnorth = bnorth;
1667          orbitalinfo->Beast = beast;          orbitalinfo->Beast = beast;
1668          orbitalinfo->Bdown = bdown;          orbitalinfo->Bdown = bdown;
1669          orbitalinfo->Babs = babs;          orbitalinfo->Babs = babs;
1670            orbitalinfo->M = dimo;
1671          orbitalinfo->BB0 = babs/bequ;          orbitalinfo->BB0 = babs/bequ;
1672          orbitalinfo->L = xl;                orbitalinfo->L = xl;      
1673          // Set Stormer vertical cutoff using L shell.          // Set Stormer vertical cutoff using L shell.
1674          orbitalinfo->cutoffsvl = 14.9/(xl*xl);          orbitalinfo->cutoffsvl = 14.295 / (xl*xl); //
1675            if(debug)cout << "L = " << xl << "\tM = " << dimo << "\tvertical cutoff:  "<< orbitalinfo->cutoffsvl << endl;
1676    
1677            
1678    //           ---------- Forwarded message ----------
1679    //           Date: Wed, 09 May 2012 12:16:47 +0200
1680    //           From: Alessandro Bruno <alessandro.bruno@ba.infn.it>
1681    //           To: Mirko Boezio <mirko.boezio@ts.infn.it>
1682    //           Cc: Francesco S. Cafagna <Francesco.Cafagna@ba.infn.it>
1683    //           Subject: Störmer vertical cutoff
1684    
1685    //           Ciao Mirko,
1686    //           volevo segnalarti che il valore dello Störmer vertical cutoff nel Level2 è
1687    //           sovrastimato di circa il 4%.
1688    //           Dopo un'approfondita analisi con l'IGRF-05 abbiamo ricavano un valore pari
1689    //           a: 14.295 / L^2 anzichè 14.9 / L^2, valore obsoleto in quanto riferito agli
1690    //           anni '50.
1691    //        
1692            //14.9/(xl*xl);
1693            orbitalinfo->igrf_icode = (Float_t)icode;
1694          //          //
1695        };              }  //check lon lat alt      
1696        //        //
1697        if ( debug ) printf(" pitch angle \n");        if ( debug ) printf(" pitch angle \n");
1698        //        //
1699        // pitch angles        // pitch angles
1700        //        //
1701        if ( orbitalinfo->mode != 10 && orbitalinfo->mode != 5 && orbitalinfo->mode !=7 && orbitalinfo->mode != 9 ){        if( orbitalinfo->TimeGap>=0){
1702          //          //
1703          Float_t Bx = -orbitalinfo->Bdown;                       //don't need for PamExp ExpOnly for all geography areas          if ( debug ) printf(" timegap %f \n",orbitalinfo->TimeGap);
1704          Float_t By = orbitalinfo->Beast;                        //don't need for PamExp ExpOnly for all geography areas          Float_t Bx = -orbitalinfo->Bdown;
1705          Float_t Bz = orbitalinfo->Bnorth;                       //don't need for PamExp ExpOnly for all geography areas          Float_t By = orbitalinfo->Beast;
1706          //          Float_t Bz = orbitalinfo->Bnorth;
1707          TMatrixD Fij = PO->ECItoGreenwich(PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3),orbitalinfo->absTime);  
1708          TMatrixD Dij = PO->GreenwichtoGEO(orbitalinfo->lat,orbitalinfo->lon,Fij);  //      TMatrixD Qiji(3,3);
1709            TMatrixD Qij = PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3);
1710            TMatrixD Dij = PO->ECItoGEO(Qij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon);
1711    
1712    //10REDNEW
1713            // If initial orientation data have reason to be inaccurate
1714           Float_t tg = 0.00;
1715           Float_t tmptg;
1716            Bool_t tgpar=false;
1717            Bool_t tgpar0=false;
1718            if (orbitalinfo->TimeGap>10.0 || ((modf(orbitalinfo->TimeGap,&tmptg)*1000>10 || modf(orbitalinfo->TimeGap,&tmptg)*1000==0.0) && orbitalinfo->TimeGap>2.0)) tgpar=true;
1719            if (orbitalinfo->TimeGap>180.0) tgpar0=true;
1720           if(MU!=0){
1721    //      if(orbitalinfo->TimeGap>0 && errq==0 && azim==0){               // 10RED CHECK  (comparison between three metod of recovering orientation)
1722    
1723           if((atime>=RTstart[MU] && atime<RTstart[MU+1] && RTbank1[MU]==0 && RTbank2[MU]==0 && (TMath::Abs(orbitalinfo->etha)>0.1 || tgpar0)) || ((RTbank1[MU]!=0 || RTbank2[MU]!=0) && atime>=RTstart[MU] && atime<RTstart[MU+1] && azim==0 && (errq!=0 || tgpar))){
1724    
1725            //found in Rotation Table this data for this time interval
1726            if(atime<RTtime1[0])
1727              orbitalinfo->azim = 5;        //means that RotationTable no started yet
1728           else{
1729                    // search for angle betwean velosity and direction to north in tangential to Earth surfase plane in satellite position
1730    
1731                  Double_t bank=RTstart[MU];
1732                  Double_t tlat=orbitalinfo->lat;
1733    
1734                  tg=modf(orbitalinfo->TimeGap,&tg)*1000;
1735    
1736                  if(atime>=RTpluto1[MU] && atime<=RTpluto2[MU]){
1737                    Double_t kar=(RTbank2[MU]-RTbank1[MU])/(RTtime2[MU]-RTtime1[MU]);
1738                    Double_t bak=RTbank1[MU]-kar*RTtime1[MU];
1739                    bank=kar*atime+bak;
1740                  }
1741                  if(atime>=RTstart[MU] && atime<RTpluto1[MU]){
1742                     Double_t s_dBdt2=(RTbpluto1[MU]-RTbank1[MU])/(Int_t)(RTpluto1[MU]-RTstart[MU]);
1743                     Double_t s_t2=((Double_t)RTpluto1[MU]+(Double_t)RTstart[MU])/2. - RTstart[MU];
1744                     Double_t s_t1=RTstart[MU]-RTstart[MU];
1745                     Double_t s_k=s_dBdt2/(s_t2-s_t1);
1746                     Double_t s_b=-s_k*s_t1;
1747                     Double_t s_t3=RTpluto1[MU]-RTstart[MU];
1748                     Double_t s_b3=RTbpluto1[MU];
1749                     Double_t s_c=s_b3-0.5*s_k*s_t3*s_t3 -s_b*s_t3;
1750                     bank=0.5*s_k*(atime-RTstart[MU])*(atime-RTstart[MU]) + s_b*(atime-RTstart[MU]) + s_c;
1751                 }
1752                  if(atime>RTpluto2[MU] && atime<=RTstart[MU+1]){
1753                     Double_t s_dBdt2=(RTbpluto2[MU] - RTbank2[MU])/(Int_t)(RTpluto2[MU]-RTstart[MU+1]);
1754                     Double_t s_t2=((Double_t)RTpluto2[MU]+(Double_t)RTstart[MU+1])/2. - RTstart[MU];
1755                     Double_t s_t1=RTstart[MU+1]-RTstart[MU];
1756                     Double_t s_k=s_dBdt2/(s_t2-s_t1);
1757                     Double_t s_b=-s_k*s_t1;
1758                     Double_t s_t3=RTpluto2[MU]-RTstart[MU];
1759                     Double_t s_b3=RTbpluto2[MU];
1760                     Double_t s_c=s_b3-0.5*s_k*s_t3*s_t3 -s_b*s_t3;
1761                   bank=0.5*s_k*(atime-RTstart[MU])*(atime-RTstart[MU]) + s_b*(atime-RTstart[MU]) + s_c;
1762                 }
1763                  if(TMath::Abs(orbitalinfo->etha-bank)>0.1){
1764                    orbitalinfo->etha=bank;
1765                    Double_t spitch = 0.00001;  // temprary not zero to avoid problem with tranzition from Euler angles to orientation matrix
1766    
1767                    //Estimations of pitch angle of satellite
1768                    if(TMath::Abs(bank)>0.7){
1769                       Float_t spitch1=TMath::DegToRad()*0.7*diro;//RTdir1[MU];
1770                       Float_t spitch2=TMath::DegToRad()*0.7*diro;//RTdir2[MU];
1771                       Float_t kva=(spitch2-spitch1)/(RTtime2[MU]-RTtime1[MU]);
1772                       Float_t bva=spitch1-kva*RTtime1[MU];
1773                       spitch=kva*atime+bva;
1774                    }
1775    
1776                    //Calculate Yaw angle accordingly with fit, see picture FitYaw.jpg
1777                    Double_t yaw=0.00001;  // temprary not zero to avoid problem with tranzition from Euler angles to orientation matrix
1778                    if(TMath::Abs(tlat)<70)
1779                      yaw = -3.7e-8*tlat*tlat*tlat*tlat + 1.4e-7*tlat*tlat*tlat - 0.0005*tlat*tlat - 0.00025*tlat + 3.6;
1780                    yaw = diro*yaw; //because should be different sign for ascending and descending orbits!
1781                    orbitalinfo->phi=yaw;
1782    
1783                    if(TMath::Abs(bank)>0.5 && TMath::Abs(yaw-orbitalinfo->phi)<3.0) yaw=orbitalinfo->phi;
1784    
1785    //              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
1786                    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
1787                    orbitalinfo->qkind = 1;
1788                 }
1789    
1790              //Qij = PO->GEOtoECI(Dij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon); // to convert from Dij to Qij
1791    
1792            } // end of if(atime<RTtime1[0]
1793            } // end of (((orbitalinfo->TimeGap>60.0 && TMath...
1794           } // end of MU~=0
1795    
1796            TMatrixD qij = PO->ColPermutation(Qij);
1797            TMatrixD Fij = PO->ECItoGreenwich(Qij,orbitalinfo->absTime);
1798            TMatrixD Gij = PO->ColPermutation(Fij);
1799            Dij = PO->ECItoGEO(Qij,orbitalinfo->absTime,orbitalinfo->lat,orbitalinfo->lon);
1800          TMatrixD Iij = PO->ColPermutation(Dij);          TMatrixD Iij = PO->ColPermutation(Dij);
1801          //  
1802            TVector3 SP = PO->GetSunPosition(orbitalinfo->absTime);
1803            // go to Pamela reference frame from Resurs reference frame
1804            Float_t tmpy = SP.Y();
1805            SP.SetY(SP.Z());
1806            SP.SetZ(-tmpy);
1807            TVector3 SunZenith;
1808            SunZenith.SetMagThetaPhi(1,23.439281*TMath::DegToRad(),TMath::Pi()/2.);
1809            TVector3 SunMag = -SP;
1810            SunMag.Rotate(-45*TMath::DegToRad(),SunZenith);
1811            tmpy=SunMag.Y();
1812            SunMag.SetY(SunMag.Z());
1813            SunMag.SetZ(-tmpy);
1814    
1815          orbitalinfo->Iij.ResizeTo(Iij);          orbitalinfo->Iij.ResizeTo(Iij);
1816          orbitalinfo->Iij = Iij;          orbitalinfo->Iij = Iij;
1817    
1818            Bool_t saso=true;
1819            if (orbitalinfo->qkind==1) saso=true;
1820            if (orbitalinfo->qkind==0 && orbitalinfo->azim>0 && orbitalinfo->azim!=5 && tgpar) saso=false;
1821            if (orbitalinfo->qkind==0 && orbitalinfo->azim==5 && TMath::Abs(orbitalinfo->etha)>0.1 && tgpar) saso=false;
1822            if (orbitalinfo->qkind==0 && orbitalinfo->azim==5 && TMath::Abs(orbitalinfo->etha)<=0.1 && tgpar0) saso=false;
1823            if (saso) orbitalinfo->mode=orbitalinfo->rtqual; else orbitalinfo->mode=2;
1824    
1825            //
1826            //      A1 = Iij(0,2);
1827            //      A2 = Iij(1,2);
1828            //      A3 = Iij(2,2);
1829          //          //
         A1 = Iij(0,2);  
         A2 = Iij(1,2);  
         A3 = Iij(2,2);  
         //        
1830          //      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
1831          //      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
1832          //          //
1833          if ( !standalone && tof->ntrk() > 0 ){          if ( debug ) printf(" matrixes done  \n");
1834            if ( !standalone ){
1835              if ( debug ) printf(" !standalone \n");
1836            //            //
1837              // Standard tracking algorithm
1838              //
1839            Int_t nn = 0;            Int_t nn = 0;
1840              if ( verbose ) printf(" standard tracking \n");
1841            for(Int_t nt=0; nt < tof->ntrk(); nt++){              for(Int_t nt=0; nt < tof->ntrk(); nt++){  
1842              //              //
1843              ToFTrkVar *ptt = tof->GetToFTrkVar(nt);              ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1844                if (debug) cout<<"tof->ntrk() = "<<tof->ntrk()<<"\tptt->trkseqno = "<<ptt->trkseqno<<"\ttrke->ntrk() = "<<trke->ntrk()<<endl;
1845              Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];              Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1846              Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];              Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1847              Double_t E11z = zin[0];              Double_t E11z = zin[0];
# Line 1199  int OrbitalInfoCore(UInt_t run, TFile *f Line 1849  int OrbitalInfoCore(UInt_t run, TFile *f
1849              Double_t E22y = ptt->ytr_tof[3];//tr->y[3];              Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1850              Double_t E22z = zin[3];              Double_t E22z = zin[3];
1851              if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){              if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1852                  TrkTrack *mytrack = trke->GetStoredTrack(ptt->trkseqno);
1853                  Float_t rig=1/mytrack->GetDeflection();
1854                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));
1855                //              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;  
1856                Px = (E22x-E11x)/norm;                Px = (E22x-E11x)/norm;
1857                Py = (E22y-E11y)/norm;                Py = (E22y-E11y)/norm;
1858                Pz = (E22z-E11z)/norm;                Pz = (E22z-E11z)/norm;
# Line 1215  int OrbitalInfoCore(UInt_t run, TFile *f Line 1863  int OrbitalInfoCore(UInt_t run, TFile *f
1863                t_orb->Eij.ResizeTo(Eij);                t_orb->Eij.ResizeTo(Eij);
1864                t_orb->Eij = Eij;                t_orb->Eij = Eij;
1865                //                //
1866                TMatrixD Sij = PO->PamelatoGEO(Fij,Px,Py,Pz);                TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1867                t_orb->Sij.ResizeTo(Sij);                t_orb->Sij.ResizeTo(Sij);
1868                t_orb->Sij = Sij;                t_orb->Sij = Sij;
1869                //                            //            
1870                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);
1871                //                //
1872                  // SunPosition in instrumental reference frame
1873                  TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1874                  TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1875                  t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1876                  t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1877                //                //
               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);  
1878                //                //
1879                t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow((1+sqrt(1-pow(orbitalinfo->L,-3/2)*cos(omega))),2));                Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1880                  TVector3 Bxy(0,By,Bz);
1881                  TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1882                  Double_t dzeta=Bxy.Angle(Exy);
1883                  if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1884                  
1885                  if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1886    
1887                  // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1888                  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));
1889                  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));
1890                  if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1891    
1892                //                //
1893                if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;                if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1894                if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;                if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1895                  if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1896                  if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1897                //                //
1898                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);
1899                //                //
# Line 1236  int OrbitalInfoCore(UInt_t run, TFile *f Line 1902  int OrbitalInfoCore(UInt_t run, TFile *f
1902                //                //
1903                t_orb->Clear();                t_orb->Clear();
1904                //                //
1905              };              }
1906              //              //
1907            };            } // end standard tracking algorithm
1908    
1909              //
1910              // Code for extended tracking algorithm:
1911              //
1912              if ( hasNucleiTrk ){
1913                Int_t ttentry = 0;
1914                if ( verbose ) printf(" hasNucleiTrk \n");
1915                for(Int_t nt=0; nt < tcNucleiTof->GetEntries() ; nt++){  
1916                  //
1917                  if ( verbose ) printf(" got1\n");
1918                  ToFTrkVar *ptt = (ToFTrkVar*)(tcNucleiTof->At(nt));
1919                  if (verbose) cout<<" tcNucleiTof->GetEntries() = "<<tcNucleiTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1920                  Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1921                  Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1922                  Double_t E11z = zin[0];
1923                  Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1924                  Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1925                  Double_t E22z = zin[3];
1926                  if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1927                    TrkTrack *mytrack = (TrkTrack*)(tcNucleiTrk->At(ptt->trkseqno));
1928                    if ( verbose ) printf(" got tcNucleiTrk \n");
1929                    Float_t rig=1/mytrack->GetDeflection();
1930                    Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1931                    //
1932                    Px = (E22x-E11x)/norm;
1933                    Py = (E22y-E11y)/norm;
1934                    Pz = (E22z-E11z)/norm;
1935                    //
1936                    t_orb->trkseqno = ptt->trkseqno;
1937                    //
1938                    TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1939                    t_orb->Eij.ResizeTo(Eij);      
1940                    t_orb->Eij = Eij;      
1941                    //
1942                    TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1943                    t_orb->Sij.ResizeTo(Sij);
1944                    t_orb->Sij = Sij;
1945                    //          
1946                    t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1947                    //
1948                    // SunPosition in instrumental reference frame
1949                    TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
1950                    TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
1951                    t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
1952                    t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
1953                    //
1954                    //
1955                    Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
1956                    TVector3 Bxy(0,By,Bz);
1957                    TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
1958                    Double_t dzeta=Bxy.Angle(Exy);
1959                    if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
1960                    
1961                    if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
1962                    
1963                    // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
1964                    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));
1965                    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));
1966                    if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
1967                    
1968                    //
1969                    if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1970                    if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1971                    if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
1972                    if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
1973                    //
1974                    if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1975                    //
1976                    TClonesArray &tt1 = *torbNucleiTrk;
1977                    new(tt1[ttentry]) OrbitalInfoTrkVar(*t_orb);
1978                    ttentry++;
1979                    //
1980                    t_orb->Clear();
1981                    //
1982                  }
1983                  //
1984                }
1985              } // end standard tracking algorithm: nuclei
1986              if ( hasExtNucleiTrk ){
1987                Int_t ttentry = 0;
1988                if ( verbose ) printf(" hasExtNucleiTrk \n");
1989                for(Int_t nt=0; nt < tcExtNucleiTof->GetEntries() ; nt++){  
1990                  //
1991                  if ( verbose ) printf(" got2\n");
1992                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtNucleiTof->At(nt));
1993                  if (verbose) cout<<" tcExtNucleiTof->GetEntries() = "<<tcExtNucleiTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
1994                  Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1995                  Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1996                  Double_t E11z = zin[0];
1997                  Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1998                  Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1999                  Double_t E22z = zin[3];
2000                  if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
2001                    ExtTrack *mytrack = (ExtTrack*)(tcExtNucleiTrk->At(ptt->trkseqno));
2002                    if ( verbose ) printf(" got tcExtNucleiTrk \n");
2003                    Float_t rig=1/mytrack->GetDeflection();
2004                    Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
2005                    //
2006                    Px = (E22x-E11x)/norm;
2007                    Py = (E22y-E11y)/norm;
2008                    Pz = (E22z-E11z)/norm;
2009                    //
2010                    t_orb->trkseqno = ptt->trkseqno;
2011                    //
2012                    TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
2013                    t_orb->Eij.ResizeTo(Eij);      
2014                    t_orb->Eij = Eij;      
2015                    //
2016                    TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
2017                    t_orb->Sij.ResizeTo(Sij);
2018                    t_orb->Sij = Sij;
2019                    //          
2020                    t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
2021                    //
2022                    // SunPosition in instrumental reference frame
2023                    TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
2024                    TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
2025                    t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
2026                    t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
2027                    //
2028                    //
2029                    Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
2030                    TVector3 Bxy(0,By,Bz);
2031                    TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
2032                    Double_t dzeta=Bxy.Angle(Exy);
2033                    if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
2034                    
2035                    if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
2036                    
2037                    // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
2038                    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));
2039                    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));
2040                    if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
2041                    
2042                    //
2043                    if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
2044                    if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
2045                    if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
2046                    if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
2047                    //
2048                    if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
2049                    //
2050                    TClonesArray &tt2 = *torbExtNucleiTrk;
2051                    new(tt2[ttentry]) OrbitalInfoTrkVar(*t_orb);
2052                    ttentry++;
2053                    //
2054                    t_orb->Clear();
2055                    //
2056                  }
2057                  //
2058                }
2059              } // end standard tracking algorithm: nuclei extended
2060             if ( hasExtTrk ){
2061                Int_t ttentry = 0;
2062                if ( verbose ) printf(" hasExtTrk \n");
2063                for(Int_t nt=0; nt < tcExtTof->GetEntries() ; nt++){  
2064                  //
2065                  if ( verbose ) printf(" got3\n");
2066                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtTof->At(nt));
2067                  if (verbose) cout<<" tcExtTof->GetEntries() = "<<tcExtTof->GetEntries()<<"\tptt->trkseqno = "<<ptt->trkseqno<<endl;
2068                  Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
2069                  Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
2070                  Double_t E11z = zin[0];
2071                  Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
2072                  Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
2073                  Double_t E22z = zin[3];
2074                  if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
2075                    ExtTrack *mytrack = (ExtTrack*)(tcExtTrk->At(ptt->trkseqno));
2076                    if ( verbose ) printf(" got tcExtTrk \n");
2077                    Float_t rig=1/mytrack->GetDeflection();
2078                    Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
2079                    //
2080                    Px = (E22x-E11x)/norm;
2081                    Py = (E22y-E11y)/norm;
2082                    Pz = (E22z-E11z)/norm;
2083                    //
2084                    t_orb->trkseqno = ptt->trkseqno;
2085                    //
2086                    TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
2087                    t_orb->Eij.ResizeTo(Eij);      
2088                    t_orb->Eij = Eij;      
2089                    //
2090                    TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
2091                    t_orb->Sij.ResizeTo(Sij);
2092                    t_orb->Sij = Sij;
2093                    //          
2094                    t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
2095                    //
2096                    // SunPosition in instrumental reference frame
2097                    TMatrixD Kij = PO->PamelatoGEO(qij,Px,Py,Pz);
2098                    TMatrixD Lij = PO->PamelatoGEO(qij,0,0,1);
2099                    t_orb->sunangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),-SP.X(),-SP.Y(),-SP.Z());
2100                    t_orb->sunmagangle=(Float_t)PO->GetPitchAngle(Kij(0,0),Kij(1,0),Kij(2,0),SunMag.X(),SunMag.Y(),SunMag.Z());
2101                    //
2102                    //
2103                    Double_t omega = PO->GetPitchAngle(-Eij(0,0),-Eij(1,0),-Eij(2,0),1,0,0) * TMath::DegToRad();
2104                    TVector3 Bxy(0,By,Bz);
2105                    TVector3 Exy(0,-Eij(1,0),-Eij(2,0));
2106                    Double_t dzeta=Bxy.Angle(Exy);
2107                    if (-Eij(1,0) < 0) dzeta=2.0*TMath::Pi() - dzeta;
2108                    
2109                    if(debug) cout << "omega = "<<omega*TMath::RadToDeg()<<"\tdzeta = "<<dzeta*TMath::RadToDeg()<<endl;
2110                    
2111                    // Formula from D.F. Smart *, M.A. Shea [2005]; A review of geomagnetic cutoff rigidities for earth-orbiting spacecraft
2112                    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));
2113                    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));
2114                    if (debug) cout << "R = " << rig << "\tcutoff = " << t_orb->cutoff << endl;
2115                    
2116                    //
2117                    if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
2118                    if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
2119                    if ( t_orb->sunangle != t_orb->sunangle ) t_orb->sunangle = -1000.;
2120                    if ( t_orb->sunmagangle != t_orb->sunmagangle ) t_orb->sunmagangle = -1000.;
2121                    //
2122                    if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
2123                    //
2124                    TClonesArray &tt3 = *torbExtTrk;
2125                    new(tt3[ttentry]) OrbitalInfoTrkVar(*t_orb);
2126                    ttentry++;
2127                    //
2128                    t_orb->Clear();
2129                    //
2130                  }
2131                  //
2132                }
2133              } // end standard tracking algorithm: extended
2134    
2135          } else {          } else {
2136            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);
2137          };          }
2138          //          //
2139        } else {        } else { // HERE IT MISS ALL CODE FOR EXTENDED TRACKING!
2140          if ( !standalone && tof->ntrk() > 0 ){          if ( !standalone ){
2141            //            //
2142              if ( verbose ) printf(" no orb info for tracks \n");
2143            Int_t nn = 0;            Int_t nn = 0;
2144            for(Int_t nt=0; nt < tof->ntrk(); nt++){              for(Int_t nt=0; nt < tof->ntrk(); nt++){  
2145              //              //
# Line 1260  int OrbitalInfoCore(UInt_t run, TFile *f Line 2154  int OrbitalInfoCore(UInt_t run, TFile *f
2154                //                            //            
2155                t_orb->pitch = -1000.;                t_orb->pitch = -1000.;
2156                //                //
2157                  t_orb->sunangle = -1000.;
2158                  //
2159                  t_orb->sunmagangle = -1000;
2160                  //
2161                t_orb->cutoff = -1000.;                t_orb->cutoff = -1000.;
2162                //                //
2163                new(tor[nn]) OrbitalInfoTrkVar(*t_orb);                new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
# Line 1267  int OrbitalInfoCore(UInt_t run, TFile *f Line 2165  int OrbitalInfoCore(UInt_t run, TFile *f
2165                //                //
2166                t_orb->Clear();                t_orb->Clear();
2167                //                //
2168              };              }
2169              //              //
2170            };                }
2171          };            //
2172        };            // Code for extended tracking algorithm:
2173              //
2174              if ( hasNucleiTrk ){
2175                Int_t ttentry = 0;
2176                if ( verbose ) printf(" hasNucleiTrk \n");
2177                for(Int_t nt=0; nt < tcNucleiTof->GetEntries() ; nt++){  
2178                  //  
2179                  ToFTrkVar *ptt = (ToFTrkVar*)(tcNucleiTof->At(nt));
2180                  if ( ptt->trkseqno != -1  ){
2181                    //
2182                    t_orb->trkseqno = ptt->trkseqno;
2183                    //
2184                    t_orb->Eij = 0;
2185                    //
2186                    t_orb->Sij = 0;
2187                    //          
2188                    t_orb->pitch = -1000.;
2189                    //
2190                    t_orb->sunangle = -1000.;
2191                    //
2192                    t_orb->sunmagangle = -1000;
2193                    //
2194                    t_orb->cutoff = -1000.;
2195                    //
2196                    TClonesArray &tz1 = *torbNucleiTrk;
2197                    new(tz1[ttentry]) OrbitalInfoTrkVar(*t_orb);
2198                    ttentry++;
2199                    //
2200                    t_orb->Clear();
2201                    //
2202                  }
2203                  //
2204                }
2205              }
2206              if ( hasExtNucleiTrk ){
2207                Int_t ttentry = 0;
2208                if ( verbose ) printf(" hasExtNucleiTrk \n");
2209                for(Int_t nt=0; nt < tcExtNucleiTof->GetEntries() ; nt++){  
2210                  //
2211                  if ( verbose ) printf(" got2\n");
2212                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtNucleiTof->At(nt));
2213                  if ( ptt->trkseqno != -1  ){
2214                    //
2215                    t_orb->trkseqno = ptt->trkseqno;
2216                    //
2217                    t_orb->Eij = 0;
2218                    //
2219                    t_orb->Sij = 0;
2220                    //          
2221                    t_orb->pitch = -1000.;
2222                    //
2223                    t_orb->sunangle = -1000.;
2224                    //
2225                    t_orb->sunmagangle = -1000;
2226                    //
2227                    t_orb->cutoff = -1000.;
2228                    //
2229                    TClonesArray &tz2 = *torbExtNucleiTrk;
2230                    new(tz2[ttentry]) OrbitalInfoTrkVar(*t_orb);
2231                    ttentry++;
2232                    //
2233                    t_orb->Clear();
2234                    //
2235                  }
2236                  //
2237                }
2238              }
2239              if ( hasExtTrk ){
2240                Int_t ttentry = 0;
2241                if ( verbose ) printf(" hasExtTrk \n");
2242                for(Int_t nt=0; nt < tcExtTof->GetEntries() ; nt++){  
2243                  //
2244                  if ( verbose ) printf(" got3\n");
2245                  ToFTrkVar *ptt = (ToFTrkVar*)(tcExtTof->At(nt));
2246                  if ( ptt->trkseqno != -1  ){
2247                    //
2248                    t_orb->trkseqno = ptt->trkseqno;
2249                    //
2250                    t_orb->Eij = 0;
2251                    //
2252                    t_orb->Sij = 0;
2253                    //          
2254                    t_orb->pitch = -1000.;
2255                    //
2256                    t_orb->sunangle = -1000.;
2257                    //
2258                    t_orb->sunmagangle = -1000;
2259                    //
2260                    t_orb->cutoff = -1000.;
2261                    //
2262                    TClonesArray &tz3 = *torbExtNucleiTrk;
2263                    new(tz3[ttentry]) OrbitalInfoTrkVar(*t_orb);
2264                    ttentry++;
2265                    //
2266                    t_orb->Clear();
2267                    //
2268                  }
2269                  //
2270                }
2271              }
2272            }
2273          } // if( orbitalinfo->TimeGap>0){
2274        //        //
2275        // Fill the class        // Fill the class
2276        //        //
2277        OrbitalInfotr->Fill();        OrbitalInfotr->Fill();
2278        //        //
2279          //      tor.Clear("C"); // memory leak?
2280          tor.Delete(); // memory leak?      
2281        delete t_orb;        delete t_orb;
2282        //        //
2283      }; // loop over the events in the run        //      printf(" q0 size %i q0 capacity %i \n",(int)q0.size(),(int)q0.capacity());
2284        } // loop over the events in the run
2285    
2286    
2287      //      //
2288      // Here you may want to clear some variables before processing another run        // Here you may want to clear some variables before processing another run  
2289      //      //
2290    
2291      //gStyle->SetOptStat(000000);      //    OrbitalInfotr->FlushBaskets();
     //gStyle->SetPalette(1);  
       
     /*TCanvas* c1 = new TCanvas("c1","",1200,800);  
     //c1->Divide(1,4);  
     c1->cd(1);  
     //q0testing->Draw("colz");  
     //c1->cd(2);  
     //q1testing->Draw("colz");  
     //c1->cd(3);  
     Pitchtesting->Draw("colz");  
     //c1->cd(4);  
     //q3testing->Draw("colz");  
     c1->SaveAs("9.Rollhyst.png");  
     delete c1;*/  
2292    
2293        if ( verbose ) printf(" Clear before new run \n");
2294      delete dbtime;      delete dbtime;
2295      if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;  
2296        if ( mcmdrc ) mcmdrc->Clear();
2297        mcmdrc = 0;
2298        
2299        if ( verbose ) printf(" Clear before new run1 \n");
2300      if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;      if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;
2301        if ( verbose ) printf(" Clear before new run2 \n");
2302        if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;
2303        if ( verbose ) printf(" Clear before new run3 \n");
2304      if ( RYPang_upper ) delete RYPang_upper;      if ( RYPang_upper ) delete RYPang_upper;
2305        if ( verbose ) printf(" Clear before new run4 \n");
2306      if ( RYPang_lower ) delete RYPang_lower;      if ( RYPang_lower ) delete RYPang_lower;
2307    }; // process all the runs  
2308      
2309        if ( l0tr ){
2310          if ( verbose ) printf(" delete l0tr\n");
2311          l0tr->Delete();
2312          l0tr = 0;
2313        }
2314        //    if ( l0head ){
2315        //      printf(" delete l0head\n");
2316        //  l0head->Reset();
2317        //  delete l0head;
2318        //  printf(" delete l0head done\n");
2319        //  l0head = 0;
2320        // }
2321        if (eh) {
2322          if ( verbose ) printf(" delete eh\n");
2323          delete eh;
2324          eh = 0;
2325        }
2326    
2327        if ( verbose ) printf(" close file \n");
2328        if ( l0File ) l0File->Close("R");
2329        if ( verbose ) printf(" End run \n");
2330    
2331        q0.clear();
2332        q1.clear();
2333        q2.clear();
2334        q3.clear();
2335        qtime.clear();
2336        qPitch.clear();
2337        qRoll.clear();
2338        qYaw.clear();
2339        qmode.clear();
2340    
2341        if (ch){
2342          if ( verbose ) printf(" delete ch\n");
2343          ch->Delete();
2344          ch = 0;
2345        }
2346      } // process all the runs    <===
2347      if ( debug ){
2348        printf("AFTER LOOP ON RUNs\n");
2349        gObjectTable->Print();
2350      }
2351      //  
2352    if (verbose) printf("\n Finished processing data \n");    if (verbose) printf("\n Finished processing data \n");
2353    //    //
2354   closeandexit:   closeandexit:
# Line 1328  int OrbitalInfoCore(UInt_t run, TFile *f Line 2370  int OrbitalInfoCore(UInt_t run, TFile *f
2370          //          //
2371          // copy orbitalinfoclone to OrbitalInfo          // copy orbitalinfoclone to OrbitalInfo
2372          //          //
2373          orbitalinfo->Clear();          //      orbitalinfo->Clear();
2374          //          //
2375          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));          memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
2376          //          //
# Line 1338  int OrbitalInfoCore(UInt_t run, TFile *f Line 2380  int OrbitalInfoCore(UInt_t run, TFile *f
2380        };        };
2381        if (verbose) printf(" Finished successful copying!\n");        if (verbose) printf(" Finished successful copying!\n");
2382      };      };
2383        //if ( OrbitalInfotrclone )    OrbitalInfotrclone->Clear();        
2384        //if ( OrbitalInfotrclone )    OrbitalInfotrclone->Delete();        
2385    };    };
2386    //    //
2387    // Close files, delete old tree(s), write and close level2 file    // Close files, delete old tree(s), write and close level2 file
2388    //    //
2389    
2390    if ( l0File ) l0File->Close();    if ( l0File ) l0File->Close();
   if ( tempfile ) tempfile->Close();              
2391    if ( myfold ) gSystem->Unlink(tempname.str().c_str());    if ( myfold ) gSystem->Unlink(tempname.str().c_str());
2392    //    //
   if ( runinfo ) runinfo->Close();      
2393    if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");        if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");    
   if ( tof ) tof->Delete();  
   if ( ttof ) ttof->Delete();  
2394    //    //
2395    if ( file ){    if ( file ){
2396      file->cd();      file->cd();
2397      file->Write();      if ( OrbitalInfotr ) OrbitalInfotr->Write("OrbitalInfo", TObject::kOverwrite); // 10 RED bug fixed
2398    };    };
2399    //    //
2400      if (verbose) printf("\n Exiting...\n");
2401    
2402    if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());    if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());
2403    //    //
2404    // the end    // the end
# Line 1364  int OrbitalInfoCore(UInt_t run, TFile *f Line 2407  int OrbitalInfoCore(UInt_t run, TFile *f
2407      dbc->Close();      dbc->Close();
2408      delete dbc;      delete dbc;
2409    };    };
   if (verbose) printf("\n Exiting...\n");  
   if(OrbitalInfotr)OrbitalInfotr->Delete();  
2410    //    //
2411      if (verbose) printf("\n Exiting...\n");
2412      if ( tempfile ) tempfile->Close();            
2413      
2414    if ( PO ) delete PO;    if ( PO ) delete PO;
2415    if ( orbitalinfo ) delete orbitalinfo;    if ( gltle ) delete gltle;
2416    if ( orbitalinfoclone ) delete orbitalinfoclone;    if ( glparam ) delete glparam;
2417      if ( glparam2 ) delete glparam2;
2418      if (verbose) printf("\n Exiting3...\n");
2419    if ( glroot ) delete glroot;    if ( glroot ) delete glroot;
2420      if (verbose) printf("\n Exiting4...\n");
2421      if ( runinfo ) runinfo->Close();    
2422    if ( runinfo ) delete runinfo;    if ( runinfo ) delete runinfo;
2423    
2424      if ( tcNucleiTrk ){
2425        tcNucleiTrk->Delete();
2426        delete tcNucleiTrk;
2427        tcNucleiTrk = NULL;
2428      }
2429      if ( tcExtNucleiTrk ){
2430        tcExtNucleiTrk->Delete();
2431        delete tcExtNucleiTrk;
2432        tcExtNucleiTrk = NULL;
2433      }
2434      if ( tcExtTrk ){
2435        tcExtTrk->Delete();
2436        delete tcExtTrk;
2437        tcExtTrk = NULL;
2438      }
2439    
2440      if ( tcNucleiTof ){
2441        tcNucleiTof->Delete();
2442        delete tcNucleiTof;
2443        tcNucleiTof = NULL;
2444      }
2445      if ( tcExtNucleiTof ){
2446        tcExtNucleiTof->Delete();
2447        delete tcExtNucleiTof;
2448        tcExtNucleiTof = NULL;
2449      }
2450      if ( tcExtTof ){
2451        tcExtTof->Delete();
2452        delete tcExtTof;
2453        tcExtTrk = NULL;
2454      }
2455    
2456    
2457      if ( tof ) delete tof;
2458      if ( trke ) delete trke;
2459    
2460      if ( debug ){  
2461      cout << "1   0x" << OrbitalInfotr << endl;
2462      cout << "2   0x" << OrbitalInfotrclone << endl;
2463      cout << "3   0x" << l0tr << endl;
2464      cout << "4   0x" << tempOrbitalInfo << endl;
2465      cout << "5   0x" << ttof << endl;
2466      }
2467    //    //
2468      if ( debug )  file->ls();
2469      //
2470      if ( debug ){
2471        printf("BEFORE EXITING\n");
2472        gObjectTable->Print();
2473      }
2474    if(code < 0)  throw code;    if(code < 0)  throw code;
2475    return(code);    return(code);
2476  }  }
# Line 1419  UInt_t holeq(Double_t lower,Double_t upp Line 2517  UInt_t holeq(Double_t lower,Double_t upp
2517    Bool_t R10l = false;     // Sign of R10 mode in lower quaternions array    Bool_t R10l = false;     // Sign of R10 mode in lower quaternions array
2518    Bool_t R10u = false;     // Sign of R10 mode in upper quaternions array    Bool_t R10u = false;     // Sign of R10 mode in upper quaternions array
2519    Bool_t insm = false;     // Sign that we inside quaternions array    Bool_t insm = false;     // Sign that we inside quaternions array
2520    Bool_t mxtml = false;    // Sign of mixt mode in lower quaternions array    //  Bool_t mxtml = false;    // Sign of mixt mode in lower quaternions array
2521    Bool_t mxtmu = false;    // Sign of mixt mode in upper quaternions array    //  Bool_t mxtmu = false;    // Sign of mixt mode in upper quaternions array
2522    Bool_t 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
2523    UInt_t NCQl = 6;       // Number of correct quaternions in lower array    UInt_t NCQl = 6;       // Number of correct quaternions in lower array
2524    UInt_t NCQu = 6;       // Number of correct quaternions in upper array    //  UInt_t NCQu = 6;       // Number of correct quaternions in upper array
2525    if (f>0){    if (f>0){
2526      insm = true;      insm = true;
2527      if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;      if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;
# Line 1435  UInt_t holeq(Double_t lower,Double_t upp Line 2533  UInt_t holeq(Double_t lower,Double_t upp
2533      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;
2534      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;
2535      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)){
2536        mxtml = true;        //      mxtml = true;
2537        for(UInt_t i = 1; i < 6; i++){        for(UInt_t i = 1; i < 6; i++){
2538          if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;          if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;
2539        }        }
2540      }      }
2541      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)){
2542        mxtmu = true;        //      mxtmu = true;
2543        for(UInt_t i = 1; i < 6; i++){        //      for(UInt_t i = 1; i < 6; i++){
2544          if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;        //        if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;
2545        }        //      }
2546      }      //    }
2547    }    }
2548        
2549    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 1477  void inclresize(vector<Double_t>& t,vect Line 2575  void inclresize(vector<Double_t>& t,vect
2575    Yaw.resize(sizee);    Yaw.resize(sizee);
2576  }  }
2577    
2578  //Find fitting sine functions for q0,q1,q2,q3 and Yaw-angle;  // geomagnetic calculation staff
2579  void sineparam(vector<Sine>& qsine, vector<Double_t>& qtime, vector<Float_t>& q, vector<Float_t>& Roll, vector<Float_t>& Pitch, Float_t limsin){  
2580    UInt_t mulast = 0;  void GM_ScanIGRF(TSQLServer *dbc, GMtype_Data *G0, GMtype_Data *G1, GMtype_Data *H1)
2581    UInt_t munow = 0;  {
2582    UInt_t munext = 0;    GL_PARAM *glp = new GL_PARAM();
2583    Bool_t increase = false;    Int_t parerror=glp->Query_GL_PARAM(1,304,dbc); // parameters stored in DB in GL_PRAM table  
2584    Bool_t decrease = false;    if ( parerror<0 ) {
2585    Bool_t Max_is_defined = false;      throw -902;
2586    Bool_t Start_point_is_defined = false;    }
2587    Bool_t Period_is_defined = false;          /*This function scans inputs G0, G1, and H1 of the IGRF table into 3 data arrays*/
2588    Bool_t Large_gap = false;    //    TString SATH="/data03/Malakhov/pam9Malakhov/installed10/calib/orb-param/";
2589    Bool_t normal_way = true;          int i;
2590    Bool_t small_gap_on_ridge = false;          double temp;
2591    Double_t t1 = 0;          char buffer[200];
2592    Double_t t1A = 0;          FILE *IGRF;
2593    Int_t sinesize = 0;          IGRF = fopen((glp->PATH+glp->NAME).Data(), "r");
2594    Int_t nfi = 0;          //      IGRF = fopen(PATH+"IGRF.tab", "r");
2595    for(UInt_t mu = 0;mu<qtime.size();mu++){          G0->size = 25;
2596      //cout<<"Roll["<<mu<<"] = "<<Roll[mu]<<endl;          G1->size = 25;
2597      if(TMath::Abs(Roll[mu])<1. && TMath::Abs(Pitch[mu])<1. && TMath::Abs(q[mu])<limsin){          H1->size = 25;
2598      //cout<<"q["<<mu<<endl<<"] = "<<q[mu]<<endl;          for( i = 0; i < 4; i++)
2599      if(mulast!=0 && munow!=0 && munext!=0){mulast=munow;munow=munext;munext=mu;}          {
2600      if(munext==0 && munow!=0)munext=mu;                  fgets(buffer, 200, IGRF);
     if(munow==0 && mulast!=0)munow=mu;  
     if(mulast==0)mulast=mu;  
       
     //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;  
     //Int_t ref;  
     //cin>>ref;  
     if(TMath::Abs(q[munow])>TMath::Abs(q[mulast]) && TMath::Abs(q[munow])>TMath::Abs(q[munext]) && q[mulast]*q[munext]>0 && qtime[munext]-qtime[mulast]>400)small_gap_on_ridge = true;  
     if(munext>mulast && (qtime[munext]-qtime[mulast]>=2000 || qtime[munext]-qtime[mulast]<0)){if(Large_gap){normal_way = false;Large_gap = false;}else{Large_gap = true;normal_way = false;}}else normal_way = true;  
     //if(normal_way)cout<<"Normal_Way"<<endl;  
     if(Large_gap || small_gap_on_ridge){  
       //cout<<"Large gap..."<<endl;  
       //if(small_gap_on_ridge)cout<<"small gap..."<<endl;  
       //cout<<"q["<<mulast<<"] = "<<q[mulast]<<"\tq["<<munow<<"] = "<<q[munow]<<"\tq["<<munext<<"] = "<<q[munext]<<endl;  
       //cout<<"qtime["<<mulast<<"] = "<<qtime[mulast]<<"\tqtime["<<munow<<"] = "<<qtime[munow]<<"\tqtime["<<munext<<"] = "<<qtime[munext]<<endl;  
       increase = false;  
       decrease = false;  
       if(nfi>0){  
         qsine.resize(qsine.size()-1);  
         sinesize = qsine.size();  
         //cout<<"nfi was larger then zero"<<endl;  
       }else{  
         //cout<<"nfi was not larger then zero :( nfi = "<<nfi<<endl;  
         //cout<<"qsine.size = "<<qsine.size()<<endl;  
         if(!Period_is_defined){  
           //cout<<"Period was defined"<<endl;  
           if(qsine.size()>1){  
             qsine[sinesize-1].b = qsine[sinesize-2].b;  
             qsine[sinesize-1].c = qsine[sinesize-2].c;  
           }else{  
             qsine[sinesize-1].b = TMath::Pi()/1591.54;  
             qsine[sinesize-1].c = qsine[sinesize-1].startPoint;  
           }  
         }  
         if(!Max_is_defined){  
           //cout<<"Max was already defined"<<endl;  
           if(qsine.size()>1)qsine[sinesize-1].A = qsine[sinesize-2].A;else qsine[sinesize-1].A = limsin;  
         }  
         qsine[sinesize-1].NeedFit = true;  
       }  
       qsine[sinesize-1].finishPoint = qtime[munow];  
       //cout<<"finish point before large gap = "<<qtime[munow]<<endl;  
       nfi = 0;  
       Max_is_defined = false;  
       Start_point_is_defined = false;  
       Period_is_defined = false;  
       small_gap_on_ridge = false;  
     }  
     //cout<<"Slope "<<increase<<"\t"<<decrease<<endl;  
     //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;  
     if((munext>munow) && (munow>mulast) && normal_way){  
       if(!increase && !decrease){  
         //cout<<"Normal way have started"<<endl;  
         qsine.resize(qsine.size()+1);  
         sinesize = qsine.size();  
         qsine[sinesize-1].startPoint=qtime[mulast];  
         if(q[munext]>q[munow] && q[munow]>q[mulast]) increase = true;  
         if(q[munext]<q[munow] && q[munow]<q[mulast]) decrease = true;  
       }  
       //if(TMath::Abs(q[munow])>TMath::Abs(q[mulast]) && TMath::Abs(q[munow])>TMath::Abs(q[munext]) && TMath::Abs(q[munow])>limsin && qtime[munow]-qtime[mulast]>=400 || qtime[munext]-qtime[munow]>=400){small_gap_on_ridge = true;mu--;continue;}  
       if(TMath::Abs(q[munow])>TMath::Abs(q[mulast]) && TMath::Abs(q[munow])>TMath::Abs(q[munext]) && TMath::Abs(q[munow])>0.9*limsin && qtime[munow]-qtime[mulast]<400 && qtime[munext]-qtime[munow]<400){  
         //cout<<"Max point is qtime = "<<qtime[munow]<<"\tq = "<<q[munow]<<endl;  
         if(q[munow]>q[mulast]){  
           increase = false;  
           decrease = true;  
         }  
         if(q[munow]<q[mulast]){  
           increase = true;  
           decrease = false;  
2601          }          }
2602          if(Max_is_defined && !Start_point_is_defined){          fscanf(IGRF, "g 1 0 %lf ", &G0->element[0]);
2603            Double_t qPer = qtime[munow]-t1A;          for(i = 1; i <= 22; i++)
2604            if(qPer>1000){          {
2605              //cout<<"qsine["<<sinesize-1<<"] = "<<qPer<<" = "<<qtime[munow]<<" - "<<t1A<<"\tlim = "<<limsin<<endl;                  fscanf(IGRF ,"%lf ", &G0->element[i]);
             qsine[sinesize-1].b=TMath::Pi()/qPer;  
             if(decrease)qsine[sinesize-1].c=-qsine[sinesize-1].b*t1A;  
             if(increase)qsine[sinesize-1].c=-qsine[sinesize-1].b*(t1A-qPer);  
             Period_is_defined = true;  
           }  
2606          }          }
2607          Max_is_defined = true;          fscanf(IGRF ,"%lf\n", &temp);
2608          qsine[sinesize-1].A = TMath::Abs(q[munow]);          G0->element[23] = temp * 5 + G0->element[22];
2609          if(Start_point_is_defined && Period_is_defined){          G0->element[24] = G0->element[23] + 5 * temp;
2610            qsine[sinesize-1].finishPoint = qtime[munow];          fscanf(IGRF, "g 1 1 %lf ", &G1->element[0]);
2611            nfi++;          for(i = 1; i <= 22; i++)
2612            qsine[sinesize-1].NeedFit = false;          {
2613            Max_is_defined = false;                  fscanf( IGRF, "%lf ", &G1->element[i]);
           Start_point_is_defined = false;  
           Period_is_defined = false;  
           qsine.resize(qsine.size()+1);  
           sinesize = qsine.size();  
           qsine[sinesize-1].startPoint = qtime[munow];  
2614          }          }
2615          if(!Start_point_is_defined) t1A=qtime[munow];          fscanf(IGRF, "%lf\n", &temp);
2616        }          G1->element[23] = temp * 5 + G1->element[22];
2617        //if((q[munow]>=0 && q[mulast]<=0) || (q[munow]<=0 && q[mulast]>=0))cout<<"cross zero point diference = "<<qtime[munext] - qtime[mulast]<<"\tqlast = "<<qtime[mulast]<<"\tqnow = "<<qtime[munow]<<"\tqnext = "<<qtime[munext]<<endl;          G1->element[24] = temp * 5 + G1->element[23];
2618        if(((q[munow]>=0 && q[mulast]<=0) || (q[munow]<=0 && q[mulast]>=0)) && qtime[munow]-qtime[mulast]<2000 && qtime[munext]-qtime[munow]<2000){          fscanf(IGRF, "h 1 1 %lf ", &H1->element[0]);
2619          Double_t tcrosszero = 0;          for(i = 1; i <= 22; i++)
2620          //cout<<"cross zero point...qtime = "<<qtime[munow]<<endl;          {
2621          if(q[munow]==0.) tcrosszero = qtime[munow];else                  fscanf( IGRF, "%lf ", &H1->element[i]);
           if(q[mulast]==0.)tcrosszero = qtime[mulast];else{  
             Double_t k_ = (q[munow]-q[mulast])/(qtime[munow]-qtime[mulast]);  
             Double_t b_ = q[munow]-k_*qtime[munow];  
             tcrosszero = -b_/k_;  
           }  
         if(Start_point_is_defined){  
           //cout<<"Start Point allready defined"<<endl;  
           Double_t qPer = tcrosszero - t1;  
           qsine[sinesize-1].b = TMath::Pi()/qPer;  
           //cout<<"qsine["<<sinesize-1<<"].b = "<<TMath::Pi()/qPer<<endl;  
           Period_is_defined = true;  
           Float_t x0 = 0;  
           if(decrease)x0 = t1;  
           if(increase)x0 = tcrosszero;  
           qsine[sinesize-1].c = -qsine[sinesize-1].b*x0;  
           if(Max_is_defined){  
             //cout<<"Max was previous defined"<<endl;  
             qsine[sinesize-1].finishPoint = qtime[munow];  
             nfi++;  
             qsine[sinesize-1].NeedFit = false;  
             Max_is_defined = false;  
             t1 = tcrosszero;  
             Start_point_is_defined = true;  
             Period_is_defined = false;  
             qsine.resize(qsine.size()+1);  
             sinesize = qsine.size();  
             qsine[sinesize-1].startPoint = qtime[munow];  
           }  
         }else{  
           t1 = tcrosszero;  
           Start_point_is_defined = true;  
2622          }          }
2623        }          fscanf(IGRF, "%lf\n", &temp);
2624      }          H1->element[23] = temp * 5 + H1->element[22];
2625      }          H1->element[24] = temp * 5 + H1->element[23];
2626      if ( glp ) delete glp;
2627      /*
2628      printf("############################## SCAN IGRF ######################################\n");
2629      printf("       G0      G1     H1\n");
2630      printf(" size  %10i %10i %10i \n",G0->size,G1->size,H1->size);
2631      for ( i = 0; i < 30; i++){
2632        printf("%5i  %10.2f %10.2f %10.2f \n",i,G0->element[i],G1->element[i],H1->element[i]);
2633      }  
2634      printf("###############################################################################\n");
2635      */
2636    } /*GM_ScanIGRF*/
2637    
2638    
2639    
2640    
2641    void GM_SetIGRF(Int_t isSecular, TString ifile1, TString ifile2, GMtype_Data *G0, GMtype_Data *G1, GMtype_Data *H1)
2642    {
2643      /*This function scans inputs G0, G1, and H1 of the IGRF table into 3 data arrays*/
2644      int i;
2645      double temp,temp2;
2646      int it1,it2;
2647      char buffer[200];
2648      FILE *IGRF;
2649      G0->size = 2;
2650      G1->size = 2;
2651      H1->size = 2;
2652    
2653      for( i = 0; i < 30; i++){
2654        G0->element[i] = 0.;
2655        G1->element[i] = 0.;
2656        H1->element[i] = 0.;
2657    }    }
2658    
2659    //cout<<"FINISH SINE INTERPOLATION FUNCTION..."<<endl<<endl;    IGRF = fopen(ifile1.Data(), "r");
2660  }    for( i = 0; i < 2; i++){
2661        fgets(buffer, 200, IGRF);
2662      }
2663      fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G0->element[0],&temp);
2664      fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G1->element[0],&H1->element[0]);
2665      fclose(IGRF);
2666    
2667      IGRF = fopen(ifile2.Data(), "r");
2668      for( i = 0; i < 2; i++){
2669        fgets(buffer, 200, IGRF);
2670      }
2671      if ( isSecular ){
2672        fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2,&temp,&temp2);
2673        G0->element[1] = temp * 5. + G0->element[0];
2674        fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2,&temp,&temp2);
2675        G1->element[1] = temp * 5. + G1->element[0];
2676        H1->element[1] = temp2 * 5. + H1->element[0];
2677      } else {
2678        fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G0->element[1],&temp);
2679        fscanf(IGRF, "%3i%3i%12lf%11lf",&it1,&it2, &G1->element[1],&H1->element[1]);
2680      }
2681      fclose(IGRF);
2682      /*
2683      printf("############################## SCAN IGRF ######################################\n");
2684      printf("       G0      G1     H1\n");
2685      printf(" size  %10i %10i %10i \n",G0->size,G1->size,H1->size);
2686      for ( i = 0; i < 30; i++){
2687        printf("%5i  %10.2f %10.2f %10.2f \n",i,G0->element[i],G1->element[i],H1->element[i]);
2688      }  
2689      printf("###############################################################################\n");
2690      */
2691    } /*GM_ScanIGRF*/
2692    
2693    void GM_SetEllipsoid(GMtype_Ellipsoid *Ellip)
2694    {
2695            /*This function sets the WGS84 reference ellipsoid to its default values*/
2696            Ellip->a        =                       6378.137; /*semi-major axis of the ellipsoid in */
2697            Ellip->b        =                       6356.7523142;/*semi-minor axis of the ellipsoid in */
2698            Ellip->fla      =                       1/298.257223563;/* flattening */
2699            Ellip->eps      =                       sqrt(1- ( Ellip->b *    Ellip->b) / (Ellip->a * Ellip->a ));  /*first eccentricity */
2700            Ellip->epssq    =                       (Ellip->eps * Ellip->eps);   /*first eccentricity squared */
2701            Ellip->re       =                       6371.2;/* Earth's radius */
2702    } /*GM_SetEllipsoid*/
2703    
2704    
2705    void GM_EarthCartToDipoleCartCD(GMtype_Pole Pole, GMtype_CoordCartesian EarthCoord, GMtype_CoordCartesian *DipoleCoords)
2706    {
2707            /*This function converts from Earth centered cartesian coordinates to dipole centered cartesian coordinates*/
2708            double X, Y, Z, CosPhi, SinPhi, CosLambda, SinLambda;
2709            CosPhi = cos(TMath::DegToRad()*Pole.phi);
2710            SinPhi = sin(TMath::DegToRad()*Pole.phi);
2711            CosLambda = cos(TMath::DegToRad()*Pole.lambda);
2712            SinLambda = sin(TMath::DegToRad()*Pole.lambda);
2713            X = EarthCoord.x;
2714            Y = EarthCoord.y;
2715            Z = EarthCoord.z;
2716            
2717            /*These equations are taken from a document by Wallace H. Campbell*/
2718            DipoleCoords->x = X * CosPhi * CosLambda + Y * CosPhi * SinLambda - Z * SinPhi;
2719            DipoleCoords->y = -X * SinLambda + Y * CosLambda;
2720            DipoleCoords->z = X * SinPhi * CosLambda + Y * SinPhi * SinLambda + Z * CosPhi;
2721    } /*GM_EarthCartToDipoleCartCD*/
2722    
2723    void GM_GeodeticToSpherical(GMtype_Ellipsoid Ellip, GMtype_CoordGeodetic CoordGeodetic, GMtype_CoordSpherical *CoordSpherical)
2724    {
2725            double CosLat, SinLat, rc, xp, zp; /*all local variables */
2726            /*
2727            ** Convert geodetic coordinates, (defined by the WGS-84
2728            ** reference ellipsoid), to Earth Centered Earth Fixed Cartesian
2729            ** coordinates, and then to spherical coordinates.
2730            */
2731    
2732            CosLat = cos(TMath::DegToRad()*CoordGeodetic.phi);
2733            SinLat = sin(TMath::DegToRad()*CoordGeodetic.phi);
2734    
2735            /* compute the local radius of curvature on the WGS-84 reference ellipsoid */
2736    
2737            rc = Ellip.a / sqrt(1.0 - Ellip.epssq * SinLat * SinLat);
2738    
2739            /* compute ECEF Cartesian coordinates of specified point (for longitude=0) */
2740    
2741            xp = (rc + CoordGeodetic.HeightAboveEllipsoid) * CosLat;
2742            zp = (rc*(1.0 - Ellip.epssq) + CoordGeodetic.HeightAboveEllipsoid) * SinLat;
2743    
2744            /* compute spherical radius and angle lambda and phi of specified point */
2745    
2746            CoordSpherical->r = sqrt(xp * xp + zp * zp);
2747            CoordSpherical->phig = TMath::RadToDeg()*asin(zp / CoordSpherical->r);     /* geocentric latitude */
2748            CoordSpherical->lambda = CoordGeodetic.lambda;                   /* longitude */
2749    } /*GM_GeodeticToSpherical*/
2750    
2751    void GM_PoleLocation(GMtype_Model Model, GMtype_Pole *Pole)
2752    {
2753            /*This function finds the location of the north magnetic pole in spherical coordinates.  The equations are
2754            **from Wallace H. Campbell's Introduction to Geomagnetic Fields*/
2755    
2756            Pole->phi = TMath::RadToDeg()*-atan(sqrt(Model.h1 * Model.h1 + Model.g1 * Model.g1)/Model.g0);
2757            Pole->lambda = TMath::RadToDeg()*atan(Model.h1/Model.g1);
2758    } /*GM_PoleLocation*/
2759    
2760    void GM_SphericalToCartesian(GMtype_CoordSpherical CoordSpherical, GMtype_CoordCartesian *CoordCartesian)
2761    {
2762            /*This function converts spherical coordinates into Cartesian coordinates*/
2763            double CosPhi = cos(TMath::DegToRad()*CoordSpherical.phig);
2764            double SinPhi = sin(TMath::DegToRad()*CoordSpherical.phig);
2765            double CosLambda = cos(TMath::DegToRad()*CoordSpherical.lambda);
2766            double SinLambda = sin(TMath::DegToRad()*CoordSpherical.lambda);
2767            
2768            CoordCartesian->x = CoordSpherical.r * CosPhi * CosLambda;
2769            CoordCartesian->y = CoordSpherical.r * CosPhi * SinLambda;
2770            CoordCartesian->z = CoordSpherical.r * SinPhi;
2771    } /*GM_SphericalToCartesian*/
2772    
2773    void GM_TimeAdjustCoefs(Float_t year, Float_t jyear, GMtype_Data g0d, GMtype_Data g1d, GMtype_Data h1d, GMtype_Model *Model)
2774    {
2775            /*This function calls GM_LinearInterpolation for the coefficients to estimate the value of the
2776            **coefficient for the given date*/
2777            int index;
2778            double x;
2779            index = (year - GM_STARTYEAR) / 5;
2780            x = (jyear - GM_STARTYEAR) / 5.;
2781            Model->g0 = GM_LinearInterpolation(index, index+1, g0d.element[index], g0d.element[index+1], x);
2782            Model->g1 = GM_LinearInterpolation(index, index+1, g1d.element[index], g1d.element[index+1], x);
2783            Model->h1 = GM_LinearInterpolation(index, index+1, h1d.element[index], h1d.element[index+1], x);
2784    } /*GM_TimeAdjustCoefs*/
2785    
2786    double GM_LinearInterpolation(double x1, double x2, double y1, double y2, double x)
2787    {
2788            /*This function takes a linear interpolation between two given points for x*/
2789            double weight, y;
2790            weight  = (x - x1) / (x2 - x1);
2791            y = y1 * (1. - weight) + y2 * weight;
2792            //        printf(" x1 %f x2 %f y1 %f y2 %f x %f ==> y %f \n",x1,x2,y1,y2,x,y);
2793            return y;
2794    }/*GM_LinearInterpolation*/
2795    
2796    void GM_CartesianToSpherical(GMtype_CoordCartesian CoordCartesian, GMtype_CoordSpherical *CoordSpherical)
2797    {
2798            /*This function converts a point from Cartesian coordinates into spherical coordinates*/
2799            double X, Y, Z;
2800            
2801            X = CoordCartesian.x;
2802            Y = CoordCartesian.y;
2803            Z = CoordCartesian.z;
2804    
2805            CoordSpherical->r = sqrt(X * X + Y * Y + Z * Z);
2806            CoordSpherical->phig = TMath::RadToDeg()*asin(Z / (CoordSpherical->r));
2807            CoordSpherical->lambda = TMath::RadToDeg()*atan2(Y, X);
2808    } /*GM_CartesianToSpherical*/

Legend:
Removed from v.1.47  
changed lines
  Added in v.1.92

  ViewVC Help
Powered by ViewVC 1.1.23