/[PAMELA software]/DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp
ViewVC logotype

Annotation of /DarthVader/OrbitalInfo/src/OrbitalInfoCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.52 - (hide annotations) (download)
Wed Nov 23 21:19:34 2011 UTC (13 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.51: +16 -6 lines
New ToF stuff, new IGRF handling and parameter files

1 mocchiut 1.1 // C/C++ headers
2     //
3     #include <fstream>
4     #include <string.h>
5     #include <iostream>
6     #include <cstring>
7     #include <stdio.h>
8     //
9     // ROOT headers
10     //
11 mocchiut 1.44 //#include <TCanvas.h>
12     //#include <TH2F.h> //for test only. Vitaly.
13     //#include <TF1.h>
14    
15 mocchiut 1.1 #include <TTree.h>
16     #include <TClassEdit.h>
17     #include <TObject.h>
18     #include <TList.h>
19 mocchiut 1.5 #include <TArrayI.h>
20 mocchiut 1.1 #include <TSystem.h>
21     #include <TSystemDirectory.h>
22     #include <TString.h>
23     #include <TFile.h>
24     #include <TClass.h>
25     #include <TSQLServer.h>
26     #include <TSQLRow.h>
27     #include <TSQLResult.h>
28     //
29 mocchiut 1.8 // RunInfo header
30     //
31     #include <RunInfo.h>
32     #include <GLTables.h>
33     //
34 mocchiut 1.1 // YODA headers
35     //
36     #include <PamelaRun.h>
37     #include <PscuHeader.h>
38     #include <PscuEvent.h>
39     #include <EventHeader.h>
40 mocchiut 1.15 #include <mcmd/McmdEvent.h>
41     #include <mcmd/McmdRecord.h>
42 mocchiut 1.1 //
43     // This program headers
44     //
45     #include <OrbitalInfo.h>
46 mocchiut 1.7 #include <OrbitalInfoVerl2.h>
47 mocchiut 1.1 #include <OrbitalInfoCore.h>
48 mocchiut 1.15 #include <InclinationInfo.h>
49 mocchiut 1.1
50 mocchiut 1.40
51 mocchiut 1.1 using namespace std;
52    
53     //
54     // CORE ROUTINE
55     //
56     //
57 mocchiut 1.25 int OrbitalInfoCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t OrbitalInfoargc, char *OrbitalInfoargv[]){
58 mocchiut 1.15 //
59 mocchiut 1.1 Int_t i = 0;
60 mocchiut 1.25 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 mocchiut 1.1 //
65 mocchiut 1.26 stringstream myquery;
66     myquery.str("");
67     myquery << "SET time_zone='+0:00'";
68     dbc->Query(myquery.str().c_str());
69     //
70 mocchiut 1.16 TString processFolder = Form("OrbitalInfoFolder_%u",run);
71 mocchiut 1.1 //
72     // Set these to true to have a very verbose output.
73     //
74     Bool_t debug = false;
75     //
76     Bool_t verbose = false;
77 mocchiut 1.30 //
78 mocchiut 1.31 Bool_t standalone = false;
79 mocchiut 1.30 //
80 mocchiut 1.1 if ( OrbitalInfoargc > 0 ){
81     i = 0;
82     while ( i < OrbitalInfoargc ){
83     if ( !strcmp(OrbitalInfoargv[i],"-processFolder") ) {
84     if ( OrbitalInfoargc < i+1 ){
85     throw -3;
86     };
87     processFolder = (TString)OrbitalInfoargv[i+1];
88     i++;
89     };
90     if ( (!strcmp(OrbitalInfoargv[i],"--debug")) || (!strcmp(OrbitalInfoargv[i],"-g")) ) {
91     verbose = true;
92 mocchiut 1.15 debug = true;
93 mocchiut 1.1 };
94     if ( (!strcmp(OrbitalInfoargv[i],"--verbose")) || (!strcmp(OrbitalInfoargv[i],"-v")) ) {
95     verbose = true;
96     };
97 mocchiut 1.30 if ( (!strcmp(OrbitalInfoargv[i],"--standalone")) ) {
98     standalone = true;
99     };
100     if ( (!strcmp(OrbitalInfoargv[i],"--calculate-pitch")) ) {
101     standalone = false;
102     };
103 mocchiut 1.1 i++;
104     };
105     };
106     //
107     const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
108     //
109     TTree *OrbitalInfotr = 0;
110 mocchiut 1.5 UInt_t nevents = 0;
111 mocchiut 1.15 UInt_t neventsm = 0;
112 mocchiut 1.1 //
113     // variables needed to reprocess data
114     //
115 mocchiut 1.6 Long64_t maxsize = 10000000000LL;
116     TTree::SetMaxTreeSize(maxsize);
117     //
118 mocchiut 1.1 TString OrbitalInfoversion;
119     ItoRunInfo *runinfo = 0;
120 mocchiut 1.5 TArrayI *runlist = 0;
121 mocchiut 1.1 TTree *OrbitalInfotrclone = 0;
122     Bool_t reproc = false;
123     Bool_t reprocall = false;
124     UInt_t nobefrun = 0;
125     UInt_t noaftrun = 0;
126     UInt_t numbofrun = 0;
127     stringstream ftmpname;
128     TString fname;
129 mocchiut 1.5 UInt_t totfileentries = 0;
130 mocchiut 1.15 UInt_t idRun = 0;
131     //
132     // My variables. Vitaly.
133     //
134 mocchiut 1.44 // UInt_t oi = 0;
135 mocchiut 1.15 Int_t tmpSize = 0;
136 mocchiut 1.1 //
137     // variables needed to handle error signals
138     //
139     Int_t code = 0;
140     Int_t sgnl;
141     //
142     // OrbitalInfo classes
143     //
144     OrbitalInfo *orbitalinfo = new OrbitalInfo();
145     OrbitalInfo *orbitalinfoclone = new OrbitalInfo();
146 mocchiut 1.44
147 mocchiut 1.1 //
148     // define variables for opening and reading level0 file
149     //
150     TFile *l0File = 0;
151     TTree *l0tr = 0;
152 mocchiut 1.37 // TTree *l0trm = 0;
153     TChain *ch = 0;
154 mocchiut 1.2 // EM: open also header branch
155     TBranch *l0head = 0;
156     pamela::EventHeader *eh = 0;
157     pamela::PscuHeader *ph = 0;
158 mocchiut 1.15 pamela::McmdEvent *mcmdev = 0;
159     pamela::McmdRecord *mcmdrc = 0;
160 mocchiut 1.2 // end EM
161 mocchiut 1.15
162     // pamela::RunHeaderEvent *reh = new pamela::RunHeaderEvent;
163     // pamela::EventHeader *eH = new pamela::EventHeader;
164    
165 mocchiut 1.1 //
166     // Define other basic variables
167     //
168     UInt_t procev = 0;
169     stringstream file2;
170     stringstream file3;
171     stringstream qy;
172     Int_t totevent = 0;
173 mocchiut 1.5 UInt_t atime = 0;
174     UInt_t re = 0;
175 mocchiut 1.15 UInt_t ik = 0;
176 mocchiut 1.7
177     // Position
178     Float_t lon, lat, alt;
179    
180     //
181     // IGRF stuff
182     //
183 mocchiut 1.40 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 mocchiut 1.7
194 mocchiut 1.1 //
195     // Working filename
196     //
197     TString outputfile;
198     stringstream name;
199     name.str("");
200     name << outDir << "/";
201     //
202     // temporary file and folder
203     //
204     TFile *tempfile = 0;
205     TTree *tempOrbitalInfo = 0;
206     stringstream tempname;
207     stringstream OrbitalInfofolder;
208 mocchiut 1.23 Bool_t myfold = false;
209 mocchiut 1.1 tempname.str("");
210     tempname << outDir;
211     tempname << "/" << processFolder.Data();
212     OrbitalInfofolder.str("");
213     OrbitalInfofolder << tempname.str().c_str();
214     tempname << "/OrbitalInfotree_run";
215     tempname << run << ".root";
216 mocchiut 1.40 UInt_t totnorun = 0;
217 mocchiut 1.1 //
218     // DB classes
219     //
220     GL_ROOT *glroot = new GL_ROOT();
221 mocchiut 1.5 GL_TIMESYNC *dbtime = 0;
222 mocchiut 1.7 GL_TLE *gltle = new GL_TLE();
223 mocchiut 1.15 //
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 mocchiut 1.7 // Initialize fortran routines!!!
234 mocchiut 1.52 Int_t ltp1 = 0;
235 mocchiut 1.7 Int_t ltp2 = 0;
236     Int_t ltp3 = 0;
237     Int_t uno = 1;
238 pam-fi 1.41 const char *niente = " ";
239 mocchiut 1.7 GL_PARAM *glparam = new GL_PARAM();
240 mocchiut 1.10 GL_PARAM *glparam2 = new GL_PARAM();
241 mocchiut 1.52 GL_PARAM *glparam3 = new GL_PARAM();
242 mocchiut 1.44
243 mocchiut 1.30 //
244 mocchiut 1.44 // Orientation variables. Vitaly
245 mocchiut 1.30 //
246     UInt_t evfrom = 0;
247     UInt_t jumped = 0;
248     Int_t itr = -1;
249     Double_t A1;
250     Double_t A2;
251     Double_t A3;
252     Double_t Px = 0;
253     Double_t Py = 0;
254     Double_t Pz = 0;
255     TTree *ttof = 0;
256     ToFLevel2 *tof = new ToFLevel2();
257     OrientationInfo *PO = new OrientationInfo();
258     Int_t nz = 6;
259     Float_t zin[6];
260     Int_t nevtofl2 = 0;
261 mocchiut 1.44 if ( verbose ) cout<<"Reading quaternions external file"<<endl;
262     cout.setf(ios::fixed,ios::floatfield);
263     /******Reading recovered quaternions...*********/
264     vector<Double_t> recqtime;
265     vector<Float_t> recq0;
266     vector<Float_t> recq1;
267     vector<Float_t> recq2;
268     vector<Float_t> recq3;
269     Float_t Norm = 1;
270     Int_t parerror=glparam->Query_GL_PARAM(1,303,dbc); // parameters stored in DB in GL_PRAM table
271 mocchiut 1.45 ifstream in((glparam->PATH+glparam->NAME).Data(),ios::in);
272 mocchiut 1.44 if ( parerror<0 ) {
273     code = parerror;
274     goto closeandexit;
275     };
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     }
290     if ( verbose ) cout<<"We have read recovered data"<<endl;
291    
292 mocchiut 1.52 UInt_t anni5 = 60. * 60. * 24. * 365. * 5 ;//1576800
293     parerror=glparam->Query_GL_PARAM(1,301,(dbc-anni5)); // parameters stored in DB in GL_PRAM table
294 mocchiut 1.7 if ( parerror<0 ) {
295     code = parerror;
296     goto closeandexit;
297     };
298 mocchiut 1.52 ltp1 = (Int_t)(glparam->PATH+glparam->NAME).Length();
299 mocchiut 1.7 if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
300     //
301 mocchiut 1.52 parerror=glparam2->Query_GL_PARAM(1,301,dbc); // parameters stored in DB in GL_PRAM table
302 mocchiut 1.7 if ( parerror<0 ) {
303     code = parerror;
304     goto closeandexit;
305     };
306 mocchiut 1.52 ltp2 = (Int_t)(glparam2->PATH+glparam->NAME).Length();
307 mocchiut 1.7 if ( verbose ) printf(" Reading Earth's Magnetic Field parameter file: %s \n",(glparam2->PATH+glparam2->NAME).Data());
308     //
309 mocchiut 1.52 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 mocchiut 1.7 //
319     // End IGRF stuff//
320     //
321 mocchiut 1.30 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 mocchiut 1.1 //
339     // Let's start!
340     //
341     // As a first thing we must check what we have to do: if run = 0 we must process all events in the file has been passed
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
343     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
344     //
345 mocchiut 1.5 if ( run == 0 ) reproc = true;
346 mocchiut 1.1 //
347     //
348     // Output file is "outputfile"
349     //
350     if ( !file->IsOpen() ){
351     //printf(" OrbitalInfo - ERROR: cannot open file for writing\n");
352     throw -901;
353     };
354     //
355     // Retrieve GL_RUN variables from the level2 file
356     //
357     OrbitalInfoversion = OrbitalInfoInfo(false); // we should decide how to handle versioning system
358     //
359     // create an interface to RunInfo called "runinfo"
360     //
361     runinfo = new ItoRunInfo(file);
362     //
363     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
364     //
365     sgnl = 0;
366     sgnl = runinfo->Update(run, "ORB", OrbitalInfoversion);
367     //sgnl = runinfo->Read(run);
368    
369     if ( sgnl ){
370     //printf("OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
371     code = sgnl;
372     goto closeandexit;
373     } else {
374     sgnl = 0;
375     };
376     //
377     // number of events in the file BEFORE the first event of our run
378     //
379     nobefrun = runinfo->GetFirstEntry();
380     //
381     // total number of events in the file
382     //
383     totfileentries = runinfo->GetFileEntries();
384     //
385     // first file entry AFTER the last event of our run
386     //
387     noaftrun = runinfo->GetLastEntry() + 1;
388     //
389     // number of run to be processed
390     //
391     numbofrun = runinfo->GetNoRun();
392 mocchiut 1.40 totnorun = runinfo->GetRunEntries();
393 mocchiut 1.1 //
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
395     //
396     OrbitalInfotrclone = (TTree*)file->Get("OrbitalInfo");
397     //
398     if ( !OrbitalInfotrclone ){
399     //
400     // tree does not exist, we are not reprocessing
401     //
402     reproc = false;
403 mocchiut 1.5 if ( run == 0 ){
404 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: you are reprocessing data but OrbitalInfo tree does not exist!\n");
405     }
406 mocchiut 1.5 if ( runinfo->IsReprocessing() && run != 0 ) {
407 mocchiut 1.1 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 {
410     //
411     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
412     //
413 mocchiut 1.6 OrbitalInfotrclone->SetAutoSave(900000000000000LL);
414 mocchiut 1.1 reproc = true;
415     //
416     //
417     if (verbose) printf("\n Preparing the pre-processing...\n");
418     //
419 mocchiut 1.22 if ( run == 0 || totnorun == 1 ){
420 mocchiut 1.1 //
421     // 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
423     //
424     reprocall = true;
425     //
426     if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing all runs\n");
427     //
428     } else {
429     //
430     // we are reprocessing a single run, we must copy to the new tree the events in the file which preceed the first event of the run
431     //
432     reprocall = false;
433     //
434 mocchiut 1.5 if (verbose) printf("\n OrbitalInfo - WARNING: Reprocessing run number %u \n",run);
435 mocchiut 1.1 //
436     // copying old tree to a new file
437     //
438 mocchiut 1.23 gSystem->MakeDirectory(OrbitalInfofolder.str().c_str());
439     myfold = true;
440 mocchiut 1.1 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
441     tempOrbitalInfo = OrbitalInfotrclone->CloneTree(-1,"fast");
442     tempOrbitalInfo->SetName("OrbitalInfo-old");
443     tempfile->Write();
444     tempfile->Close();
445     }
446     //
447     // Delete the old tree from old file and memory
448     //
449     OrbitalInfotrclone->Delete("all");
450     //
451     if (verbose) printf(" ...done!\n");
452     //
453     };
454     //
455     // create mydetector tree mydect
456     //
457     file->cd();
458     OrbitalInfotr = new TTree("OrbitalInfo-new","PAMELA OrbitalInfo data");
459 mocchiut 1.6 OrbitalInfotr->SetAutoSave(900000000000000LL);
460 mocchiut 1.30 orbitalinfo->Set();//ELENA **TEMPORANEO?**
461 mocchiut 1.1 OrbitalInfotr->Branch("OrbitalInfo","OrbitalInfo",&orbitalinfo);
462     //
463     if ( reproc && !reprocall ){
464     //
465     // open new file and retrieve also tree informations
466     //
467     tempfile = new TFile(tempname.str().c_str(),"READ");
468     OrbitalInfotrclone = (TTree*)tempfile->Get("OrbitalInfo-old");
469 mocchiut 1.6 OrbitalInfotrclone->SetAutoSave(900000000000000LL);
470 mocchiut 1.1 OrbitalInfotrclone->SetBranchAddress("OrbitalInfo",&orbitalinfoclone);
471     //
472     if ( nobefrun > 0 ){
473     if (verbose){
474 mocchiut 1.7 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 %u \n",nobefrun,run);
476     printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
477 mocchiut 1.1 }
478     for (UInt_t j = 0; j < nobefrun; j++){
479     //
480 mocchiut 1.43 if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;
481 mocchiut 1.1 //
482     // copy orbitalinfoclone to mydec
483     //
484 mocchiut 1.3 orbitalinfo->Clear();
485 mocchiut 1.5 //
486 mocchiut 1.1 memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
487     //
488     // Fill entry in the new tree
489     //
490     OrbitalInfotr->Fill();
491     //
492     };
493     if (verbose) printf(" Finished successful copying!\n");
494     };
495     };
496     //
497 mocchiut 1.44 //
498 mocchiut 1.1 // 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();
501     //
502     // Loop over the run to be processed
503     //
504     for (UInt_t irun=0; irun < numbofrun; irun++){
505     //
506     // retrieve the first run ID to be processed using the RunInfo list
507     //
508 mocchiut 1.15
509 mocchiut 1.1 idRun = runlist->At(irun);
510     if (verbose){
511     printf("\n\n\n ####################################################################### \n");
512     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
513     printf(" ####################################################################### \n\n\n");
514     }
515     //
516 mocchiut 1.5 runinfo->ID_ROOT_L0 = 0;
517 mocchiut 1.1 //
518     // store in the runinfo class the GL_RUN variables for our run
519     //
520     sgnl = 0;
521     sgnl = runinfo->GetRunInfo(idRun);
522     if ( sgnl ){
523 mocchiut 1.5 if ( debug ) printf("\n OrbitalInfo - ERROR: RunInfo exited with non-zero status\n");
524 mocchiut 1.1 code = sgnl;
525     goto closeandexit;
526     } else {
527     sgnl = 0;
528     };
529     //
530     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
531     //
532 mocchiut 1.5 if ( runinfo->ID_ROOT_L0 == 0 ){
533     if ( debug ) printf("\n OrbitalInfo - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
534 mocchiut 1.1 code = -5;
535     goto closeandexit;
536     };
537     //
538 mocchiut 1.5 // prepare the timesync for the db
539     //
540     dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
541 mocchiut 1.15
542 mocchiut 1.5 //
543 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
544     //
545 mocchiut 1.5 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
546 mocchiut 1.1 //
547     ftmpname.str("");
548     ftmpname << glroot->PATH.Data() << "/";
549     ftmpname << glroot->NAME.Data();
550     fname = ftmpname.str().c_str();
551 mocchiut 1.15 ftmpname.str("");
552 mocchiut 1.1 //
553 mocchiut 1.40 // print nout informations
554 mocchiut 1.1 //
555 mocchiut 1.5 totevent = runinfo->NEVENTS;
556 mocchiut 1.30 evfrom = runinfo->EV_FROM;
557 mocchiut 1.15 //cout<<"totevents = "<<totevent<<"\n";
558 mocchiut 1.1 if (verbose){
559     printf("\n LEVEL0 data file: %s \n",fname.Data());
560 mocchiut 1.5 printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
561     printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
562 mocchiut 1.15 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 mocchiut 1.1 }//
564 mocchiut 1.27 //
565 mocchiut 1.28 // if ( !totevent ) goto closeandexit;
566 mocchiut 1.1 // Open Level0 file
567     l0File = new TFile(fname.Data());
568     if ( !l0File ) {
569 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: problems opening Level0 file\n");
570 mocchiut 1.1 code = -6;
571     goto closeandexit;
572     };
573     l0tr = (TTree*)l0File->Get("Physics");
574     if ( !l0tr ) {
575 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: no Physics tree in Level0 file\n");
576 mocchiut 1.1 l0File->Close();
577     code = -7;
578     goto closeandexit;
579     };
580 mocchiut 1.2 // EM: open header branch as well
581     l0head = l0tr->GetBranch("Header");
582     if ( !l0head ) {
583 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: no Header branch in Level0 tree\n");
584 mocchiut 1.2 l0File->Close();
585     code = -8;
586     goto closeandexit;
587     };
588     l0tr->SetBranchAddress("Header", &eh);
589     // end EM
590 mocchiut 1.5 nevents = l0head->GetEntries();
591 mocchiut 1.1 //
592 mocchiut 1.28 if ( nevents < 1 && totevent ) {
593 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: Level0 file is empty\n\n");
594 mocchiut 1.1 l0File->Close();
595     code = -11;
596     goto closeandexit;
597     };
598 mocchiut 1.20 //
599 mocchiut 1.28 if ( runinfo->EV_TO > nevents-1 && totevent ) {
600 mocchiut 1.5 if ( debug ) printf(" OrbitalInfo - ERROR: too few entries in the registry tree\n");
601 mocchiut 1.1 l0File->Close();
602     code = -12;
603     goto closeandexit;
604     };
605     //
606 mocchiut 1.44 // 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 mocchiut 1.15 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 mocchiut 1.37 //
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 mocchiut 1.18 // neventsm = 0;
710 mocchiut 1.15 //
711     if (neventsm == 0){
712 mocchiut 1.18 if ( debug ) printf("InclinationInfo - WARNING: No quaternions in this File");
713     // l0File->Close();
714     code = 900;
715     // goto closeandexit;
716 mocchiut 1.15 }
717     //
718    
719 mocchiut 1.37 // l0trm->SetBranchAddress("Mcmd", &mcmdev);
720 mocchiut 1.18 // l0trm->SetBranchAddress("Header", &eh);
721 mocchiut 1.15 //
722     //
723     //
724 mocchiut 1.44
725     // UInt_t mctren = 0;
726     // UInt_t mcreen = 0;
727 mocchiut 1.15 UInt_t numrec = 0;
728     //
729     Double_t upperqtime = 0;
730     Double_t lowerqtime = 0;
731    
732 mocchiut 1.44 // Double_t incli = 0;
733     // oi = 0;
734     // UInt_t ooi = 0;
735 mocchiut 1.15 //
736 mocchiut 1.44 // init quaternions information from mcmd-packets
737 mocchiut 1.15 //
738     Bool_t isf = true;
739 mocchiut 1.44 // 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 mocchiut 1.15 //
771 mocchiut 1.1 // run over all the events of the run
772     //
773     if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
774     //
775 mocchiut 1.44 //
776 mocchiut 1.5 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
777 mocchiut 1.1 //
778 mocchiut 1.5 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
779 mocchiut 1.15 if ( debug ) printf(" %i \n",procev);
780 mocchiut 1.1 //
781 mocchiut 1.43 if ( l0head->GetEntry(re) <= 0 ) throw -36;
782 mocchiut 1.1 //
783     // absolute time of this event
784     //
785 mocchiut 1.5 ph = eh->GetPscuHeader();
786 mocchiut 1.15 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
787 mocchiut 1.40 if ( debug ) printf(" %i absolute time \n",procev);
788 mocchiut 1.1 //
789     // paranoid check
790     //
791 mocchiut 1.34 if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1)) ) {
792 mocchiut 1.1 if (verbose) printf(" OrbitalInfo - WARNING: event at time outside the run time window, skipping it\n");
793 mocchiut 1.30 jumped++;
794 mocchiut 1.44 // debug = true;
795 mocchiut 1.7 continue;
796     }
797 mocchiut 1.30
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 mocchiut 1.43 if ( ttof->GetEntry(itr) <= 0 ) throw -36;
820 mocchiut 1.30 //
821     };
822 mocchiut 1.1 //
823     procev++;
824     //
825     // start processing
826     //
827 mocchiut 1.40 if ( debug ) printf(" %i start processing \n",procev);
828 mocchiut 1.3 orbitalinfo->Clear();
829 mocchiut 1.5 //
830 mocchiut 1.30 OrbitalInfoTrkVar *t_orb = new OrbitalInfoTrkVar();
831     if( !(orbitalinfo->OrbitalInfoTrk) ) orbitalinfo->OrbitalInfoTrk = new TClonesArray("OrbitalInfoTrkVar",2);
832     TClonesArray &tor = *orbitalinfo->OrbitalInfoTrk;
833     //
834 mocchiut 1.15 // Fill OBT, pkt_num and absTime
835     //
836 mocchiut 1.2 orbitalinfo->pkt_num = ph->GetCounter();
837     orbitalinfo->OBT = ph->GetOrbitalTime();
838 mocchiut 1.15 orbitalinfo->absTime = atime;
839 mocchiut 1.40 if ( debug ) printf(" %i pktnum obt abstime \n",procev);
840 mocchiut 1.15 //
841     // Propagate the orbit from the tle time to atime, using SGP(D)4.
842     //
843 mocchiut 1.40 if ( debug ) printf(" %i sgp4 \n",procev);
844 mocchiut 1.15 cCoordGeo coo;
845 mocchiut 1.40 Float_t jyear=0.;
846 mocchiut 1.7 //
847     if(atime >= gltle->GetToTime()) {
848 mocchiut 1.9 if ( !gltle->Query(atime, dbc) ){
849 mocchiut 1.15 //
850 mocchiut 1.9 // Compute the magnetic dipole moment.
851 mocchiut 1.15 //
852 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment \n",procev);
853 mocchiut 1.9 UInt_t year, month, day, hour, min, sec;
854 mocchiut 1.15 //
855 mocchiut 1.9 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 mocchiut 1.44 + (hour*3600.+min*60.+(float)sec)/(24.*3600.*365.);
861 mocchiut 1.15 //
862 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment get dipole moment for year\n",procev);
863 mocchiut 1.9 feldcof_(&jyear, &dimo); // get dipole moment for year
864 mocchiut 1.40 if ( debug ) printf(" %i compute magnetic dipole moment end\n",procev);
865 mocchiut 1.9 } else {
866     code = -56;
867     goto closeandexit;
868     };
869 mocchiut 1.7 }
870 mocchiut 1.15 coo = getCoo(atime, gltle->GetFromTime(), gltle->GetTle());
871     //
872     cOrbit orbits(*gltle->GetTle());
873     //
874 mocchiut 1.18 if ( debug ) printf(" I am Here \n");
875     //
876 mocchiut 1.15 // synchronize with quaternions data
877     //
878 mocchiut 1.18 if ( isf && neventsm>0 ){
879 mocchiut 1.15 //
880     // First event
881     //
882     isf = false;
883     upperqtime = atime;
884     lowerqtime = runinfo->RUNHEADER_TIME;
885 mocchiut 1.44 for ( ik = 0; ik < neventsm; ik++){ //number of macrocommad packets
886 mocchiut 1.43 if ( ch->GetEntry(ik) <= 0 ) throw -36;
887 mocchiut 1.15 tmpSize = mcmdev->Records->GetEntries();
888     numrec = tmpSize;
889 mocchiut 1.44 for (Int_t j3 = 0;j3<tmpSize;j3++){ //number of subpackets
890 mocchiut 1.37 if ( debug ) printf(" ik %i j3 %i eh eh eh \n",ik,j3);
891 mocchiut 1.15 mcmdrc = (pamela::McmdRecord*)mcmdev->Records->At(j3);
892 mocchiut 1.38 if ( mcmdrc ){ // missing inclination bug [8RED 090116]
893 mocchiut 1.46 if ( debug ) printf(" pluto \n");
894 mocchiut 1.44 if ((int)mcmdrc->ID1 == 226 && mcmdrc->Mcmd_Block_crc_ok == 1){ //Check that it is Inclination Packet
895 mocchiut 1.47 L_QQ_Q_l_upper->fill(mcmdrc->McmdData);
896 mocchiut 1.38 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 mocchiut 1.46 if ( debug ) printf(" here1 %i \n",ui);
900 mocchiut 1.44 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     for(Int_t mu = nt;mu<recSize;mu++){
903     if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
904     nt=mu;
905     Int_t sizeqmcmd = qtime.size();
906     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
907     qtime[sizeqmcmd]=recqtime[mu];
908     q0[sizeqmcmd]=recq0[mu];
909     q1[sizeqmcmd]=recq1[mu];
910     q2[sizeqmcmd]=recq2[mu];
911     q3[sizeqmcmd]=recq3[mu];
912     qmode[sizeqmcmd]=-10;
913     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
914     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]);
915     qRoll[sizeqmcmd]=RYPang_upper->Kren;
916     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
917     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
918     }
919     if(recqtime[mu]>=u_time){
920     Int_t sizeqmcmd = qtime.size();
921     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
922     qtime[sizeqmcmd]=u_time;
923     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][0];
924     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][1];
925     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][2];
926     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[ui][3];
927     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
928     lowerqtime = u_time;
929     orbits.getPosition((double) (u_time - 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,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]);
931     qRoll[sizeqmcmd]=RYPang_upper->Kren;
932     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
933     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
934     break;
935 mocchiut 1.38 }
936     }
937     }
938     }else{
939 mocchiut 1.46 if ( debug ) printf(" here2 %i \n",ui);
940 mocchiut 1.44 Double_t u_time = dbtime->DBabsTime((UInt_t)(L_QQ_Q_l_upper->time[0]*1000-DeltaOBT*1000));
941     if(lowerqtime>u_time)nt=0;
942     Int_t recSize = recqtime.size();
943     for(Int_t mu = nt;mu<recSize;mu++){
944     if(recqtime[mu]>lowerqtime && recqtime[mu]<u_time){
945     nt=mu;
946     Int_t sizeqmcmd = qtime.size();
947     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
948     qtime[sizeqmcmd]=recqtime[mu];
949     q0[sizeqmcmd]=recq0[mu];
950     q1[sizeqmcmd]=recq1[mu];
951     q2[sizeqmcmd]=recq2[mu];
952     q3[sizeqmcmd]=recq3[mu];
953     qmode[sizeqmcmd]=-10;
954     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
955     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]);
956     qRoll[sizeqmcmd]=RYPang_upper->Kren;
957     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
958     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
959     }
960     if(recqtime[mu]>=u_time){
961     Int_t sizeqmcmd = qtime.size();
962     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
963     qtime[sizeqmcmd]=u_time;
964     q0[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][0];
965     q1[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][1];
966     q2[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][2];
967     q3[sizeqmcmd]=L_QQ_Q_l_upper->quat[0][3];
968     qmode[sizeqmcmd]=holeq(lowerqtime,qtime[sizeqmcmd],L_QQ_Q_l_lower,L_QQ_Q_l_upper,ui);
969     lowerqtime = u_time;
970     orbits.getPosition((double) (u_time - gltle->GetFromTime())/60., &eCi);
971     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]);
972     qRoll[sizeqmcmd]=RYPang_upper->Kren;
973     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
974     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
975 mocchiut 1.15 CopyQ(L_QQ_Q_l_lower,L_QQ_Q_l_upper);
976 mocchiut 1.44 break;
977 mocchiut 1.15 }
978     }
979 mocchiut 1.44 }
980 mocchiut 1.38 }
981 mocchiut 1.15 }
982 mocchiut 1.44 }
983 mocchiut 1.46 if ( debug ) printf(" ciccio \n");
984 mocchiut 1.15 }
985 mocchiut 1.44 }
986 mocchiut 1.47
987     if(qtime.size()==0){
988 mocchiut 1.50 for(UInt_t my=0;my<recqtime.size();my++){
989 mocchiut 1.47 Int_t sizeqmcmd = qtime.size();
990     inclresize(qtime,q0,q1,q2,q3,qmode,qRoll,qPitch,qYaw);
991     qtime[sizeqmcmd]=recqtime[my];
992     q0[sizeqmcmd]=recq0[my];
993     q1[sizeqmcmd]=recq1[my];
994     q2[sizeqmcmd]=recq2[my];
995     q3[sizeqmcmd]=recq3[my];
996     qmode[sizeqmcmd]=-10;
997     orbits.getPosition((double) (qtime[sizeqmcmd] - gltle->GetFromTime())/60., &eCi);
998     RYPang_upper->TransAngle(eCi.getPos().m_x,eCi.getPos().m_y,eCi.getPos().m_z,eCi.getVel().m_x,eCi.getVel().m_y,eCi.getVel().m_z,recq0[my],recq1[my],recq2[my],recq3[my]);
999     qRoll[sizeqmcmd]=RYPang_upper->Kren;
1000     qYaw[sizeqmcmd]=RYPang_upper->Ryskanie;
1001     qPitch[sizeqmcmd]=RYPang_upper->Tangazh;
1002     }
1003     }
1004    
1005 mocchiut 1.46 if ( debug ) printf(" fuffi \n");
1006 mocchiut 1.49
1007     //sineparam(q0sine,qtime,q0,qRoll,qPitch,0.60);
1008     //sineparam(q1sine,qtime,q1,qRoll,qPitch,0.82);
1009     //sineparam(q2sine,qtime,q2,qRoll,qPitch,0.82);
1010     //sineparam(q3sine,qtime,q3,qRoll,qPitch,0.60);
1011     //sineparam(Yawsine,qtime,qYaw,qRoll,qPitch,4);
1012    
1013 mocchiut 1.46 if ( debug ) printf(" puffi \n");
1014 mocchiut 1.44 Double_t tmin = 9999999999.;
1015     Double_t tmax = 0.;
1016     for(UInt_t tre = 0;tre<qtime.size();tre++){
1017     if(qtime[tre]>tmax)tmax = qtime[tre];
1018     if(qtime[tre]<tmin)tmin = qtime[tre];
1019     }
1020 mocchiut 1.46 if ( debug ) printf(" gnfuffi \n");
1021    
1022 mocchiut 1.44 //q0testing->SetName("q0testing");
1023     //q1testing->SetName("q1testing");
1024     //q2testing->SetName("q2testing");
1025     //q3testing->SetName("q3testing");
1026 mocchiut 1.15
1027 mocchiut 1.44 // Int_t ss=10.*(tmax-tmin);
1028     //q0testing->SetBins(ss,tmin,tmax,1000,-1.,1.);
1029     //Pitchtesting->SetBins(ss,tmin,tmax,1000,-40.,40.);
1030    
1031     // for(Int_t tre = 0;tre<qtime.size();tre++){
1032     //cout<<"q0["<<tre<<" = "<<q0[tre]<<endl;
1033     //q0testing->Fill(qtime[tre],q0[tre]);
1034     //q1testing->Fill(qtime[tre],q1[tre]);
1035     //Pitchtesting->Fill(qtime[tre],qPitch[tre],100);
1036     //if(qmode[tre] == -10)Pitchtesting->Fill(qtime[tre],10,100);
1037     //q2testing->Fill(qtime[tre],q2[tre],100);
1038     //q3testing->Fill(qtime[tre],q3[tre],100);
1039     // }
1040 mocchiut 1.15
1041 mocchiut 1.44 //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;
1042     //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;
1043     } // if we processed first event
1044    
1045     //Filling Inclination information
1046     Double_t incli = 0;
1047 mocchiut 1.46 if ( qtime.size() > 1 ){
1048 mocchiut 1.44 for(UInt_t mu = must;mu<qtime.size()-1;mu++){
1049 mocchiut 1.46 if ( debug ) printf(" ??grfuffi %i sixe %i must %i \n",mu,qtime.size()-1,must);
1050 mocchiut 1.44 if(qtime[mu+1]>qtime[mu]){
1051 mocchiut 1.46 if ( debug ) printf(" grfuffi2 %i \n",mu);
1052 mocchiut 1.44 if(atime<=qtime[mu+1] && atime>=qtime[mu]){
1053     must = mu;
1054     incli = (qPitch[mu+1]-qPitch[mu])/(qtime[mu+1]-qtime[mu]);
1055     orbitalinfo->theta = incli*atime+qPitch[mu+1]-incli*qtime[mu+1];
1056     incli = (qRoll[mu+1]-qRoll[mu])/(qtime[mu+1]-qtime[mu]);
1057     orbitalinfo->etha = incli*atime+qRoll[mu+1]-incli*qtime[mu+1];
1058     incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1059     orbitalinfo->phi = incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1060    
1061     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1062 mocchiut 1.49 orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1063 mocchiut 1.44 incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1064 mocchiut 1.49 orbitalinfo->q1 = incli*atime+q1[mu+1]-incli*qtime[mu+1];
1065 mocchiut 1.44 incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1066 mocchiut 1.49 orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1067 mocchiut 1.44 incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1068 mocchiut 1.49 orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1069 mocchiut 1.44
1070     orbitalinfo->TimeGap = qtime[mu+1]-qtime[mu];
1071     orbitalinfo->mode = qmode[mu+1];
1072 mocchiut 1.49 //if(qmode[mu+1]==-10) orbitalinfo->R10r = true;else orbitalinfo->R10r = false;
1073     //reserved for next versions Vitaly.
1074     /*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){
1075 mocchiut 1.44 //linear interpolation
1076     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1077     orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1078     incli = (q1[mu+1]-q1[mu])/(qtime[mu+1]-qtime[mu]);
1079     orbitalinfo->q1 = incli*atime+q1[mu+1]-incli*qtime[mu+1];
1080     incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1081     orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1082     incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1083     orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1084     }else{
1085     //sine interpolation
1086     for(UInt_t mt=0;mt<q0sine.size();mt++){
1087     if(atime<=q0sine[mt].finishPoint && atime>=q0sine[mt].startPoint){
1088     if(!q0sine[mt].NeedFit)orbitalinfo->q0=q0sine[mt].A*sin(q0sine[mt].b*atime+q0sine[mt].c);else{
1089     incli = (q0[mu+1]-q0[mu])/(qtime[mu+1]-qtime[mu]);
1090     orbitalinfo->q0 = incli*atime+q0[mu+1]-incli*qtime[mu+1];
1091     }
1092     }
1093     if(atime<=q1sine[mt].finishPoint && atime>=q1sine[mt].startPoint){
1094     if(!q1sine[mt].NeedFit)orbitalinfo->q1=q1sine[mt].A*sin(q1sine[mt].b*atime+q1sine[mt].c);else{
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     }
1098     }
1099     if(atime<=q2sine[mt].finishPoint && atime>=q2sine[mt].startPoint){
1100     if(!q2sine[mt].NeedFit)orbitalinfo->q2=q0sine[mt].A*sin(q2sine[mt].b*atime+q2sine[mt].c);else{
1101     incli = (q2[mu+1]-q2[mu])/(qtime[mu+1]-qtime[mu]);
1102     orbitalinfo->q2 = incli*atime+q2[mu+1]-incli*qtime[mu+1];
1103     }
1104     }
1105     if(atime<=q3sine[mt].finishPoint && atime>=q3sine[mt].startPoint){
1106     if(!q3sine[mt].NeedFit)orbitalinfo->q3=q0sine[mt].A*sin(q3sine[mt].b*atime+q3sine[mt].c);else{
1107     incli = (q3[mu+1]-q3[mu])/(qtime[mu+1]-qtime[mu]);
1108     orbitalinfo->q3 = incli*atime+q3[mu+1]-incli*qtime[mu+1];
1109     }
1110     }
1111     if(atime<=Yawsine[mt].finishPoint && atime>=Yawsine[mt].startPoint){
1112     if(!Yawsine[mt].NeedFit)orbitalinfo->phi=Yawsine[mt].A*sin(Yawsine[mt].b*atime+Yawsine[mt].c);else{
1113     incli = (qYaw[mu+1]-qYaw[mu])/(qtime[mu+1]-qtime[mu]);
1114     orbitalinfo->phi = incli*atime+qYaw[mu+1]-incli*qtime[mu+1];
1115     }
1116     }
1117     }
1118 mocchiut 1.49 }*/
1119 mocchiut 1.44 //q0testing->Fill(atime,orbitalinfo->q0,100);
1120     //q1testing->Fill(atime,orbitalinfo->q1,100);
1121     //Pitchtesting->Fill(atime,orbitalinfo->etha);
1122     //q2testing->Fill(atime,orbitalinfo->q2);
1123     //q3testing->Fill(atime,orbitalinfo->q3);
1124     break;
1125     }
1126 mocchiut 1.15 }
1127 mocchiut 1.44 }
1128 mocchiut 1.46 }
1129 mocchiut 1.30 //
1130     // ops no inclination information
1131     //
1132 mocchiut 1.44
1133 mocchiut 1.30 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 ){
1134     orbitalinfo->mode = 10;
1135     orbitalinfo->q0 = -1000.;
1136     orbitalinfo->q1 = -1000.;
1137     orbitalinfo->q2 = -1000.;
1138     orbitalinfo->q3 = -1000.;
1139     orbitalinfo->etha = -1000.;
1140     orbitalinfo->phi = -1000.;
1141     orbitalinfo->theta = -1000.;
1142 mocchiut 1.15 };
1143 mocchiut 1.30 //
1144     // #########################################################################################################################
1145 mocchiut 1.15 //
1146     // fill orbital positions
1147     //
1148 mocchiut 1.7 // Build coordinates in the right range. We want to convert,
1149     // longitude from (0, 2*pi) to (-180deg, 180deg). Altitude is
1150     // in meters.
1151     lon = (coo.m_Lon > M_PI) ? rad2deg(coo.m_Lon - 2*M_PI) : rad2deg(coo.m_Lon);
1152     lat = rad2deg(coo.m_Lat);
1153     alt = coo.m_Alt;
1154 mocchiut 1.15 //
1155 mocchiut 1.7 if( lon<180 && lon>-180 && lat<90 && lat>-90 && alt>0 ){
1156 mocchiut 1.15 //
1157 mocchiut 1.7 orbitalinfo->lon = lon;
1158     orbitalinfo->lat = lat;
1159     orbitalinfo->alt = alt ;
1160 mocchiut 1.15 //
1161 mocchiut 1.7 // compute mag field components and L shell.
1162 mocchiut 1.15 //
1163 mocchiut 1.7 feldg_(&lat, &lon, &alt, &bnorth, &beast, &bdown, &babs);
1164     shellg_(&lat, &lon, &alt, &dimo, &xl, &icode, &bab1);
1165     findb0_(&stps, &bdel, &value, &bequ, &rr0);
1166 mocchiut 1.15 //
1167 mocchiut 1.7 orbitalinfo->Bnorth = bnorth;
1168     orbitalinfo->Beast = beast;
1169     orbitalinfo->Bdown = bdown;
1170     orbitalinfo->Babs = babs;
1171     orbitalinfo->BB0 = babs/bequ;
1172 mocchiut 1.15 orbitalinfo->L = xl;
1173 mocchiut 1.7 // Set Stormer vertical cutoff using L shell.
1174 mocchiut 1.33 orbitalinfo->cutoffsvl = 14.9/(xl*xl);
1175 mocchiut 1.15 //
1176     };
1177 pamelaprod 1.11 //
1178 mocchiut 1.40 if ( debug ) printf(" pitch angle \n");
1179     //
1180 mocchiut 1.30 // pitch angles
1181     //
1182 mocchiut 1.49 //if ( orbitalinfo->mode != 10 && orbitalinfo->mode != 5 && orbitalinfo->mode !=7 && orbitalinfo->mode != 9 ){
1183     if( orbitalinfo->TimeGap>0 && orbitalinfo->TimeGap<2000000){
1184 mocchiut 1.30 //
1185 mocchiut 1.49 Float_t Bx = -orbitalinfo->Bdown;
1186     Float_t By = orbitalinfo->Beast;
1187     Float_t Bz = orbitalinfo->Bnorth;
1188 mocchiut 1.30 //
1189     TMatrixD Fij = PO->ECItoGreenwich(PO->QuatoECI(orbitalinfo->q0,orbitalinfo->q1,orbitalinfo->q2,orbitalinfo->q3),orbitalinfo->absTime);
1190 mocchiut 1.49 TMatrixD Gij = PO->ColPermutation(Fij);
1191 mocchiut 1.30 TMatrixD Dij = PO->GreenwichtoGEO(orbitalinfo->lat,orbitalinfo->lon,Fij);
1192     TMatrixD Iij = PO->ColPermutation(Dij);
1193     //
1194 mocchiut 1.33 orbitalinfo->Iij.ResizeTo(Iij);
1195     orbitalinfo->Iij = Iij;
1196     //
1197 mocchiut 1.30 A1 = Iij(0,2);
1198     A2 = Iij(1,2);
1199     A3 = Iij(2,2);
1200 mocchiut 1.49 //
1201 mocchiut 1.33 // orbitalinfo->pamzenitangle = (Float_t)PO->GetPitchAngle(1,0,0,A1,A2,A3); // Angle between zenit and Pamela's main axiz
1202     // orbitalinfo->pamBangle = (Float_t)PO->GetPitchAngle(A1,A2,A3,Bx,By,Bz); // Angle between Pamela's main axiz and B
1203 mocchiut 1.30 //
1204     if ( !standalone && tof->ntrk() > 0 ){
1205     //
1206     Int_t nn = 0;
1207     for(Int_t nt=0; nt < tof->ntrk(); nt++){
1208     //
1209     ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1210     Double_t E11x = ptt->xtr_tof[0]; // tr->x[0];
1211     Double_t E11y = ptt->ytr_tof[0]; //tr->y[0];
1212     Double_t E11z = zin[0];
1213     Double_t E22x = ptt->xtr_tof[3];//tr->x[3];
1214     Double_t E22y = ptt->ytr_tof[3];//tr->y[3];
1215     Double_t E22z = zin[3];
1216 mocchiut 1.32 if ( (E11x < 100. && E11y < 100. && E22x < 100. && E22y < 100.) || ptt->trkseqno != -1 ){
1217 mocchiut 1.30 Double_t norm = sqrt(pow(E22x-E11x,2)+pow(E22y-E11y,2)+pow(E22z-E11z,2));
1218 mocchiut 1.44 // Double_t MyAzim = TMath::RadToDeg()*atan(TMath::Abs(E22y-E11y)/TMath::Abs(E22x-E11x));
1219     // if(E22x-E11x>=0 && E22y-E11y <0) MyAzim = 360. - MyAzim;
1220     // if(E22x-E11x>=0 && E22y-E11y >=0) MyAzim = MyAzim;
1221     // if(E22x-E11x<0 && E22y-E11y >0) MyAzim = 180. - MyAzim;
1222     // if(E22x-E11x<0 && E22y-E11y <0) MyAzim = 180. + MyAzim;
1223 mocchiut 1.30 Px = (E22x-E11x)/norm;
1224     Py = (E22y-E11y)/norm;
1225     Pz = (E22z-E11z)/norm;
1226     //
1227 mocchiut 1.33 t_orb->trkseqno = ptt->trkseqno;
1228     //
1229     TMatrixD Eij = PO->PamelatoGEO(Iij,Px,Py,Pz);
1230     t_orb->Eij.ResizeTo(Eij);
1231     t_orb->Eij = Eij;
1232     //
1233 pam-mep 1.51 TMatrixD Sij = PO->PamelatoGEO(Gij,Px,Py,Pz);
1234 mocchiut 1.33 t_orb->Sij.ResizeTo(Sij);
1235     t_orb->Sij = Sij;
1236 mocchiut 1.30 //
1237     t_orb->pitch = (Float_t)PO->GetPitchAngle(Eij(0,0),Eij(1,0),Eij(2,0),Bx,By,Bz);
1238 mocchiut 1.33 //
1239     //
1240     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);
1241     //
1242     t_orb->cutoff = 59.3/(pow(orbitalinfo->L,2)*pow((1+sqrt(1-pow(orbitalinfo->L,-3/2)*cos(omega))),2));
1243     //
1244 mocchiut 1.36 if ( t_orb->pitch != t_orb->pitch ) t_orb->pitch = -1000.;
1245     if ( t_orb->cutoff != t_orb->cutoff ) t_orb->cutoff = -1000.;
1246     //
1247 mocchiut 1.33 if ( debug ) printf(" orbitalinfo->cutoffsvl %f vitaly %f \n",orbitalinfo->cutoffsvl,t_orb->cutoff);
1248 mocchiut 1.30 //
1249     new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1250     nn++;
1251     //
1252     t_orb->Clear();
1253     //
1254     };
1255     //
1256     };
1257     } else {
1258     if ( debug ) printf(" mmm... mode %u standalone %i ntrk %i \n",orbitalinfo->mode,standalone,tof->ntrk());
1259     };
1260     //
1261 mocchiut 1.35 } else {
1262     if ( !standalone && tof->ntrk() > 0 ){
1263     //
1264     Int_t nn = 0;
1265     for(Int_t nt=0; nt < tof->ntrk(); nt++){
1266     //
1267     ToFTrkVar *ptt = tof->GetToFTrkVar(nt);
1268     if ( ptt->trkseqno != -1 ){
1269     //
1270     t_orb->trkseqno = ptt->trkseqno;
1271     //
1272     t_orb->Eij = 0;
1273     //
1274     t_orb->Sij = 0;
1275     //
1276     t_orb->pitch = -1000.;
1277     //
1278     t_orb->cutoff = -1000.;
1279     //
1280     new(tor[nn]) OrbitalInfoTrkVar(*t_orb);
1281     nn++;
1282     //
1283     t_orb->Clear();
1284     //
1285     };
1286     //
1287     };
1288     };
1289 mocchiut 1.30 };
1290     //
1291 mocchiut 1.15 // Fill the class
1292 pamelaprod 1.11 //
1293 mocchiut 1.1 OrbitalInfotr->Fill();
1294 mocchiut 1.15 //
1295 mocchiut 1.30 delete t_orb;
1296     //
1297 mocchiut 1.15 }; // loop over the events in the run
1298 mocchiut 1.1 //
1299     // Here you may want to clear some variables before processing another run
1300     //
1301 mocchiut 1.44
1302     //gStyle->SetOptStat(000000);
1303     //gStyle->SetPalette(1);
1304    
1305     /*TCanvas* c1 = new TCanvas("c1","",1200,800);
1306     //c1->Divide(1,4);
1307     c1->cd(1);
1308     //q0testing->Draw("colz");
1309     //c1->cd(2);
1310     //q1testing->Draw("colz");
1311     //c1->cd(3);
1312     Pitchtesting->Draw("colz");
1313     //c1->cd(4);
1314     //q3testing->Draw("colz");
1315     c1->SaveAs("9.Rollhyst.png");
1316     delete c1;*/
1317    
1318 mocchiut 1.5 delete dbtime;
1319 mocchiut 1.18 if ( L_QQ_Q_l_upper ) delete L_QQ_Q_l_upper;
1320     if ( L_QQ_Q_l_lower ) delete L_QQ_Q_l_lower;
1321     if ( RYPang_upper ) delete RYPang_upper;
1322     if ( RYPang_lower ) delete RYPang_lower;
1323 mocchiut 1.1 }; // process all the runs
1324 mocchiut 1.15
1325 mocchiut 1.1 if (verbose) printf("\n Finished processing data \n");
1326     //
1327     closeandexit:
1328     //
1329     // we have finished processing the run(s). If we processed a single run now we must copy all the events after our run from the old tree to the new one and delete the old tree.
1330     //
1331     if ( !reprocall && reproc && code >= 0 ){
1332     if ( totfileentries > noaftrun ){
1333     if (verbose){
1334     printf("\n Post-processing: copying events from the old tree after the processed run\n");
1335     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
1336     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
1337     }
1338     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1339     //
1340     // Get entry from old tree
1341     //
1342 mocchiut 1.43 if ( OrbitalInfotrclone->GetEntry(j) <= 0 ) throw -36;
1343 mocchiut 1.1 //
1344     // copy orbitalinfoclone to OrbitalInfo
1345     //
1346 mocchiut 1.3 orbitalinfo->Clear();
1347 mocchiut 1.5 //
1348 mocchiut 1.1 memcpy(&orbitalinfo,&orbitalinfoclone,sizeof(orbitalinfoclone));
1349     //
1350     // Fill entry in the new tree
1351     //
1352     OrbitalInfotr->Fill();
1353     };
1354     if (verbose) printf(" Finished successful copying!\n");
1355     };
1356     };
1357     //
1358     // Close files, delete old tree(s), write and close level2 file
1359     //
1360     if ( l0File ) l0File->Close();
1361     if ( tempfile ) tempfile->Close();
1362 mocchiut 1.23 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
1363 mocchiut 1.5 //
1364 mocchiut 1.1 if ( runinfo ) runinfo->Close();
1365     if ( OrbitalInfotr ) OrbitalInfotr->SetName("OrbitalInfo");
1366 mocchiut 1.30 if ( tof ) tof->Delete();
1367     if ( ttof ) ttof->Delete();
1368     //
1369 mocchiut 1.1 if ( file ){
1370     file->cd();
1371     file->Write();
1372     };
1373     //
1374 mocchiut 1.23 if ( myfold ) gSystem->Unlink(OrbitalInfofolder.str().c_str());
1375 mocchiut 1.1 //
1376     // the end
1377     //
1378 mocchiut 1.25 if ( dbc ){
1379     dbc->Close();
1380     delete dbc;
1381     };
1382 mocchiut 1.1 if (verbose) printf("\n Exiting...\n");
1383     if(OrbitalInfotr)OrbitalInfotr->Delete();
1384 mocchiut 1.4 //
1385 mocchiut 1.30 if ( PO ) delete PO;
1386 mocchiut 1.4 if ( orbitalinfo ) delete orbitalinfo;
1387     if ( orbitalinfoclone ) delete orbitalinfoclone;
1388     if ( glroot ) delete glroot;
1389     if ( runinfo ) delete runinfo;
1390     //
1391 mocchiut 1.1 if(code < 0) throw code;
1392     return(code);
1393     }
1394    
1395 mocchiut 1.7
1396     //
1397     // Returns the cCoordGeo structure holding the geographical
1398     // coordinates for the event (see sgp4.h).
1399     //
1400     // atime is the abstime of the event in UTC unix time.
1401     // tletime is the time of the tle in UTC unix time.
1402     // tle is the previous and nearest tle (compared to atime).
1403     cCoordGeo getCoo(UInt_t atime, UInt_t tletime, cTle *tle)
1404     {
1405     cEci eci;
1406     cOrbit orbit(*tle);
1407     orbit.getPosition((double) (atime - tletime)/60., &eci);
1408    
1409     return eci.toGeo();
1410     }
1411 mocchiut 1.15
1412     // function of copyng of quatrnions classes
1413    
1414     void CopyQ(Quaternions *Q1, Quaternions *Q2){
1415     for(UInt_t i = 0; i < 6; i++){
1416     Q1->time[i]=Q2->time[i];
1417     for (UInt_t j = 0; j < 4; j++)Q1->quat[i][j]=Q2->quat[i][j];
1418     }
1419     return;
1420     }
1421    
1422     // functions of copyng InclinationInfo classes
1423    
1424     void CopyAng(InclinationInfo *A1, InclinationInfo *A2){
1425     A1->Tangazh = A2->Tangazh;
1426     A1->Ryskanie = A2->Ryskanie;
1427     A1->Kren = A2->Kren;
1428     return;
1429     }
1430    
1431     UInt_t holeq(Double_t lower,Double_t upper,Quaternions *Qlower, Quaternions *Qupper, UInt_t f){
1432    
1433     UInt_t hole = 10;
1434 mocchiut 1.40 Bool_t R10l = false; // Sign of R10 mode in lower quaternions array
1435     Bool_t R10u = false; // Sign of R10 mode in upper quaternions array
1436     Bool_t insm = false; // Sign that we inside quaternions array
1437     Bool_t mxtml = false; // Sign of mixt mode in lower quaternions array
1438     Bool_t mxtmu = false; // Sign of mixt mode in upper quaternions array
1439     Bool_t npasm = false; // Sign of normall pass between R10 and non R10 or between non R10 and R10
1440 mocchiut 1.15 UInt_t NCQl = 6; // Number of correct quaternions in lower array
1441     UInt_t NCQu = 6; // Number of correct quaternions in upper array
1442     if (f>0){
1443     insm = true;
1444     if(Qupper->time[f]-Qupper->time[f-1]==30) R10u = false;
1445     if(Qupper->time[f]-Qupper->time[f-1]<1) R10u = true;
1446     }else{
1447     insm = false;
1448     if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]<2)) R10l = true;
1449     if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]<2)) R10u = true;
1450     if((Qlower->time[5]-Qlower->time[0]==150)&&(Qlower->time[1]-Qlower->time[0]==30)) R10l = false;
1451     if((Qupper->time[5]-Qupper->time[0]==150)&&(Qupper->time[1]-Qupper->time[0]==30)) R10u = false;
1452     if((Qlower->time[5]-Qlower->time[0]<2)&&(Qlower->time[1]-Qlower->time[0]==30)){
1453     mxtml = true;
1454     for(UInt_t i = 1; i < 6; i++){
1455     if(Qlower->time[i]-Qlower->time[0]==30*i) NCQl=i;
1456     }
1457     }
1458     if((Qupper->time[5]-Qupper->time[0]<2)&&(Qupper->time[1]-Qupper->time[0]==30)){
1459     mxtmu = true;
1460     for(UInt_t i = 1; i < 6; i++){
1461     if(Qupper->time[i]-Qupper->time[0]==30*i) NCQu=i;
1462     }
1463     }
1464     }
1465    
1466     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;
1467    
1468    
1469     if (R10u&&insm) hole=0; // best event R10
1470     if ((upper-lower<=5)&&(!insm)&&R10l&&R10u) hole = 1; // when first of 6 quaternions in array is correct
1471     if (((!R10u)&&insm)||((!insm)&&(!R10u)&&(!R10l)&&((upper-lower==210+(6-NCQl)*30)||(upper-lower==30)))) hole = 2; //non R10
1472     if (npasm&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 3; //normall pass from R10 to non R10 or from non R10 to R10
1473     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
1474     if ((upper-lower>=300)&&(!insm)&&((R10l&&!R10u)||(R10u&&!R10l))) hole = 5; //uneliminable hole between R10 and non R10 or between non R10 and R10
1475     if ((upper-lower>5)&&(upper-lower<=300)&&R10u&&R10l) hole = 6; // eliminable hole inside R10
1476     if ((upper-lower>300)&&R10u&&R10l) hole = 7; //uneliminable hole inside R10
1477     if ((upper-lower>210)&&(upper-lower<=1200)&&(!R10u)&&(!R10l)) hole = 8; //eliminable hole inside non R10
1478     if ((upper-lower>1200)&&!R10u&&!R10l) hole = 9; // uneliminable hole inside non R10
1479     return hole;
1480     }
1481    
1482 mocchiut 1.44 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){
1483     Int_t sizee = t.size()+1;
1484     t.resize(sizee);
1485     q0.resize(sizee);
1486     q1.resize(sizee);
1487     q2.resize(sizee);
1488     q3.resize(sizee);
1489     mode.resize(sizee);
1490     Roll.resize(sizee);
1491     Pitch.resize(sizee);
1492     Yaw.resize(sizee);
1493     }
1494    
1495     //Find fitting sine functions for q0,q1,q2,q3 and Yaw-angle;
1496     void sineparam(vector<Sine>& qsine, vector<Double_t>& qtime, vector<Float_t>& q, vector<Float_t>& Roll, vector<Float_t>& Pitch, Float_t limsin){
1497     UInt_t mulast = 0;
1498     UInt_t munow = 0;
1499     UInt_t munext = 0;
1500     Bool_t increase = false;
1501     Bool_t decrease = false;
1502     Bool_t Max_is_defined = false;
1503     Bool_t Start_point_is_defined = false;
1504     Bool_t Period_is_defined = false;
1505     Bool_t Large_gap = false;
1506     Bool_t normal_way = true;
1507     Bool_t small_gap_on_ridge = false;
1508     Double_t t1 = 0;
1509     Double_t t1A = 0;
1510     Int_t sinesize = 0;
1511     Int_t nfi = 0;
1512     for(UInt_t mu = 0;mu<qtime.size();mu++){
1513 mocchiut 1.47 //cout<<"Roll["<<mu<<"] = "<<Roll[mu]<<endl;
1514     if(TMath::Abs(Roll[mu])<1. && TMath::Abs(Pitch[mu])<1. && TMath::Abs(q[mu])<limsin){
1515     //cout<<"q["<<mu<<endl<<"] = "<<q[mu]<<endl;
1516     if(mulast!=0 && munow!=0 && munext!=0){mulast=munow;munow=munext;munext=mu;}
1517 mocchiut 1.44 if(munext==0 && munow!=0)munext=mu;
1518     if(munow==0 && mulast!=0)munow=mu;
1519     if(mulast==0)mulast=mu;
1520 mocchiut 1.47
1521     //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;
1522     //Int_t ref;
1523     //cin>>ref;
1524     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;
1525 mocchiut 1.44 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;
1526 mocchiut 1.47 //if(normal_way)cout<<"Normal_Way"<<endl;
1527 mocchiut 1.44 if(Large_gap || small_gap_on_ridge){
1528     //cout<<"Large gap..."<<endl;
1529     //if(small_gap_on_ridge)cout<<"small gap..."<<endl;
1530 mocchiut 1.47 //cout<<"q["<<mulast<<"] = "<<q[mulast]<<"\tq["<<munow<<"] = "<<q[munow]<<"\tq["<<munext<<"] = "<<q[munext]<<endl;
1531     //cout<<"qtime["<<mulast<<"] = "<<qtime[mulast]<<"\tqtime["<<munow<<"] = "<<qtime[munow]<<"\tqtime["<<munext<<"] = "<<qtime[munext]<<endl;
1532 mocchiut 1.44 increase = false;
1533     decrease = false;
1534     if(nfi>0){
1535     qsine.resize(qsine.size()-1);
1536     sinesize = qsine.size();
1537 mocchiut 1.47 //cout<<"nfi was larger then zero"<<endl;
1538 mocchiut 1.44 }else{
1539 mocchiut 1.47 //cout<<"nfi was not larger then zero :( nfi = "<<nfi<<endl;
1540     //cout<<"qsine.size = "<<qsine.size()<<endl;
1541 mocchiut 1.44 if(!Period_is_defined){
1542 mocchiut 1.47 //cout<<"Period was defined"<<endl;
1543 mocchiut 1.44 if(qsine.size()>1){
1544     qsine[sinesize-1].b = qsine[sinesize-2].b;
1545     qsine[sinesize-1].c = qsine[sinesize-2].c;
1546     }else{
1547     qsine[sinesize-1].b = TMath::Pi()/1591.54;
1548     qsine[sinesize-1].c = qsine[sinesize-1].startPoint;
1549     }
1550     }
1551     if(!Max_is_defined){
1552 mocchiut 1.47 //cout<<"Max was already defined"<<endl;
1553 mocchiut 1.44 if(qsine.size()>1)qsine[sinesize-1].A = qsine[sinesize-2].A;else qsine[sinesize-1].A = limsin;
1554     }
1555     qsine[sinesize-1].NeedFit = true;
1556     }
1557     qsine[sinesize-1].finishPoint = qtime[munow];
1558 mocchiut 1.47 //cout<<"finish point before large gap = "<<qtime[munow]<<endl;
1559 mocchiut 1.44 nfi = 0;
1560     Max_is_defined = false;
1561     Start_point_is_defined = false;
1562     Period_is_defined = false;
1563     small_gap_on_ridge = false;
1564     }
1565 mocchiut 1.47 //cout<<"Slope "<<increase<<"\t"<<decrease<<endl;
1566     //cout<<"mulast = "<<mulast<<"\tmunow = "<<munow<<"\tmunext = "<<munext<<endl;
1567     if((munext>munow) && (munow>mulast) && normal_way){
1568 mocchiut 1.44 if(!increase && !decrease){
1569 mocchiut 1.47 //cout<<"Normal way have started"<<endl;
1570 mocchiut 1.44 qsine.resize(qsine.size()+1);
1571     sinesize = qsine.size();
1572     qsine[sinesize-1].startPoint=qtime[mulast];
1573     if(q[munext]>q[munow] && q[munow]>q[mulast]) increase = true;
1574     if(q[munext]<q[munow] && q[munow]<q[mulast]) decrease = true;
1575     }
1576     //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;}
1577 mocchiut 1.47 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){
1578 mocchiut 1.44 //cout<<"Max point is qtime = "<<qtime[munow]<<"\tq = "<<q[munow]<<endl;
1579     if(q[munow]>q[mulast]){
1580     increase = false;
1581     decrease = true;
1582     }
1583     if(q[munow]<q[mulast]){
1584     increase = true;
1585     decrease = false;
1586     }
1587     if(Max_is_defined && !Start_point_is_defined){
1588     Double_t qPer = qtime[munow]-t1A;
1589     if(qPer>1000){
1590     //cout<<"qsine["<<sinesize-1<<"] = "<<qPer<<" = "<<qtime[munow]<<" - "<<t1A<<"\tlim = "<<limsin<<endl;
1591     qsine[sinesize-1].b=TMath::Pi()/qPer;
1592     if(decrease)qsine[sinesize-1].c=-qsine[sinesize-1].b*t1A;
1593     if(increase)qsine[sinesize-1].c=-qsine[sinesize-1].b*(t1A-qPer);
1594     Period_is_defined = true;
1595     }
1596     }
1597     Max_is_defined = true;
1598     qsine[sinesize-1].A = TMath::Abs(q[munow]);
1599     if(Start_point_is_defined && Period_is_defined){
1600     qsine[sinesize-1].finishPoint = qtime[munow];
1601     nfi++;
1602     qsine[sinesize-1].NeedFit = false;
1603     Max_is_defined = false;
1604     Start_point_is_defined = false;
1605     Period_is_defined = false;
1606     qsine.resize(qsine.size()+1);
1607     sinesize = qsine.size();
1608     qsine[sinesize-1].startPoint = qtime[munow];
1609     }
1610     if(!Start_point_is_defined) t1A=qtime[munow];
1611     }
1612     //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;
1613     if(((q[munow]>=0 && q[mulast]<=0) || (q[munow]<=0 && q[mulast]>=0)) && qtime[munow]-qtime[mulast]<2000 && qtime[munext]-qtime[munow]<2000){
1614     Double_t tcrosszero = 0;
1615     //cout<<"cross zero point...qtime = "<<qtime[munow]<<endl;
1616     if(q[munow]==0.) tcrosszero = qtime[munow];else
1617     if(q[mulast]==0.)tcrosszero = qtime[mulast];else{
1618     Double_t k_ = (q[munow]-q[mulast])/(qtime[munow]-qtime[mulast]);
1619     Double_t b_ = q[munow]-k_*qtime[munow];
1620     tcrosszero = -b_/k_;
1621     }
1622     if(Start_point_is_defined){
1623     //cout<<"Start Point allready defined"<<endl;
1624     Double_t qPer = tcrosszero - t1;
1625     qsine[sinesize-1].b = TMath::Pi()/qPer;
1626     //cout<<"qsine["<<sinesize-1<<"].b = "<<TMath::Pi()/qPer<<endl;
1627     Period_is_defined = true;
1628     Float_t x0 = 0;
1629     if(decrease)x0 = t1;
1630     if(increase)x0 = tcrosszero;
1631     qsine[sinesize-1].c = -qsine[sinesize-1].b*x0;
1632     if(Max_is_defined){
1633     //cout<<"Max was previous defined"<<endl;
1634     qsine[sinesize-1].finishPoint = qtime[munow];
1635     nfi++;
1636     qsine[sinesize-1].NeedFit = false;
1637     Max_is_defined = false;
1638     t1 = tcrosszero;
1639     Start_point_is_defined = true;
1640     Period_is_defined = false;
1641     qsine.resize(qsine.size()+1);
1642     sinesize = qsine.size();
1643     qsine[sinesize-1].startPoint = qtime[munow];
1644     }
1645     }else{
1646     t1 = tcrosszero;
1647     Start_point_is_defined = true;
1648     }
1649     }
1650     }
1651     }
1652     }
1653    
1654     //cout<<"FINISH SINE INTERPOLATION FUNCTION..."<<endl<<endl;
1655     }

  ViewVC Help
Powered by ViewVC 1.1.23