/[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.10 by mocchiut, Wed Jan 24 13:16:26 2007 UTC revision 1.60 by pam-mep, Thu Jun 7 14:04:00 2012 UTC
# Line 1  Line 1 
 //  
1  // C/C++ headers  // C/C++ headers
2  //  //
3  #include <fstream>  #include <fstream>
# Line 9  Line 8 
8  //  //
9  // ROOT headers  // ROOT headers
10  //  //
11    //#include <TCanvas.h>
12    #include <TH2F.h> //for test only. Vitaly.
13    #include <TVector3.h>
14    //#include <TF1.h>
15    
16  #include <TTree.h>  #include <TTree.h>
17  #include <TClassEdit.h>  #include <TClassEdit.h>
18  #include <TObject.h>  #include <TObject.h>
# Line 34  Line 38 
38  #include <PscuHeader.h>  #include <PscuHeader.h>
39  #include <PscuEvent.h>  #include <PscuEvent.h>
40  #include <EventHeader.h>  #include <EventHeader.h>
41    #include <mcmd/McmdEvent.h>
42    #include <mcmd/McmdRecord.h>
43  //  //
44  // This program headers  // This program headers
45  //  //
46  #include <OrbitalInfo.h>  #include <OrbitalInfo.h>
47  #include <OrbitalInfoVerl2.h>  #include <OrbitalInfoVerl2.h>
48  #include <OrbitalInfoCore.h>  #include <OrbitalInfoCore.h>
49    #include <InclinationInfo.h>
50    
51    
52  using namespace std;  using namespace std;
53    
# Line 47  using namespace std; Line 55  using namespace std;
55  // CORE ROUTINE  // CORE ROUTINE
56  //  //
57  //  //
58  int OrbitalInfoCore(UInt_t run, TFile *file, TSQLServer *dbc, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){  int OrbitalInfoCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){
59      //
 //   // Temporary check to use igrf routines.  We need dat files in the  
 //   // current directory.  
 //   fstream igrfdat1("igrf05.dat");  
 //   fstream igrfdat2("igrf05s.dat");  
 //   if( (!igrfdat1) && (!igrfdat2)) {  
 //     cerr<<"\n**************************************\n"  
 //      <<"igrf05.dat or igrf05s.dat not in the current directory.  Exiting.\n"  
 //      <<"**************************************\n";  
 //     exit(EXIT_FAILURE);  
 //   }  
 //   igrfdat1.close();  
 //   igrfdat2.close();  
 //   // end of temporary code  
 //   //  
   
60    Int_t i = 0;    Int_t i = 0;
61      TString host = glt->CGetHost();
62      TString user = glt->CGetUser();
63      TString psw = glt->CGetPsw();
64      TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
65      //
66      stringstream myquery;
67      myquery.str("");
68      myquery << "SET time_zone='+0:00'";
69      delete dbc->Query(myquery.str().c_str());
70    //    //
71    TString processFolder = "OrbitalInfoFolder";    TString processFolder = Form("OrbitalInfoFolder_%u",run);
72    //    //
73    // Set these to true to have a very verbose output.    // Set these to true to have a very verbose output.
74    //    //
75    Bool_t debug = false;    Bool_t debug = false;
76    //    //
77    Bool_t verbose = false;    Bool_t verbose = false;
78      //
79      Bool_t standalone = false;
80      //
81    if ( OrbitalInfoargc > 0 ){    if ( OrbitalInfoargc > 0 ){
82      i = 0;      i = 0;
83      while ( i < OrbitalInfoargc ){      while ( i < OrbitalInfoargc ){
# Line 86  int OrbitalInfoCore(UInt_t run, TFile *f Line 90  int OrbitalInfoCore(UInt_t run, TFile *f
90        };        };
91        if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {        if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {
92          verbose = true;          verbose = true;
93            debug = true;
94        };        };
95        if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {        if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {
96          verbose = true;          verbose = true;
97        };        };
98          if ( (!strcmp(OrbitalInfoargv[i],"--standalone")) ) {
99            standalone = true;
100          };
101          if ( (!strcmp(OrbitalInfoargv[i],"--calculate-pitch")) ) {
102            standalone = false;
103          };
104        i++;        i++;
105      };      };
106    };    };
# Line 98  int OrbitalInfoCore(UInt_t run, TFile *f Line 109  int OrbitalInfoCore(UInt_t run, TFile *f
109    //    //
110    TTree *OrbitalInfotr = 0;    TTree *OrbitalInfotr = 0;
111    UInt_t nevents = 0;    UInt_t nevents = 0;
112      UInt_t neventsm = 0;
113    //    //
114    // variables needed to reprocess data    // variables needed to reprocess data
115    //    //
# Line 116  int OrbitalInfoCore(UInt_t run, TFile *f Line 128  int OrbitalInfoCore(UInt_t run, TFile *f
128    stringstream ftmpname;    stringstream ftmpname;
129    TString fname;    TString fname;
130    UInt_t totfileentries = 0;    UInt_t totfileentries = 0;
131    UInt_t idRun = 0;    UInt_t idRun = 0;
132      UInt_t anni5 = 60 * 60 * 24 * 365 * 5 ;//1576800
133      //
134      // My variables. Vitaly.
135      //
136    //  UInt_t oi = 0;
137      Int_t tmpSize = 0;
138    //    //
139    // variables needed to handle error signals    // variables needed to handle error signals
140    //    //
# Line 127  int OrbitalInfoCore(UInt_t run, TFile *f Line 145  int OrbitalInfoCore(UInt_t run, TFile *f
145    //    //
146    OrbitalInfo *orbitalinfo = new OrbitalInfo();    OrbitalInfo *orbitalinfo = new OrbitalInfo();
147    OrbitalInfo *orbitalinfoclone = new OrbitalInfo();    OrbitalInfo *orbitalinfoclone = new OrbitalInfo();
148    
149    //    //
150    // define variables for opening and reading level0 file    // define variables for opening and reading level0 file
151    //    //
152    TFile *l0File = 0;    TFile *l0File = 0;
153    TTree *l0tr = 0;    TTree *l0tr = 0;
154      //  TTree *l0trm = 0;
155      TChain *ch = 0;
156    // EM: open also header branch    // EM: open also header branch
157    TBranch *l0head = 0;    TBranch *l0head = 0;
158    pamela::EventHeader *eh = 0;    pamela::EventHeader *eh = 0;
159    pamela::PscuHeader *ph = 0;    pamela::PscuHeader *ph = 0;
160      pamela::McmdEvent *mcmdev = 0;
161      pamela::McmdRecord *mcmdrc = 0;
162    // end EM    // end EM
163      
164      //  pamela::RunHeaderEvent *reh = new pamela::RunHeaderEvent;
165      //  pamela::EventHeader    *eH  = new pamela::EventHeader;
166      
167    //    //
168    // Define other basic variables    // Define other basic variables
169    //    //
# Line 147  int OrbitalInfoCore(UInt_t run, TFile *f Line 174  int OrbitalInfoCore(UInt_t run, TFile *f
174    Int_t totevent = 0;    Int_t totevent = 0;
175    UInt_t atime = 0;    UInt_t atime = 0;
176    UInt_t re = 0;    UInt_t re = 0;
177      UInt_t ik = 0;
178    
179    // Position    // Position
180    Float_t lon, lat, alt;    Float_t lon, lat, alt;
# Line 154  int OrbitalInfoCore(UInt_t run, TFile *f Line 182  int OrbitalInfoCore(UInt_t run, TFile *f
182    //    //
183    // IGRF stuff    // IGRF stuff
184    //    //
185    float dimo = 0.0; // dipole moment (computed from dat files)    Float_t dimo = 0.0; // dipole moment (computed from dat files)
186    float bnorth, beast, bdown, babs;    Float_t bnorth, beast, bdown, babs;
187    float xl; // L value    Float_t xl; // L value
188    float icode; // code value for L accuracy (see fortran code)    Float_t icode; // code value for L accuracy (see fortran code)
189    float bab1; // What's  the difference with babs?    Float_t bab1; // What's  the difference with babs?
190    float stps = 0.005; // step size for field line tracing    Float_t stps = 0.005; // step size for field line tracing
191    float bdel = 0.01; // required accuracy    Float_t bdel = 0.01; // required accuracy
192    float bequ;  // equatorial b value (also called b_0)    Float_t bequ;  // equatorial b value (also called b_0)
193    bool value = 0; // false if bequ is not the minimum b value    Bool_t value = 0; // false if bequ is not the minimum b value
194    float rr0; // equatorial radius normalized to earth radius    Float_t rr0; // equatorial radius normalized to earth radius
195    
196    //    //
197    // Working filename    // Working filename
# Line 179  int OrbitalInfoCore(UInt_t run, TFile *f Line 207  int OrbitalInfoCore(UInt_t run, TFile *f
207    TTree *tempOrbitalInfo = 0;    TTree *tempOrbitalInfo = 0;
208    stringstream tempname;    stringstream tempname;
209    stringstream OrbitalInfofolder;    stringstream OrbitalInfofolder;
210      Bool_t myfold = false;
211    tempname.str("");    tempname.str("");
212    tempname << outDir;    tempname << outDir;
213    tempname << "/" << processFolder.Data();    tempname << "/" << processFolder.Data();
214    OrbitalInfofolder.str("");    OrbitalInfofolder.str("");
215    OrbitalInfofolder << tempname.str().c_str();    OrbitalInfofolder << tempname.str().c_str();
   gSystem->MakeDirectory(OrbitalInfofolder.str().c_str());  
216    tempname << "/OrbitalInfotree_run";    tempname << "/OrbitalInfotree_run";
217    tempname << run << ".root";      tempname << run << ".root";  
218      UInt_t totnorun = 0;
219    //    //
220    // DB classes    // DB classes
221    //    //
222    GL_ROOT *glroot = new GL_ROOT();    GL_ROOT *glroot = new GL_ROOT();
223    GL_TIMESYNC *dbtime = 0;    GL_TIMESYNC *dbtime = 0;
224    GL_TLE *gltle = new GL_TLE();    GL_TLE *gltle = new GL_TLE();
225      //
226      //Quaternions classes
227      //
228      Quaternions *L_QQ_Q_l_lower = 0;
229      InclinationInfo *RYPang_lower = 0;
230      Quaternions *L_QQ_Q_l_upper = 0;
231      InclinationInfo *RYPang_upper = 0;
232      
233      cEci eCi;
234      
235    // Initialize fortran routines!!!    // Initialize fortran routines!!!
236      Int_t ltp1 = 0;
237    Int_t ltp2 = 0;    Int_t ltp2 = 0;
238    Int_t ltp3 = 0;    Int_t ltp3 = 0;
239    Int_t uno = 1;    //  Int_t uno = 1;
240    char *niente = " ";    //  const char *niente = " ";
241    GL_PARAM *glparam = new GL_PARAM();    GL_PARAM *glparam = new GL_PARAM();
242    GL_PARAM *glparam2 = new GL_PARAM();    GL_PARAM *glparam2 = new GL_PARAM();
243    Int_t parerror=glparam->Query_GL_PARAM(1,301,dbc); // parameters stored in DB in GL_PRAM table    GL_PARAM *glparam3 = new GL_PARAM();
244    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());  
245    //    //
246    parerror=glparam2->Query_GL_PARAM(1,302,dbc); // parameters stored in DB in GL_PRAM table    // Orientation variables. Vitaly
247      //
248      UInt_t evfrom = 0;
249      UInt_t jumped = 0;
250      Int_t itr = -1;    
251      Double_t A1;
252      Double_t A2;
253      Double_t A3;
254      Double_t Px = 0;
255      Double_t Py = 0;      
256      Double_t Pz = 0;  
257      TTree *ttof = 0;
258      ToFLevel2 *tof = new ToFLevel2();
259      OrientationInfo *PO = new OrientationInfo();
260      Int_t nz = 6;
261      Float_t zin[6];
262      Int_t nevtofl2 = 0;
263      if ( verbose ) cout<<"Reading quaternions external file"<<endl;
264      cout.setf(ios::fixed,ios::floatfield);  
265      /******Reading recovered quaternions...*********/
266      vector<Double_t> recqtime;
267      vector<Float_t> recq0;
268      vector<Float_t> recq1;
269      vector<Float_t> recq2;
270      vector<Float_t> recq3;
271      Float_t Norm = 1;
272      Int_t parerror=glparam->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table  
273      ifstream in((glparam->PATH+glparam->NAME).Data(),ios::in);
274    if ( parerror<0 ) {    if ( parerror<0 ) {
275      code = parerror;      code = parerror;
276      goto closeandexit;      goto closeandexit;
277    };    }
278    ltp3 = (Int_t)(glparam2->PATH+glparam2->NAME).Length();    while(!in.eof()){
279    if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());      recqtime.resize(recqtime.size()+1);
280    //      Int_t sizee = recqtime.size();
281    initize_((char *)niente,&uno,(char *)(glparam->PATH+glparam->NAME).Data(),&ltp2,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp3);      recq0.resize(sizee);
282        recq1.resize(sizee);
283        recq2.resize(sizee);
284        recq3.resize(sizee);
285        in>>recqtime[sizee-1];
286        in>>recq0[sizee-1];
287        in>>recq1[sizee-1];
288        in>>recq2[sizee-1];
289        in>>recq3[sizee-1];
290        in>>Norm;
291      }
292      if ( verbose ) cout<<"We have read recovered data"<<endl;
293    
294    //    // IGRF stuff moved inside run loop!  
   // End IGRF stuff//  
   //  
295    
296      for (Int_t ip=0;ip<nz;ip++){
297        zin[ip] = tof->GetZTOF(tof->GetToFPlaneID(ip));
298      };
299      //
300      if ( !standalone ){
301        //
302        // Does it contain the Tracker tree?
303        //
304        ttof = (TTree*)file->Get("ToF");
305        if ( !ttof ) {
306          if ( verbose ) printf(" OrbitalInfo - ERROR: no tof tree\n");
307          code = -900;
308          goto closeandexit;
309        };
310        ttof->SetBranchAddress("ToFLevel2",&tof);  
311        nevtofl2 = ttof->GetEntries();
312      };
313    //    //
314    // Let's start!    // Let's start!
315    //    //
# Line 276  int OrbitalInfoCore(UInt_t run, TFile *f Line 364  int OrbitalInfoCore(UInt_t run, TFile *f
364    // number of run to be processed    // number of run to be processed
365    //    //
366    numbofrun = runinfo->GetNoRun();    numbofrun = runinfo->GetNoRun();
367      totnorun = runinfo->GetRunEntries();
368    //    //
369    // Try to access the OrbitalInfo tree in the file, if it exists we are reprocessing data if not we are processing a new run    // Try to access the OrbitalInfo tree in the file, if it exists we are reprocessing data if not we are processing a new run
370    //    //
# Line 302  int OrbitalInfoCore(UInt_t run, TFile *f Line 391  int OrbitalInfoCore(UInt_t run, TFile *f
391      //      //
392      if (verbose) printf("\n Preparing the pre-processing...\n");      if (verbose) printf("\n Preparing the pre-processing...\n");
393      //      //
394      if ( run == 0 ){      if ( run == 0 || totnorun == 1 ){
395        //        //
396        // we are reprocessing all the file        // we are reprocessing all the file
397        // if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately        // if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately
398        //        //
399        reprocall = true;        reprocall = true;
400        //        //
401        if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n");        if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n Deleting old tree...\n");
402        //        //
403      } else {      } else {
404        //        //
# Line 321  int OrbitalInfoCore(UInt_t run, TFile *f Line 410  int OrbitalInfoCore(UInt_t run, TFile *f
410        //        //
411        // copying old tree to a new file        // copying old tree to a new file
412        //        //
413          gSystem->MakeDirectory(OrbitalInfofolder.str().c_str());
414          myfold = true;
415        tempfile = new TFile(tempname.str().c_str(),"RECREATE");        tempfile = new TFile(tempname.str().c_str(),"RECREATE");
416        tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");        tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");
417        tempOrbitalInfo->SetName("OrbitalInfo-old");        tempOrbitalInfo->SetName("OrbitalInfo-old");
418        tempfile->Write();        tempfile->Write();
419          tempOrbitalInfo->Delete();
420        tempfile->Close();          tempfile->Close();  
421      }      }
422      //      //
423      // Delete the old tree from old file and memory      // Delete the old tree from old file and memory
424      //      //
425        OrbitalInfotrclone->Clear();
426      OrbitalInfotrclone->Delete("all");      OrbitalInfotrclone->Delete("all");
427      //      //
428      if (verbose) printf(" ...done!\n");      if (verbose) printf(" ...done!\n");
# Line 341  int OrbitalInfoCore(UInt_t run, TFile *f Line 434  int OrbitalInfoCore(UInt_t run, TFile *f
434    file->cd();    file->cd();
435    OrbitalInfotr = new TTree("OrbitalInfo-new","PAMELA OrbitalInfo data");    OrbitalInfotr = new TTree("OrbitalInfo-new","PAMELA OrbitalInfo data");
436    OrbitalInfotr->SetAutoSave(900000000000000LL);    OrbitalInfotr->SetAutoSave(900000000000000LL);
437      orbitalinfo->Set();//ELENA **TEMPORANEO?**
438    OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);    OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);
439    //    //
440    if ( reproc && !reprocall ){    if ( reproc && !reprocall ){
# Line 360  int OrbitalInfoCore(UInt_t run, TFile *f Line 454  int OrbitalInfoCore(UInt_t run, TFile *f
454        }        }
455        for (UInt_t j = 0; j < nobefrun; j++){        for (UInt_t j = 0; j < nobefrun; j++){
456          //          //
457          OrbitalInfotrclone->GetEntry(j);                    if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;    
458          //          //
459          // copy orbitalinfoclone to mydec          // copy orbitalinfoclone to mydec
460          //          //
# Line 374  int OrbitalInfoCore(UInt_t run, TFile *f Line 468  int OrbitalInfoCore(UInt_t run, TFile *f
468          //          //
469        };        };
470        if (verbose) printf(" Finished successful copying!\n");        if (verbose) printf(" Finished successful copying!\n");
471      };                };
472    };    };
473    //    //
474      //
475    // 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.
476    //    //
477    runlist = runinfo->GetRunList();    runlist = runinfo->GetRunList();
# Line 384  int OrbitalInfoCore(UInt_t run, TFile *f Line 479  int OrbitalInfoCore(UInt_t run, TFile *f
479    // Loop over the run to be processed    // Loop over the run to be processed
480    //    //
481    for (UInt_t irun=0; irun < numbofrun; irun++){    for (UInt_t irun=0; irun < numbofrun; irun++){
482    
483        L_QQ_Q_l_lower = new Quaternions();
484        RYPang_lower = new InclinationInfo();
485        L_QQ_Q_l_upper = new Quaternions();
486        RYPang_upper = new InclinationInfo();
487    
488      //      //
489      // retrieve the first run ID to be processed using the RunInfo list      // retrieve the first run ID to be processed using the RunInfo list
490      //      //
491        
492      idRun = runlist->At(irun);      idRun = runlist->At(irun);
493      if (verbose){      if (verbose){
494        printf("\n\n\n ####################################################################### \n");        printf("\n\n\n ####################################################################### \n");
# Line 419  int OrbitalInfoCore(UInt_t run, TFile *f Line 521  int OrbitalInfoCore(UInt_t run, TFile *f
521      // prepare the timesync for the db      // prepare the timesync for the db
522      //      //
523      dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);      dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
524      
525      //      //
526      // Search in the DB the path and name of the LEVEL0 file to be processed.      // Search in the DB the path and name of the LEVEL0 file to be processed.
527      //      //
# Line 428  int OrbitalInfoCore(UInt_t run, TFile *f Line 531  int OrbitalInfoCore(UInt_t run, TFile *f
531      ftmpname << glroot->PATH.Data() << "/";      ftmpname << glroot->PATH.Data() << "/";
532      ftmpname << glroot->NAME.Data();      ftmpname << glroot->NAME.Data();
533      fname = ftmpname.str().c_str();      fname = ftmpname.str().c_str();
534        ftmpname.str("");
535      //      //
536      // print out informations      // print nout informations
537      //      //
538      totevent = runinfo->NEVENTS;      totevent = runinfo->NEVENTS;
539        evfrom = runinfo->EV_FROM;
540        //cout<<"totevents = "<<totevent<<"\n";
541      if (verbose){      if (verbose){
542        printf("\n LEVEL0 data file: %s \n",fname.Data());        printf("\n LEVEL0 data file: %s \n",fname.Data());
543        printf(" RUN HEADER absolute time is:  %u \n",runinfo->RUNHEADER_TIME);        printf(" RUN HEADER absolute time is:  %u \n",runinfo->RUNHEADER_TIME);
544        printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);        printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
545        printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM,runinfo->EV_FROM+totevent);        printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM+1,runinfo->EV_FROM+totevent);
546      }//      }//
547        //
548        //    if ( !totevent ) goto closeandexit;
549      // Open Level0 file      // Open Level0 file
550        if ( l0File ) l0File->Close();
551      l0File = new TFile(fname.Data());      l0File = new TFile(fname.Data());
552      if ( !l0File ) {      if ( !l0File ) {
553        if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");        if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
# Line 464  int OrbitalInfoCore(UInt_t run, TFile *f Line 573  int OrbitalInfoCore(UInt_t run, TFile *f
573      // end EM      // end EM
574      nevents = l0head->GetEntries();      nevents = l0head->GetEntries();
575      //      //
576      if ( nevents < 1 ) {      if ( nevents < 1 && totevent ) {
577        if ( debug ) printf(" OrbitalInfo - ERROR: Level0 file is empty\n\n");        if ( debug ) printf(" OrbitalInfo - ERROR: Level0 file is empty\n\n");
578        l0File->Close();        l0File->Close();
579        code = -11;        code = -11;
580        goto closeandexit;        goto closeandexit;
581      };      };
582      //      //
583      if ( runinfo->EV_TO > nevents-1 ) {      if ( runinfo->EV_TO > nevents-1 && totevent ) {
584        if ( debug ) printf(" OrbitalInfo - ERROR: too few entries in the registry tree\n");        if ( debug ) printf(" OrbitalInfo - ERROR: too few entries in the registry tree\n");
585        l0File->Close();        l0File->Close();
586        code = -12;        code = -12;
587        goto closeandexit;        goto closeandexit;
588      };      };
589    
590        //
591        // open IGRF files and do it only once if we are processing a full level2 file
592        //
593        if ( irun == 0 ){
594          if ( l0head->GetEntry(runinfo->EV_FROM) <= 0 ) throw -36;
595          //
596          // absolute time of first event of the run (it should not matter a lot)
597          //
598          ph = eh->GetPscuHeader();
599          atime = dbtime->DBabsTime(ph->GetOrbitalTime());
600          
601          parerror=glparam->Query_GL_PARAM(atime-anni5,301,dbc); // parameters stored in DB in GL_PRAM table  
602          if ( parerror<0 ) {
603            code = parerror;
604            goto closeandexit;
605        };
606          ltp1 = (Int_t)(glparam->PATH+glparam->NAME).Length();
607          if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
608          //
609          parerror=glparam2->Query_GL_PARAM(atime,301,dbc); // parameters stored in DB in GL_PRAM table  
610          if ( parerror<0 ) {
611            code = parerror;
612            goto closeandexit;
613          };
614          ltp2 = (Int_t)(glparam2->PATH+glparam->NAME).Length();
615          if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());
616          //
617          parerror=glparam3->Query_GL_PARAM(atime,302,dbc); // parameters stored in DB in GL_PRAM table
618          if ( parerror<0 ) {
619            code = parerror;
620            goto closeandexit;
621          };
622          ltp3 = (Int_t)(glparam3->PATH+glparam2->NAME).Length();
623          if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam3->PATH+glparam3->NAME).Data());
624          //
625          initize_((char *)(glparam->PATH+glparam->NAME).Data(),&ltp1,(char *)(glparam2->PATH+glparam2->NAME).Data(),&ltp2,(char *)(glparam3->PATH+glparam3->NAME).Data(),&ltp3);
626          //
627        }
628        //
629        // End IGRF stuff//
630        //
631    
632        //
633        //     TTree *tp = (TTree*)l0File->Get("RunHeader");
634        //     tp->SetBranchAddress("Header", &eH);
635        //     tp->SetBranchAddress("RunHeader", &reh);
636        //     tp->GetEntry(0);
637        //     ph = eH->GetPscuHeader();
638        //     ULong_t TimeSync = reh->LAST_TIME_SYNC_INFO;
639        //     ULong_t ObtSync = reh->OBT_TIME_SYNC;    
640        //     if ( debug ) printf(" 1 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",TimeSync,ObtSync,TimeSync-ObtSync);
641        //
642        ULong_t TimeSync = (ULong_t)dbtime->GetTimesync();
643        ULong_t ObtSync = (ULong_t)(dbtime->GetObt0()/1000);
644        ULong_t DeltaOBT = TimeSync - ObtSync;
645    
646        if ( debug ) printf(" 2 TimeSync %lu ObtSync %lu DeltaOBT %lu\n",(ULong_t)(dbtime->GetTimesync()/1000),(ULong_t)dbtime->GetObt0(),TimeSync-ObtSync);
647        //
648        // Read MCMDs from up to 11 files, 5 before and 5 after the present one in order to have some kind of inclination information
649        //
650        ch = new TChain("Mcmd","Mcmd");
651        //
652        // look in the DB to find the closest files to this run
653        //
654        TSQLResult *pResult = 0;
655        TSQLRow *Row = 0;
656        stringstream myquery;
657        UInt_t l0fid[10];
658        Int_t i = 0;
659        memset(l0fid,0,10*sizeof(Int_t));
660        //
661        myquery.str("");
662        myquery << "select ID_ROOT_L0 from GL_RUN where RUNHEADER_TIME<=" << runinfo->RUNHEADER_TIME << " group by ID_ROOT_L0 order by RUNHEADER_TIME desc limit 5;";
663        //
664        pResult = dbc->Query(myquery.str().c_str());
665        //
666        i = 9;
667        if( pResult ){
668          //
669          Row = pResult->Next();
670          //
671          while ( Row ){
672            //
673            // store infos and exit
674            //
675            l0fid[i] = (UInt_t)atoll(Row->GetField(0));
676            i--;
677            Row = pResult->Next();  
678            //
679          };
680          pResult->Delete();
681        };
682        //
683        myquery.str("");
684        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;";
685        //
686        pResult = dbc->Query(myquery.str().c_str());
687        //
688        i = 0;
689        if( pResult ){
690          //
691          Row = pResult->Next();
692          //
693          while ( Row ){
694            //
695            // store infos and exit
696            //
697            l0fid[i] = (UInt_t)atoll(Row->GetField(0));
698            i++;
699            Row = pResult->Next();  
700            //
701          };
702          pResult->Delete();
703        };
704        //
705        i = 0;
706        UInt_t previd = 0;
707        while ( i < 10 ){
708          if ( l0fid[i] && previd != l0fid[i] ){
709            previd = l0fid[i];
710            myquery.str("");
711            myquery << "select PATH,NAME from GL_ROOT where ID=" << l0fid[i] << " ;";
712            //
713            pResult = dbc->Query(myquery.str().c_str());
714            //
715            if( pResult ){
716              //
717              Row = pResult->Next();
718              //
719              if ( debug ) printf(" Using inclination informations from file: %s \n",(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1)).Data());
720              ch->Add(((TString)gSystem->ExpandPathName(Row->GetField(0)))+"/"+(TString)Row->GetField(1));
721              //
722              pResult->Delete();
723            };
724          };
725          i++;
726        };
727        //
728        //    l0trm = (TTree*)l0File->Get("Mcmd");
729        //    ch->ls();
730        ch->SetBranchAddress("Mcmd",&mcmdev);
731        //    printf(" entries %llu \n", ch->GetEntries());
732        //    l0trm = ch->GetTree();
733        //    neventsm = l0trm->GetEntries();
734        neventsm = ch->GetEntries();
735        if ( debug ) printf(" entries %u \n", neventsm);
736        //    neventsm = 0;
737        //
738        if (neventsm == 0){
739          if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");
740          //      l0File->Close();
741          code = 900;
742          //      goto closeandexit;
743        }
744        //
745        
746        //    l0trm->SetBranchAddress("Mcmd", &mcmdev);
747        //    l0trm->SetBranchAddress("Header", &eh);
748        //
749        //
750        //
751    
752    //    UInt_t mctren = 0;    
753    //    UInt_t mcreen = 0;        
754        UInt_t numrec = 0;
755        //
756        Double_t upperqtime = 0;
757        Double_t lowerqtime = 0;
758        
759    //    Double_t incli = 0;
760    //    oi = 0;
761    //    UInt_t ooi = 0;
762        //
763        // init quaternions information from mcmd-packets
764        //
765        Bool_t isf = true;
766    //    Int_t fgh = 0;
767    
768        vector<Float_t> q0;
769        vector<Float_t> q1;
770        vector<Float_t> q2;
771        vector<Float_t> q3;
772        vector<Double_t> qtime;
773        vector<Float_t> qPitch;
774        vector<Float_t> qRoll;
775        vector<Float_t> qYaw;
776        vector<Int_t> qmode;
777    
778        Int_t nt = 0;
779        
780        //init sine-function interpolation
781        
782        //cout<<"Sine coeficient initialisation..."<<endl;
783        vector<Sine> q0sine;
784        vector<Sine> q1sine;
785        vector<Sine> q2sine;
786        vector<Sine> q3sine;
787        vector<Sine> Yawsine;
788    
789        /*TH2F* q0testing = new TH2F();
790          TH2F* q1testing = new TH2F();
791          TH2F* q2testing = new TH2F();
792          TH2F* q3testing = new TH2F();
793          TH2F* Pitchtesting = new TH2F();
794        */
795        UInt_t must = 0;
796    
797      //      //
798      // run over all the events of the run      // run over all the events of the run
799      //      //
800      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");
801      //      //
802        //
803      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
804        //        //
805        if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);          if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);  
806          if ( debug ) printf(" %i \n",procev);      
807        //        //
808        l0head->GetEntry(re);        if ( l0head->GetEntry(re) <= 0 ) throw -36;
809        //        //
810        // absolute time of this event        // absolute time of this event
811        //        //
812        ph = eh->GetPscuHeader();        ph = eh->GetPscuHeader();
813        atime = dbtime->DBabsTime(ph->GetOrbitalTime());          atime = dbtime->DBabsTime(ph->GetOrbitalTime());
814          if ( debug ) printf(" %i absolute time \n",procev);      
815        //        //
816        // paranoid check        // paranoid check
817        //        //
818        if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME)  ) {        if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1))  ) {
819          if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");          if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");
820          debug = true;          jumped++;
821            //      debug = true;
822          continue;          continue;
823        }        }
824    
825          //
826          // retrieve tof informations
827          //
828          if ( !reprocall ){
829            itr = nobefrun + (re - evfrom - jumped);
830            //itr = re-(46438+200241);
831          } else {
832            itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
833          };
834          //
835          if ( !standalone ){
836            if ( itr > nevtofl2 ){  
837              if ( verbose ) printf(" OrbitalInfo - ERROR: no tof events with entry = %i in Level2 file\n",itr);
838              if ( debug ) printf(" nobefrun %u re %u evfrom %u jumped %u reprocall %i \n",nobefrun,re,evfrom,jumped,reprocall);
839              l0File->Close();
840              code = -901;
841              goto closeandexit;
842            };
843            //
844            tof->Clear();
845            //
846            if ( ttof->GetEntry(itr) <= 0 ) throw -36;
847            //
848          };
849        //        //
850        procev++;        procev++;
851        //        //
852        // start processing        // start processing
853        //        //
854          if ( debug ) printf(" %i start processing \n",procev);      
855        orbitalinfo->Clear();        orbitalinfo->Clear();
856        //        //
857        // CHANGE HERE!!!!        OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();
858          if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);
859          TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;
860        //        //
861        orbitalinfo->absTime = atime;        // Fill OBT, pkt_num and absTime
862        // EM: add OBT and plt_num infos from the header        //      
       ph = eh->GetPscuHeader();  
863        orbitalinfo->pkt_num = ph->GetCounter();        orbitalinfo->pkt_num = ph->GetCounter();
864        orbitalinfo->OBT = ph->GetOrbitalTime();        orbitalinfo->OBT = ph->GetOrbitalTime();
865          orbitalinfo->absTime = atime;
866        // If the absolute time of the event overpass the time of the        if ( debug ) printf(" %i pktnum obt abstime \n",procev);      
867        // tle, get a new tle.  GL_TLE::GetToTime() default to zero.        //
868          // Propagate the orbit from the tle time to atime, using SGP(D)4.
869          //
870          if ( debug ) printf(" %i sgp4 \n",procev);      
871          cCoordGeo coo;
872          Float_t jyear=0.;    
873        //        //
       // I also use this condition to compute the dipole moment dimo.  
       // It's really redundant to compute it so often because  
       // probably it will not change at all.  But the overhead is  
       // minimum.  
       float jyear=0;  
   
874        if(atime >= gltle->GetToTime()) {        if(atime >= gltle->GetToTime()) {
875          if ( !gltle->Query(atime, dbc) ){          if ( !gltle->Query(atime, dbc) ){
876                        //      
877            // Compute the magnetic dipole moment.            // Compute the magnetic dipole moment.
878              //
879              if ( debug ) printf(" %i compute magnetic dipole moment \n",procev);      
880            UInt_t year, month, day, hour, min, sec;            UInt_t year, month, day, hour, min, sec;
881                        //
882            TTimeStamp t = TTimeStamp(atime, kTRUE);            TTimeStamp t = TTimeStamp(atime, kTRUE);
883            t.GetDate(kTRUE, 0, &year, &month, &day);            t.GetDate(kTRUE, 0, &year, &month, &day);
884            t.GetTime(kTRUE, 0, &hour, &min, &sec);            t.GetTime(kTRUE, 0, &hour, &min, &sec);
885            jyear = (float) year            jyear = (float) year
886              + (month*31.+ (float) day)/365.              + (month*31.+ (float) day)/365.
887              + (hour*3600.+min*60.+(float)sec)/(24*3600*365.);              + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);
888                        //
889              if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);      
890              if ( debug ) printf(" %i jyear %f dimo %f \n",procev,jyear,dimo);      
891            feldcof_(&jyear, &dimo); // get dipole moment for year            feldcof_(&jyear, &dimo); // get dipole moment for year
892              if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);      
893          } else {          } else {
894            code = -56;            code = -56;
895            goto closeandexit;            goto closeandexit;
896          };          };
897        }        }
898          coo = getCoo(atime, gltle->GetFromTime(), gltle->GetTle());
899          //
900          cOrbit orbits(*gltle->GetTle());
901          //
902          if ( debug ) printf(" I am Here \n");
903          //
904          // synchronize with quaternions data
905          //
906          if ( isf && neventsm>0 ){
907            //
908            // First event
909            //
910            isf = false;
911            upperqtime = atime;
912            lowerqtime = runinfo->RUNHEADER_TIME;
913            for ( ik = 0; ik < neventsm; ik++){  //number of macrocommad packets
914              if ( ch->GetEntry(ik) <= 0 ) throw -36;
915              tmpSize = mcmdev->Records->GetEntries();
916              numrec = tmpSize;
917              for (Int_t j3 = 0;j3<tmpSize;j3++){  //number of subpackets
918                if ( debug ) printf(" ik %i j3 %i eh eh eh \n",ik,j3);
919                mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);
920                if ( mcmdrc ){ // missing inclination bug [8RED 090116]
921                  if ( debug ) printf(" pluto \n");
922                  if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet
923                    L_QQ_Q_l_upper->fill(mcmdrc->McmdData);
924                    for (UInt_t ui = 0; ui < 6; ui++){
925                      if (ui>0){
926                        if (L_QQ_Q_l_upper->time[ui]>L_QQ_Q_l_upper->time[0]){
927                            if ( debug ) printf(" here1 %i \n",ui);
928                          Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[ui]*1000-DeltaOBT*1000));
929                          Int_t recSize = recqtime.size();
930                          if(lowerqtime > recqtime[recSize-1]){
931                              Int_t sizeqmcmd = qtime.size();
932                              inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
933                              qtime[sizeqmcmd]=u_time;
934                              q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
935                              q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
936                              q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
937                              q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
938                              qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
939                              lowerqtime = u_time;
940                              orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
941                              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]);
942                              qRoll[sizeqmcmd]=RYPang_upper->Kren;
943                              qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
944                              qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
945                          }
946                          for(Int_t mu = nt;mu<recSize;mu++){
947                            if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
948                              nt=mu;
949                              Int_t sizeqmcmd = qtime.size();
950                              inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
951                              qtime[sizeqmcmd]=recqtime[mu];
952                              q0[sizeqmcmd]=recq0[mu];
953                              q1[sizeqmcmd]=recq1[mu];
954                              q2[sizeqmcmd]=recq2[mu];
955                              q3[sizeqmcmd]=recq3[mu];
956                              qmode[sizeqmcmd]=-10;
957                              orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
958                              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]);
959                              qRoll[sizeqmcmd]=RYPang_upper->Kren;
960                              qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
961                              qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
962                            }
963                            if(recqtime[mu]>=u_time){
964                              Int_t sizeqmcmd = qtime.size();
965                              inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
966                              qtime[sizeqmcmd]=u_time;
967                              q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
968                              q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
969                              q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
970                              q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
971                              qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
972                              lowerqtime = u_time;
973                              orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
974                              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]);
975                              qRoll[sizeqmcmd]=RYPang_upper->Kren;
976                              qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
977                              qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
978                              break;
979                            }
980                          }
981                        }
982                      }else{
983                            if ( debug ) printf(" here2 %i \n",ui);
984                        Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));
985                        if(lowerqtime>u_time)nt=0;
986                        Int_t recSize = recqtime.size();
987                        if(lowerqtime > recqtime[recSize-1]){
988                            Int_t sizeqmcmd = qtime.size();
989                            inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
990                            qtime[sizeqmcmd]=u_time;
991                            q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
992                            q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
993                            q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
994                            q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
995                            qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
996                            lowerqtime = u_time;
997                            orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
998                            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]);
999                            qRoll[sizeqmcmd]=RYPang_upper->Kren;
1000                            qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1001                            qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1002                        }
1003                        for(Int_t mu = nt;mu<recSize;mu++){
1004                          if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
1005                            nt=mu;
1006                            Int_t sizeqmcmd = qtime.size();
1007                            inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1008                            qtime[sizeqmcmd]=recqtime[mu];
1009                            q0[sizeqmcmd]=recq0[mu];
1010                            q1[sizeqmcmd]=recq1[mu];
1011                            q2[sizeqmcmd]=recq2[mu];
1012                            q3[sizeqmcmd]=recq3[mu];
1013                            qmode[sizeqmcmd]=-10;
1014                            orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1015                            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]);
1016                            qRoll[sizeqmcmd]=RYPang_upper->Kren;
1017                            qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1018                            qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1019                          }
1020                          if(recqtime[mu]>=u_time){
1021                            Int_t sizeqmcmd = qtime.size();
1022                            inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1023                            qtime[sizeqmcmd]=u_time;
1024                            q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
1025                            q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
1026                            q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
1027                            q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
1028                            qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
1029                            lowerqtime = u_time;
1030                            orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
1031                            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]);
1032                            qRoll[sizeqmcmd]=RYPang_upper->Kren;
1033                            qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1034                            qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1035                            CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);
1036                            break;
1037                          }
1038                        }
1039                      }
1040                    }
1041                  }
1042                }
1043                if ( debug ) printf(" ciccio \n");
1044              }
1045            }
1046            
1047            if(qtime.size()==0){
1048                for(UInt_t my=0;my<recqtime.size();my++){
1049                    Int_t sizeqmcmd = qtime.size();
1050                    inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
1051                    qtime[sizeqmcmd]=recqtime[my];
1052                    q0[sizeqmcmd]=recq0[my];
1053                    q1[sizeqmcmd]=recq1[my];
1054                    q2[sizeqmcmd]=recq2[my];
1055                    q3[sizeqmcmd]=recq3[my];
1056                    qmode[sizeqmcmd]=-10;
1057                    orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
1058                    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]);
1059                    qRoll[sizeqmcmd]=RYPang_upper->Kren;
1060                    qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1061                    qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1062                }
1063            }
1064            
1065            if ( debug ) printf(" fuffi \n");
1066    
1067        // Propagate the orbit from the tle time to atime, using SGP(D)4.          if ( debug ) printf(" puffi \n");
1068        cCoordGeo coo = getCoo(atime, gltle->GetFromTime(), gltle->GetTle());          Double_t tmin = 9999999999.;
1069            Double_t tmax = 0.;
1070            for(UInt_t tre = 0;tre<qtime.size();tre++){
1071              if(qtime[tre]>tmax)tmax = qtime[tre];
1072              if(qtime[tre]<tmin)tmin = qtime[tre];
1073            }
1074            if ( debug ) printf(" gnfuffi \n");
1075    
1076          } // if we processed first event
1077    
1078          
1079          //Filling Inclination information
1080          Double_t incli = 0;
1081          if ( qtime.size() > 1 ){
1082          for(UInt_t mu = must;mu<qtime.size()-1;mu++){
1083            if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);
1084            if(qtime[mu+1]>qtime[mu]){
1085              if ( debug ) printf(" grfuffi2 %i \n",mu);
1086              if(atime<=qtime[mu+1] && atime>=qtime[mu]){
1087                must = mu;
1088                incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);
1089                orbitalinfo->theta =  incli*atime+qPitch[mu+1]-incli*qtime[mu+1];
1090                incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);
1091                orbitalinfo->etha =  incli*atime+qRoll[mu+1]-incli*qtime[mu+1];
1092                incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1093                orbitalinfo->phi =  incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1094                
1095                incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1096                orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];
1097                incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1098                orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];
1099                incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1100                orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];
1101                incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1102                orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];
1103                
1104                orbitalinfo->TimeGap = qtime[mu+1]-qtime[mu];
1105                orbitalinfo->mode = qmode[mu+1];
1106                //if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;
1107                //reserved for next versions Vitaly.
1108                /*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){
1109                  //linear interpolation
1110                  incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1111                  orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];
1112                  incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1113                  orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];
1114                  incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1115                  orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];
1116                  incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1117                  orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];
1118                }else{
1119                  //sine interpolation
1120                  for(UInt_t mt=0;mt<q0sine.size();mt++){
1121                    if(atime<=q0sine[mt].finishPoint && atime>=q0sine[mt].startPoint){
1122                      if(!q0sine[mt].NeedFit)orbitalinfo->q0=q0sine[mt].A*sin(q0sine[mt].b*atime+q0sine[mt].c);else{
1123                        incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1124                        orbitalinfo->q0 =  incli*atime+q0[mu+1]-incli*qtime[mu+1];
1125                      }
1126                    }
1127                    if(atime<=q1sine[mt].finishPoint && atime>=q1sine[mt].startPoint){
1128                      if(!q1sine[mt].NeedFit)orbitalinfo->q1=q1sine[mt].A*sin(q1sine[mt].b*atime+q1sine[mt].c);else{
1129                        incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1130                        orbitalinfo->q1 =  incli*atime+q1[mu+1]-incli*qtime[mu+1];
1131                      }
1132                    }
1133                    if(atime<=q2sine[mt].finishPoint && atime>=q2sine[mt].startPoint){
1134                      if(!q2sine[mt].NeedFit)orbitalinfo->q2=q0sine[mt].A*sin(q2sine[mt].b*atime+q2sine[mt].c);else{
1135                        incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1136                        orbitalinfo->q2 =  incli*atime+q2[mu+1]-incli*qtime[mu+1];
1137                      }
1138                    }
1139                    if(atime<=q3sine[mt].finishPoint && atime>=q3sine[mt].startPoint){
1140                      if(!q3sine[mt].NeedFit)orbitalinfo->q3=q0sine[mt].A*sin(q3sine[mt].b*atime+q3sine[mt].c);else{
1141                        incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1142                        orbitalinfo->q3 =  incli*atime+q3[mu+1]-incli*qtime[mu+1];
1143                      }
1144                    }
1145                    if(atime<=Yawsine[mt].finishPoint && atime>=Yawsine[mt].startPoint){
1146                      if(!Yawsine[mt].NeedFit)orbitalinfo->phi=Yawsine[mt].A*sin(Yawsine[mt].b*atime+Yawsine[mt].c);else{
1147                        incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1148                        orbitalinfo->phi =  incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1149                      }
1150                    }
1151                  }
1152                }*/
1153                //q0testing->Fill(atime,orbitalinfo->q0,100);
1154                //q1testing->Fill(atime,orbitalinfo->q1,100);
1155                //Pitchtesting->Fill(atime,orbitalinfo->etha);
1156                //q2testing->Fill(atime,orbitalinfo->q2);
1157                //q3testing->Fill(atime,orbitalinfo->q3);
1158                break;
1159              }
1160            }
1161          }
1162          }
1163          //
1164          // ops no inclination information
1165          //
1166    
1167          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 ){
1168            orbitalinfo->mode = 10;
1169            orbitalinfo->q0 = -1000.;
1170            orbitalinfo->q1 = -1000.;
1171            orbitalinfo->q2 = -1000.;
1172            orbitalinfo->q3 = -1000.;
1173            orbitalinfo->etha = -1000.;
1174            orbitalinfo->phi = -1000.;
1175            orbitalinfo->theta = -1000.;
1176          };
1177          //
1178          // #########################################################################################################################  
1179          //
1180          // fill orbital positions
1181          //        
1182        // Build coordinates in the right range.  We want to convert,        // Build coordinates in the right range.  We want to convert,
1183        // longitude from (0, 2*pi) to (-180deg, 180deg).  Altitude is        // longitude from (0, 2*pi) to (-180deg, 180deg).  Altitude is
1184        // in meters.        // in meters.
1185        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);
1186        lat = rad2deg(coo.m_Lat);        lat = rad2deg(coo.m_Lat);
1187        alt = coo.m_Alt;        alt = coo.m_Alt;
1188          //
   
       //      if((lon>180) || (lon<-180) || (lat>90) || (lat<-90) || (alt<0))  
       //        continue;  
1189        if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){          if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){  
1190            //      
1191          orbitalinfo->lon = lon;          orbitalinfo->lon = lon;
1192          orbitalinfo->lat = lat;          orbitalinfo->lat = lat;
1193          orbitalinfo->alt = alt ;          orbitalinfo->alt = alt ;
1194                    //
1195          // compute mag field components and L shell.          // compute mag field components and L shell.
1196            //
1197          feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);          feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);
1198          shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);          shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);
1199          findb0_(&stps, &bdel, &value, &bequ, &rr0);          findb0_(&stps, &bdel, &value, &bequ, &rr0);
1200                    //
1201          orbitalinfo->Bnorth = bnorth;          orbitalinfo->Bnorth = bnorth;
1202          orbitalinfo->Beast = beast;          orbitalinfo->Beast = beast;
1203          orbitalinfo->Bdown = bdown;          orbitalinfo->Bdown = bdown;
1204          orbitalinfo->Babs = babs;          orbitalinfo->Babs = babs;
1205            orbitalinfo->M = dimo;
1206          orbitalinfo->BB0 = babs/bequ;          orbitalinfo->BB0 = babs/bequ;
1207          orbitalinfo->L = xl;          orbitalinfo->L = xl;      
           
1208          // Set Stormer vertical cutoff using L shell.          // Set Stormer vertical cutoff using L shell.
1209          orbitalinfo->cutoff[0] = 14.9/(xl*xl);          orbitalinfo->cutoffsvl = 14.295 / (xl*xl); //
1210            /*
1211    ---------- Forwarded message ----------
1212    Date: Wed, 09 May 2012 12:16:47 +0200
1213    From: Alessandro Bruno <alessandro.bruno@ba.infn.it>
1214    To: Mirko Boezio <mirko.boezio@ts.infn.it>
1215    Cc: Francesco S. Cafagna <Francesco.Cafagna@ba.infn.it>
1216    Subject: Störmer vertical cutoff
1217    
1218    Ciao Mirko,
1219    volevo segnalarti che il valore dello Störmer vertical cutoff nel Level2 è
1220    sovrastimato di circa il 4%.
1221    Dopo un'approfondita analisi con l'IGRF-05 abbiamo ricavano un valore pari
1222    a: 14.295 / L^2 anzichè 14.9 / L^2, valore obsoleto in quanto riferito agli
1223    anni '50.
1224    */
1225            //14.9/(xl*xl);
1226            orbitalinfo->igrf_icode = icode;
1227            //
1228          };      
1229          //
1230          if ( debug ) printf(" pitch angle \n");
1231          //
1232          // pitch angles
1233          //
1234          //if ( orbitalinfo->mode != 10 && orbitalinfo->mode != 5 && orbitalinfo->mode !=7 && orbitalinfo->mode != 9 ){
1235          if( orbitalinfo->TimeGap>0 && orbitalinfo->TimeGap<2000000){
1236            //
1237            Float_t Bx = -orbitalinfo->Bdown;
1238            Float_t By = orbitalinfo->Beast;
1239            Float_t Bz = orbitalinfo->Bnorth;
1240            //
1241            TMatrixD Fij = PO->ECItoGreenwich(PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3),orbitalinfo->absTime);
1242            TMatrixD Gij = PO->ColPermutation(Fij);
1243            TMatrixD Dij = PO->GreenwichtoGEO(orbitalinfo->lat,orbitalinfo->lon,Fij);
1244            TMatrixD Iij = PO->ColPermutation(Dij);
1245            //
1246            orbitalinfo->Iij.ResizeTo(Iij);
1247            orbitalinfo->Iij = Iij;
1248            //
1249            A1 = Iij(0,2);
1250            A2 = Iij(1,2);
1251            A3 = Iij(2,2);
1252            //
1253            //      orbitalinfo->pamzenitangle = (Float_t)PO->GetPitchAngle(1,0,0,A1,A2,A3);                        // Angle between zenit and Pamela's main axiz
1254            //      orbitalinfo->pamBangle = (Float_t)PO->GetPitchAngle(A1,A2,A3,Bx,By,Bz);                 // Angle between Pamela's main axiz and B
1255            //
1256            if ( !standalone && tof->ntrk() > 0 ){
1257              //
1258              Int_t nn = 0;
1259              for(Int_t nt=0; nt < tof->ntrk(); nt++){  
1260                //
1261                ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1262                Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1263                Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1264                Double_t E11z = zin[0];
1265                Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1266                Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1267                Double_t E22z = zin[3];
1268                if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1  ){
1269                  Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1270                  //              Double_t MyAzim = TMath::RadToDeg()*atan(TMath::Abs(E22y-E11y)/TMath::Abs(E22x-E11x));
1271                  //              if(E22x-E11x>=0 && E22y-E11y <0) MyAzim =  360. - MyAzim;
1272                  //              if(E22x-E11x>=0 && E22y-E11y >=0) MyAzim = MyAzim;
1273                  //              if(E22x-E11x<0 && E22y-E11y >0) MyAzim = 180. - MyAzim;
1274                  //              if(E22x-E11x<0 && E22y-E11y <0) MyAzim = 180. + MyAzim;
1275                  Px = (E22x-E11x)/norm;
1276                  Py = (E22y-E11y)/norm;
1277                  Pz = (E22z-E11z)/norm;
1278                  //
1279                  t_orb->trkseqno = ptt->trkseqno;
1280                  //
1281                  TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1282                  t_orb->Eij.ResizeTo(Eij);
1283                  t_orb->Eij = Eij;
1284                  //
1285                  TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1286                  t_orb->Sij.ResizeTo(Sij);
1287                  t_orb->Sij = Sij;
1288                  //            
1289                  t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1290                  //
1291                  //
1292                  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);
1293                  //
1294                  t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow((1+sqrt(1-pow(orbitalinfo->L,-3/2)*cos(omega))),2));
1295                  //
1296                  if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1297                  if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1298                  //
1299                  if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1300                  //
1301                  new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1302                  nn++;
1303                  //
1304                  t_orb->Clear();
1305                  //
1306                };
1307                //
1308              };
1309            } else {
1310              if ( debug ) printf(" mmm... mode %u standalone %i ntrk %i \n",orbitalinfo->mode,standalone,tof->ntrk());
1311            };
1312            //
1313          } else {
1314            if ( !standalone && tof->ntrk() > 0 ){
1315              //
1316              Int_t nn = 0;
1317              for(Int_t nt=0; nt < tof->ntrk(); nt++){  
1318                //
1319                ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1320                if ( ptt->trkseqno != -1  ){
1321                  //
1322                  t_orb->trkseqno = ptt->trkseqno;
1323                  //
1324                  t_orb->Eij = 0;  
1325                  //
1326                  t_orb->Sij = 0;
1327                  //            
1328                  t_orb->pitch = -1000.;
1329                  //
1330                  t_orb->cutoff = -1000.;
1331                  //
1332                  new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1333                  nn++;
1334                  //
1335                  t_orb->Clear();
1336                  //
1337                };
1338                //
1339              };    
1340            };
1341        };        };
1342                //
1343        // end EM        // Fill the class
1344          //
1345        OrbitalInfotr->Fill();        OrbitalInfotr->Fill();
1346      }        //
1347              delete t_orb;
1348          //
1349        }; // loop over the events in the run
1350      //      //
1351      // Here you may want to clear some variables before processing another run        // Here you may want to clear some variables before processing another run  
1352      //      //
1353    
1354        //gStyle->SetOptStat(000000);
1355        //gStyle->SetPalette(1);
1356        
1357        /*TCanvas* c1 = new TCanvas("c1","",1200,800);
1358        //c1->Divide(1,4);
1359        c1->cd(1);
1360        //q0testing->Draw("colz");
1361        //c1->cd(2);
1362        //q1testing->Draw("colz");
1363        //c1->cd(3);
1364        Pitchtesting->Draw("colz");
1365        //c1->cd(4);
1366        //q3testing->Draw("colz");
1367        c1->SaveAs("9.Rollhyst.png");
1368        delete c1;*/
1369    
1370        if ( verbose ) printf(" Clear before new run \n");
1371      delete dbtime;      delete dbtime;
1372    
1373        mcmdrc->Clear();
1374        mcmdrc = 0;
1375        
1376        if ( verbose ) printf(" Clear before new run1 \n");
1377        if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;
1378        if ( verbose ) printf(" Clear before new run2 \n");
1379        if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;
1380        if ( verbose ) printf(" Clear before new run3 \n");
1381        if ( RYPang_upper ) delete RYPang_upper;
1382        if ( verbose ) printf(" Clear before new run4 \n");
1383        if ( RYPang_lower ) delete RYPang_lower;
1384    
1385        if ( l0tr ) l0tr->Delete();
1386        
1387        if ( verbose ) printf(" End run \n");
1388    
1389    }; // process all the runs    }; // process all the runs
1390    //    
1391    if (verbose) printf("\n Finished processing data \n");    if (verbose) printf("\n Finished processing data \n");
1392    //    //
1393   closeandexit:   closeandexit:
# Line 606  int OrbitalInfoCore(UInt_t run, TFile *f Line 1405  int OrbitalInfoCore(UInt_t run, TFile *f
1405          //          //
1406          // Get entry from old tree          // Get entry from old tree
1407          //          //
1408          OrbitalInfotrclone->GetEntry(j);                    if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;    
1409          //          //
1410          // copy orbitalinfoclone to OrbitalInfo          // copy orbitalinfoclone to OrbitalInfo
1411          //          //
# Line 620  int OrbitalInfoCore(UInt_t run, TFile *f Line 1419  int OrbitalInfoCore(UInt_t run, TFile *f
1419        };        };
1420        if (verbose) printf(" Finished successful copying!\n");        if (verbose) printf(" Finished successful copying!\n");
1421      };      };
1422        //if ( OrbitalInfotrclone )    OrbitalInfotrclone->Clear();        
1423        //if ( OrbitalInfotrclone )    OrbitalInfotrclone->Delete();        
1424    };    };
1425    //    //
1426    // Close files, delete old tree(s), write and close level2 file    // Close files, delete old tree(s), write and close level2 file
1427    //    //
1428    if ( l0File ) l0File->Close();    if ( l0File ) l0File->Close();
1429    if ( tempfile ) tempfile->Close();                if ( myfold ) gSystem->Unlink(tempname.str().c_str());
   gSystem->Unlink(tempname.str().c_str());  
1430    //    //
   if ( runinfo ) runinfo->Close();      
1431    if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");        if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");    
1432      //
1433    if ( file ){    if ( file ){
1434      file->cd();      file->cd();
1435      file->Write();      OrbitalInfotr->Write("OrbitalInfo", TObject::kOverwrite);
1436    };    };
1437    //    //
1438    gSystem->Unlink(OrbitalInfofolder.str().c_str());    if (verbose) printf("\n Exiting...\n");
1439    
1440      if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());
1441    //    //
1442    // the end    // the end
1443    //    //
1444    if (verbose) printf("\n Exiting...\n");    if ( dbc ){
1445    if(OrbitalInfotr)OrbitalInfotr->Delete();      dbc->Close();
1446        delete dbc;
1447      };
1448    //    //
1449    if ( orbitalinfo ) delete orbitalinfo;    if (verbose) printf("\n Exiting...\n");
1450    if ( orbitalinfoclone ) delete orbitalinfoclone;    if ( tempfile ) tempfile->Close();            
1451      
1452      if ( PO ) delete PO;
1453      if ( gltle ) delete gltle;
1454      if ( glparam ) delete glparam;
1455      if ( glparam2 ) delete glparam2;
1456      if ( glparam3 ) delete glparam3;
1457      if (verbose) printf("\n Exiting3...\n");
1458    if ( glroot ) delete glroot;    if ( glroot ) delete glroot;
1459      if (verbose) printf("\n Exiting4...\n");
1460      if ( runinfo ) runinfo->Close();    
1461    if ( runinfo ) delete runinfo;    if ( runinfo ) delete runinfo;
1462    
1463      if ( debug ){  
1464      cout << "1   0x" << OrbitalInfotr << endl;
1465      cout << "2   0x" << OrbitalInfotrclone << endl;
1466      cout << "3   0x" << l0tr << endl;
1467      cout << "4   0x" << tempOrbitalInfo << endl;
1468      cout << "5   0x" << ttof << endl;
1469      }
1470      //
1471      if ( debug )  file->ls();
1472    //    //
1473    if(code < 0)  throw code;    if(code < 0)  throw code;
1474    return(code);    return(code);
# Line 663  cCoordGeo getCoo(UInt_t atime, UInt_t tl Line 1486  cCoordGeo getCoo(UInt_t atime, UInt_t tl
1486  {  {
1487    cEci eci;    cEci eci;
1488    cOrbit orbit(*tle);    cOrbit orbit(*tle);
     
1489    orbit.getPosition((double) (atime - tletime)/60., &eci);    orbit.getPosition((double) (atime - tletime)/60., &eci);
1490        
1491    return eci.toGeo();    return eci.toGeo();
1492  }  }
1493    
1494    // function of copyng of quatrnions classes
1495    
1496    void CopyQ(Quaternions *Q1, Quaternions *Q2){
1497      for(UInt_t i = 0; i < 6; i++){
1498        Q1->time[i]=Q2->time[i];
1499        for (UInt_t j = 0; j < 4; j++)Q1->quat[i][j]=Q2->quat[i][j];
1500      }
1501      return;
1502    }
1503    
1504    // functions of copyng InclinationInfo classes
1505    
1506    void CopyAng(InclinationInfo *A1, InclinationInfo *A2){
1507      A1->Tangazh = A2->Tangazh;
1508      A1->Ryskanie = A2->Ryskanie;
1509      A1->Kren = A2->Kren;
1510      return;
1511    }
1512    
1513    UInt_t holeq(Double_t lower,Double_t upper,Quaternions *Qlower, Quaternions *Qupper, UInt_t f){
1514      
1515      UInt_t hole = 10;
1516      Bool_t R10l = false;     // Sign of R10 mode in lower quaternions array
1517      Bool_t R10u = false;     // Sign of R10 mode in upper quaternions array
1518      Bool_t insm = false;     // Sign that we inside quaternions array
1519      Bool_t mxtml = false;    // Sign of mixt mode in lower quaternions array
1520      Bool_t mxtmu = false;    // Sign of mixt mode in upper quaternions array
1521      Bool_t npasm = false;     // Sign of normall pass between R10 and non R10 or between non R10 and R10
1522      UInt_t NCQl = 6;       // Number of correct quaternions in lower array
1523      UInt_t NCQu = 6;       // Number of correct quaternions in upper array
1524      if (f>0){
1525        insm = true;
1526        if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;
1527        if(Qupper->time[f]-Qupper->time[f-1]<1) R10u = true;
1528      }else{
1529        insm = false;
1530        if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]<2)) R10l = true;
1531        if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]<2)) R10u = true;
1532        if((Qlower->time[5]-Qlower->time[0]==150)&&(Qlower->time[1]-Qlower->time[0]==30)) R10l = false;
1533        if((Qupper->time[5]-Qupper->time[0]==150)&&(Qupper->time[1]-Qupper->time[0]==30)) R10u = false;
1534        if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]==30)){
1535          mxtml = true;
1536          for(UInt_t i = 1; i < 6; i++){
1537            if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;
1538          }
1539        }
1540        if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]==30)){
1541          mxtmu = true;
1542          for(UInt_t i = 1; i < 6; i++){
1543            if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;
1544          }
1545        }
1546      }
1547      
1548      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;
1549      
1550      
1551      if (R10u&&insm) hole=0; // best event R10
1552      if ((upper-lower<=5)&&(!insm)&&R10l&&R10u) hole = 1; // when first of 6 quaternions in array is correct
1553      if (((!R10u)&&insm)||((!insm)&&(!R10u)&&(!R10l)&&((upper-lower==210+(6-NCQl)*30)||(upper-lower==30)))) hole = 2; //non R10
1554      if (npasm&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 3; //normall pass from R10 to non R10 or from non R10 to R10
1555      if ((!npasm)&&(upper-lower<=300)&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 4; // eliminable hole between R10 and non R10 or between non R10 and R10
1556      if ((upper-lower>=300)&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 5; //uneliminable hole between R10 and non R10 or between non R10 and R10
1557      if ((upper-lower>5)&&(upper-lower<=300)&&R10u&&R10l) hole = 6; // eliminable hole inside R10
1558      if ((upper-lower>300)&&R10u&&R10l) hole = 7; //uneliminable hole inside R10
1559      if ((upper-lower>210)&&(upper-lower<=1200)&&(!R10u)&&(!R10l)) hole = 8; //eliminable hole inside non R10
1560      if ((upper-lower>1200)&&!R10u&&!R10l) hole = 9; // uneliminable hole inside non R10
1561      return hole;
1562    }
1563    
1564    void inclresize(vector<Double_t>& t,vector<Float_t>& q0,vector<Float_t>& q1,vector<Float_t>& q2,vector<Float_t>& q3,vector<Int_t>& mode,vector<Float_t>& Roll,vector<Float_t>& Pitch,vector<Float_t>& Yaw){
1565      Int_t sizee = t.size()+1;
1566      t.resize(sizee);
1567      q0.resize(sizee);
1568      q1.resize(sizee);
1569      q2.resize(sizee);
1570      q3.resize(sizee);
1571      mode.resize(sizee);
1572      Roll.resize(sizee);
1573      Pitch.resize(sizee);
1574      Yaw.resize(sizee);
1575    }
1576    
1577    //Find fitting sine functions for q0,q1,q2,q3 and Yaw-angle;
1578    void sineparam(vector<Sine>& qsine, vector<Double_t>& qtime, vector<Float_t>& q, vector<Float_t>& Roll, vector<Float_t>& Pitch, Float_t limsin){
1579      UInt_t mulast = 0;
1580      UInt_t munow = 0;
1581      UInt_t munext = 0;
1582      Bool_t increase = false;
1583      Bool_t decrease = false;
1584      Bool_t Max_is_defined = false;
1585      Bool_t Start_point_is_defined = false;
1586      Bool_t Period_is_defined = false;
1587      Bool_t Large_gap = false;
1588      Bool_t normal_way = true;
1589      Bool_t small_gap_on_ridge = false;
1590      Double_t t1 = 0;
1591      Double_t t1A = 0;
1592      Int_t sinesize = 0;
1593      Int_t nfi = 0;
1594      for(UInt_t mu = 0;mu<qtime.size();mu++){
1595        //cout<<"Roll["<<mu<<"] = "<<Roll[mu]<<endl;
1596        if(TMath::Abs(Roll[mu])<1. && TMath::Abs(Pitch[mu])<1. && TMath::Abs(q[mu])<limsin){
1597        //cout<<"q["<<mu<<endl<<"] = "<<q[mu]<<endl;
1598        if(mulast!=0 && munow!=0 && munext!=0){mulast=munow;munow=munext;munext=mu;}
1599        if(munext==0 && munow!=0)munext=mu;
1600        if(munow==0 && mulast!=0)munow=mu;
1601        if(mulast==0)mulast=mu;
1602        
1603        //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;
1604        //Int_t ref;
1605        //cin>>ref;
1606        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;
1607        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;
1608        //if(normal_way)cout<<"Normal_Way"<<endl;
1609        if(Large_gap || small_gap_on_ridge){
1610          //cout<<"Large gap..."<<endl;
1611          //if(small_gap_on_ridge)cout<<"small gap..."<<endl;
1612          //cout<<"q["<<mulast<<"] = "<<q[mulast]<<"\tq["<<munow<<"] = "<<q[munow]<<"\tq["<<munext<<"] = "<<q[munext]<<endl;
1613          //cout<<"qtime["<<mulast<<"] = "<<qtime[mulast]<<"\tqtime["<<munow<<"] = "<<qtime[munow]<<"\tqtime["<<munext<<"] = "<<qtime[munext]<<endl;
1614          increase = false;
1615          decrease = false;
1616          if(nfi>0){
1617            qsine.resize(qsine.size()-1);
1618            sinesize = qsine.size();
1619            //cout<<"nfi was larger then zero"<<endl;
1620          }else{
1621            //cout<<"nfi was not larger then zero :( nfi = "<<nfi<<endl;
1622            //cout<<"qsine.size = "<<qsine.size()<<endl;
1623            if(!Period_is_defined){
1624              //cout<<"Period was defined"<<endl;
1625              if(qsine.size()>1){
1626                qsine[sinesize-1].b = qsine[sinesize-2].b;
1627                qsine[sinesize-1].c = qsine[sinesize-2].c;
1628              }else{
1629                qsine[sinesize-1].b = TMath::Pi()/1591.54;
1630                qsine[sinesize-1].c = qsine[sinesize-1].startPoint;
1631              }
1632            }
1633            if(!Max_is_defined){
1634              //cout<<"Max was already defined"<<endl;
1635              if(qsine.size()>1)qsine[sinesize-1].A = qsine[sinesize-2].A;else qsine[sinesize-1].A = limsin;
1636            }
1637            qsine[sinesize-1].NeedFit = true;
1638          }
1639          qsine[sinesize-1].finishPoint = qtime[munow];
1640          //cout<<"finish point before large gap = "<<qtime[munow]<<endl;
1641          nfi = 0;
1642          Max_is_defined = false;
1643          Start_point_is_defined = false;
1644          Period_is_defined = false;
1645          small_gap_on_ridge = false;
1646        }
1647        //cout<<"Slope "<<increase<<"\t"<<decrease<<endl;
1648        //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;
1649        if((munext>munow) && (munow>mulast) && normal_way){
1650          if(!increase && !decrease){
1651            //cout<<"Normal way have started"<<endl;
1652            qsine.resize(qsine.size()+1);
1653            sinesize = qsine.size();
1654            qsine[sinesize-1].startPoint=qtime[mulast];
1655            if(q[munext]>q[munow] && q[munow]>q[mulast]) increase = true;
1656            if(q[munext]<q[munow] && q[munow]<q[mulast]) decrease = true;
1657          }
1658          //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;}
1659          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){
1660            //cout<<"Max point is qtime = "<<qtime[munow]<<"\tq = "<<q[munow]<<endl;
1661            if(q[munow]>q[mulast]){
1662              increase = false;
1663              decrease = true;
1664            }
1665            if(q[munow]<q[mulast]){
1666              increase = true;
1667              decrease = false;
1668            }
1669            if(Max_is_defined && !Start_point_is_defined){
1670              Double_t qPer = qtime[munow]-t1A;
1671              if(qPer>1000){
1672                //cout<<"qsine["<<sinesize-1<<"] = "<<qPer<<" = "<<qtime[munow]<<" - "<<t1A<<"\tlim = "<<limsin<<endl;
1673                qsine[sinesize-1].b=TMath::Pi()/qPer;
1674                if(decrease)qsine[sinesize-1].c=-qsine[sinesize-1].b*t1A;
1675                if(increase)qsine[sinesize-1].c=-qsine[sinesize-1].b*(t1A-qPer);
1676                Period_is_defined = true;
1677              }
1678            }
1679            Max_is_defined = true;
1680            qsine[sinesize-1].A = TMath::Abs(q[munow]);
1681            if(Start_point_is_defined && Period_is_defined){
1682              qsine[sinesize-1].finishPoint = qtime[munow];
1683              nfi++;
1684              qsine[sinesize-1].NeedFit = false;
1685              Max_is_defined = false;
1686              Start_point_is_defined = false;
1687              Period_is_defined = false;
1688              qsine.resize(qsine.size()+1);
1689              sinesize = qsine.size();
1690              qsine[sinesize-1].startPoint = qtime[munow];
1691            }
1692            if(!Start_point_is_defined) t1A=qtime[munow];
1693          }
1694          //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;
1695          if(((q[munow]>=0 && q[mulast]<=0) || (q[munow]<=0 && q[mulast]>=0)) && qtime[munow]-qtime[mulast]<2000 && qtime[munext]-qtime[munow]<2000){
1696            Double_t tcrosszero = 0;
1697            //cout<<"cross zero point...qtime = "<<qtime[munow]<<endl;
1698            if(q[munow]==0.) tcrosszero = qtime[munow];else
1699              if(q[mulast]==0.)tcrosszero = qtime[mulast];else{
1700                Double_t k_ = (q[munow]-q[mulast])/(qtime[munow]-qtime[mulast]);
1701                Double_t b_ = q[munow]-k_*qtime[munow];
1702                tcrosszero = -b_/k_;
1703              }
1704            if(Start_point_is_defined){
1705              //cout<<"Start Point allready defined"<<endl;
1706              Double_t qPer = tcrosszero - t1;
1707              qsine[sinesize-1].b = TMath::Pi()/qPer;
1708              //cout<<"qsine["<<sinesize-1<<"].b = "<<TMath::Pi()/qPer<<endl;
1709              Period_is_defined = true;
1710              Float_t x0 = 0;
1711              if(decrease)x0 = t1;
1712              if(increase)x0 = tcrosszero;
1713              qsine[sinesize-1].c = -qsine[sinesize-1].b*x0;
1714              if(Max_is_defined){
1715                //cout<<"Max was previous defined"<<endl;
1716                qsine[sinesize-1].finishPoint = qtime[munow];
1717                nfi++;
1718                qsine[sinesize-1].NeedFit = false;
1719                Max_is_defined = false;
1720                t1 = tcrosszero;
1721                Start_point_is_defined = true;
1722                Period_is_defined = false;
1723                qsine.resize(qsine.size()+1);
1724                sinesize = qsine.size();
1725                qsine[sinesize-1].startPoint = qtime[munow];
1726              }
1727            }else{
1728              t1 = tcrosszero;
1729              Start_point_is_defined = true;
1730            }
1731          }
1732        }
1733        }
1734      }
1735    
1736      //cout<<"FINISH SINE INTERPOLATION FUNCTION..."<<endl<<endl;
1737    }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.60

  ViewVC Help
Powered by ViewVC 1.1.23