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

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

  ViewVC Help
Powered by ViewVC 1.1.23