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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.54

  ViewVC Help
Powered by ViewVC 1.1.23