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

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.50

  ViewVC Help
Powered by ViewVC 1.1.23